forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog.cc
More file actions
191 lines (166 loc) · 5.25 KB
/
log.cc
File metadata and controls
191 lines (166 loc) · 5.25 KB
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
// 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.
#include "google/cloud/log.h"
#include "google/cloud/internal/getenv.h"
#include "absl/time/time.h"
#include <array>
#include <thread>
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
static_assert(sizeof(Severity) <= sizeof(int),
"Expected Severity to fit in an integer");
static_assert(static_cast<int>(Severity::GCP_LS_LOWEST) <
static_cast<int>(Severity::GCP_LS_HIGHEST),
"Expect LOWEST severity to be smaller than HIGHEST severity");
namespace {
struct Timestamp {
explicit Timestamp(std::chrono::system_clock::time_point const& tp)
: t(absl::FromChrono(tp)) {}
absl::Time t;
};
std::ostream& operator<<(std::ostream& os, Timestamp const& ts) {
auto constexpr kFormat = "%E4Y-%m-%dT%H:%M:%E9SZ";
return os << absl::FormatTime(kFormat, ts.t, absl::UTCTimeZone());
}
auto constexpr kSeverityCount = static_cast<int>(Severity::GCP_LS_HIGHEST) + 1;
// Double braces needed to workaround a clang-3.8 bug.
std::array<char const*, kSeverityCount> constexpr kSeverityNames{{
"TRACE",
"DEBUG",
"INFO",
"NOTICE",
"WARNING",
"ERROR",
"CRITICAL",
"ALERT",
"FATAL",
}};
} // namespace
std::ostream& operator<<(std::ostream& os, Severity x) {
auto index = static_cast<int>(x);
return os << kSeverityNames[index];
}
std::ostream& operator<<(std::ostream& os, LogRecord const& rhs) {
return os << Timestamp{rhs.timestamp} << " [" << rhs.severity << "]"
<< " <" << std::this_thread::get_id() << ">"
<< " " << rhs.message << " (" << rhs.filename << ':' << rhs.lineno
<< ')';
}
LogSink::LogSink()
: empty_(true),
minimum_severity_(static_cast<int>(Severity::GCP_LS_LOWEST_ENABLED)) {}
LogSink& LogSink::Instance() {
static auto* const kInstance = [] {
auto* p = new LogSink;
if (internal::GetEnv("GOOGLE_CLOUD_CPP_ENABLE_CLOG").has_value()) {
p->EnableStdClogImpl();
}
return p;
}();
return *kInstance;
}
// NOLINTNEXTLINE(google-runtime-int)
long LogSink::AddBackend(std::shared_ptr<LogBackend> backend) {
std::unique_lock<std::mutex> lk(mu_);
return AddBackendImpl(std::move(backend));
}
// NOLINTNEXTLINE(google-runtime-int)
void LogSink::RemoveBackend(long id) {
std::unique_lock<std::mutex> lk(mu_);
RemoveBackendImpl(id);
}
void LogSink::ClearBackends() {
std::unique_lock<std::mutex> lk(mu_);
backends_.clear();
clog_backend_id_ = 0;
empty_.store(backends_.empty());
}
std::size_t LogSink::BackendCount() const {
std::unique_lock<std::mutex> lk(mu_);
return backends_.size();
}
void LogSink::Log(LogRecord log_record) {
// Make a copy of the backends because calling user-defined functions while
// holding a lock is a bad idea: the application may change the backends while
// we are holding this lock, and soon deadlock occurs.
auto copy = [this]() {
std::unique_lock<std::mutex> lk(mu_);
return backends_;
}();
if (copy.empty()) {
return;
}
// In general, we just give each backend a const-reference and the backends
// must make a copy if needed. But if there is only one backend we can give
// the backend an opportunity to optimize things by transferring ownership of
// the LogRecord to it.
if (copy.size() == 1) {
copy.begin()->second->ProcessWithOwnership(std::move(log_record));
return;
}
for (auto& kv : copy) {
kv.second->Process(log_record);
}
}
namespace {
class StdClogBackend : public LogBackend {
public:
StdClogBackend() = default;
void Process(LogRecord const& lr) override {
std::lock_guard<std::mutex> lk(mu_);
std::clog << lr << "\n";
if (lr.severity >= Severity::GCP_LS_WARNING) {
std::clog << std::flush;
}
}
void ProcessWithOwnership(LogRecord lr) override { Process(lr); }
private:
std::mutex mu_;
};
} // namespace
void LogSink::EnableStdClogImpl() {
std::unique_lock<std::mutex> lk(mu_);
if (clog_backend_id_ != 0) {
return;
}
clog_backend_id_ = AddBackendImpl(std::make_shared<StdClogBackend>());
}
void LogSink::DisableStdClogImpl() {
std::unique_lock<std::mutex> lk(mu_);
if (clog_backend_id_ == 0) {
return;
}
RemoveBackendImpl(clog_backend_id_);
clog_backend_id_ = 0;
}
// NOLINTNEXTLINE(google-runtime-int)
long LogSink::AddBackendImpl(std::shared_ptr<LogBackend> backend) {
auto const id = ++next_id_;
backends_.emplace(id, std::move(backend));
empty_.store(backends_.empty());
return id;
}
// NOLINTNEXTLINE(google-runtime-int)
void LogSink::RemoveBackendImpl(long id) {
auto it = backends_.find(id);
if (backends_.end() == it) {
return;
}
backends_.erase(it);
empty_.store(backends_.empty());
}
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google