Skip to content

Commit 500c706

Browse files
committed
refactor
1 parent 1ac094b commit 500c706

File tree

14 files changed

+96
-75
lines changed

14 files changed

+96
-75
lines changed

example/spans.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#include <rsl/log>
22

3-
using enum rsl::logging::LogLevel;
4-
53
void bar() {
64
// TODO
7-
rsl::logging::ContextGuard context{"bar", INFO, std::monostate()};
5+
RSL_LOG_CONTEXT("bar", rsl::log_level::INFO, x=1, y=2);
86
rsl::info("from bar");
97
}
108

@@ -19,7 +17,7 @@ int main() {
1917

2018
// rsl::logging::ContextGuard context{INFO, "main"};
2119
auto x = 42;
22-
auto ctx = rsl::log::context("main", INFO);
20+
auto ctx = rsl::log::context("main", rsl::log_level::INFO);
2321
// ctx.extra = ExtraFields{{rsl::_log_impl::Field(^^x).set_ptr(&x)}};
2422
ctx.enter();
2523
rsl::error("test error");

include/rsl/log

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#pragma once
22
#include <rsl/logging/_impl/formatter.hpp>
33
#include <rsl/logging/_impl/args.hpp>
4-
#include <rsl/logging/logger.hpp>
4+
#include <rsl/logging/output.hpp>
55
#include <rsl/logging/context.hpp>
66

7+
#include <rsl/logging/flavor/default.hpp>
8+
#include <rsl/logging/flavor/null.hpp>
9+
710
#include <utility>
811

912
#include <rsl/kwargs>

include/rsl/logging/_impl/args.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ranges>
44

55
#include <rsl/logging/field.hpp>
6+
#include <rsl/util/to_string.hpp>
67

78
namespace rsl::logging {
89
namespace _impl {
@@ -27,20 +28,24 @@ struct FunctionScope {
2728
}
2829

2930
template <typename... Ts>
30-
static ExtraFields capture_args(Ts&&... args) {
31+
static std::array<Field, max_idx> capture_args(Ts&&... args) {
3132
std::array<Field, max_idx> arguments;
33+
std::size_t unnamed_count = 0;
34+
3235
template for (constexpr auto Idx : std::views::iota(0ZU, max_idx)) {
3336
constexpr static auto param = parameters_of(scope)[Idx];
34-
constexpr static auto name = define_static_string(identifier_of(param));
3537

3638
typename[:type_of(param):]* ptr = nullptr;
3739
if constexpr (sizeof...(Ts) >= Idx) {
3840
ptr = args...[Idx];
3941
}
40-
arguments[Idx] = Field(name, ptr);
42+
43+
arguments[Idx] = Field(has_identifier(param) ? identifier_of(param)
44+
: "unnamed_" + rsl::util::utos(unnamed_count++),
45+
ptr);
4146
}
4247

43-
return ExtraFields({arguments.begin(), arguments.end()});
48+
return arguments;
4449
}
4550
};
4651
} // namespace rsl::logging

include/rsl/logging/_impl/default_config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include <rsl/logging/logger.hpp>
2+
#include <rsl/logging/flavor/default.hpp>
33
#include <rsl/logging/level.hpp>
44
#include <rsl/logging/event.hpp>
55

include/rsl/logging/_impl/formatter.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <rsl/logging/event.hpp>
1616
#include <rsl/logging/context.hpp>
1717
#include <rsl/logging/field.hpp>
18-
#include <rsl/logging/logger.hpp>
1918

2019
namespace rsl::logging {
2120
namespace _impl {

include/rsl/logging/context.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,18 @@ extern thread_local Context* current_context;
8383

8484
template <typename T = std::monostate>
8585
struct ContextGuard : private Context {
86-
T extra;
86+
T extra_data;
8787

8888
explicit ContextGuard(std::string name,
8989
LogLevel min_level,
90-
T extra = {},
9190
ExtraFields arguments = {},
91+
T extra_fields = {},
9292
rsl::source_location const& sloc = std::source_location::current())
93-
: Context(name, min_level, arguments, {}, sloc) {
94-
// TODO bind T as ExtraFields
93+
: Context(name, min_level, arguments, {}, sloc)
94+
, extra_data(extra_fields) {
95+
if constexpr (not std::same_as<T, std::monostate>) {
96+
extra = ExtraFields(extra_fields);
97+
}
9598
enter<T>();
9699
}
97100

@@ -105,7 +108,6 @@ struct ContextGuard : private Context {
105108
using Context::enabled_for;
106109
};
107110

108-
109111
namespace _log_impl {
110112
template <typename T>
111113
concept has_member_op_co_await = requires(T t) { t.operator co_await(); };

include/rsl/logging/field.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include <meta>
88
#include <atomic>
99

10-
1110
#include <rsl/serialize>
1211
#include <rsl/repr>
12+
#include <rsl/kwargs>
1313

1414
namespace rsl::logging {
1515
class Field {
@@ -51,7 +51,7 @@ class Field {
5151
auto const& control_block = *static_cast<RefCounted const*>(field->ptr);
5252
// control_block.references++;
5353
auto val = control_block.references.fetch_add(1);
54-
return Field(field->ptr, field->vtable, field->name);
54+
return {field->ptr, field->vtable, field->name};
5555
}
5656

5757
static void destroy(void* p) {
@@ -86,7 +86,7 @@ class Field {
8686
return &table;
8787
}
8888

89-
void* ptr = nullptr;
89+
void* ptr = nullptr;
9090
VTable const* vtable = nullptr;
9191

9292
void destroy() {
@@ -117,10 +117,7 @@ class Field {
117117
Field(const Field& other) : Field(copy_from(other)) {}
118118

119119
// move constructor
120-
Field(Field&& other) noexcept
121-
: ptr(other.ptr)
122-
, vtable(other.vtable)
123-
, name(other.name) {
120+
Field(Field&& other) noexcept : ptr(other.ptr), vtable(other.vtable), name(other.name) {
124121
other.ptr = nullptr;
125122
other.vtable = nullptr;
126123
}
@@ -148,7 +145,7 @@ class Field {
148145
template <typename T>
149146
requires(not std::same_as<T, void>)
150147
Field(std::string_view name, T* value)
151-
: ptr(value)
148+
: ptr((void*)value)
152149
, vtable(make_vtable<T, false>())
153150
, name(name) {}
154151

@@ -191,22 +188,26 @@ class Field {
191188
};
192189

193190
struct ExtraFields {
194-
//TODO this could be a span that views memory on the stack and transitions to heap if its elts do
191+
// TODO this could be a span that views memory on the stack and transitions to heap if its elts do
195192
std::vector<Field> fields;
196193

197194
ExtraFields() = default;
195+
196+
template <std::size_t N>
197+
explicit(false) ExtraFields(std::array<Field, N> const& fields) {}
198+
198199
explicit(false) ExtraFields(std::vector<Field> fields) : fields(std::move(fields)) {}
199200
template <typename T>
200-
requires std::is_aggregate_v<std::remove_cvref_t<T>>
201-
explicit(false) ExtraFields(T&& kwargs) {
201+
requires is_kwargs<T>
202+
explicit(false) ExtraFields(T const& kwargs) {
202203
template for (constexpr auto member : std::define_static_array(
203204
nonstatic_data_members_of(^^typename std::remove_cvref_t<T>::type,
204205
std::meta::access_context::current()))) {
205206
static constexpr auto name = std::define_static_string(identifier_of(member));
206207
// we need to clone here
207208
// the kwargs wrapper will not live long enough, it'll be destroyed
208209
// after the full expression in which this container is created
209-
fields.push_back(Field(name, &kwargs.[:member:]).clone());
210+
fields.push_back(Field(name, &kwargs.[:member:]));
210211
}
211212
}
212213

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "logger.hpp"
1+
#include "default.hpp"
22

33
namespace rsl::logging {
44
struct AsyncLogger : DefaultLogger {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
#include <rsl/logging/event.hpp>
3+
#include <rsl/logging/output.hpp>
4+
5+
namespace rsl::logging {
6+
namespace _impl {
7+
template <LogLevel Level, typename... Args>
8+
struct FormatString;
9+
}
10+
11+
struct DefaultLogger {
12+
static void context(Metadata const& meta, bool entered, bool async_handover) {
13+
if (auto* output = current_output()) {
14+
output->context(meta, entered, async_handover);
15+
}
16+
}
17+
18+
template <LogLevel Severity, typename... Args>
19+
static void emit(Metadata& meta, _impl::FormatString<Severity, Args...> fmt, Args&&... args) {
20+
auto message = fmt.make_message(std::forward<Args>(args)...);
21+
auto event = Event{.meta = meta, .text = message};
22+
if (auto* output = current_output()) {
23+
output->emit(event);
24+
}
25+
}
26+
27+
static void set_output(OutputBase& output) {
28+
current_output() = &output;
29+
}
30+
31+
private:
32+
static OutputBase*& current_output();
33+
};
34+
35+
} // namespace rsl::logging
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#include <rsl/logging/event.hpp>
3+
4+
namespace rsl::logging {
5+
namespace _impl {
6+
template <LogLevel Level, typename... Args>
7+
struct FormatString;
8+
}
9+
10+
struct NullLogger {
11+
static void context(Metadata const& meta, bool entered, bool async_handover) {}
12+
13+
template <LogLevel Severity, typename... Args>
14+
static void emit(Metadata& meta, _impl::FormatString<Severity, Args...> fmt, Args&&... args) {}
15+
};
16+
17+
} // namespace rsl::logging

0 commit comments

Comments
 (0)