MetaImGUI 1.0.0
ImGui Application Template for C++20
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/*
2 MetaImGUI
3 Copyright (C) 2026 A P Nicholson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <filesystem>
22#include <fstream>
23#include <memory>
24#include <mutex>
25#include <sstream>
26#include <string>
27#include <string_view>
28
29namespace MetaImGUI {
30
34enum class LogLevel {
35 Debug,
36 Info,
37 Warning,
38 Error,
39 Fatal
40};
41
58class Logger {
59public:
63 static Logger& Instance();
64
65 // Delete copy and move
66 Logger(const Logger&) = delete;
67 Logger& operator=(const Logger&) = delete;
68 Logger(Logger&&) = delete;
69 Logger& operator=(Logger&&) = delete;
70
76 void Initialize(const std::filesystem::path& logFilePath = "", LogLevel minLevel = LogLevel::Info);
77
81 void Shutdown();
82
86 void SetLevel(LogLevel level);
87
91 LogLevel GetLevel() const;
92
96 void SetConsoleOutput(bool enable);
97
101 void SetFileOutput(bool enable);
102
106 void Flush();
107
111 std::filesystem::path GetLogFilePath() const;
112
113 // Logging methods
114 template <typename... Args>
115 void Debug(std::string_view format, Args&&... args) {
116 Log(LogLevel::Debug, format, std::forward<Args>(args)...);
117 }
118
119 template <typename... Args>
120 void Info(std::string_view format, Args&&... args) {
121 Log(LogLevel::Info, format, std::forward<Args>(args)...);
122 }
123
124 template <typename... Args>
125 void Warning(std::string_view format, Args&&... args) {
126 Log(LogLevel::Warning, format, std::forward<Args>(args)...);
127 }
128
129 template <typename... Args>
130 void Error(std::string_view format, Args&&... args) {
131 Log(LogLevel::Error, format, std::forward<Args>(args)...);
132 }
133
134 template <typename... Args>
135 void Fatal(std::string_view format, Args&&... args) {
136 Log(LogLevel::Fatal, format, std::forward<Args>(args)...);
137 }
138
139 // Simple overloads for no-argument messages
140 void Debug(std::string_view message) {
141 Log(LogLevel::Debug, message);
142 }
143 void Info(std::string_view message) {
144 Log(LogLevel::Info, message);
145 }
146 void Warning(std::string_view message) {
147 Log(LogLevel::Warning, message);
148 }
149 void Error(std::string_view message) {
150 Log(LogLevel::Error, message);
151 }
152 void Fatal(std::string_view message) {
153 Log(LogLevel::Fatal, message);
154 }
155
156private:
157 Logger();
158 ~Logger();
159
160 template <typename... Args>
161 void Log(LogLevel level, std::string_view format, Args&&... args) {
162 if (level < m_minLevel) {
163 return;
164 }
165
166 const std::string message = Format(format, std::forward<Args>(args)...);
167 LogMessage(level, message);
168 }
169
170 void Log(LogLevel level, std::string_view message) {
171 if (level < m_minLevel) {
172 return;
173 }
174 LogMessage(level, std::string(message));
175 }
176
177 void LogMessage(LogLevel level, const std::string& message);
178
179 // Simple format function (basic placeholder replacement)
180 template <typename... Args>
181 std::string Format(std::string_view format, Args&&... args) {
182 std::ostringstream oss;
183 FormatImpl(oss, format, std::forward<Args>(args)...);
184 return oss.str();
185 }
186
187 template <typename T, typename... Args>
188 void FormatImpl(std::ostringstream& oss, std::string_view format, T&& first, Args&&... rest) {
189 const size_t pos = format.find("{}");
190 if (pos != std::string_view::npos) {
191 oss << format.substr(0, pos) << std::forward<T>(first);
192 FormatImpl(oss, format.substr(pos + 2), std::forward<Args>(rest)...);
193 } else {
194 oss << format;
195 }
196 }
197
198 void FormatImpl(std::ostringstream& oss, std::string_view format) {
199 oss << format;
200 }
201
202 std::string GetTimestamp() const;
203 std::string LevelToString(LogLevel level) const;
204 const char* LevelToColor(LogLevel level) const;
205
206 LogLevel m_minLevel = LogLevel::Info;
207 bool m_consoleOutput = true;
208 bool m_fileOutput = false;
209 std::filesystem::path m_logFilePath;
210 std::ofstream m_logFile;
211 mutable std::mutex m_mutex;
212};
213
214} // namespace MetaImGUI
215
216// Convenience macros
217#define LOG_DEBUG(...) MetaImGUI::Logger::Instance().Debug(__VA_ARGS__)
218#define LOG_INFO(...) MetaImGUI::Logger::Instance().Info(__VA_ARGS__)
219#define LOG_WARNING(...) MetaImGUI::Logger::Instance().Warning(__VA_ARGS__)
220#define LOG_ERROR(...) MetaImGUI::Logger::Instance().Error(__VA_ARGS__)
221#define LOG_FATAL(...) MetaImGUI::Logger::Instance().Fatal(__VA_ARGS__)
Simple logging system with file and console output.
Definition Logger.h:58
void SetFileOutput(bool enable)
Enable/disable file output.
Definition Logger.cpp:96
Logger & operator=(const Logger &)=delete
void Debug(std::string_view message)
Definition Logger.h:140
void SetConsoleOutput(bool enable)
Enable/disable console output.
Definition Logger.cpp:91
Logger & operator=(Logger &&)=delete
void Debug(std::string_view format, Args &&... args)
Definition Logger.h:115
void Warning(std::string_view format, Args &&... args)
Definition Logger.h:125
void Info(std::string_view format, Args &&... args)
Definition Logger.h:120
void Info(std::string_view message)
Definition Logger.h:143
void Warning(std::string_view message)
Definition Logger.h:146
void Shutdown()
Shutdown logger and flush buffers.
Definition Logger.cpp:72
void Error(std::string_view format, Args &&... args)
Definition Logger.h:130
Logger(Logger &&)=delete
Logger(const Logger &)=delete
void Initialize(const std::filesystem::path &logFilePath="", LogLevel minLevel=LogLevel::Info)
Initialize logger with optional log file.
Definition Logger.cpp:39
LogLevel GetLevel() const
Get current log level.
Definition Logger.cpp:86
void Error(std::string_view message)
Definition Logger.h:149
void Fatal(std::string_view format, Args &&... args)
Definition Logger.h:135
static Logger & Instance()
Get singleton instance.
Definition Logger.cpp:34
void SetLevel(LogLevel level)
Set minimum log level.
Definition Logger.cpp:81
void Fatal(std::string_view message)
Definition Logger.h:152
std::filesystem::path GetLogFilePath() const
Get log file path.
Definition Logger.cpp:110
void Flush()
Flush log buffers.
Definition Logger.cpp:101
LogLevel
Log severity levels.
Definition Logger.h:34
@ Warning
Warning messages.
@ Info
Informational messages.
@ Fatal
Fatal error messages.
@ Error
Error messages.
@ Debug
Detailed debugging information.
@ Info
Information icon.