Skip to content

Commit fef6fae

Browse files
committed
integrate <rsl/kwargs>
1 parent 70b721e commit fef6fae

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

example/spans.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#define RSL_DOLLAR_MACROS
12
#include <rsl/log>
23

34
void bar() {
4-
// TODO
5-
RSL_LOG_CONTEXT("bar", rsl::log_level::INFO, x=1, y=2);
5+
$context("bar", rsl::log_level::INFO, x=1, y=2);
66
rsl::info("from bar");
77
}
88

@@ -15,12 +15,10 @@ void foo() {
1515
int main() {
1616
rsl::error("error before main context");
1717

18-
// rsl::logging::ContextGuard context{INFO, "main"};
1918
auto x = 42;
2019
auto ctx = rsl::log::context("main", rsl::log_level::INFO);
21-
// ctx.extra = ExtraFields{{rsl::_log_impl::Field(^^x).set_ptr(&x)}};
2220
ctx.enter();
23-
rsl::error("test error");
21+
rsl::error("test error", $args(foo=123));
2422
foo();
2523
ctx.exit();
2624
}

include/rsl/log

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
#include <utility>
1212

1313
#include <rsl/kwargs>
14-
# define RSL_LOG_CONTEXT(name, level, ...) \
15-
rsl::log::context_guard _(name, level, RSL_LOG_ARGS, RSL_KWARGS(__VA_ARGS__))
16-
14+
#define RSL_LOG_CONTEXT(name, level, ...) \
15+
rsl::log::context_guard _(name, level, RSL_LOG_ARGS, RSL_KWARGS(__VA_ARGS__))
1716

1817
#define RSL_TRACE(...) \
1918
(void)(is_enabled_for(::rsl::logging::LogLevel::TRACE) && \
@@ -36,7 +35,7 @@
3635

3736
#ifdef RSL_DOLLAR_MACROS
3837
# define $log_args RSL_LOG_ARGS
39-
# define $context(name, ...) RSL_LOG_CONTEXT(name, __VA_ARGS__)
38+
# define $context(...) RSL_LOG_CONTEXT(__VA_ARGS__)
4039
# define $trace(...) RSL_TRACE(__VA_ARGS__)
4140
# define $debug(...) RSL_DEBUG(__VA_ARGS__)
4241
# define $info(...) RSL_INFO(__VA_ARGS__)

include/rsl/logging/_impl/formatter.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <rsl/source_location>
1313
#include <rsl/format>
14+
#include <rsl/kwargs>
1415

1516
#include <rsl/logging/event.hpp>
1617
#include <rsl/logging/context.hpp>
@@ -76,7 +77,11 @@ void emit_event(ExtraFields const* fnc_args,
7677
.context = context ? *context : Context(),
7778
.arguments = fnc_args ? *fnc_args : ExtraFields{},
7879
.sloc = fmt.sloc};
79-
80+
if constexpr (sizeof...(Args) > 0) {
81+
if constexpr (is_kwargs<std::remove_cvref_t<Args...[sizeof...(Args) - 1]>>) {
82+
meta.extra = ExtraFields(args...[sizeof...(Args) - 1]);
83+
}
84+
}
8085
selected_logger<Empty...>.emit(meta, fmt, std::forward<Args>(args)...);
8186
}
8287
}

include/rsl/logging/event.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct Metadata {
1919
std::thread::id thread_id;
2020
Context context;
2121
ExtraFields arguments;
22+
ExtraFields extra;
2223

2324
// set by the formatter
2425
rsl::source_location sloc;

include/rsl/logging/field.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ struct ExtraFields {
194194
ExtraFields() = default;
195195

196196
template <std::size_t N>
197-
explicit(false) ExtraFields(std::array<Field, N> const& fields) {}
197+
explicit(false) ExtraFields(std::array<Field, N> const& fields) {
198+
// TODO
199+
}
198200

199201
explicit(false) ExtraFields(std::vector<Field> fields) : fields(std::move(fields)) {}
200202
template <typename T>

src/sinks/terminal.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace rsl::logging {
66
void TerminalSink::emit_event(Event const& event) {
77
std::println("{} ({: >8} {}) {}", event.meta.timestamp, event.meta.context.name, event.meta.context.id, std::string(event.text));
8+
for (auto const& extra : event.meta.extra) {
9+
std::println(" {} = {}", extra.name, extra.to_string());
10+
}
811
}
912

1013
void TerminalSink::enter_context(Metadata const& meta, bool handover) {

0 commit comments

Comments
 (0)