Skip to content

Commit a6c471d

Browse files
srslog: pass log_label as shared_ptr
1 parent 6decc4c commit a6c471d

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

include/srsran/srslog/detail/log_entry_metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +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;
37+
std::shared_ptr<const std::string> log_label;
3838
std::vector<uint8_t> hex_dump;
3939
};
4040

include/srsran/srslog/log_channel.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ 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-
5447
/// A log channel is the entity used for logging messages.
5548
///
5649
/// It can deliver a log entry to one or more different sinks, for example a
@@ -146,7 +139,7 @@ class log_channel
146139
/// Builds the provided log entry and passes it to the backend. When the
147140
/// channel is disabled the log entry will be discarded.
148141
template <typename... Args>
149-
void operator()(const log_label_t& label, const char* fmtstr, Args&&... args)
142+
void operator()(std::shared_ptr<const std::string> log_label, const char* fmtstr, Args&&... args)
150143
{
151144
if (!enabled()) {
152145
return;
@@ -171,7 +164,7 @@ class log_channel
171164
store,
172165
log_name,
173166
log_tag,
174-
label.log_label}};
167+
std::move(log_label)}};
175168
backend.push(std::move(entry));
176169
}
177170

@@ -247,7 +240,11 @@ class log_channel
247240
/// Builds the provided log entry and passes it to the backend. When the
248241
/// channel is disabled the log entry will be discarded.
249242
template <typename... Args>
250-
void operator()(const log_label_t& label, const uint8_t* buffer, size_t len, const char* fmtstr, Args&&... args)
243+
void operator()(std::shared_ptr<const std::string> log_label,
244+
const uint8_t* buffer,
245+
size_t len,
246+
const char* fmtstr,
247+
Args&&... args)
251248
{
252249
if (!enabled()) {
253250
return;
@@ -277,7 +274,7 @@ class log_channel
277274
store,
278275
log_name,
279276
log_tag,
280-
label.log_label,
277+
std::move(log_label),
281278
std::vector<uint8_t>(buffer, buffer + len)}};
282279
backend.push(std::move(entry));
283280
}
@@ -361,7 +358,8 @@ class log_channel
361358
/// Builds the provided log entry and passes it to the backend. When the
362359
/// channel is disabled the log entry will be discarded.
363360
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)
361+
void
362+
operator()(std::shared_ptr<const std::string> log_label, It it_begin, It it_end, const char* fmtstr, Args&&... args)
365363
{
366364
if (!enabled()) {
367365
return;
@@ -391,7 +389,7 @@ class log_channel
391389
store,
392390
log_name,
393391
log_tag,
394-
label.log_label,
392+
std::move(log_label),
395393
std::vector<uint8_t>(it_begin, it_end)}};
396394
backend.push(std::move(entry));
397395
}

include/srsran/support/prefixed_logger.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class prefixed_logger
3535
{
3636
fmt::memory_buffer buffer;
3737
fmt::format_to(buffer, "{}{}", prefix, prefix_separator);
38-
log_label.log_label = fmt::to_string(buffer);
38+
log_label = std::make_shared<const std::string>(fmt::to_string(buffer));
3939
}
4040

4141
template <typename... Args>
@@ -199,8 +199,8 @@ class prefixed_logger
199199
srslog::basic_logger& get_basic_logger() { return logger; }
200200

201201
private:
202-
srslog::basic_logger& logger;
203-
srslog::log_label_t log_label;
202+
srslog::basic_logger& logger;
203+
std::shared_ptr<const std::string> log_label;
204204

205205
template <typename... Args>
206206
void log_helper(srslog::log_channel& channel, const char* fmt, Args&&... args) const

lib/srslog/formatters/text_formatter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ 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);
64+
if (metadata.log_label != nullptr && !metadata.log_label->empty()) {
65+
fmt::format_to(buffer, "{}", *metadata.log_label);
6666
}
6767
}
6868

0 commit comments

Comments
 (0)