r-type  0.0.0
R-Type main
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 public:
32 Logger(const Logger &) = delete;
33 Logger &operator=(const Logger &) = delete;
34 Logger(Logger &&) = delete;
35 Logger &operator=(Logger &&) = delete;
36
37 static void init();
38
39 template <typename Func> static void logExecutionTime(const std::string &message, Func &&func)
40 {
41 const auto start = std::chrono::high_resolution_clock::now();
42 func();
43 const auto end = std::chrono::high_resolution_clock::now();
44 const auto duration = std::chrono::duration<float, std::milli>(end - start).count();
45
46 std::cout << getColorForDuration(duration)
47 << formatLogMessage(LogLevel::INFO, message + " took " + std::to_string(duration) + " ms")
49 }
50
51 static void log(const std::string &message, const LogLevel &logLevel)
52 {
54 << formatLogMessage(logLevel, message) << LOG_LEVEL_COLOR[COLOR_RESET];
55 }
56
57 private:
65
66 static constexpr std::array<const char *, 4> LOG_LEVEL_COLOR = {
67 "\033[31m", // ERROR/slow execution
68 "\033[32m", // INFO/fast execution
69 "\033[33m", // WARNING/medium execution
70 "\033[0m\n" // RESET + newline
71 };
72
73 static constexpr std::array<const char *, 2> LOG_LEVEL_STRING = {"INFO", "WARNING"};
74
75 Logger() = default;
76 ~Logger() = default;
77
78 [[nodiscard]] static const char *getColorForDuration(const float duration)
79 {
80 return duration < 20.0F
83 }
84
85 [[nodiscard]] static std::string formatLogMessage(LogLevel level, const std::string &message)
86 {
87 const auto inTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
88 std::ostringstream ss;
89 ss << "[" << std::put_time(std::localtime(&inTime), "%Y-%m-%d %X") << "] ";
90 ss << "[" << LOG_LEVEL_STRING[static_cast<uint8_t>(level)] << "] " << message;
91 return ss.str();
92 }
93
94 }; // class Logger
95
96} // namespace utl
Class for logging.
Definition Logger.hpp:29
Logger & operator=(const Logger &)=delete
@ COLOR_WARNING
Definition Logger.hpp:62
Logger()=default
Logger & operator=(Logger &&)=delete
static constexpr std::array< const char *, 2 > LOG_LEVEL_STRING
Definition Logger.hpp:73
static constexpr std::array< const char *, 4 > LOG_LEVEL_COLOR
Definition Logger.hpp:66
static std::string formatLogMessage(LogLevel level, const std::string &message)
Definition Logger.hpp:85
~Logger()=default
static void init()
Definition logger.cpp:7
Logger(const Logger &)=delete
static void log(const std::string &message, const LogLevel &logLevel)
Definition Logger.hpp:51
Logger(Logger &&)=delete
static void logExecutionTime(const std::string &message, Func &&func)
Definition Logger.hpp:39
static const char * getColorForDuration(const float duration)
Definition Logger.hpp:78
LogLevel
Definition Logger.hpp:18