15 #include "google/cloud/log.h" 20 static_assert(
sizeof(
Severity) <=
sizeof(
int),
21 "Expected Severity to fit in an integer");
25 "Expect LOWEST severity to be smaller than HIGHEST severity");
27 std::ostream& operator<<(std::ostream& os,
Severity x) {
28 char const* names[] = {
29 "TRACE",
"DEBUG",
"INFO",
"NOTICE",
"WARNING",
30 "ERROR",
"CRITICAL",
"ALERT",
"FATAL",
32 auto index =
static_cast<
int>(x);
33 return os << names[index];
36 std::ostream& operator<<(std::ostream& os,
LogRecord const& rhs) {
45 clog_backend_id_(0) {}
53 std::unique_lock<std::mutex> lk(mu_);
54 return AddBackendImpl(std::move(backend));
58 std::unique_lock<std::mutex> lk(mu_);
59 RemoveBackendImpl(id);
63 std::unique_lock<std::mutex> lk(mu_);
66 empty_.store(backends_.empty());
70 std::unique_lock<std::mutex> lk(mu_);
71 return backends_.size();
78 auto copy = [
this]() {
79 std::unique_lock<std::mutex> lk(mu_);
89 if (1U == copy.size()) {
93 for (
auto& kv : copy) {
101 StdClogBackend() =
default;
103 void Process(
LogRecord const& lr)
override {
104 std::clog << lr <<
"\n";
106 std::clog << std::flush;
109 void ProcessWithOwnership(
LogRecord lr)
override { Process(lr); }
113 void LogSink::EnableStdClogImpl() {
114 std::unique_lock<std::mutex> lk(mu_);
115 if (clog_backend_id_ != 0) {
118 clog_backend_id_ = AddBackendImpl(std::make_shared<StdClogBackend>());
121 void LogSink::DisableStdClogImpl() {
122 std::unique_lock<std::mutex> lk(mu_);
123 if (clog_backend_id_ == 0) {
126 RemoveBackendImpl(clog_backend_id_);
127 clog_backend_id_ = 0;
131 long id = ++next_id_;
132 backends_.emplace(id, std::move(backend));
133 empty_.store(backends_.empty());
137 void LogSink::RemoveBackendImpl(
long id) {
138 auto it = backends_.find(id);
139 if (backends_.end() == it) {
143 empty_.store(backends_.empty());
A sink to receive log records.
#define GOOGLE_CLOUD_CPP_NS
Contains all the Google Cloud C++ Library APIs.
long AddBackend(std::shared_ptr< LogBackend > backend)
void Log(LogRecord log_record)
The highest possible severity level.
The lowest possible severity level.
virtual void ProcessWithOwnership(LogRecord log_record)=0
void RemoveBackend(long id)
Severity
Define the severity levels for Google Cloud Platform C++ Libraries logging.
Represents a single log message.
static LogSink & Instance()
Return the singleton instance for this application.
std::size_t BackendCount() const
The lowest level that is enabled at compile-time.
An indication of problems, users may need to take action.
virtual void Process(LogRecord const &log_record)=0