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