summaryrefslogtreecommitdiff
path: root/asl/logging/logging.cpp
blob: aa630aca5ab0d6e89fe46639ad747eb66feb5a7a (plain)
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
#include "asl/logging/logging.hpp"
#include "asl/print.hpp"
#include "asl/string_builder.hpp"

// @Todo Don't use internal get_stdout_writer, make console module

static asl::log::DefaultLogger<asl::Writer*> g_default_logger{asl::print_internals::get_stdout_writer()};
static asl::log::Logger* g_head = &g_default_logger;

static constexpr asl::string_view kLevelName[] = {
    "  DEBUG  ",
    "  INFO   ",
    " WARNING ",
    "  ERROR  ",
};

void asl::log::register_logger(box<Logger> logger_box)
{
    auto* logger = leak(ASL_MOVE(logger_box));
    logger->m_next = exchange(g_head, logger);
}

void asl::log::DefaultLoggerBase::log_inner(Writer& writer, const message& msg)
{
    asl::format(&writer, "[{}] {}:{}: {}\n",
        kLevelName[msg.level], // NOLINT
        msg.location.file,
        msg.location.line,
        msg.message);
}

void asl::log::log_inner(
    level l,
    string_view fmt, span<const format_internals::type_erased_arg> args,
    const source_location& sl)
{
    // @Todo Use temporary allocator
    StringWriter msg_writer{};
    asl::format_internals::format(&msg_writer, fmt, args);

    message m{
        .level = l,
        .message = msg_writer.as_string_view(),
        .location = sl,
    };

    for (auto* it = g_head; it != nullptr; it = it->next_logger())
    {
        it->log(m);
    }
}