Flutter Engine
logging.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_FML_LOGGING_H_
6 #define FLUTTER_FML_LOGGING_H_
7 
8 #include <sstream>
9 
10 #include "flutter/fml/log_level.h"
11 #include "flutter/fml/macros.h"
12 
13 namespace fml {
14 
16  public:
17  void operator&(std::ostream&) {}
18 };
19 
20 class LogMessage {
21  public:
22  LogMessage(LogSeverity severity,
23  const char* file,
24  int line,
25  const char* condition);
26  ~LogMessage();
27 
28  std::ostream& stream() { return stream_; }
29 
30  private:
31  std::ostringstream stream_;
32  const LogSeverity severity_;
33  const char* file_;
34  const int line_;
35 
37 };
38 
39 // Gets the FML_VLOG default verbosity level.
40 int GetVlogVerbosity();
41 
42 // Returns true if |severity| is at or above the current minimum log level.
43 // LOG_FATAL and above is always true.
44 bool ShouldCreateLogMessage(LogSeverity severity);
45 
46 [[noreturn]] void KillProcess();
47 
48 } // namespace fml
49 
50 #define FML_LOG_STREAM(severity) \
51  ::fml::LogMessage(::fml::LOG_##severity, __FILE__, __LINE__, nullptr).stream()
52 
53 #define FML_LAZY_STREAM(stream, condition) \
54  !(condition) ? (void)0 : ::fml::LogMessageVoidify() & (stream)
55 
56 #define FML_EAT_STREAM_PARAMETERS(ignored) \
57  true || (ignored) \
58  ? (void)0 \
59  : ::fml::LogMessageVoidify() & \
60  ::fml::LogMessage(::fml::LOG_FATAL, 0, 0, nullptr).stream()
61 
62 #define FML_LOG_IS_ON(severity) \
63  (::fml::ShouldCreateLogMessage(::fml::LOG_##severity))
64 
65 #define FML_LOG(severity) \
66  FML_LAZY_STREAM(FML_LOG_STREAM(severity), FML_LOG_IS_ON(severity))
67 
68 #define FML_CHECK(condition) \
69  FML_LAZY_STREAM( \
70  ::fml::LogMessage(::fml::LOG_FATAL, __FILE__, __LINE__, #condition) \
71  .stream(), \
72  !(condition))
73 
74 #define FML_VLOG_IS_ON(verbose_level) \
75  ((verbose_level) <= ::fml::GetVlogVerbosity())
76 
77 // The VLOG macros log with negative verbosities.
78 #define FML_VLOG_STREAM(verbose_level) \
79  ::fml::LogMessage(-verbose_level, __FILE__, __LINE__, nullptr).stream()
80 
81 #define FML_VLOG(verbose_level) \
82  FML_LAZY_STREAM(FML_VLOG_STREAM(verbose_level), FML_VLOG_IS_ON(verbose_level))
83 
84 #ifndef NDEBUG
85 #define FML_DLOG(severity) FML_LOG(severity)
86 #define FML_DCHECK(condition) FML_CHECK(condition)
87 #else
88 #define FML_DLOG(severity) FML_EAT_STREAM_PARAMETERS(true)
89 #define FML_DCHECK(condition) FML_EAT_STREAM_PARAMETERS(condition)
90 #endif
91 
92 #define FML_UNREACHABLE() \
93  { \
94  FML_LOG(ERROR) << "Reached unreachable code."; \
95  ::fml::KillProcess(); \
96  }
97 
98 #endif // FLUTTER_FML_LOGGING_H_
std::ostream & stream()
Definition: logging.h:28
void operator &(std::ostream &)
Definition: logging.h:17
Definition: ascii_trie.cc:9
int LogSeverity
Definition: log_level.h:10
int GetVlogVerbosity()
Definition: logging.cc:100
bool ShouldCreateLogMessage(LogSeverity severity)
Definition: logging.cc:104
void KillProcess()
Definition: logging.cc:108
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27