Skip to content

Commit 6decc4c

Browse files
srslog: move prefix rendering to backend
Introduce an optional "log_label" metadata that allows for passing custom labels (prefixes) that shall be prepended to each log message upon rendering in the backend.
1 parent f527eae commit 6decc4c

File tree

4 files changed

+135
-15
lines changed

4 files changed

+135
-15
lines changed

include/srsran/srslog/detail/log_entry_metadata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct log_entry_metadata {
3434
fmt::dynamic_format_arg_store<fmt::format_context>* store;
3535
std::string log_name;
3636
char log_tag;
37+
std::string log_label;
3738
std::vector<uint8_t> hex_dump;
3839
};
3940

include/srsran/srslog/log_channel.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ struct log_channel_config {
4444
bool should_print_context = false;
4545
};
4646

47+
/// Strong type to wrap a constant log label that is prepended to each log message.
48+
struct log_label_t {
49+
/// Optional log label. If set, will get printed in front of each log message.
50+
/// Disabled by default.
51+
std::string log_label;
52+
};
53+
4754
/// A log channel is the entity used for logging messages.
4855
///
4956
/// It can deliver a log entry to one or more different sinks, for example a
@@ -136,6 +143,38 @@ class log_channel
136143
backend.push(std::move(entry));
137144
}
138145

146+
/// Builds the provided log entry and passes it to the backend. When the
147+
/// channel is disabled the log entry will be discarded.
148+
template <typename... Args>
149+
void operator()(const log_label_t& label, const char* fmtstr, Args&&... args)
150+
{
151+
if (!enabled()) {
152+
return;
153+
}
154+
155+
// Populate the store with all incoming arguments.
156+
auto* store = backend.alloc_arg_store();
157+
if (!store) {
158+
return;
159+
}
160+
(void)std::initializer_list<int>{(push_back(store, std::forward<Args>(args)), 0)...};
161+
162+
// Send the log entry to the backend.
163+
log_formatter& formatter = log_sink.get_formatter();
164+
detail::log_entry entry = {&log_sink,
165+
[&formatter](detail::log_entry_metadata&& metadata, fmt::memory_buffer& buffer) {
166+
formatter.format(std::move(metadata), buffer);
167+
},
168+
{std::chrono::high_resolution_clock::now(),
169+
{ctx_value64, should_print_context},
170+
fmtstr,
171+
store,
172+
log_name,
173+
log_tag,
174+
label.log_label}};
175+
backend.push(std::move(entry));
176+
}
177+
139178
/// Builds the provided log entry and passes it to the backend. When the
140179
/// channel is disabled the log entry will be discarded.
141180
template <typename... Args>
@@ -200,6 +239,45 @@ class log_channel
200239
store,
201240
log_name,
202241
log_tag,
242+
{},
243+
std::vector<uint8_t>(buffer, buffer + len)}};
244+
backend.push(std::move(entry));
245+
}
246+
247+
/// Builds the provided log entry and passes it to the backend. When the
248+
/// channel is disabled the log entry will be discarded.
249+
template <typename... Args>
250+
void operator()(const log_label_t& label, const uint8_t* buffer, size_t len, const char* fmtstr, Args&&... args)
251+
{
252+
if (!enabled()) {
253+
return;
254+
}
255+
256+
// Populate the store with all incoming arguments.
257+
auto* store = backend.alloc_arg_store();
258+
if (!store) {
259+
return;
260+
}
261+
(void)std::initializer_list<int>{(push_back(store, std::forward<Args>(args)), 0)...};
262+
263+
// Calculate the length to capture in the buffer.
264+
if (hex_max_size >= 0) {
265+
len = std::min<size_t>(len, hex_max_size);
266+
}
267+
268+
// Send the log entry to the backend.
269+
log_formatter& formatter = log_sink.get_formatter();
270+
detail::log_entry entry = {&log_sink,
271+
[&formatter](detail::log_entry_metadata&& metadata, fmt::memory_buffer& buffer_) {
272+
formatter.format(std::move(metadata), buffer_);
273+
},
274+
{std::chrono::high_resolution_clock::now(),
275+
{ctx_value64, should_print_context},
276+
fmtstr,
277+
store,
278+
log_name,
279+
log_tag,
280+
label.log_label,
203281
std::vector<uint8_t>(buffer, buffer + len)}};
204282
backend.push(std::move(entry));
205283
}
@@ -237,6 +315,7 @@ class log_channel
237315
store,
238316
log_name,
239317
log_tag,
318+
{},
240319
std::vector<uint8_t>(buffer, buffer + len)}};
241320
backend.push(std::move(entry));
242321
}
@@ -274,6 +353,45 @@ class log_channel
274353
store,
275354
log_name,
276355
log_tag,
356+
{},
357+
std::vector<uint8_t>(it_begin, it_end)}};
358+
backend.push(std::move(entry));
359+
}
360+
361+
/// Builds the provided log entry and passes it to the backend. When the
362+
/// channel is disabled the log entry will be discarded.
363+
template <typename It, typename... Args, typename std::enable_if<detail::is_byte_iterable<It>::value, int>::type = 0>
364+
void operator()(const log_label_t& label, It it_begin, It it_end, const char* fmtstr, Args&&... args)
365+
{
366+
if (!enabled()) {
367+
return;
368+
}
369+
370+
// Populate the store with all incoming arguments.
371+
auto* store = backend.alloc_arg_store();
372+
if (!store) {
373+
return;
374+
}
375+
(void)std::initializer_list<int>{(push_back(store, std::forward<Args>(args)), 0)...};
376+
377+
// Calculate the length to capture in the buffer.
378+
if (hex_max_size >= 0 && hex_max_size < std::distance(it_begin, it_end)) {
379+
it_end = it_begin + hex_max_size;
380+
}
381+
382+
// Send the log entry to the backend.
383+
log_formatter& formatter = log_sink.get_formatter();
384+
detail::log_entry entry = {&log_sink,
385+
[&formatter](detail::log_entry_metadata&& metadata, fmt::memory_buffer& buffer) {
386+
formatter.format(std::move(metadata), buffer);
387+
},
388+
{std::chrono::high_resolution_clock::now(),
389+
{ctx_value64, should_print_context},
390+
fmtstr,
391+
store,
392+
log_name,
393+
log_tag,
394+
label.log_label,
277395
std::vector<uint8_t>(it_begin, it_end)}};
278396
backend.push(std::move(entry));
279397
}

include/srsran/support/prefixed_logger.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ template <typename Prefix>
2525
class prefixed_logger
2626
{
2727
public:
28-
prefixed_logger(const std::string& log_name, Prefix prefix_, const char* prefix_separator_ = "") :
29-
logger(srslog::fetch_basic_logger(log_name, false)), prefix(prefix_), prefix_separator(prefix_separator_)
28+
prefixed_logger(const std::string& log_name, Prefix prefix, const char* prefix_separator = "") :
29+
logger(srslog::fetch_basic_logger(log_name, false))
3030
{
31+
set_prefix(prefix, prefix_separator);
3132
}
3233

33-
void set_prefix(Prefix prefix_) { prefix = prefix_; }
34-
Prefix get_prefix() const { return prefix; }
34+
void set_prefix(Prefix prefix, const char* prefix_separator = "")
35+
{
36+
fmt::memory_buffer buffer;
37+
fmt::format_to(buffer, "{}{}", prefix, prefix_separator);
38+
log_label.log_label = fmt::to_string(buffer);
39+
}
3540

3641
template <typename... Args>
3742
void log_debug(const char* fmt, Args&&... args) const
@@ -195,18 +200,15 @@ class prefixed_logger
195200

196201
private:
197202
srslog::basic_logger& logger;
198-
Prefix prefix;
199-
const char* prefix_separator;
203+
srslog::log_label_t log_label;
200204

201205
template <typename... Args>
202206
void log_helper(srslog::log_channel& channel, const char* fmt, Args&&... args) const
203207
{
204208
if (!channel.enabled()) {
205209
return;
206210
}
207-
fmt::memory_buffer buffer;
208-
fmt::format_to(buffer, fmt, std::forward<Args>(args)...);
209-
channel("{}{}{}", prefix, prefix_separator, to_c_str(buffer));
211+
channel(log_label, fmt, std::forward<Args>(args)...);
210212
}
211213

212214
template <typename It, typename... Args>
@@ -215,9 +217,7 @@ class prefixed_logger
215217
if (!channel.enabled()) {
216218
return;
217219
}
218-
fmt::memory_buffer buffer;
219-
fmt::format_to(buffer, fmt, std::forward<Args>(args)...);
220-
channel(it_begin, it_end, "{}{}{}", prefix, prefix_separator, to_c_str(buffer));
220+
channel(log_label, it_begin, it_end, fmt, std::forward<Args>(args)...);
221221
}
222222

223223
template <typename... Args>
@@ -226,9 +226,7 @@ class prefixed_logger
226226
if (!channel.enabled()) {
227227
return;
228228
}
229-
fmt::memory_buffer buffer;
230-
fmt::format_to(buffer, fmt, std::forward<Args>(args)...);
231-
channel(msg, len, "{}{}{}", prefix, prefix_separator, to_c_str(buffer));
229+
channel(log_label, msg, len, fmt, std::forward<Args>(args)...);
232230
}
233231
};
234232

lib/srslog/formatters/text_formatter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ static void format_metadata(const detail::log_entry_metadata& metadata, fmt::mem
6161
ctx_buffer.push_back('\0');
6262
fmt::format_to(buffer, "[{: >8}] ", ctx_buffer.data());
6363
}
64+
if (!metadata.log_label.empty()) {
65+
fmt::format_to(buffer, "{}", metadata.log_label);
66+
}
6467
}
6568

6669
void text_formatter::format(detail::log_entry_metadata&& metadata, fmt::memory_buffer& buffer)

0 commit comments

Comments
 (0)