Engine
Raylib based game framework
Loading...
Searching...
No Matches
Logger.h
1#pragma once
2
3#include "Log/Log.h"
4#include "Types.h"
5
6#include <fstream>
7#include <mutex>
8
13enum class LogLevel : u8
14{
15 DEBUG,
16 INFO,
17 WARN,
18 ERROR,
19};
20
25class Logger
26{
27public:
28
35 static void SetLogLevel(const LogLevel levelIn);
36
45 static void SetLogFile(const char* path);
46
63 template <typename... Args>
64 static void Write(const LogLevel levelIn, Args&&... args)
65 {
66 if (levelIn < level)
67 {
68 return;
69 }
70
71 std::lock_guard<std::mutex> lock(mutex);
72
73 if (file.is_open())
74 {
75 file << "[" << GetCurrentTimeString() << "] " << "[" << LevelName(level) << "] ";
76 (file << ... << std::forward<Args>(args)) << '\n';
77 }
78
79 std::cerr << LevelColor(level) << "[" << GetCurrentTimeString() << "] " << "[" << LevelName(level) << "] ";
80 (std::cerr << ... << std::forward<Args>(args)) << ANSI_RESET << '\n';
81 }
82
83private:
84
85 static const char* LevelName(const LogLevel level);
86
87 static const char* LevelColor(const LogLevel level);
88
89 inline static std::ofstream file;
90 inline static LogLevel level = LogLevel::DEBUG;
91 inline static std::mutex mutex;
92};
Thread safe logging utility with differing levels for both console and file.
Definition Logger.h:26
static void SetLogLevel(const LogLevel levelIn)
Sets minimum log level.
Definition Logger.cpp:3
static void SetLogFile(const char *path)
Sets the output file.
Definition Logger.cpp:10
static void Write(const LogLevel levelIn, Args &&... args)
Writes a log message.
Definition Logger.h:64