File size: 1,367 Bytes
89e64cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @file logger.hpp
 * @brief Structured logging using modern C++23 std::format.
 */

#pragma once

#include <string>
#include <format>
#include <iostream>
#include <chrono>
#include <source_location>

namespace app::utils {

enum class LogLevel {
    Debug,
    Info,
    Warn,
    Error
};

class Logger {
public:
    explicit Logger(std::string name) : name_(std::move(name)) {}

    template<typename... Args>
    void info(std::format_string<Args...> fmt, Args&&... args,
              std::source_location loc = std::source_location::current()) {
        log(LogLevel::Info, std::format(fmt, std::forward<Args>(args)...), loc);
    }

    template<typename... Args>
    void error(std::format_string<Args...> fmt, Args&&... args,
               std::source_location loc = std::source_location::current()) {
        log(LogLevel::Error, std::format(fmt, std::forward<Args>(args)...), loc);
    }

    template<typename... Args>
    void debug(std::format_string<Args...> fmt, Args&&... args,
               std::source_location loc = std::source_location::current()) {
        log(LogLevel::Debug, std::format(fmt, std::forward<Args>(args)...), loc);
    }

private:
    void log(LogLevel level, const std::string& message, const std::source_location& loc);

    std::string name_;
};

Logger& get_logger(const std::string& name);

} // namespace app::utils