game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// logger.cpp
4// ==============================================================================
5// Implementation of centralized logging system
6// ==============================================================================
7
8#include "logger.h"
9#include <chrono>
10#include <ctime>
11#include <iomanip>
12
13// ==============================================================================
14// Initialization & Configuration
15// ==============================================================================
16
17void Logger::init(const std::string& logFilePath, LogLevel minLevel)
18{
19 auto& inst = instance();
20 std::lock_guard<std::mutex> lock(inst.logMutex_);
21
22 inst.minLevel_ = minLevel;
23
24 if (!logFilePath.empty())
25 {
26 if (inst.logFile_.is_open())
27 inst.logFile_.close();
28 inst.logFile_.open(logFilePath, std::ios::app);
29 if (!inst.logFile_.is_open())
30 {
31 std::cerr << "[Logger] Failed to open log file: " << logFilePath << std::endl;
32 }
33 else
34 {
35 inst.logFile_ << "\n=== Logger initialized at " << getCurrentTimestamp()
36 << " ===" << std::endl;
37 }
38 }
39}
40
42{
43 auto& inst = instance();
44 std::lock_guard<std::mutex> lock(inst.logMutex_);
45 inst.minLevel_ = level;
46}
47
48void Logger::setConsoleOutput(bool enabled)
49{
50 auto& inst = instance();
51 std::lock_guard<std::mutex> lock(inst.logMutex_);
52 inst.consoleOutput_ = enabled;
53}
54
55void Logger::setTimestamps(bool enabled)
56{
57 auto& inst = instance();
58 std::lock_guard<std::mutex> lock(inst.logMutex_);
59 inst.timestamps_ = enabled;
60}
61
63{
64 auto& inst = instance();
65 std::lock_guard<std::mutex> lock(inst.logMutex_);
66
67 if (inst.logFile_.is_open())
68 {
69 inst.logFile_ << "=== Logger shutdown at " << getCurrentTimestamp() << " ===" << std::endl;
70 inst.logFile_.close();
71 }
72}
73
74// ==============================================================================
75// Logging Implementation
76// ==============================================================================
77
78void Logger::log(LogLevel level, const std::string& message)
79{
80 auto& inst = instance();
81 std::lock_guard<std::mutex> lock(inst.logMutex_);
82
83 // Filter by log level
84 if (level < inst.minLevel_)
85 {
86 return;
87 }
88
89 // Build log message
90 std::ostringstream oss;
91
92 if (inst.timestamps_)
93 {
94 oss << "[" << getCurrentTimestamp() << "] ";
95 }
96
97 oss << "[" << getLevelString(level) << "] " << message;
98 std::string fullMessage = oss.str();
99
100 // Output to console
101 if (inst.consoleOutput_)
102 {
103 if (level >= LogLevel::ERROR)
104 {
105 std::cerr << fullMessage << std::endl;
106 }
107 else
108 {
109 std::cout << fullMessage << std::endl;
110 }
111 }
112
113 // Output to file
114 if (inst.logFile_.is_open())
115 {
116 inst.logFile_ << fullMessage << std::endl;
117 inst.logFile_.flush(); // Ensure immediate write
118 }
119}
120
121std::string Logger::getLevelString(LogLevel level)
122{
123 switch (level)
124 {
125 case LogLevel::DEBUG:
126 return "DEBUG";
127 case LogLevel::INFO:
128 return "INFO ";
129 case LogLevel::WARN:
130 return "WARN ";
131 case LogLevel::ERROR:
132 return "ERROR";
133 case LogLevel::FATAL:
134 return "FATAL";
135 default:
136 return "?????";
137 }
138}
139
140std::string Logger::getCurrentTimestamp()
141{
142 auto now = std::chrono::system_clock::now();
143 auto time = std::chrono::system_clock::to_time_t(now);
144 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
145
146 std::ostringstream oss;
147 std::tm tm{};
148 localtime_r(&time, &tm);
149 oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
150 oss << "." << std::setfill('0') << std::setw(3) << ms.count();
151
152 return oss.str();
153}
static void setLogLevel(LogLevel level)
Set minimum log level.
Definition logger.cpp:41
static void setConsoleOutput(bool enabled)
Enable or disable console output.
Definition logger.cpp:48
static void shutdown()
Shutdown logger and close log file.
Definition logger.cpp:62
static void setTimestamps(bool enabled)
Enable or disable timestamps in log messages.
Definition logger.cpp:55
static void init(const std::string &logFilePath="", LogLevel minLevel=LogLevel::INFO)
Initialize logger with optional file output.
Definition logger.cpp:17
LogLevel
Definition logger.h:37
@ FATAL
Fatal errors (application will terminate).
Definition logger.h:42
@ WARN
Warning messages (non-critical issues).
Definition logger.h:40
@ INFO
General informational messages.
Definition logger.h:39
@ ERROR
Error messages (recoverable failures).
Definition logger.h:41
@ DEBUG
Verbose debugging information.
Definition logger.h:38