deltaFlow
Logger.H
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
32#ifndef LOGGER_H
33#define LOGGER_H
34
35#include <chrono>
36#include <fmt/chrono.h>
37#include <fmt/color.h>
38#include <fstream>
39#include <iostream>
40#include <string>
41
42#ifdef _WIN32
43 #ifdef ERROR
44 #undef ERROR
45 #endif
46#endif
47
60enum class Level : uint8_t {
61 NOTSET,
62 DEBUG,
63 INFO,
64 WARN,
65 ERROR,
67};
68
85#define LOG_DEBUG(msg, ...) Logger::getLogger().log(fmt::format(FMT_STRING(msg), ##__VA_ARGS__), Level::DEBUG)
86#define LOG_INFO(msg, ...) Logger::getLogger().log(fmt::format(FMT_STRING(msg), ##__VA_ARGS__), Level::INFO)
87#define LOG_WARN(msg, ...) Logger::getLogger().log(fmt::format(FMT_STRING(msg), ##__VA_ARGS__), Level::WARN)
88#define LOG_ERROR(msg, ...) Logger::getLogger().log(fmt::format(FMT_STRING(msg), ##__VA_ARGS__), Level::ERROR)
89#define LOG_CRITICAL(msg, ...) Logger::getLogger().log(fmt::format(FMT_STRING(msg), ##__VA_ARGS__), Level::CRITICAL)
90#define LOG_MESSAGE(msg, ...) fmt::print("{}\n", fmt::format(FMT_STRING(msg), ##__VA_ARGS__))
91
99class Logger final {
100 public:
105 static Logger& getLogger();
106
112 void log(const std::string& msg, const Level& level);
113
114 private:
116 std::string m_FilePath;
117 std::ofstream file;
118
124 Logger(const std::string& name, Level level);
125
129 ~Logger();
130
131 // Disable copying
132 Logger(const Logger&) = delete;
133 Logger& operator=(const Logger&) = delete;
134};
135
136#endif
Level
Log severity levels.
Definition Logger.H:60
@ NOTSET
No level set.
@ WARN
Warning conditions.
@ INFO
Informational messages.
@ CRITICAL
Critical conditions.
@ ERROR
Error conditions.
@ DEBUG
Debug messages.
Singleton logger class for deltaFlow.
Definition Logger.H:99
static Logger & getLogger()
Get the singleton Logger instance.
Definition Logger.C:29
Level m_Level
Minimum logging level.
Definition Logger.H:115
Logger & operator=(const Logger &)=delete
std::string m_FilePath
Path to the log file.
Definition Logger.H:116
~Logger()
Destructor flushes and closes log file.
Definition Logger.C:51
void log(const std::string &msg, const Level &level)
Log a message at a given severity level.
Definition Logger.C:61
Logger(const Logger &)=delete
std::ofstream file
Output file stream.
Definition Logger.H:117