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 #if defined(_WIN32)
17 // prevent compile error because MSVC doesn't realize in debug build that
18 // LOG(FATAL) finally invokes abort()
19 #pragma warning(disable : 4716)
20 #endif // _WIN32
21
22 #ifndef TENSORFLOW_CORE_PLATFORM_DEFAULT_LOGGING_H_
23 #define TENSORFLOW_CORE_PLATFORM_DEFAULT_LOGGING_H_
24
25 // IWYU pragma: private, include "third_party/tensorflow/core/platform/logging.h"
26 // IWYU pragma: friend third_party/tensorflow/core/platform/logging.h
27
28 #include <atomic>
29 #include <limits>
30 #include <memory>
31 #include <sstream>
32 #include <vector>
33
34 #ifdef TF_ANDROID_ENABLE_LOGSINK
35 #include "absl/base/log_severity.h"
36 #include "absl/strings/string_view.h"
37 #endif // TF_ANDROID_ENABLE_LOGSINK
38 #include "tensorflow/core/platform/macros.h"
39 #include "tensorflow/core/platform/types.h"
40
41 // TODO(mrry): Prevent this Windows.h #define from leaking out of our headers.
42 #undef ERROR
43
44 namespace tensorflow {
45 const int INFO = 0; // base_logging::INFO;
46 const int WARNING = 1; // base_logging::WARNING;
47 const int ERROR = 2; // base_logging::ERROR;
48 const int FATAL = 3; // base_logging::FATAL;
49 const int NUM_SEVERITIES = 4; // base_logging::NUM_SEVERITIES;
50
51 namespace internal {
52
53 class LogMessage : public std::basic_ostringstream<char> {
54 public:
55 LogMessage(const char* fname, int line, int severity);
56 ~LogMessage() override;
57
58 // Change the location of the log message.
59 LogMessage& AtLocation(const char* fname, int line);
60
61 // Returns the maximum log level for VLOG statements.
62 // E.g., if MaxVLogLevel() is 2, then VLOG(2) statements will produce output,
63 // but VLOG(3) will not. Defaults to 0.
64 static int64_t MaxVLogLevel();
65
66 // Returns whether VLOG level lvl is activated for the file fname.
67 //
68 // E.g. if the environment variable TF_CPP_VMODULE contains foo=3 and fname is
69 // foo.cc and lvl is <= 3, this will return true. It will also return true if
70 // the level is lower or equal to TF_CPP_MAX_VLOG_LEVEL (default zero).
71 //
72 // It is expected that the result of this query will be cached in the VLOG-ing
73 // call site to avoid repeated lookups. This routine performs a hash-map
74 // access against the VLOG-ing specification provided by the env var.
75 static bool VmoduleActivated(const char* fname, int level);
76
77 protected:
78 void GenerateLogMessage();
79
80 private:
81 const char* fname_;
82 int line_;
83 int severity_;
84 };
85
86 // Uses the lower operator & precedence to voidify a LogMessage reference, so
87 // that the ternary VLOG() implementation is balanced, type wise.
88 struct Voidifier {
89 template <typename T>
90 void operator&(const T&) const {}
91 };
92
93 // LogMessageFatal ensures the process will exit in failure after
94 // logging this message.
95 class LogMessageFatal : public LogMessage {
96 public:
97 LogMessageFatal(const char* file, int line) TF_ATTRIBUTE_COLD;
98 TF_ATTRIBUTE_NORETURN ~LogMessageFatal() override;
99 };
100
101 // LogMessageNull supports the DVLOG macro by simply dropping any log messages.
102 class LogMessageNull : public std::basic_ostringstream<char> {
103 public:
LogMessageNull()104 LogMessageNull() {}
~LogMessageNull()105 ~LogMessageNull() override {}
106 };
107
108 #define _TF_LOG_INFO \
109 ::tensorflow::internal::LogMessage(__FILE__, __LINE__, ::tensorflow::INFO)
110 #define _TF_LOG_WARNING \
111 ::tensorflow::internal::LogMessage(__FILE__, __LINE__, ::tensorflow::WARNING)
112 #define _TF_LOG_ERROR \
113 ::tensorflow::internal::LogMessage(__FILE__, __LINE__, ::tensorflow::ERROR)
114 #define _TF_LOG_FATAL \
115 ::tensorflow::internal::LogMessageFatal(__FILE__, __LINE__)
116
117 #define _TF_LOG_QFATAL _TF_LOG_FATAL
118
119 #define LOG(severity) _TF_LOG_##severity
120
121 #ifdef IS_MOBILE_PLATFORM
122
123 // Turn VLOG off when under mobile devices for considerations of binary size.
124 #define VLOG_IS_ON(lvl) ((lvl) <= 0)
125
126 #else
127
128 // Otherwise, set TF_CPP_MAX_VLOG_LEVEL environment to update minimum log level
129 // of VLOG, or TF_CPP_VMODULE to set the minimum log level for individual
130 // translation units.
131 #define VLOG_IS_ON(lvl) \
132 (([](int level, const char* fname) { \
133 static const bool vmodule_activated = \
134 ::tensorflow::internal::LogMessage::VmoduleActivated(fname, level); \
135 return vmodule_activated; \
136 })(lvl, __FILE__))
137
138 #endif
139
140 #define VLOG(level) \
141 TF_PREDICT_TRUE(!VLOG_IS_ON(level)) \
142 ? (void)0 \
143 : ::tensorflow::internal::Voidifier() & \
144 ::tensorflow::internal::LogMessage(__FILE__, __LINE__, \
145 tensorflow::INFO)
146
147 // `DVLOG` behaves like `VLOG` in debug mode (i.e. `#ifndef NDEBUG`).
148 // Otherwise, it compiles away and does nothing.
149 #ifndef NDEBUG
150 #define DVLOG VLOG
151 #else
152 #define DVLOG(verbose_level) \
153 while (false && (verbose_level) > 0) ::tensorflow::internal::LogMessageNull()
154 #endif
155
156 class LogEveryNState {
157 public:
158 bool ShouldLog(int n);
counter()159 uint32_t counter() { return counter_.load(std::memory_order_relaxed); }
160
161 private:
162 std::atomic<uint32> counter_{0};
163 };
164
165 class LogFirstNState {
166 public:
167 bool ShouldLog(int n);
counter()168 uint32 counter() { return counter_.load(std::memory_order_relaxed); }
169
170 private:
171 std::atomic<uint32> counter_{0};
172 };
173
174 class LogEveryPow2State {
175 public:
176 bool ShouldLog(int ignored);
counter()177 uint32 counter() { return counter_.load(std::memory_order_relaxed); }
178
179 private:
180 std::atomic<uint32> counter_{0};
181 };
182
183 class LogEveryNSecState {
184 public:
185 bool ShouldLog(double seconds);
counter()186 uint32 counter() { return counter_.load(std::memory_order_relaxed); }
187
188 private:
189 std::atomic<uint32> counter_{0};
190 // Cycle count according to CycleClock that we should next log at.
191 std::atomic<int64_t> next_log_time_cycles_{0};
192 };
193
194 // This macro has a lot going on!
195 //
196 // * A local static (`logging_internal_stateful_condition_state`) is
197 // declared in a scope such that each `LOG_EVERY_N` (etc.) line has its own
198 // state.
199 // * `COUNTER`, the third variable, is used to support `<< COUNTER`. It is not
200 // mangled, so shadowing can be a problem, albeit more of a
201 // shoot-yourself-in-the-foot one. Don't name your variables `COUNTER`.
202 // * A single for loop can declare state and also test
203 // `condition && state.ShouldLog()`, but there's no way to constrain it to run
204 // only once (or not at all) without declaring another variable. The outer
205 // for-loop declares this variable (`do_log`).
206 // * Using for loops instead of if statements means there's no risk of an
207 // ambiguous dangling else statement.
208 #define LOGGING_INTERNAL_STATEFUL_CONDITION(kind, condition, arg) \
209 for (bool logging_internal_stateful_condition_do_log(condition); \
210 logging_internal_stateful_condition_do_log; \
211 logging_internal_stateful_condition_do_log = false) \
212 for (static ::tensorflow::internal::Log##kind##State \
213 logging_internal_stateful_condition_state; \
214 logging_internal_stateful_condition_do_log && \
215 logging_internal_stateful_condition_state.ShouldLog(arg); \
216 logging_internal_stateful_condition_do_log = false) \
217 for (const uint32_t COUNTER ABSL_ATTRIBUTE_UNUSED = \
218 logging_internal_stateful_condition_state.counter(); \
219 logging_internal_stateful_condition_do_log; \
220 logging_internal_stateful_condition_do_log = false)
221
222 // An instance of `LOG_EVERY_N` increments a hidden zero-initialized counter
223 // every time execution passes through it and logs the specified message when
224 // the counter's value is a multiple of `n`, doing nothing otherwise. Each
225 // instance has its own counter. The counter's value can be logged by streaming
226 // the symbol `COUNTER`. `LOG_EVERY_N` is thread-safe.
227 // Example:
228 //
229 // for (const auto& user : all_users) {
230 // LOG_EVERY_N(INFO, 1000) << "Processing user #" << COUNTER;
231 // ProcessUser(user);
232 // }
233 #define LOG_EVERY_N(severity, n) \
234 LOGGING_INTERNAL_STATEFUL_CONDITION(EveryN, true, n) \
235 LOG(severity)
236 // `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is
237 // logged when the counter's value is less than `n`. `LOG_FIRST_N` is
238 // thread-safe.
239 #define LOG_FIRST_N(severity, n) \
240 LOGGING_INTERNAL_STATEFUL_CONDITION(FirstN, true, n) \
241 LOG(severity)
242 // `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified
243 // message is logged when the counter's value is a power of 2.
244 // `LOG_EVERY_POW_2` is thread-safe.
245 #define LOG_EVERY_POW_2(severity) \
246 LOGGING_INTERNAL_STATEFUL_CONDITION(EveryPow2, true, 0) \
247 LOG(severity)
248 // An instance of `LOG_EVERY_N_SEC` uses a hidden state variable to log the
249 // specified message at most once every `n_seconds`. A hidden counter of
250 // executions (whether a message is logged or not) is also maintained and can be
251 // logged by streaming the symbol `COUNTER`. `LOG_EVERY_N_SEC` is thread-safe.
252 // Example:
253 //
254 // LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far";
255 #define LOG_EVERY_N_SEC(severity, n_seconds) \
256 LOGGING_INTERNAL_STATEFUL_CONDITION(EveryNSec, true, n_seconds) \
257 LOG(severity)
258
259 // CHECK dies with a fatal error if condition is not true. It is *not*
260 // controlled by NDEBUG, so the check will be executed regardless of
261 // compilation mode. Therefore, it is safe to do things like:
262 // CHECK(fp->Write(x) == 4)
263 #define CHECK(condition) \
264 if (TF_PREDICT_FALSE(!(condition))) \
265 LOG(FATAL) << "Check failed: " #condition " "
266
267 // Function is overloaded for integral types to allow static const
268 // integrals declared in classes and not defined to be used as arguments to
269 // CHECK* macros. It's not encouraged though.
270 template <typename T>
GetReferenceableValue(const T & t)271 inline const T& GetReferenceableValue(const T& t) {
272 return t;
273 }
GetReferenceableValue(char t)274 inline char GetReferenceableValue(char t) { return t; }
GetReferenceableValue(unsigned char t)275 inline unsigned char GetReferenceableValue(unsigned char t) { return t; }
GetReferenceableValue(signed char t)276 inline signed char GetReferenceableValue(signed char t) { return t; }
GetReferenceableValue(int16_t t)277 inline int16 GetReferenceableValue(int16_t t) { return t; }
GetReferenceableValue(uint16 t)278 inline uint16 GetReferenceableValue(uint16 t) { return t; }
GetReferenceableValue(int t)279 inline int GetReferenceableValue(int t) { return t; }
GetReferenceableValue(unsigned int t)280 inline unsigned int GetReferenceableValue(unsigned int t) { return t; }
GetReferenceableValue(int64_t t)281 inline int64_t GetReferenceableValue(int64_t t) { return t; }
GetReferenceableValue(uint64 t)282 inline uint64 GetReferenceableValue(uint64 t) { return t; }
283
284 // This formats a value for a failing CHECK_XX statement. Ordinarily,
285 // it uses the definition for operator<<, with a few special cases below.
286 template <typename T>
MakeCheckOpValueString(std::ostream * os,const T & v)287 inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
288 (*os) << v;
289 }
290
291 // Overrides for char types provide readable values for unprintable
292 // characters.
293 template <>
294 void MakeCheckOpValueString(std::ostream* os, const char& v);
295 template <>
296 void MakeCheckOpValueString(std::ostream* os, const signed char& v);
297 template <>
298 void MakeCheckOpValueString(std::ostream* os, const unsigned char& v);
299
300 #if LANG_CXX11
301 // We need an explicit specialization for std::nullptr_t.
302 template <>
303 void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& v);
304 #endif
305
306 // A container for a string pointer which can be evaluated to a bool -
307 // true iff the pointer is non-NULL.
308 struct CheckOpString {
CheckOpStringCheckOpString309 explicit CheckOpString(string* str) : str_(str) {}
310 // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
311 // so there's no point in cleaning up str_.
312 explicit operator bool() const { return TF_PREDICT_FALSE(str_ != nullptr); }
313 string* str_;
314 };
315
316 // Build the error message string. Specify no inlining for code size.
317 template <typename T1, typename T2>
318 string* MakeCheckOpString(const T1& v1, const T2& v2,
319 const char* exprtext) TF_ATTRIBUTE_NOINLINE;
320
321 // A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX
322 // statement. See MakeCheckOpString for sample usage. Other
323 // approaches were considered: use of a template method (e.g.,
324 // base::BuildCheckOpString(exprtext, base::Print<T1>, &v1,
325 // base::Print<T2>, &v2), however this approach has complications
326 // related to volatile arguments and function-pointer arguments).
327 class CheckOpMessageBuilder {
328 public:
329 // Inserts "exprtext" and " (" to the stream.
330 explicit CheckOpMessageBuilder(const char* exprtext);
331 // Deletes "stream_".
332 ~CheckOpMessageBuilder();
333 // For inserting the first variable.
ForVar1()334 std::ostream* ForVar1() { return stream_; }
335 // For inserting the second variable (adds an intermediate " vs. ").
336 std::ostream* ForVar2();
337 // Get the result (inserts the closing ")").
338 string* NewString();
339
340 private:
341 std::ostringstream* stream_;
342 };
343
344 template <typename T1, typename T2>
MakeCheckOpString(const T1 & v1,const T2 & v2,const char * exprtext)345 string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext) {
346 CheckOpMessageBuilder comb(exprtext);
347 MakeCheckOpValueString(comb.ForVar1(), v1);
348 MakeCheckOpValueString(comb.ForVar2(), v2);
349 return comb.NewString();
350 }
351
352 // Helper functions for CHECK_OP macro.
353 // We use the full name Check_EQ, Check_NE, etc. in case the file including
354 // base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
355 // This happens if, for example, those are used as token names in a
356 // yacc grammar.
357 // The (int, int) overload works around the issue that the compiler
358 // will not instantiate the template version of the function on values of
359 // unnamed enum type - see comment below.
360 #define TF_DEFINE_CHECK_OP_IMPL(name, op) \
361 template <typename T1, typename T2> \
362 inline string* name##Impl(const T1& v1, const T2& v2, \
363 const char* exprtext) { \
364 if (TF_PREDICT_TRUE(v1 op v2)) \
365 return NULL; \
366 else \
367 return ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext); \
368 } \
369 inline string* name##Impl(int v1, int v2, const char* exprtext) { \
370 return name##Impl<int, int>(v1, v2, exprtext); \
371 }
372
373 // The (size_t, int) and (int, size_t) specialization are to handle unsigned
374 // comparison errors while still being thorough with the comparison.
375
376 TF_DEFINE_CHECK_OP_IMPL(Check_EQ, ==)
377 // Compilation error with CHECK_EQ(NULL, x)?
378 // Use CHECK(x == NULL) instead.
379
Check_EQImpl(int v1,size_t v2,const char * exprtext)380 inline string* Check_EQImpl(int v1, size_t v2, const char* exprtext) {
381 if (TF_PREDICT_FALSE(v1 < 0))
382 ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext);
383
384 return Check_EQImpl(size_t(v1), v2, exprtext);
385 }
386
Check_EQImpl(size_t v1,int v2,const char * exprtext)387 inline string* Check_EQImpl(size_t v1, int v2, const char* exprtext) {
388 return Check_EQImpl(v2, v1, exprtext);
389 }
390
391 TF_DEFINE_CHECK_OP_IMPL(Check_NE, !=)
392
Check_NEImpl(int v1,size_t v2,const char * exprtext)393 inline string* Check_NEImpl(int v1, size_t v2, const char* exprtext) {
394 if (v1 < 0) return NULL;
395
396 return Check_NEImpl(size_t(v1), v2, exprtext);
397 }
398
Check_NEImpl(size_t v1,int v2,const char * exprtext)399 inline string* Check_NEImpl(size_t v1, int v2, const char* exprtext) {
400 return Check_NEImpl(v2, v1, exprtext);
401 }
402
403 TF_DEFINE_CHECK_OP_IMPL(Check_LE, <=)
404
Check_LEImpl(int v1,size_t v2,const char * exprtext)405 inline string* Check_LEImpl(int v1, size_t v2, const char* exprtext) {
406 if (v1 <= 0) return NULL;
407
408 return Check_LEImpl(size_t(v1), v2, exprtext);
409 }
410
Check_LEImpl(size_t v1,int v2,const char * exprtext)411 inline string* Check_LEImpl(size_t v1, int v2, const char* exprtext) {
412 if (TF_PREDICT_FALSE(v2 < 0))
413 return ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext);
414 return Check_LEImpl(v1, size_t(v2), exprtext);
415 }
416
417 TF_DEFINE_CHECK_OP_IMPL(Check_LT, <)
418
Check_LTImpl(int v1,size_t v2,const char * exprtext)419 inline string* Check_LTImpl(int v1, size_t v2, const char* exprtext) {
420 if (v1 < 0) return NULL;
421
422 return Check_LTImpl(size_t(v1), v2, exprtext);
423 }
424
Check_LTImpl(size_t v1,int v2,const char * exprtext)425 inline string* Check_LTImpl(size_t v1, int v2, const char* exprtext) {
426 if (v2 < 0)
427 return ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext);
428 return Check_LTImpl(v1, size_t(v2), exprtext);
429 }
430
431 // Implement GE,GT in terms of LE,LT
432 template <typename T1, typename T2>
Check_GEImpl(const T1 & v1,const T2 & v2,const char * exprtext)433 inline string* Check_GEImpl(const T1& v1, const T2& v2, const char* exprtext) {
434 return Check_LEImpl(v2, v1, exprtext);
435 }
436
437 template <typename T1, typename T2>
Check_GTImpl(const T1 & v1,const T2 & v2,const char * exprtext)438 inline string* Check_GTImpl(const T1& v1, const T2& v2, const char* exprtext) {
439 return Check_LTImpl(v2, v1, exprtext);
440 }
441
442 #undef TF_DEFINE_CHECK_OP_IMPL
443
444 // In optimized mode, use CheckOpString to hint to compiler that
445 // the while condition is unlikely.
446 #define CHECK_OP_LOG(name, op, val1, val2) \
447 while (::tensorflow::internal::CheckOpString _result{ \
448 ::tensorflow::internal::name##Impl( \
449 ::tensorflow::internal::GetReferenceableValue(val1), \
450 ::tensorflow::internal::GetReferenceableValue(val2), \
451 #val1 " " #op " " #val2)}) \
452 ::tensorflow::internal::LogMessageFatal(__FILE__, __LINE__) << *(_result.str_)
453
454 #define CHECK_OP(name, op, val1, val2) CHECK_OP_LOG(name, op, val1, val2)
455
456 // CHECK_EQ/NE/...
457 #define CHECK_EQ(val1, val2) CHECK_OP(Check_EQ, ==, val1, val2)
458 #define CHECK_NE(val1, val2) CHECK_OP(Check_NE, !=, val1, val2)
459 #define CHECK_LE(val1, val2) CHECK_OP(Check_LE, <=, val1, val2)
460 #define CHECK_LT(val1, val2) CHECK_OP(Check_LT, <, val1, val2)
461 #define CHECK_GE(val1, val2) CHECK_OP(Check_GE, >=, val1, val2)
462 #define CHECK_GT(val1, val2) CHECK_OP(Check_GT, >, val1, val2)
463 #define CHECK_NOTNULL(val) \
464 ::tensorflow::internal::CheckNotNull(__FILE__, __LINE__, \
465 "'" #val "' Must be non NULL", (val))
466
467 #ifndef NDEBUG
468 // DCHECK_EQ/NE/...
469 #define DCHECK(condition) CHECK(condition)
470 #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
471 #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
472 #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
473 #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
474 #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
475 #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
476
477 #else
478
479 #define DCHECK(condition) \
480 while (false && (condition)) LOG(FATAL)
481
482 // NDEBUG is defined, so DCHECK_EQ(x, y) and so on do nothing.
483 // However, we still want the compiler to parse x and y, because
484 // we don't want to lose potentially useful errors and warnings.
485 // _DCHECK_NOP is a helper, and should not be used outside of this file.
486 #define _TF_DCHECK_NOP(x, y) \
487 while (false && ((void)(x), (void)(y), 0)) LOG(FATAL)
488
489 #define DCHECK_EQ(x, y) _TF_DCHECK_NOP(x, y)
490 #define DCHECK_NE(x, y) _TF_DCHECK_NOP(x, y)
491 #define DCHECK_LE(x, y) _TF_DCHECK_NOP(x, y)
492 #define DCHECK_LT(x, y) _TF_DCHECK_NOP(x, y)
493 #define DCHECK_GE(x, y) _TF_DCHECK_NOP(x, y)
494 #define DCHECK_GT(x, y) _TF_DCHECK_NOP(x, y)
495
496 #endif
497
498 // These are for when you don't want a CHECK failure to print a verbose
499 // stack trace. The implementation of CHECK* in this file already doesn't.
500 #define QCHECK(condition) CHECK(condition)
501 #define QCHECK_EQ(x, y) CHECK_EQ(x, y)
502 #define QCHECK_NE(x, y) CHECK_NE(x, y)
503 #define QCHECK_LE(x, y) CHECK_LE(x, y)
504 #define QCHECK_LT(x, y) CHECK_LT(x, y)
505 #define QCHECK_GE(x, y) CHECK_GE(x, y)
506 #define QCHECK_GT(x, y) CHECK_GT(x, y)
507
508 template <typename T>
CheckNotNull(const char * file,int line,const char * exprtext,T && t)509 T&& CheckNotNull(const char* file, int line, const char* exprtext, T&& t) {
510 if (t == nullptr) {
511 LogMessageFatal(file, line) << string(exprtext);
512 }
513 return std::forward<T>(t);
514 }
515
516 int64_t MinLogLevelFromEnv();
517
518 int64_t MaxVLogLevelFromEnv();
519
520 } // namespace internal
521
522 #ifdef TF_ANDROID_ENABLE_LOGSINK
523 // LogSink support adapted from //base/logging.h
524 //
525 // `LogSink` is an interface which can be extended to intercept and process
526 // all log messages. LogSink implementations must be thread-safe. A single
527 // instance will be called from whichever thread is performing a logging
528 // operation.
529 class TFLogEntry {
AsAbslLogSeverity(int severity)530 static absl::LogSeverity AsAbslLogSeverity(int severity) {
531 return static_cast<absl::LogSeverity>(severity);
532 }
533
534 public:
TFLogEntry(int severity,absl::string_view message)535 explicit TFLogEntry(int severity, absl::string_view message)
536 : severity_(AsAbslLogSeverity(severity)), message_(message) {}
537
TFLogEntry(int severity,absl::string_view fname,int line,absl::string_view message)538 explicit TFLogEntry(int severity, absl::string_view fname, int line,
539 absl::string_view message)
540 : severity_(AsAbslLogSeverity(severity)),
541 fname_(fname),
542 line_(line),
543 message_(message) {}
544
log_severity()545 absl::LogSeverity log_severity() const { return severity_; }
FName()546 std::string FName() const { return fname_; }
Line()547 int Line() const { return line_; }
ToString()548 std::string ToString() const { return message_; }
text_message()549 absl::string_view text_message() const { return message_; }
550
551 private:
552 const absl::LogSeverity severity_;
553 const std::string fname_;
554 int line_ = -1;
555 const std::string message_;
556 };
557
558 class TFLogSink {
559 public:
560 virtual ~TFLogSink() = default;
561
562 // `Send` is called synchronously during the log statement. The logging
563 // module guarantees not to call `Send` concurrently on the same log sink.
564 // Implementations should be careful not to call`LOG` or `CHECK` or take
565 // any locks that might be held by the `LOG` caller, to avoid deadlock.
566 //
567 // `e` is guaranteed to remain valid until the subsequent call to
568 // `WaitTillSent` completes, so implementations may store a pointer to or
569 // copy of `e` (e.g. in a thread local variable) for use in `WaitTillSent`.
570 virtual void Send(const TFLogEntry& entry) = 0;
571
572 // `WaitTillSent` blocks the calling thread (the thread that generated a log
573 // message) until the sink has finished processing the log message.
574 // `WaitTillSent` is called once per log message, following the call to
575 // `Send`. This may be useful when log messages are buffered or processed
576 // asynchronously by an expensive log sink.
577 // The default implementation returns immediately. Like `Send`,
578 // implementations should be careful not to call `LOG` or `CHECK or take any
579 // locks that might be held by the `LOG` caller, to avoid deadlock.
WaitTillSent()580 virtual void WaitTillSent() {}
581 };
582
583 // This is the default log sink. This log sink is used if there are no other
584 // log sinks registered. To disable the default log sink, set the
585 // "no_default_logger" Bazel config setting to true or define a
586 // NO_DEFAULT_LOGGER preprocessor symbol. This log sink will always log to
587 // stderr.
588 class TFDefaultLogSink : public TFLogSink {
589 public:
590 void Send(const TFLogEntry& entry) override;
591 };
592
593 // Add or remove a `LogSink` as a consumer of logging data. Thread-safe.
594 void TFAddLogSink(TFLogSink* sink);
595 void TFRemoveLogSink(TFLogSink* sink);
596
597 // Get all the log sinks. Thread-safe.
598 std::vector<TFLogSink*> TFGetLogSinks();
599 #endif // TF_ANDROID_ENABLE_LOGSINK
600
601 // Change verbose level of pre-defined files if envorionment
602 // variable `env_var` is defined. This is currently a no op.
603 void UpdateLogVerbosityIfDefined(const char* env_var);
604
605 } // namespace tensorflow
606
607 #endif // TENSORFLOW_CORE_PLATFORM_DEFAULT_LOGGING_H_
608