Flutter Engine
 
Loading...
Searching...
No Matches
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
11#include "flutter/fml/macros.h"
12
13namespace fml {
14
15namespace testing {
16struct LogCapture {
17 LogCapture();
19
20 std::string str() const;
21
22 private:
23 std::ostringstream stream_;
24};
25} // namespace testing
26
28 public:
29 void operator&(std::ostream&) {}
30};
31
33 public:
34 LogMessage(LogSeverity severity,
35 const char* file,
36 int line,
37 const char* condition);
39
40 std::ostream& stream() { return stream_; }
41
42 static void CaptureNextLog(std::ostringstream* stream);
43
44 private:
45 // This is a raw pointer so that we avoid having a non-trivially-destructible
46 // static. It is only ever for use in unit tests.
47 static thread_local std::ostringstream* capture_next_log_stream_;
48 std::ostringstream stream_;
49 const LogSeverity severity_;
50 const char* file_;
51 const int line_;
52
54};
55
56// Gets the FML_VLOG default verbosity level.
58
59// Returns true if |severity| is at or above the current minimum log level.
60// kLogFatal and above is always true.
62
64 bool true_arg) {
65 if (true_arg) {
66 return ShouldCreateLogMessage(severity);
67 }
68 return false;
69}
70
71[[noreturn]] void KillProcess();
72
73[[noreturn]] constexpr void KillProcessConstexpr(bool true_arg) {
74 if (true_arg) {
76 }
77#if defined(_MSC_VER) && !defined(__clang__)
78 __assume(false);
79#else // defined(_MSC_VER) && !defined(__clang__)
80 __builtin_unreachable();
81#endif // defined(_MSC_VER) && !defined(__clang__)
82}
83
84} // namespace fml
85
86#define FML_LOG_STREAM(severity) \
87 ::fml::LogMessage(::fml::LOG_##severity, __FILE__, __LINE__, nullptr).stream()
88
89#define FML_LAZY_STREAM(stream, condition) \
90 !(condition) ? (void)0 : ::fml::LogMessageVoidify() & (stream)
91
92#define FML_EAT_STREAM_PARAMETERS(ignored) \
93 true || (ignored) \
94 ? (void)0 \
95 : ::fml::LogMessageVoidify() & \
96 ::fml::LogMessage(::fml::kLogFatal, 0, 0, nullptr).stream()
97
98#define FML_LOG_IS_ON(severity) \
99 (::fml::ShouldCreateLogMessageConstexpr(::fml::LOG_##severity, true))
100
101#define FML_LOG(severity) \
102 FML_LAZY_STREAM(FML_LOG_STREAM(severity), FML_LOG_IS_ON(severity))
103
104#define FML_CHECK(condition) \
105 FML_LAZY_STREAM( \
106 ::fml::LogMessage(::fml::kLogFatal, __FILE__, __LINE__, #condition) \
107 .stream(), \
108 !(condition))
109
110#define FML_VLOG_IS_ON(verbose_level) \
111 ((verbose_level) <= ::fml::GetVlogVerbosity())
112
113// The VLOG macros log with negative verbosities.
114#define FML_VLOG_STREAM(verbose_level) \
115 ::fml::LogMessage(-verbose_level, __FILE__, __LINE__, nullptr).stream()
116
117#define FML_VLOG(verbose_level) \
118 FML_LAZY_STREAM(FML_VLOG_STREAM(verbose_level), FML_VLOG_IS_ON(verbose_level))
119
120#ifndef NDEBUG
121#define FML_DLOG(severity) FML_LOG(severity)
122#define FML_DCHECK(condition) FML_CHECK(condition)
123#else
124#define FML_DLOG(severity) FML_EAT_STREAM_PARAMETERS(true)
125#define FML_DCHECK(condition) FML_EAT_STREAM_PARAMETERS(condition)
126#endif
127
128#define FML_UNREACHABLE() \
129 { \
130 FML_LOG(ERROR) << "Reached unreachable code."; \
131 ::fml::KillProcessConstexpr(true); \
132 }
133
134#endif // FLUTTER_FML_LOGGING_H_
std::ostream & stream()
Definition logging.h:40
static void CaptureNextLog(std::ostringstream *stream)
Definition logging.cc:129
void operator&(std::ostream &)
Definition logging.h:29
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
void KillProcess()
Definition logging.cc:220
int LogSeverity
Definition log_level.h:11
bool ShouldCreateLogMessage(LogSeverity severity)
Definition logging.cc:216
constexpr void KillProcessConstexpr(bool true_arg)
Definition logging.h:73
int GetVlogVerbosity()
Definition logging.cc:212
constexpr bool ShouldCreateLogMessageConstexpr(LogSeverity severity, bool true_arg)
Definition logging.h:63
std::string str() const
Definition logging.cc:122