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