• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 #ifdef TF_ANDROID_ENABLE_LOG_EVERY_N_SECONDS
184 class LogEveryNSecState {
185  public:
186   bool ShouldLog(double seconds);
counter()187   uint32 counter() { return counter_.load(std::memory_order_relaxed); }
188 
189  private:
190   std::atomic<uint32> counter_{0};
191   // Cycle count according to CycleClock that we should next log at.
192   std::atomic<int64> next_log_time_cycles_{0};
193 };
194 #endif
195 
196 // This macro has a lot going on!
197 //
198 // * A local static (`logging_internal_stateful_condition_state`) is
199 //   declared in a scope such that each `LOG_EVERY_N` (etc.) line has its own
200 //   state.
201 // * `COUNTER`, the third variable, is used to support `<< COUNTER`. It is not
202 //   mangled, so shadowing can be a problem, albeit more of a
203 //   shoot-yourself-in-the-foot one.  Don't name your variables `COUNTER`.
204 // * A single for loop can declare state and also test
205 //   `condition && state.ShouldLog()`, but there's no way to constrain it to run
206 //   only once (or not at all) without declaring another variable.  The outer
207 //   for-loop declares this variable (`do_log`).
208 // * Using for loops instead of if statements means there's no risk of an
209 //   ambiguous dangling else statement.
210 #define LOGGING_INTERNAL_STATEFUL_CONDITION(kind, condition, arg)   \
211   for (bool logging_internal_stateful_condition_do_log(condition);  \
212        logging_internal_stateful_condition_do_log;                  \
213        logging_internal_stateful_condition_do_log = false)          \
214     for (static ::tensorflow::internal::Log##kind##State            \
215              logging_internal_stateful_condition_state;             \
216          logging_internal_stateful_condition_do_log &&              \
217          logging_internal_stateful_condition_state.ShouldLog(arg);  \
218          logging_internal_stateful_condition_do_log = false)        \
219       for (const uint32_t COUNTER ABSL_ATTRIBUTE_UNUSED =           \
220                logging_internal_stateful_condition_state.counter(); \
221            logging_internal_stateful_condition_do_log;              \
222            logging_internal_stateful_condition_do_log = false)
223 
224 // An instance of `LOG_EVERY_N` increments a hidden zero-initialized counter
225 // every time execution passes through it and logs the specified message when
226 // the counter's value is a multiple of `n`, doing nothing otherwise.  Each
227 // instance has its own counter.  The counter's value can be logged by streaming
228 // the symbol `COUNTER`.  `LOG_EVERY_N` is thread-safe.
229 // Example:
230 //
231 //   for (const auto& user : all_users) {
232 //     LOG_EVERY_N(INFO, 1000) << "Processing user #" << COUNTER;
233 //     ProcessUser(user);
234 //   }
235 #define LOG_EVERY_N(severity, n)                       \
236   LOGGING_INTERNAL_STATEFUL_CONDITION(EveryN, true, n) \
237   LOG(severity)
238 // `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is
239 // logged when the counter's value is less than `n`.  `LOG_FIRST_N` is
240 // thread-safe.
241 #define LOG_FIRST_N(severity, n)                       \
242   LOGGING_INTERNAL_STATEFUL_CONDITION(FirstN, true, n) \
243   LOG(severity)
244 // `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified
245 // message is logged when the counter's value is a power of 2.
246 // `LOG_EVERY_POW_2` is thread-safe.
247 #define LOG_EVERY_POW_2(severity)                         \
248   LOGGING_INTERNAL_STATEFUL_CONDITION(EveryPow2, true, 0) \
249   LOG(severity)
250 // An instance of `LOG_EVERY_N_SEC` uses a hidden state variable to log the
251 // specified message at most once every `n_seconds`.  A hidden counter of
252 // executions (whether a message is logged or not) is also maintained and can be
253 // logged by streaming the symbol `COUNTER`.  `LOG_EVERY_N_SEC` is thread-safe.
254 // Example:
255 //
256 //   LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far";
257 #define LOG_EVERY_N_SEC(severity, n_seconds)                      \
258   LOGGING_INTERNAL_STATEFUL_CONDITION(EveryNSec, true, n_seconds) \
259   LOG(severity)
260 
261 // CHECK dies with a fatal error if condition is not true.  It is *not*
262 // controlled by NDEBUG, so the check will be executed regardless of
263 // compilation mode.  Therefore, it is safe to do things like:
264 //    CHECK(fp->Write(x) == 4)
265 #define CHECK(condition)              \
266   if (TF_PREDICT_FALSE(!(condition))) \
267   LOG(FATAL) << "Check failed: " #condition " "
268 
269 // Function is overloaded for integral types to allow static const
270 // integrals declared in classes and not defined to be used as arguments to
271 // CHECK* macros. It's not encouraged though.
272 template <typename T>
GetReferenceableValue(const T & t)273 inline const T& GetReferenceableValue(const T& t) {
274   return t;
275 }
GetReferenceableValue(char t)276 inline char GetReferenceableValue(char t) { return t; }
GetReferenceableValue(unsigned char t)277 inline unsigned char GetReferenceableValue(unsigned char t) { return t; }
GetReferenceableValue(signed char t)278 inline signed char GetReferenceableValue(signed char t) { return t; }
GetReferenceableValue(int16_t t)279 inline int16 GetReferenceableValue(int16_t t) { return t; }
GetReferenceableValue(uint16 t)280 inline uint16 GetReferenceableValue(uint16 t) { return t; }
GetReferenceableValue(int t)281 inline int GetReferenceableValue(int t) { return t; }
GetReferenceableValue(unsigned int t)282 inline unsigned int GetReferenceableValue(unsigned int t) { return t; }
GetReferenceableValue(int64_t t)283 inline int64 GetReferenceableValue(int64_t t) { return t; }
GetReferenceableValue(uint64 t)284 inline uint64 GetReferenceableValue(uint64 t) { return t; }
285 
286 // This formats a value for a failing CHECK_XX statement.  Ordinarily,
287 // it uses the definition for operator<<, with a few special cases below.
288 template <typename T>
MakeCheckOpValueString(std::ostream * os,const T & v)289 inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
290   (*os) << v;
291 }
292 
293 // Overrides for char types provide readable values for unprintable
294 // characters.
295 template <>
296 void MakeCheckOpValueString(std::ostream* os, const char& v);
297 template <>
298 void MakeCheckOpValueString(std::ostream* os, const signed char& v);
299 template <>
300 void MakeCheckOpValueString(std::ostream* os, const unsigned char& v);
301 
302 #if LANG_CXX11
303 // We need an explicit specialization for std::nullptr_t.
304 template <>
305 void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& v);
306 #endif
307 
308 // A container for a string pointer which can be evaluated to a bool -
309 // true iff the pointer is non-NULL.
310 struct CheckOpString {
CheckOpStringCheckOpString311   explicit CheckOpString(string* str) : str_(str) {}
312   // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
313   // so there's no point in cleaning up str_.
314   explicit operator bool() const { return TF_PREDICT_FALSE(str_ != nullptr); }
315   string* str_;
316 };
317 
318 // Build the error message string. Specify no inlining for code size.
319 template <typename T1, typename T2>
320 string* MakeCheckOpString(const T1& v1, const T2& v2,
321                           const char* exprtext) TF_ATTRIBUTE_NOINLINE;
322 
323 // A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX
324 // statement.  See MakeCheckOpString for sample usage.  Other
325 // approaches were considered: use of a template method (e.g.,
326 // base::BuildCheckOpString(exprtext, base::Print<T1>, &v1,
327 // base::Print<T2>, &v2), however this approach has complications
328 // related to volatile arguments and function-pointer arguments).
329 class CheckOpMessageBuilder {
330  public:
331   // Inserts "exprtext" and " (" to the stream.
332   explicit CheckOpMessageBuilder(const char* exprtext);
333   // Deletes "stream_".
334   ~CheckOpMessageBuilder();
335   // For inserting the first variable.
ForVar1()336   std::ostream* ForVar1() { return stream_; }
337   // For inserting the second variable (adds an intermediate " vs. ").
338   std::ostream* ForVar2();
339   // Get the result (inserts the closing ")").
340   string* NewString();
341 
342  private:
343   std::ostringstream* stream_;
344 };
345 
346 template <typename T1, typename T2>
MakeCheckOpString(const T1 & v1,const T2 & v2,const char * exprtext)347 string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext) {
348   CheckOpMessageBuilder comb(exprtext);
349   MakeCheckOpValueString(comb.ForVar1(), v1);
350   MakeCheckOpValueString(comb.ForVar2(), v2);
351   return comb.NewString();
352 }
353 
354 // Helper functions for CHECK_OP macro.
355 // The (int, int) specialization works around the issue that the compiler
356 // will not instantiate the template version of the function on values of
357 // unnamed enum type - see comment below.
358 // The (size_t, int) and (int, size_t) specialization are to handle unsigned
359 // comparison errors while still being thorough with the comparison.
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   inline string* name##Impl(const size_t v1, const int v2,                \
373                             const char* exprtext) {                       \
374     if (TF_PREDICT_FALSE(v2 < 0)) {                                       \
375       return ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext); \
376     }                                                                     \
377     return name##Impl<size_t, size_t>(v1, v2, exprtext);                  \
378   }                                                                       \
379   inline string* name##Impl(const int v1, const size_t v2,                \
380                             const char* exprtext) {                       \
381     if (TF_PREDICT_FALSE(v2 >= std::numeric_limits<int>::max())) {        \
382       return ::tensorflow::internal::MakeCheckOpString(v1, v2, exprtext); \
383     }                                                                     \
384     const size_t uval = (size_t)((unsigned)v2);                           \
385     return name##Impl<size_t, size_t>(v1, uval, exprtext);                \
386   }
387 
388 // We use the full name Check_EQ, Check_NE, etc. in case the file including
389 // base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
390 // This happens if, for example, those are used as token names in a
391 // yacc grammar.
392 TF_DEFINE_CHECK_OP_IMPL(Check_EQ,
393                         ==)  // Compilation error with CHECK_EQ(NULL, x)?
394 TF_DEFINE_CHECK_OP_IMPL(Check_NE, !=)  // Use CHECK(x == NULL) instead.
395 TF_DEFINE_CHECK_OP_IMPL(Check_LE, <=)
396 TF_DEFINE_CHECK_OP_IMPL(Check_LT, <)
397 TF_DEFINE_CHECK_OP_IMPL(Check_GE, >=)
398 TF_DEFINE_CHECK_OP_IMPL(Check_GT, >)
399 #undef TF_DEFINE_CHECK_OP_IMPL
400 
401 // In optimized mode, use CheckOpString to hint to compiler that
402 // the while condition is unlikely.
403 #define CHECK_OP_LOG(name, op, val1, val2)                     \
404   while (::tensorflow::internal::CheckOpString _result{        \
405       ::tensorflow::internal::name##Impl(                      \
406           ::tensorflow::internal::GetReferenceableValue(val1), \
407           ::tensorflow::internal::GetReferenceableValue(val2), \
408           #val1 " " #op " " #val2)})                           \
409   ::tensorflow::internal::LogMessageFatal(__FILE__, __LINE__) << *(_result.str_)
410 
411 #define CHECK_OP(name, op, val1, val2) CHECK_OP_LOG(name, op, val1, val2)
412 
413 // CHECK_EQ/NE/...
414 #define CHECK_EQ(val1, val2) CHECK_OP(Check_EQ, ==, val1, val2)
415 #define CHECK_NE(val1, val2) CHECK_OP(Check_NE, !=, val1, val2)
416 #define CHECK_LE(val1, val2) CHECK_OP(Check_LE, <=, val1, val2)
417 #define CHECK_LT(val1, val2) CHECK_OP(Check_LT, <, val1, val2)
418 #define CHECK_GE(val1, val2) CHECK_OP(Check_GE, >=, val1, val2)
419 #define CHECK_GT(val1, val2) CHECK_OP(Check_GT, >, val1, val2)
420 #define CHECK_NOTNULL(val)                                 \
421   ::tensorflow::internal::CheckNotNull(__FILE__, __LINE__, \
422                                        "'" #val "' Must be non NULL", (val))
423 
424 #ifndef NDEBUG
425 // DCHECK_EQ/NE/...
426 #define DCHECK(condition) CHECK(condition)
427 #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
428 #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
429 #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
430 #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
431 #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
432 #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
433 
434 #else
435 
436 #define DCHECK(condition) \
437   while (false && (condition)) LOG(FATAL)
438 
439 // NDEBUG is defined, so DCHECK_EQ(x, y) and so on do nothing.
440 // However, we still want the compiler to parse x and y, because
441 // we don't want to lose potentially useful errors and warnings.
442 // _DCHECK_NOP is a helper, and should not be used outside of this file.
443 #define _TF_DCHECK_NOP(x, y) \
444   while (false && ((void)(x), (void)(y), 0)) LOG(FATAL)
445 
446 #define DCHECK_EQ(x, y) _TF_DCHECK_NOP(x, y)
447 #define DCHECK_NE(x, y) _TF_DCHECK_NOP(x, y)
448 #define DCHECK_LE(x, y) _TF_DCHECK_NOP(x, y)
449 #define DCHECK_LT(x, y) _TF_DCHECK_NOP(x, y)
450 #define DCHECK_GE(x, y) _TF_DCHECK_NOP(x, y)
451 #define DCHECK_GT(x, y) _TF_DCHECK_NOP(x, y)
452 
453 #endif
454 
455 // These are for when you don't want a CHECK failure to print a verbose
456 // stack trace.  The implementation of CHECK* in this file already doesn't.
457 #define QCHECK(condition) CHECK(condition)
458 #define QCHECK_EQ(x, y) CHECK_EQ(x, y)
459 #define QCHECK_NE(x, y) CHECK_NE(x, y)
460 #define QCHECK_LE(x, y) CHECK_LE(x, y)
461 #define QCHECK_LT(x, y) CHECK_LT(x, y)
462 #define QCHECK_GE(x, y) CHECK_GE(x, y)
463 #define QCHECK_GT(x, y) CHECK_GT(x, y)
464 
465 template <typename T>
CheckNotNull(const char * file,int line,const char * exprtext,T && t)466 T&& CheckNotNull(const char* file, int line, const char* exprtext, T&& t) {
467   if (t == nullptr) {
468     LogMessageFatal(file, line) << string(exprtext);
469   }
470   return std::forward<T>(t);
471 }
472 
473 int64 MinLogLevelFromEnv();
474 
475 int64 MaxVLogLevelFromEnv();
476 
477 }  // namespace internal
478 
479 #ifdef TF_ANDROID_ENABLE_LOGSINK
480 // LogSink support adapted from //base/logging.h
481 //
482 // `LogSink` is an interface which can be extended to intercept and process
483 // all log messages. LogSink implementations must be thread-safe. A single
484 // instance will be called from whichever thread is performing a logging
485 // operation.
486 class TFLogEntry {
AsAbslLogSeverity(int severity)487   static absl::LogSeverity AsAbslLogSeverity(int severity) {
488     return static_cast<absl::LogSeverity>(severity);
489   }
490 
491  public:
TFLogEntry(int severity,absl::string_view message)492   explicit TFLogEntry(int severity, absl::string_view message)
493       : severity_(AsAbslLogSeverity(severity)), message_(message) {}
494 
TFLogEntry(int severity,absl::string_view fname,int line,absl::string_view message)495   explicit TFLogEntry(int severity, absl::string_view fname, int line,
496                       absl::string_view message)
497       : severity_(AsAbslLogSeverity(severity)),
498         fname_(fname),
499         line_(line),
500         message_(message) {}
501 
log_severity()502   absl::LogSeverity log_severity() const { return severity_; }
FName()503   std::string FName() const { return fname_; }
Line()504   int Line() const { return line_; }
ToString()505   std::string ToString() const { return message_; }
text_message()506   absl::string_view text_message() const { return message_; }
507 
508  private:
509   const absl::LogSeverity severity_;
510   const std::string fname_;
511   int line_ = -1;
512   const std::string message_;
513 };
514 
515 class TFLogSink {
516  public:
517   virtual ~TFLogSink() = default;
518 
519   // `Send` is called synchronously during the log statement.  The logging
520   // module guarantees not to call `Send` concurrently on the same log sink.
521   // Implementations should be careful not to call`LOG` or `CHECK` or take
522   // any locks that might be held by the `LOG` caller, to avoid deadlock.
523   //
524   // `e` is guaranteed to remain valid until the subsequent call to
525   // `WaitTillSent` completes, so implementations may store a pointer to or
526   // copy of `e` (e.g. in a thread local variable) for use in `WaitTillSent`.
527   virtual void Send(const TFLogEntry& entry) = 0;
528 
529   // `WaitTillSent` blocks the calling thread (the thread that generated a log
530   // message) until the sink has finished processing the log message.
531   // `WaitTillSent` is called once per log message, following the call to
532   // `Send`.  This may be useful when log messages are buffered or processed
533   // asynchronously by an expensive log sink.
534   // The default implementation returns immediately.  Like `Send`,
535   // implementations should be careful not to call `LOG` or `CHECK or take any
536   // locks that might be held by the `LOG` caller, to avoid deadlock.
WaitTillSent()537   virtual void WaitTillSent() {}
538 };
539 
540 // This is the default log sink. This log sink is used if there are no other
541 // log sinks registered. To disable the default log sink, set the
542 // "no_default_logger" Bazel config setting to true or define a
543 // NO_DEFAULT_LOGGER preprocessor symbol. This log sink will always log to
544 // stderr.
545 class TFDefaultLogSink : public TFLogSink {
546  public:
547   void Send(const TFLogEntry& entry) override;
548 };
549 
550 // Add or remove a `LogSink` as a consumer of logging data.  Thread-safe.
551 void TFAddLogSink(TFLogSink* sink);
552 void TFRemoveLogSink(TFLogSink* sink);
553 // Get all the log sinks.  Thread-safe.
554 std::vector<TFLogSink*> TFGetLogSinks();
555 #endif  // TF_ANDROID_ENABLE_LOGSINK
556 
557 }  // namespace tensorflow
558 
559 #endif  // TENSORFLOW_CORE_PLATFORM_DEFAULT_LOGGING_H_
560