deltaFlow
Logger.C
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
26#include "Display.H"
27#include "Logger.H"
28
30 static Logger instance("deltaFlow.log", Level::DEBUG);
31 return instance;
32}
33
34Logger::Logger(const std::string& name, Level level) : m_FilePath(name), m_Level(level) {
35 file.open(m_FilePath);
36 if (!file.is_open()) {
37 std::cerr << "Failed to open log file: " << m_FilePath << std::endl;
38 }
39
40 // Write banner to log file
41 if (file.is_open()) {
43 auto now = std::time(nullptr);
44 auto ts = fmt::format("{:%d-%b-%Y %H:%M:%S}", fmt::localtime(now));
45 file << fmt::format("\n Log started: {}\n", ts);
46 file << " " << Display::separator('-') << "\n\n";
47 file.flush();
48 }
49}
50
52 if (file.is_open()) {
53 auto now = std::time(nullptr);
54 auto ts = fmt::format("{:%d-%b-%Y %H:%M:%S}", fmt::localtime(now));
55 file << "\n " << Display::separator('-') << "\n";
56 file << fmt::format(" Log ended: {}\n", ts);
57 file.close();
58 }
59}
60
61void Logger::log(const std::string& msg, const Level& level) {
62 std::string levelStr;
63 m_Level = level;
64 fmt::color levelColor = fmt::color::white;
65
66 switch (m_Level) {
67 case Level::DEBUG:
68 levelStr = "DEBUG"; levelColor = fmt::color::light_blue; break;
69 case Level::INFO:
70 levelStr = "INFO"; levelColor = fmt::color::green; break;
71 case Level::WARN:
72 levelStr = "WARN"; levelColor = fmt::color::yellow; break;
73 case Level::ERROR:
74 levelStr = "ERROR"; levelColor = fmt::color::orange_red; break;
75 case Level::CRITICAL:
76 levelStr = "CRITICAL"; levelColor = fmt::color::red; break;
77 default:
78 levelStr = "LOG"; break;
79 }
80
81 auto now = std::time(nullptr);
82 auto timestamp = fmt::format("{:%d-%m-%Y %H:%M:%S}", fmt::localtime(now));
83
84 // Plain text to log file
85 std::string logMessage = fmt::format(
86 "{} :: {:<8} :: {}\n", timestamp, levelStr, msg
87 );
88
89 if (file.is_open()) {
90 file << logMessage;
91 file.flush();
92 }
93
94 // Colored output to terminal
95 logMessage = fmt::format(
96 "{} :: {} :: {}\n",
97 timestamp,
98 fmt::format(fg(levelColor) | fmt::emphasis::bold, "{:<8}", levelStr),
99 msg
100 );
101
102 fmt::print("{}", logMessage);
103}
Display and formatting utilities for terminal and file output.
Logger utilities for deltaFlow, providing logging macros and a singleton Logger class.
Level
Log severity levels.
Definition Logger.H:60
@ 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(const std::string &name, Level level)
Construct a logger with file name and log level.
Definition Logger.C:34
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
std::ofstream file
Output file stream.
Definition Logger.H:117
std::string separator(char ch='=')
Returns a separator line of the given character.
Definition Display.H:82
std::string fileBanner()
Returns a full plain-text banner for output/log files.
Definition Display.H:217