vengine  0.0.1
3D 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 ven
5///
6
7#pragma once
8
9#include <iostream>
10#include <array>
11#include <sstream>
12#include <iomanip>
13
14#ifdef _WIN32
15 #include <windows.h>
16#endif
17
19
20
21namespace ven {
22
23 enum class LogLevel : uint8_t {
24 INFO,
26 };
27
28 class Logger {
29
30 public:
31
32 static Logger& getInstance() { static Logger instance; return instance; }
33
34 template <typename Func>
35 static void logExecutionTime(const std::string& message, Func&& func)
36 {
37 Clock clock;
38 func();
39 clock.update();
40 const float duration = clock.getDeltaTimeMS();
41
42 std::cout << getColorForDuration(duration) << formatLogMessage(LogLevel::INFO, message + " took " + std::to_string(duration) + " ms") << LOG_LEVEL_COLOR.at(3);
43 }
44
45 static void logWarning(const std::string& message) { std::cout << LOG_LEVEL_COLOR.at(2) << formatLogMessage(LogLevel::WARNING, message) << LOG_LEVEL_COLOR.at(3); }
46
47 private:
48
49 static constexpr std::array<const char*, 2> LOG_LEVEL_STRING = {"INFO", "WARNING"};
50 static constexpr std::array<const char*, 4> LOG_LEVEL_COLOR = {"\033[31m", "\033[32m", "\033[33m", "\033[0m\n"};
51
52 Logger();
53
54 static const char* getColorForDuration(const float duration) { return duration < 20.0F ? LOG_LEVEL_COLOR.at(1) : (duration < 90.0F ? LOG_LEVEL_COLOR.at(2) : LOG_LEVEL_COLOR.at(0)); }
55
56 [[nodiscard]] static std::string formatLogMessage(LogLevel level, const std::string& message);
57
58 }; // class Logger
59
60} // namespace ven
This file contains the Clock class.
Class for clock.
Definition Clock.hpp:20
float getDeltaTimeMS() const
Definition Clock.hpp:39
void update()
Definition clock.cpp:3
static void logWarning(const std::string &message)
Definition Logger.hpp:45
static constexpr std::array< const char *, 2 > LOG_LEVEL_STRING
Definition Logger.hpp:49
static std::string formatLogMessage(LogLevel level, const std::string &message)
Definition logger.cpp:14
static constexpr std::array< const char *, 4 > LOG_LEVEL_COLOR
Definition Logger.hpp:50
static const char * getColorForDuration(const float duration)
Definition Logger.hpp:54
static Logger & getInstance()
Definition Logger.hpp:32
static void logExecutionTime(const std::string &message, Func &&func)
Definition Logger.hpp:35
LogLevel
Definition Logger.hpp:23