1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include "asl/base/utility.hpp"
#include "asl/formatting/format.hpp"
#include "asl/containers/intrusive_list.hpp"
namespace asl::log
{
enum level : uint8_t
{
kDebug = 0,
kInfo,
kWarning,
kError,
};
struct message
{
level level;
string_view message;
source_location location;
};
class Logger : public intrusive_list_node<Logger>
{
public:
Logger() = default;
ASL_DEFAULT_COPY_MOVE(Logger);
virtual ~Logger() = default;
virtual void log(const message&) = 0;
};
class DefaultLoggerBase : public Logger
{
protected:
static void log_inner(Writer&, const message&);
};
template<derefs_as<Writer> W>
class DefaultLogger : public DefaultLoggerBase
{
W m_writer;
public:
template<typename U>
explicit constexpr DefaultLogger(U&& writer)
requires constructible_from<W, U&&>
: m_writer{std::forward<U>(writer)}
{}
constexpr void log(const message& m) override
{
log_inner(deref<Writer>(m_writer), m);
}
};
void register_logger(Logger*);
void unregister_logger(Logger*);
void remove_default_logger();
// @Todo Add a way to remove loggers (including all)
void log_inner(level l, string_view fmt, span<const format_internals::type_erased_arg> args, const source_location& sl);
template<formattable... Args>
void log(level l, const source_location& sl, string_view fmt, const Args&... args)
{
if constexpr (sizeof...(Args) == 0)
{
log_inner(l, fmt, {}, sl);
}
else
{
format_internals::type_erased_arg type_erased_args[] = {
format_internals::type_erased_arg(args)...
};
log_inner(l, fmt, type_erased_args, sl);
}
}
} // namespace asl::log
// @Todo Compile-time configuration of logging
#define ASL_LOG_DEBUG(...) ::asl::log::log(::asl::log::kDebug, ::asl::source_location{}, __VA_ARGS__)
#define ASL_LOG_INFO(...) ::asl::log::log(::asl::log::kInfo, ::asl::source_location{}, __VA_ARGS__)
#define ASL_LOG_WARNING(...) ::asl::log::log(::asl::log::kWarning, ::asl::source_location{}, __VA_ARGS__)
#define ASL_LOG_ERROR(...) ::asl::log::log(::asl::log::kError, ::asl::source_location{}, __VA_ARGS__)
|