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
13namespace fml {
14
15class LogMessageVoidify {
16 public:
17 void operator&(std::ostream&) {}
18};
19
20class 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
36 FML_DISALLOW_COPY_AND_ASSIGN(LogMessage);
37};
38
39// Gets the FML_VLOG default verbosity level.
40int GetVlogVerbosity();
41
42// Returns true if |severity| is at or above the current minimum log level.
43// LOG_FATAL and above is always true.
44bool ShouldCreateLogMessage(LogSeverity severity);
45
46} // namespace fml
47
48#define FML_LOG_STREAM(severity) \
49 ::fml::LogMessage(::fml::LOG_##severity, __FILE__, __LINE__, nullptr).stream()
50
51#define FML_LAZY_STREAM(stream, condition) \
52 !(condition) ? (void)0 : ::fml::LogMessageVoidify() & (stream)
53
54#define FML_EAT_STREAM_PARAMETERS(ignored) \
55 true || (ignored) \
56 ? (void)0 \
57 : ::fml::LogMessageVoidify() & \
58 ::fml::LogMessage(::fml::LOG_FATAL, 0, 0, nullptr).stream()
59
60#define FML_LOG_IS_ON(severity) \
61 (::fml::ShouldCreateLogMessage(::fml::LOG_##severity))
62
63#define FML_LOG(severity) \
64 FML_LAZY_STREAM(FML_LOG_STREAM(severity), FML_LOG_IS_ON(severity))
65
66#define FML_CHECK(condition) \
67 FML_LAZY_STREAM( \
68 ::fml::LogMessage(::fml::LOG_FATAL, __FILE__, __LINE__, #condition) \
69 .stream(), \
70 !(condition))
71
72#define FML_VLOG_IS_ON(verbose_level) \
73 ((verbose_level) <= ::fml::GetVlogVerbosity())
74
75// The VLOG macros log with negative verbosities.
76#define FML_VLOG_STREAM(verbose_level) \
77 ::fml::LogMessage(-verbose_level, __FILE__, __LINE__, nullptr).stream()
78
79#define FML_VLOG(verbose_level) \
80 FML_LAZY_STREAM(FML_VLOG_STREAM(verbose_level), FML_VLOG_IS_ON(verbose_level))
81
82#ifndef NDEBUG
83#define FML_DLOG(severity) FML_LOG(severity)
84#define FML_DCHECK(condition) FML_CHECK(condition)
85#else
86#define FML_DLOG(severity) FML_EAT_STREAM_PARAMETERS(true)
87#define FML_DCHECK(condition) FML_EAT_STREAM_PARAMETERS(condition)
88#endif
89
90#define FML_NOTREACHED() FML_DCHECK(false)
91
92#define FML_NOTIMPLEMENTED() \
93 FML_LOG(ERROR) << "Not implemented in: " << __PRETTY_FUNCTION__
94
95#endif // FLUTTER_FML_LOGGING_H_
96