-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.cpp
More file actions
238 lines (200 loc) · 6.16 KB
/
Copy pathlogging.cpp
File metadata and controls
238 lines (200 loc) · 6.16 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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/logging.hpp"
#include "datadog/core.hpp"
#include "datadog/impl/core/core.hpp"
#include "datadog/impl/core/feature.hpp"
#include "datadog/impl/core/util/diagnostics.hpp"
#include "datadog/impl/logging/logger.hpp"
#include "datadog/impl/logging/logging.hpp"
namespace datadog {
LoggerConfig::LoggerConfig() = default;
LoggerConfig::~LoggerConfig() = default;
LoggerConfig::LoggerConfig(const LoggerConfig&) = default;
LoggerConfig& LoggerConfig::operator=(const LoggerConfig&) = default;
LoggerConfig::LoggerConfig(LoggerConfig&&) = default;
LoggerConfig& LoggerConfig::operator=(LoggerConfig&&) = default;
LoggerConfig& LoggerConfig::SetRemoteSampleRate(float value) {
remote_sample_rate = value;
return *this;
}
LoggerConfig& LoggerConfig::SetService(std::string_view value) {
if (value.empty()) {
service.reset();
} else {
service = value;
}
return *this;
}
LoggerConfig& LoggerConfig::SetName(std::string_view value) {
if (value.empty()) {
name.reset();
} else {
name = value;
}
return *this;
}
LoggerConfig& LoggerConfig::SetRemoteLogThreshold(LogLevel value) {
remote_log_threshold = value;
return *this;
}
LoggerConfig& LoggerConfig::SetInitialAttributeCapacity(size_t value) {
initial_attribute_capacity = value;
return *this;
}
LoggerConfig& LoggerConfig::SetEnrichWithRumContext(bool value) {
enrich_with_rum_context = value;
return *this;
}
Logger::Logger(
std::unique_ptr<impl::Logger>&& impl,
DiagnosticHandler diagnostic_handler,
DiagnosticLevel diagnostic_threshold,
PrivateCtorTag
)
: _impl(std::move(impl)),
_diagnostic_handler(std::move(diagnostic_handler)),
_diagnostic_threshold(diagnostic_threshold) {}
Logger::~Logger() = default;
void Logger::AddAttribute(std::string_view name, const Attribute& value) {
if (_impl) {
_impl->AddAttribute(name, value);
}
}
void Logger::RemoveAttribute(std::string_view name) {
if (_impl) {
_impl->RemoveAttribute(name);
}
}
void Logger::AddTag(std::string_view tag) {
if (_impl) {
_impl->AddTag(
tag, impl::DiagnosticLogger{_diagnostic_handler, _diagnostic_threshold}
);
}
}
void Logger::AddTag(std::string_view key, std::string_view value) {
if (_impl) {
_impl->AddTag(
key, value, impl::DiagnosticLogger{_diagnostic_handler, _diagnostic_threshold}
);
}
}
void Logger::RemoveTag(std::string_view tag) {
if (_impl) {
_impl->RemoveTag(tag);
}
}
void Logger::RemoveTagsWithKey(std::string_view key) {
if (_impl) {
_impl->RemoveTagsWithKey(key);
}
}
void Logger::Log(
LogLevel level,
std::string_view message,
const LogError& err,
const Attribute& attributes
) {
if (_impl) {
_impl->Log(level, message, err, attributes);
}
}
void Logger::Debug(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Debug, message, err, attributes);
}
void Logger::Info(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Info, message, err, attributes);
}
void Logger::Notice(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Notice, message, err, attributes);
}
void Logger::Warn(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Warn, message, err, attributes);
}
void Logger::Error(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Error, message, err, attributes);
}
void Logger::Critical(
std::string_view message, const LogError& err, const Attribute& attributes
) {
Log(LogLevel::Critical, message, err, attributes);
}
Logging::Logging(Logging::PrivateCtorTag)
: _impl(nullptr),
_diagnostic_handler(nullptr),
_diagnostic_threshold(DiagnosticLevel::Error) {}
Logging::Logging(
std::shared_ptr<impl::Logging>&& impl,
DiagnosticHandler diagnostic_handler,
DiagnosticLevel diagnostic_threshold,
Logging::PrivateCtorTag
)
: _impl(std::move(impl)),
_diagnostic_handler(std::move(diagnostic_handler)),
_diagnostic_threshold(diagnostic_threshold) {
// The C++ logging API doesn't emit any diagnostic messages, but storing these values
// ensures that we can do so in the future without ABI changes
(void)_diagnostic_handler;
(void)_diagnostic_threshold;
}
Logging::~Logging() = default;
std::shared_ptr<Logging> Logging::Register(const std::shared_ptr<Core>& core) {
// Return a no-op Logging interface if called without a valid core
if (!core || !core->_impl) {
return std::make_shared<Logging>(Logging::PrivateCtorTag{});
}
// Get essential state from the Core
const platform::IClock& clock = core->_impl->GetClock();
// Initialize our Logging feature implementation
auto logging_impl = std::make_shared<impl::Logging>(clock);
// Register the feature with the core, returning a no-op interface on failure
if (!core->_impl->RegisterFeature(logging_impl)) {
return std::make_shared<Logging>(Logging::PrivateCtorTag{});
}
// Initialize and return the API object that represents our user-facing interface for
// the logging feature
return std::make_shared<Logging>(
std::move(logging_impl),
core->_diagnostic_handler,
core->_diagnostic_threshold,
Logging::PrivateCtorTag{}
);
}
void Logging::AddAttribute(std::string_view name, const Attribute& value) {
if (_impl) {
_impl->AddAttribute(name, value);
}
}
void Logging::RemoveAttribute(std::string_view name) {
if (_impl) {
_impl->RemoveAttribute(name);
}
}
std::shared_ptr<Logger> Logging::CreateLogger(const LoggerConfig& config) {
if (!_impl) {
return std::make_shared<Logger>(
nullptr, nullptr, DiagnosticLevel::Error, Logger::PrivateCtorTag{}
);
}
return std::make_shared<Logger>(
_impl->CreateLogger(config),
_diagnostic_handler,
_diagnostic_threshold,
Logger::PrivateCtorTag{}
);
}
} // namespace datadog