15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_LOG_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_LOG_H
18#include "google/cloud/version.h"
19#include "absl/types/optional.h"
36#define GOOGLE_CLOUD_CPP_PP_CONCAT(a, b) a##b
39
40
41
42
43
44
45
46
47#define GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#define GOOGLE_CLOUD_CPP_LOG_I(level, sink)
79 for (::google::cloud::Logger<::google::cloud::LogSink::CompileTimeEnabled(
80 ::google::cloud::Severity::level)>
82 ::google::cloud::Severity::level, __func__, __FILE__, __LINE__,
95
96
100#ifndef GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED
101#define GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED GCP_LS_DEBUG
105
106
107
108
109
110
111
112
113
149std::ostream& operator<<(std::ostream& os,
Severity x);
152
153
160 std::chrono::system_clock::time_point
timestamp;
165std::ostream& operator<<(std::ostream& os,
LogRecord const& rhs);
168
169
176 virtual void Flush() {}
180
181
192
193
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 bool empty()
const {
return empty_.load(std::memory_order_relaxed); }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
231 auto minimum = minimum_severity_.load(std::memory_order_relaxed);
232 return static_cast<
int>(severity) >= minimum;
236 minimum_severity_.store(
static_cast<
int>(minimum));
239 return static_cast<
Severity>(minimum_severity_.load());
247 using BackendId =
long;
260
261
262
263
264
271
272
273
274
278 void EnableStdClogImpl(
Severity min_severity);
279 void DisableStdClogImpl();
280 void SetDefaultBackend(std::shared_ptr<
LogBackend> backend);
281 BackendId AddBackendImpl(std::shared_ptr<
LogBackend> backend);
282 void RemoveBackendImpl(BackendId id);
284 std::map<BackendId, std::shared_ptr<
LogBackend>> CopyBackends();
286 std::atomic<
bool> empty_;
287 std::atomic<
int> minimum_severity_;
288 std::mutex
mutable mu_;
289 BackendId next_id_ = 0;
290 BackendId default_backend_id_ = 0;
291 std::map<BackendId, std::shared_ptr<
LogBackend>> backends_;
295
296
297
298
299
300
301
304 template <
typename T>
311
312
313
314
315
316
317template <
bool CompileTimeEnabled>
320 Logger(
Severity severity,
char const* function,
char const* filename,
332 bool enabled()
const {
return enabled_; }
336 if (!stream_ || !enabled_) {
345 record
.thread_id = std::this_thread::get_id();
346 record
.timestamp = std::chrono::system_clock::now();
348 sink
.Log(std::move(record)
);
353 if (!stream_) stream_ = std::make_unique<std::ostringstream>();
360 char const* function_;
361 char const* filename_;
363 std::unique_ptr<std::ostringstream> stream_;
367
368
373 : severity_(severity) {}
381
382
384 bool enabled()
const {
return false; }
395std::shared_ptr<
LogBackend> DefaultLogBackend();
The logging backend interface.
Definition: log.h:170
virtual void ProcessWithOwnership(LogRecord log_record)=0
virtual void Process(LogRecord const &log_record)=0
virtual void Flush()
Definition: log.h:176
virtual ~LogBackend()=default
A sink to receive log records.
Definition: log.h:182
static LogSink & Instance()
Return the singleton instance for this application.
void Log(LogRecord log_record)
static void DisableStdClog()
Disable std::clog on LogSink::Instance().
Definition: log.h:275
bool empty() const
Return true if this object has no backends.
Definition: log.h:212
BackendId AddBackend(std::shared_ptr< LogBackend > backend)
Severity minimum_severity() const
Definition: log.h:238
void set_minimum_severity(Severity minimum)
Definition: log.h:235
void RemoveBackend(BackendId id)
bool is_enabled(Severity severity) const
Return true if severity is enabled.
Definition: log.h:230
void Flush()
Flush all the current backends.
std::size_t BackendCount() const
static void EnableStdClog(Severity min_severity=Severity::GCP_LS_LOWEST_ENABLED)
Enable std::clog on LogSink::Instance().
Definition: log.h:265
static bool constexpr CompileTimeEnabled(Severity level)
Return true if the severity is enabled at compile time.
Definition: log.h:187
~Logger()
Definition: log.h:375
bool enabled() const
Definition: log.h:384
Logger(Severity severity, char const *, char const *, int, LogSink &)
Definition: log.h:372
NullStream Stream()
Definition: log.h:387
void LogTo(LogSink &)
Definition: log.h:385
Define the class to capture a log message.
Definition: log.h:318
std::ostream & Stream()
Return the iostream that captures the log message.
Definition: log.h:352
Logger(Severity severity, char const *function, char const *filename, int lineno, LogSink &sink)
Definition: log.h:320
~Logger()
Definition: log.h:328
bool enabled() const
Definition: log.h:332
void LogTo(LogSink &sink)
Send the log record captured by this object to sink.
Definition: log.h:335
#define GOOGLE_CLOUD_CPP_PP_CONCAT(a, b)
Definition: log.h:36
#define GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER
Definition: log.h:47
#define GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED
Definition: log.h:101
#define GOOGLE_CLOUD_CPP_LOG_I(level, sink)
Definition: log.h:78
Contains all the Google Cloud C++ Library APIs.
Definition: async_operation.h:23
absl::optional< Severity > ParseSeverity(std::string const &name)
Convert a human-readable representation to a Severity.
Severity
Define the severity levels for Google Cloud Platform C++ Libraries logging.
Definition: log.h:114
@ GCP_LS_TRACE
Use this level for messages that indicate the code is entering and leaving functions.
@ GCP_LS_FATAL
The system is unusable. GCP_LOG(FATAL) will call std::abort().
@ GCP_LS_LOWEST
The lowest possible severity level.
@ GCP_LS_ERROR
An error has been detected.
@ GCP_LS_INFO
Informational messages, such as normal progress.
@ GCP_LS_LOWEST_ENABLED
The lowest level that is enabled at compile-time.
@ GCP_LS_HIGHEST
The highest possible severity level.
@ GCP_LS_NOTICE
Informational messages, such as unusual, but expected conditions.
@ GCP_LS_CRITICAL
The system is in a critical state, such as running out of local resources.
@ GCP_LS_DEBUG
Use this level for debug messages that should not be present in production.
@ GCP_LS_ALERT
The system is at risk of immediate failure.
@ GCP_LS_WARNING
An indication of problems, users may need to take action.
Definition: async_operation.h:22
Represents a single log message.
Definition: log.h:154
int lineno
Definition: log.h:158
std::string filename
Definition: log.h:157
Severity severity
Definition: log.h:155
std::string function
Definition: log.h:156
std::chrono::system_clock::time_point timestamp
Definition: log.h:160
std::string message
Definition: log.h:161
std::thread::id thread_id
Definition: log.h:159
Implements operator<< for all types, without any effect.
Definition: log.h:302
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
Definition: version.h:45
#define GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
Definition: version.h:43