1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/platform/default/logging.h"
17 #include "tensorflow/core/platform/env_time.h"
18 #include "tensorflow/core/platform/macros.h"
19
20 #if defined(PLATFORM_POSIX_ANDROID)
21 #include <android/log.h>
22 #include <iostream>
23 #include <sstream>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include <string>
31 #include <unordered_map>
32
33 namespace tensorflow {
34 namespace internal {
35
36 #if defined(PLATFORM_POSIX_ANDROID)
GenerateLogMessage()37 void LogMessage::GenerateLogMessage() {
38 int android_log_level;
39 switch (severity_) {
40 case INFO:
41 android_log_level = ANDROID_LOG_INFO;
42 break;
43 case WARNING:
44 android_log_level = ANDROID_LOG_WARN;
45 break;
46 case ERROR:
47 android_log_level = ANDROID_LOG_ERROR;
48 break;
49 case FATAL:
50 android_log_level = ANDROID_LOG_FATAL;
51 break;
52 default:
53 if (severity_ < INFO) {
54 android_log_level = ANDROID_LOG_VERBOSE;
55 } else {
56 android_log_level = ANDROID_LOG_ERROR;
57 }
58 break;
59 }
60
61 std::stringstream ss;
62 const char* const partial_name = strrchr(fname_, '/');
63 ss << (partial_name != nullptr ? partial_name + 1 : fname_) << ":" << line_
64 << " " << str();
65 __android_log_write(android_log_level, "native", ss.str().c_str());
66
67 // Also log to stderr (for standalone Android apps).
68 std::cerr << "native : " << ss.str() << std::endl;
69
70 // Android logging at level FATAL does not terminate execution, so abort()
71 // is still required to stop the program.
72 if (severity_ == FATAL) {
73 abort();
74 }
75 }
76
77 #else
78
79 void LogMessage::GenerateLogMessage() {
80 static EnvTime* env_time = tensorflow::EnvTime::Default();
81 uint64 now_micros = env_time->NowMicros();
82 time_t now_seconds = static_cast<time_t>(now_micros / 1000000);
83 int32 micros_remainder = static_cast<int32>(now_micros % 1000000);
84 const size_t time_buffer_size = 30;
85 char time_buffer[time_buffer_size];
86 strftime(time_buffer, time_buffer_size, "%Y-%m-%d %H:%M:%S",
87 localtime(&now_seconds));
88
89 // TODO(jeff,sanjay): Replace this with something that logs through the env.
90 fprintf(stderr, "%s.%06d: %c %s:%d] %s\n", time_buffer, micros_remainder,
91 "IWEF"[severity_], fname_, line_, str().c_str());
92 }
93 #endif
94
95 namespace {
96
ParseInteger(const char * str,size_t size)97 int ParseInteger(const char* str, size_t size) {
98 // Ideally we would use env_var / safe_strto64, but it is
99 // hard to use here without pulling in a lot of dependencies,
100 // so we use std:istringstream instead
101 string integer_str(str, size);
102 std::istringstream ss(integer_str);
103 int level = 0;
104 ss >> level;
105 return level;
106 }
107
108 // Parse log level (int64) from environment variable (char*)
LogLevelStrToInt(const char * tf_env_var_val)109 int64 LogLevelStrToInt(const char* tf_env_var_val) {
110 if (tf_env_var_val == nullptr) {
111 return 0;
112 }
113 return ParseInteger(tf_env_var_val, strlen(tf_env_var_val));
114 }
115
116 // Using StringPiece breaks Windows build.
117 struct StringData {
118 struct Hasher {
operator ()tensorflow::internal::__anondd6483390111::StringData::Hasher119 size_t operator()(const StringData& sdata) const {
120 // For dependency reasons, we cannot use hash.h here. Use DBJHash instead.
121 size_t hash = 5381;
122 const char* data = sdata.data;
123 for (const char* top = data + sdata.size; data < top; ++data) {
124 hash = ((hash << 5) + hash) + (*data);
125 }
126 return hash;
127 }
128 };
129
130 StringData() = default;
StringDatatensorflow::internal::__anondd6483390111::StringData131 StringData(const char* data, size_t size) : data(data), size(size) {}
132
operator ==tensorflow::internal::__anondd6483390111::StringData133 bool operator==(const StringData& rhs) const {
134 return size == rhs.size && memcmp(data, rhs.data, size) == 0;
135 }
136
137 const char* data = nullptr;
138 size_t size = 0;
139 };
140
141 using VmoduleMap = std::unordered_map<StringData, int, StringData::Hasher>;
142
143 // Returns a mapping from module name to VLOG level, derived from the
144 // TF_CPP_VMOUDLE environment variable; ownership is transferred to the caller.
VmodulesMapFromEnv()145 VmoduleMap* VmodulesMapFromEnv() {
146 // The value of the env var is supposed to be of the form:
147 // "foo=1,bar=2,baz=3"
148 const char* env = getenv("TF_CPP_VMODULE");
149 if (env == nullptr) {
150 // If there is no TF_CPP_VMODULE configuration (most common case), return
151 // nullptr so that the ShouldVlogModule() API can fast bail out of it.
152 return nullptr;
153 }
154 // The memory returned by getenv() can be invalidated by following getenv() or
155 // setenv() calls. And since we keep references to it in the VmoduleMap in
156 // form of StringData objects, make a copy of it.
157 const char* env_data = strdup(env);
158 VmoduleMap* result = new VmoduleMap();
159 while (true) {
160 const char* eq = strchr(env_data, '=');
161 if (eq == nullptr) {
162 break;
163 }
164 const char* after_eq = eq + 1;
165
166 // Comma either points at the next comma delimiter, or at a null terminator.
167 // We check that the integer we parse ends at this delimiter.
168 const char* comma = strchr(after_eq, ',');
169 const char* new_env_data;
170 if (comma == nullptr) {
171 comma = strchr(after_eq, '\0');
172 new_env_data = comma;
173 } else {
174 new_env_data = comma + 1;
175 }
176 (*result)[StringData(env_data, eq - env_data)] =
177 ParseInteger(after_eq, comma - after_eq);
178 env_data = new_env_data;
179 }
180 return result;
181 }
182
183 } // namespace
184
MinLogLevelFromEnv()185 int64 MinLogLevelFromEnv() {
186 // We don't want to print logs during fuzzing as that would slow fuzzing down
187 // by almost 2x. So, if we are in fuzzing mode (not just running a test), we
188 // return a value so that nothing is actually printed. Since LOG uses >=
189 // (see ~LogMessage in this file) to see if log messages need to be printed,
190 // the value we're interested on to disable printing is the maximum severity.
191 // See also http://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode
192 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
193 return tensorflow::NUM_SEVERITIES;
194 #else
195 const char* tf_env_var_val = getenv("TF_CPP_MIN_LOG_LEVEL");
196 return LogLevelStrToInt(tf_env_var_val);
197 #endif
198 }
199
MinVLogLevelFromEnv()200 int64 MinVLogLevelFromEnv() {
201 // We don't want to print logs during fuzzing as that would slow fuzzing down
202 // by almost 2x. So, if we are in fuzzing mode (not just running a test), we
203 // return a value so that nothing is actually printed. Since VLOG uses <=
204 // (see VLOG_IS_ON in logging.h) to see if log messages need to be printed,
205 // the value we're interested on to disable printing is 0.
206 // See also http://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode
207 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
208 return 0;
209 #else
210 const char* tf_env_var_val = getenv("TF_CPP_MIN_VLOG_LEVEL");
211 return LogLevelStrToInt(tf_env_var_val);
212 #endif
213 }
214
LogMessage(const char * fname,int line,int severity)215 LogMessage::LogMessage(const char* fname, int line, int severity)
216 : fname_(fname), line_(line), severity_(severity) {}
217
~LogMessage()218 LogMessage::~LogMessage() {
219 // Read the min log level once during the first call to logging.
220 static int64 min_log_level = MinLogLevelFromEnv();
221 if (severity_ >= min_log_level) {
222 GenerateLogMessage();
223 }
224 }
225
MinVLogLevel()226 int64 LogMessage::MinVLogLevel() {
227 static int64 min_vlog_level = MinVLogLevelFromEnv();
228 return min_vlog_level;
229 }
230
VmoduleActivated(const char * fname,int level)231 bool LogMessage::VmoduleActivated(const char* fname, int level) {
232 if (level <= MinVLogLevel()) {
233 return true;
234 }
235 static VmoduleMap* vmodules = VmodulesMapFromEnv();
236 if (TF_PREDICT_TRUE(vmodules == nullptr)) {
237 return false;
238 }
239 const char* last_slash = strrchr(fname, '/');
240 const char* module_start = last_slash == nullptr ? fname : last_slash + 1;
241 const char* dot_after = strchr(module_start, '.');
242 const char* module_limit =
243 dot_after == nullptr ? strchr(fname, '\0') : dot_after;
244 StringData module(module_start, module_limit - module_start);
245 auto it = vmodules->find(module);
246 return it != vmodules->end() && it->second >= level;
247 }
248
LogMessageFatal(const char * file,int line)249 LogMessageFatal::LogMessageFatal(const char* file, int line)
250 : LogMessage(file, line, FATAL) {}
~LogMessageFatal()251 LogMessageFatal::~LogMessageFatal() {
252 // abort() ensures we don't return (we promised we would not via
253 // ATTRIBUTE_NORETURN).
254 GenerateLogMessage();
255 abort();
256 }
257
LogString(const char * fname,int line,int severity,const string & message)258 void LogString(const char* fname, int line, int severity,
259 const string& message) {
260 LogMessage(fname, line, severity) << message;
261 }
262
263 template <>
MakeCheckOpValueString(std::ostream * os,const char & v)264 void MakeCheckOpValueString(std::ostream* os, const char& v) {
265 if (v >= 32 && v <= 126) {
266 (*os) << "'" << v << "'";
267 } else {
268 (*os) << "char value " << static_cast<short>(v);
269 }
270 }
271
272 template <>
MakeCheckOpValueString(std::ostream * os,const signed char & v)273 void MakeCheckOpValueString(std::ostream* os, const signed char& v) {
274 if (v >= 32 && v <= 126) {
275 (*os) << "'" << v << "'";
276 } else {
277 (*os) << "signed char value " << static_cast<short>(v);
278 }
279 }
280
281 template <>
MakeCheckOpValueString(std::ostream * os,const unsigned char & v)282 void MakeCheckOpValueString(std::ostream* os, const unsigned char& v) {
283 if (v >= 32 && v <= 126) {
284 (*os) << "'" << v << "'";
285 } else {
286 (*os) << "unsigned char value " << static_cast<unsigned short>(v);
287 }
288 }
289
290 #if LANG_CXX11
291 template <>
MakeCheckOpValueString(std::ostream * os,const std::nullptr_t & p)292 void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& p) {
293 (*os) << "nullptr";
294 }
295 #endif
296
CheckOpMessageBuilder(const char * exprtext)297 CheckOpMessageBuilder::CheckOpMessageBuilder(const char* exprtext)
298 : stream_(new std::ostringstream) {
299 *stream_ << "Check failed: " << exprtext << " (";
300 }
301
~CheckOpMessageBuilder()302 CheckOpMessageBuilder::~CheckOpMessageBuilder() { delete stream_; }
303
ForVar2()304 std::ostream* CheckOpMessageBuilder::ForVar2() {
305 *stream_ << " vs. ";
306 return stream_;
307 }
308
NewString()309 string* CheckOpMessageBuilder::NewString() {
310 *stream_ << ")";
311 return new string(stream_->str());
312 }
313
314 } // namespace internal
315 } // namespace tensorflow
316