forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
469 lines (427 loc) · 16.6 KB
/
log.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_LOG_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_LOG_H
/**
* @file log.h
*
* Google Cloud Platform C++ Libraries logging framework.
*
* Some of the libraries need to log information to simplify troubleshooting.
* The functions and macros used for logging are defined in this file. In
* general, we abide by the following principles:
*
* - Logging should controlled by the application developer. Unless explicitly
* instructed, the libraries produce no output to the console, except
* to emit a message to `std::clog` immediately before a GCP_LOG(FATAL)
* terminates the process.
* - Logging should have very low cost:
* - It should be possible to disable logs at compile time, they should
* disappear as-if there were `#%ifdef`/`#%endif` directives around them.
* - A log line at a disabled log level should be about as expensive as an
* extra if() statement. At the very least it should not incur additional
* memory allocations or locks.
* - It should be easy to log complex objects: the logging library should play
* well with the C++ iostream classes.
* - The application should be able to intercept log records and re-direct them
* to their own logging framework.
*
* @par Example: Logging From Library
* Use the `GCP_LOG()` macro to log from a Google Cloud Platform C++ library:
*
* @code
* void LibraryCode(ComplexThing const& thing) {
* GCP_LOG(INFO) << "I am here";
* if (thing.is_bad()) {
* GCP_LOG(ERROR) << "Poor thing is bad: " << thing;
* }
* }
* @endcode
*
* @par Example: Enable Logs to `std::clog`
* To enable logs to `std::clog` the application can call:
*
* @code
* void AppCode() {
* google::cloud::LogSink::EnableStdClog();
* }
* @endcode
*
* Alternatively, the application can enable logging to `std::clog` without any
* code changes or recompiling by setting the "GOOGLE_CLOUD_CPP_ENABLE_CLOG"
* environment variable before the program starts. The existence of this
* variable is all that matters; the value is ignored.
*
* Note that while `std::clog` is buffered, the framework will flush any log
* message at severity `WARNING` or higher.
*
* @par Example: Capture Logs
* The application can implement simple backends by wrapping a functor:
*
* @code
* void AppCode() {
* auto id = google::cloud::LogSink::AttachFunctor(
* [](google::cloud::LogRecord record) {
* if (record.severity >= google::cloud::Severity::CRITICAL) {
* std::cerr << record << "\n";
* }
* });
* // Use "id" to remove the capture.
* }
* @endcode
*/
#include "google/cloud/version.h"
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
/// Concatenate two pre-processor tokens.
#define GOOGLE_CLOUD_CPP_PP_CONCAT(a, b) a##b
/**
* Create a unique, or most likely unique identifier.
*
* In GCP_LOG() we need an identifier for the logger, we can easily create a C++
* scope to make it work with any name, say "foo". However the user may have a
* variable called "foo" that they want to use in the scope (for example, to log
* the value of "foo". We try to make it unlikely that there will be collision
* by using an identifier that has a long prefix and depends on the line number.
*/
#define GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER \
GOOGLE_CLOUD_CPP_PP_CONCAT(google_cloud_cpp_log_, __LINE__)
/**
* The main entry point for the Google Cloud Platform C++ Library loggers.
*
* Typically this used only in tests, applications should use GCP_LOG(). This
* macro introduces a new scope (via the for-loop) with a single new identifier:
* GOOGLE_CLOUD_CPP_LOG_RECORD_IDENTIFIER
* We use a for-loop (as opposed to an if-statement, or a do-while), because
* then we can say:
*
* @code
* GCP_LOG(WARNING) << a << b << c;
* @endcode
*
* and the variables are scoped correctly. The for-loop also affords us an
* opportunity to check that the log level is enabled before entering the body
* of the loop, and if disabled we can minimize the cost of the log. For
* example, disabled logs do not create an iostream at all.
*
* Finally, the type of the GOOGLE_CLOUD_CPP_LOG_RECORD_IDENTIFIER changes if
* the log level is disabled at compile-time. For compile-time disabled log
* levels the compiler should be able to determine that the loop will not
* execute at all, and completely eliminate the code (we verified this using
* godbolt.org with modern GCC and Clang versions).
*
* Note the use of fully-qualified class names, including the initial `::`, this
* is to prevent problems if anybody uses this macro in a context where `Logger`
* is defined by the enclosing namespaces.
*/
#define GOOGLE_CLOUD_CPP_LOG_I(level, sink) \
for (::google::cloud::Logger<::google::cloud::LogSink::CompileTimeEnabled( \
::google::cloud::Severity::level)> \
GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER( \
::google::cloud::Severity::level, __func__, __FILE__, __LINE__, \
sink); \
GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER.enabled(); \
GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER.LogTo(sink)) \
GOOGLE_CLOUD_CPP_LOGGER_IDENTIFIER.Stream()
// Note that we do not use `GOOGLE_CLOUD_CPP_PP_CONCAT` here: we actually want
// to concatenate `GCP_LS` with the literal `level`. On some platforms, the
// level names are used as macros (e.g., on macOS, `DEBUG` is often a macro with
// value 1 for debug builds). If we were to use `GOOGLE_CLOUD_CPP_PP_CONCAT` and
// `level` is a a macro, then we would get `GCP_LS_<macro_value>`, i.e.,
// `GCP_LS_1` (incorrect) instead of `GCP_LS_DEBUG` (correct).
/**
* Log a message with the Google Cloud Platform C++ Libraries logging framework.
*/
#define GCP_LOG(level) \
GOOGLE_CLOUD_CPP_LOG_I(GCP_LS_##level, ::google::cloud::LogSink::Instance())
#ifndef GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED
#define GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED GCP_LS_DEBUG
#endif // GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED
/**
* Define the severity levels for Google Cloud Platform C++ Libraries logging.
*
* These are modelled after the severity level in syslog(1) and many derived
* tools.
*
* We force the enum to be represented as an `int` because we will store the
* values in an `std::atomic<>` and the implementations usually optimize
* `std::atomic<int>` but not `std::atomic<Foo>`
*/
enum class Severity : int {
/// Use this level for messages that indicate the code is entering and leaving
/// functions.
GCP_LS_TRACE, // NOLINT(readability-identifier-naming)
/// Use this level for debug messages that should not be present in
/// production.
GCP_LS_DEBUG,
/// Informational messages, such as normal progress.
GCP_LS_INFO, // NOLINT(readability-identifier-naming)
/// Informational messages, such as unusual, but expected conditions.
GCP_LS_NOTICE, // NOLINT(readability-identifier-naming)
/// An indication of problems, users may need to take action.
GCP_LS_WARNING, // NOLINT(readability-identifier-naming)
/// An error has been detected. Do not use for normal conditions, such as
/// remote servers disconnecting.
GCP_LS_ERROR, // NOLINT(readability-identifier-naming)
/// The system is in a critical state, such as running out of local resources.
GCP_LS_CRITICAL, // NOLINT(readability-identifier-naming)
/// The system is at risk of immediate failure.
GCP_LS_ALERT, // NOLINT(readability-identifier-naming)
/// The system is unusable. GCP_LOG(FATAL) will call std::abort().
GCP_LS_FATAL, // NOLINT(readability-identifier-naming)
/// The highest possible severity level.
GCP_LS_HIGHEST = int(GCP_LS_FATAL), // NOLINT(readability-identifier-naming)
/// The lowest possible severity level.
GCP_LS_LOWEST = int(GCP_LS_TRACE), // NOLINT(readability-identifier-naming)
/// The lowest level that is enabled at compile-time.
// NOLINTNEXTLINE(readability-identifier-naming)
GCP_LS_LOWEST_ENABLED = int(GOOGLE_CLOUD_CPP_LOGGING_MIN_SEVERITY_ENABLED),
};
/// Streaming operator, writes a human readable representation.
std::ostream& operator<<(std::ostream& os, Severity x);
/**
* Represents a single log message.
*/
struct LogRecord {
Severity severity;
std::string function;
std::string filename;
int lineno;
std::thread::id thread_id;
std::chrono::system_clock::time_point timestamp;
std::string message;
};
/// Default formatting of a LogRecord.
std::ostream& operator<<(std::ostream& os, LogRecord const& rhs);
/**
* The logging backend interface.
*/
class LogBackend {
public:
virtual ~LogBackend() = default;
virtual void Process(LogRecord const& log_record) = 0;
virtual void ProcessWithOwnership(LogRecord log_record) = 0;
virtual void Flush() {}
};
/**
* A sink to receive log records.
*/
class LogSink {
public:
LogSink();
/// Return true if the severity is enabled at compile time.
static bool constexpr CompileTimeEnabled(Severity level) {
return level >= Severity::GCP_LS_LOWEST_ENABLED;
}
/**
* Return the singleton instance for this application.
*/
static LogSink& Instance();
/**
* Return true if this object has no backends.
*
* We want to avoid synchronization overhead when checking if a log message is
* enabled. Most of the time, most messages will be disabled, so incurring the
* locking overhead on each message would be too expensive and would
* discourage developers from creating logs. Furthermore, missing a few
* messages while the change of state "propagates" to other threads does not
* affect the correctness of the program.
*
* Note that `memory_order_relaxed` does not provide a compiler barrier
* either, so in theory stores into the atomic could be reordered by the
* optimizer. We have no reason to worry about that because all the writes
* are done inside a critical section protected by a mutex. The compiler
* cannot (or should not) reorder operations around those.
*/
bool empty() const { return empty_.load(std::memory_order_relaxed); }
/**
* Return true if @p severity is enabled.
*
* We want to avoid synchronization overhead when checking if a log message is
* enabled. Most of the time, most messages will be disabled, so incurring the
* locking overhead on each message would be too expensive and would
* discourage developers from creating logs. Furthermore, missing a few
* messages while the change of state "propagates" to other threads does not
* affect the correctness of the program.
*
* Note that `memory_order_relaxed` does not provide a compiler barrier
* either, so in theory stores into the atomic could be reordered by the
* optimizer. We have no reason to worry about that because all the writes
* are done inside a critical section protected by a mutex. The compiler
* cannot (or should not) reorder operations around those.
*/
bool is_enabled(Severity severity) const {
auto minimum = minimum_severity_.load(std::memory_order_relaxed);
return static_cast<int>(severity) >= minimum;
}
void set_minimum_severity(Severity minimum) {
minimum_severity_.store(static_cast<int>(minimum));
}
Severity minimum_severity() const {
return static_cast<Severity>(minimum_severity_.load());
}
// A `long` is perfectly fine here, it is guaranteed to be at least 32-bits
// wide, it is unlikely that an application would need more than 2 billion
// logging backends during its lifetime. The style guide recommends using
// `std::int32_t` for such a use-case, unfortunately I (coryan) picked `long`
// before we had tooling to enforce this style guide rule.
using BackendId = long; // NOLINT(google-runtime-int)
BackendId AddBackend(std::shared_ptr<LogBackend> backend);
void RemoveBackend(BackendId id);
void ClearBackends();
std::size_t BackendCount() const;
void Log(LogRecord log_record);
/// Flush all the current backends
void Flush();
/**
* Enable `std::clog` on `LogSink::Instance()`.
*
* This is also enabled if the "GOOGLE_CLOUD_CPP_ENABLE_CLOG" environment
* variable is set.
*/
static void EnableStdClog(
Severity min_severity = Severity::GCP_LS_LOWEST_ENABLED) {
Instance().EnableStdClogImpl(min_severity);
}
/**
* Disable `std::clog` on `LogSink::Instance()`.
*
* Note that this will remove the default logging backend.
*/
static void DisableStdClog() { Instance().DisableStdClogImpl(); }
private:
void EnableStdClogImpl(Severity min_severity);
void DisableStdClogImpl();
void SetDefaultBackend(std::shared_ptr<LogBackend> backend);
BackendId AddBackendImpl(std::shared_ptr<LogBackend> backend);
void RemoveBackendImpl(BackendId id);
std::map<BackendId, std::shared_ptr<LogBackend>> CopyBackends();
std::atomic<bool> empty_;
std::atomic<int> minimum_severity_;
std::mutex mutable mu_;
BackendId next_id_ = 0;
BackendId default_backend_id_ = 0;
std::map<BackendId, std::shared_ptr<LogBackend>> backends_;
};
/**
* Implements operator<< for all types, without any effect.
*
* It is desirable to disable at compile-time tracing, debugging, and other low
* severity messages. The Google Cloud Platform C++ Libraries logging adaptors
* return an object of this class when the particular log-line is disabled at
* compile-time.
*/
struct NullStream {
/// Generic do-nothing streaming operator
template <typename T>
NullStream& operator<<(T) {
return *this;
}
};
/**
* Define the class to capture a log message.
*
* @tparam CompileTimeEnabled this represents whether the severity has been
* disabled at compile-time. The class is specialized for `false` to minimize
* the run-time impact of (and, in practice, completely elide) disabled logs.
*/
template <bool CompileTimeEnabled>
class Logger {
public:
Logger(Severity severity, char const* function, char const* filename,
int lineno, LogSink& sink)
: enabled_(!sink.empty() && sink.is_enabled(severity)),
severity_(severity),
function_(function),
filename_(filename),
lineno_(lineno) {}
~Logger() {
if (severity_ >= Severity::GCP_LS_FATAL) std::abort();
}
bool enabled() const { return enabled_; }
/// Send the log record captured by this object to @p sink.
void LogTo(LogSink& sink) {
if (!stream_ || !enabled_) {
return;
}
enabled_ = false;
LogRecord record;
record.severity = severity_;
record.function = function_;
record.filename = filename_;
record.lineno = lineno_;
record.thread_id = std::this_thread::get_id();
record.timestamp = std::chrono::system_clock::now();
record.message = stream_->str();
sink.Log(std::move(record));
}
/// Return the iostream that captures the log message.
std::ostream& Stream() {
if (!stream_) {
stream_.reset(new std::ostringstream);
}
return *stream_;
}
private:
bool enabled_;
Severity severity_;
char const* function_;
char const* filename_;
int lineno_;
std::unique_ptr<std::ostringstream> stream_;
};
/**
* Define the logger for a disabled log level.
*/
template <>
class Logger<false> {
public:
Logger<false>(Severity severity, char const*, char const*, int, LogSink&)
: severity_(severity) {}
~Logger() {
if (severity_ >= Severity::GCP_LS_FATAL) std::abort();
}
//@{
/**
* @name Provide trivial implementations that meet the generic `Logger<bool>`
* interface.
*/
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool enabled() const { return false; }
void LogTo(LogSink&) {}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
NullStream Stream() { return NullStream(); }
//@}
private:
Severity severity_;
};
namespace internal {
std::shared_ptr<LogBackend> DefaultLogBackend();
} // namespace internal
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_LOG_H