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