Skip to content

Commit e6d92f2

Browse files
committed
refactor, add kwargs support
1 parent fee5580 commit e6d92f2

5 files changed

Lines changed: 20 additions & 23 deletions

File tree

example/systemd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main() {
3333
logger.set_as_default();
3434
// set_output(logger);
3535
{
36-
$context("main", rsl::log_level::INFO);
36+
$context("main", rsl::log_level::INFO, foo=123);
3737
rsl::info("from main context");
3838
foo(42, 'c');
3939
}

include/rsl/log

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
#include <utility>
88

9-
#ifdef RSL_KWARGS
10-
// #include <kwargs.h>
9+
#include <rsl/kwargs>
1110
# define RSL_LOG_CONTEXT(name, level, ...) \
12-
rsl::log::context_guard _(name, level, make_args(__VA_ARGS__), RSL_LOG_ARGS)
13-
#else
14-
# define RSL_LOG_CONTEXT(name, level, ...) \
15-
rsl::log::context_guard<std::monostate> _(name, level, std::monostate(), RSL_LOG_ARGS)
16-
#endif
11+
rsl::log::context_guard _(name, level, RSL_LOG_ARGS, RSL_KWARGS(__VA_ARGS__))
12+
1713

1814
#define RSL_TRACE(...) \
1915
(void)(is_enabled_for(::rsl::logging::LogLevel::TRACE) && \

include/rsl/logging/_impl/formatter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct FormatString {
4242
}
4343
using initialize_t = void (FormatString::*)(std::string_view, rsl::source_location);
4444

45-
template <typename T> //, typename... Empty>
45+
template <typename T>
4646
requires std::convertible_to<T const&, std::string_view>
4747
consteval explicit(false)
4848
FormatString(T const& fmt,

include/rsl/logging/logger.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ struct Sink : Filter {
4747
}
4848
};
4949

50-
struct LoggerBase {
51-
virtual ~LoggerBase() = default;
50+
struct OutputBase {
51+
virtual ~OutputBase() = default;
5252
virtual void context(Metadata const& meta, bool entered, bool async_handover) = 0;
5353
virtual void emit(Event const& ev) = 0;
5454
};
@@ -61,7 +61,7 @@ void set_output(Output<Sinks...>& sinks);
6161

6262
template <typename... Ts>
6363
struct Output final
64-
: LoggerBase
64+
: OutputBase
6565
, Any<Ts...> {
6666
template <typename... Us>
6767
requires((std::same_as<std::remove_cvref_t<Us>, std::remove_cvref_t<Ts>> && ...))
@@ -88,25 +88,26 @@ struct NullLogger {
8888

8989
struct DefaultLogger {
9090
static void context(Metadata const& meta, bool entered, bool async_handover) {
91-
if (auto* logger = output()) {
92-
logger->context(meta, entered, async_handover);
91+
if (auto* output = current_output()) {
92+
output->context(meta, entered, async_handover);
9393
}
9494
}
9595

9696
template <LogLevel Severity, typename... Args>
9797
static void emit(Metadata& meta, _impl::FormatString<Severity, Args...> fmt, Args&&... args) {
9898
auto message = fmt.make_message(std::forward<Args>(args)...);
9999
auto event = Event{.meta = meta, .text = message};
100-
if (auto* logger = output()) {
101-
logger->emit(event);
100+
if (auto* output = current_output()) {
101+
output->emit(event);
102102
}
103103
}
104104

105-
static LoggerBase*& output();
106-
template <typename... Sinks>
107-
static void set_output(Output<Sinks...>& logger) {
108-
output() = &logger;
105+
static void set_output(OutputBase& output) {
106+
current_output() = &output;
109107
}
108+
109+
private:
110+
static OutputBase*& current_output();
110111
};
111112

112113
} // namespace rsl::logging

src/logger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
namespace rsl::logging {
55
namespace {
6-
LoggerBase* get_default_logger() {
6+
OutputBase* get_default_logger() {
77
static auto logger = Output(TerminalSink());
88
return &logger;
99
}
1010
} // namespace
1111

12-
LoggerBase*& DefaultLogger::output() {
13-
static LoggerBase* current = get_default_logger();
12+
OutputBase*& DefaultLogger::current_output() {
13+
static OutputBase* current = get_default_logger();
1414
return current;
1515
}
1616

0 commit comments

Comments
 (0)