ArkScript
A small, fast, functional and scripting language for video games
Logger.hpp
Go to the documentation of this file.
1/**
2 * @file Logger.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Internal logger
5 * @date 2024-08-30
6 *
7 * @copyright Copyright (c) 2024-2025)
8 */
9#ifndef ARK_LOGGER_HPP
10#define ARK_LOGGER_HPP
11
12#include <iostream>
13#include <fmt/format.h>
14
15#include <string>
16#include <chrono>
17#include <vector>
18#include <unordered_map>
19#include <fmt/color.h>
20
21namespace Ark::internal
22{
23 class Logger
24 {
25 public:
26 /**
27 * @brief Construct a new Logger object
28 *
29 * @param name the pass name, used for logging
30 * @param debug_level debug level
31 */
32 Logger(std::string name, unsigned debug_level);
33
34 [[nodiscard]] inline unsigned debugLevel() const { return m_debug; }
35
36 [[nodiscard]] inline bool shouldInfo() const { return m_debug >= 1; }
37 [[nodiscard]] inline bool shouldDebug() const { return m_debug >= 2; }
38 [[nodiscard]] inline bool shouldTrace() const { return m_debug >= 3; }
39
40 /**
41 * @brief Write an info level log using fmtlib
42 * @tparam Args
43 * @param fmt format string
44 * @param args
45 */
46 template <typename... Args>
47 void info(const char* fmt, Args&&... args)
48 {
49 if (shouldInfo())
50 fmt::println(
51 "{} [{}] {}",
52 fmt::styled("INFO ", fmt::fg(fmt::color::cornflower_blue)),
53 fmt::styled(m_name, fmt::fg(m_pass_color)),
54 fmt::vformat(fmt, fmt::make_format_args(args...)));
55 }
56
57 /**
58 * @brief Write a debug level log using fmtlib
59 * @tparam Args
60 * @param fmt format string
61 * @param args
62 */
63 template <typename... Args>
64 void debug(const char* fmt, Args&&... args)
65 {
66 if (shouldDebug())
67 fmt::println(
68 "{} [{}] {}",
69 fmt::styled("DEBUG", fmt::fg(fmt::color::pale_violet_red)),
70 fmt::styled(m_name, fmt::fg(m_pass_color)),
71 fmt::vformat(fmt, fmt::make_format_args(args...)));
72 }
73
74 inline void traceStart(std::string&& trace_name)
75 {
76 m_trace_starts[trace_name] = std::chrono::high_resolution_clock::now();
77 m_active_traces.push_back(trace_name);
78 }
79
80 inline void traceEnd()
81 {
82 std::string trace_name = m_active_traces.back();
83 m_active_traces.pop_back();
84
85 const auto time = std::chrono::high_resolution_clock::now();
86 const std::chrono::duration<double, std::milli> ms_double = time - m_trace_starts[trace_name];
87 trace("{} took {:.3f}ms", trace_name, ms_double.count());
88 }
89
90 /**
91 * @brief Write a trace level log using fmtlib
92 * @tparam Args
93 * @param fmt format string
94 * @param args
95 */
96 template <typename... Args>
97 void trace(const char* fmt, Args&&... args)
98 {
99 if (shouldTrace())
100 fmt::println(
101 "{} [{}] {}",
102 fmt::styled("TRACE", fmt::fg(fmt::color::golden_rod)),
103 fmt::styled(m_name, fmt::fg(m_pass_color)),
104 fmt::vformat(fmt, fmt::make_format_args(args...)));
105 }
106
107 private:
108 unsigned m_debug;
109 std::string m_name;
110 fmt::color m_pass_color;
111 std::unordered_map<std::string, std::chrono::time_point<std::chrono::high_resolution_clock>> m_trace_starts;
112 std::vector<std::string> m_active_traces;
113 };
114}
115
116#endif // ARK_LOGGER_HPP
bool shouldTrace() const
Definition Logger.hpp:38
std::unordered_map< std::string, std::chrono::time_point< std::chrono::high_resolution_clock > > m_trace_starts
Definition Logger.hpp:111
void info(const char *fmt, Args &&... args)
Write an info level log using fmtlib.
Definition Logger.hpp:47
void trace(const char *fmt, Args &&... args)
Write a trace level log using fmtlib.
Definition Logger.hpp:97
Logger(std::string name, unsigned debug_level)
Construct a new Logger object.
Definition Logger.cpp:29
bool shouldDebug() const
Definition Logger.hpp:37
void debug(const char *fmt, Args &&... args)
Write a debug level log using fmtlib.
Definition Logger.hpp:64
std::string m_name
Definition Logger.hpp:109
bool shouldInfo() const
Definition Logger.hpp:36
std::vector< std::string > m_active_traces
Definition Logger.hpp:112
void traceStart(std::string &&trace_name)
Definition Logger.hpp:74
unsigned debugLevel() const
Definition Logger.hpp:34
fmt::color m_pass_color
Definition Logger.hpp:110