cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1///
2/// @file Logger.hpp
3/// @brief This file contains the Logger class
4/// @namespace utl
5///
6
7#pragma once
8
9#include <array>
10#include <chrono>
11#include <iomanip>
12#include <iostream>
13
14namespace utl
15{
16
17 enum class LogLevel : uint8_t
18 {
19 INFO,
21 };
22
23 ///
24 /// @class Logger
25 /// @brief Class for logging
26 /// @namespace utl
27 ///
28 class Logger
29 {
30
31 private:
32 template <typename E> static constexpr auto to_underlying(E e) noexcept
33 {
34 return static_cast<std::underlying_type_t<E>>(e);
35 }
36
37 public:
38 Logger(const Logger &) = delete;
39 Logger &operator=(const Logger &) = delete;
40 Logger(Logger &&) = delete;
41 Logger &operator=(Logger &&) = delete;
42
43 ///
44 /// @brief Initialize the logger
45 ///
46 static void init();
47
48 ///
49 /// @tparam Func Function to be measured
50 /// @param message Message to be logged
51 /// @param func Function to be measured
52 /// @brief Log the execution time of a function
53 ///
54 template <typename Func> static void logExecutionTime(const std::string &message, Func &&func)
55 {
56 const auto start = std::chrono::high_resolution_clock::now();
57 func();
58 const auto end = std::chrono::high_resolution_clock::now();
59 const auto duration = std::chrono::duration<float, std::milli>(end - start).count();
60
61 std::cout << getColorForDuration(duration)
62 << formatLogMessage(LogLevel::INFO, message + " took " + std::to_string(duration) + " ms")
64 }
65
66 ///
67 /// @param message Message to be logged
68 /// @param logLevel Log level of the message
69 /// @brief Log a message with a specific log level
70 ///
71 static void log(const std::string &message, const LogLevel &logLevel)
72 {
75 << formatLogMessage(logLevel, message)
77 }
78
79 private:
80 enum class ColorIndex : uint8_t
81 {
82 COLOR_ERROR = 0,
83 COLOR_INFO = 1,
84 COLOR_WARNING = 2,
85 COLOR_RESET = 3
86 };
87
88 static constexpr std::array<const char *, 4> LOG_LEVEL_COLOR = {
89 "\033[31m", // ERROR/slow execution
90 "\033[32m", // INFO/fast execution
91 "\033[33m", // WARNING/medium execution
92 "\033[0m\n" // RESET + newline
93 };
94
95 static constexpr std::array<const char *, 2> LOG_LEVEL_STRING = {"INFO", "WARNING"};
96
97 Logger() = default;
98 ~Logger() = default;
99
100 [[nodiscard]] static const char *getColorForDuration(const float duration)
101 {
102 return duration < 20.0F ? LOG_LEVEL_COLOR[to_underlying(ColorIndex::COLOR_INFO)]
105 }
106
107 [[nodiscard]] static std::string formatLogMessage(LogLevel level, const std::string &message)
108 {
109 using namespace std::chrono;
110
111 const auto now = system_clock::now();
112 const auto inTimeT = system_clock::to_time_t(now);
113 const auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
114
115 std::ostringstream ss;
116 ss << "[" << std::put_time(std::localtime(&inTimeT), "%Y-%m-%d %H:%M:%S");
117 ss << ":" << std::setfill('0') << std::setw(3) << ms.count() << "] ";
118 ss << "[" << LOG_LEVEL_STRING[static_cast<uint8_t>(level)] << "] " << message;
119
120 return ss.str();
121 }
122
123 }; // class Logger
124
125} // namespace utl
Class for logging.
Definition Logger.hpp:29
Logger & operator=(const Logger &)=delete
Logger()=default
Logger & operator=(Logger &&)=delete
static constexpr std::array< const char *, 2 > LOG_LEVEL_STRING
Definition Logger.hpp:95
static constexpr auto to_underlying(E e) noexcept
Definition Logger.hpp:32
static constexpr std::array< const char *, 4 > LOG_LEVEL_COLOR
Definition Logger.hpp:88
static std::string formatLogMessage(LogLevel level, const std::string &message)
Definition Logger.hpp:107
~Logger()=default
static void init()
Initialize the logger.
Definition logger.cpp:7
Logger(const Logger &)=delete
static void log(const std::string &message, const LogLevel &logLevel)
Log a message with a specific log level.
Definition Logger.hpp:71
Logger(Logger &&)=delete
static void logExecutionTime(const std::string &message, Func &&func)
Log the execution time of a function.
Definition Logger.hpp:54
static const char * getColorForDuration(const float duration)
Definition Logger.hpp:100
LogLevel
Definition Logger.hpp:18