SVCS - English Documentation
Loading...
Searching...
No Matches
Logger.hxx
Go to the documentation of this file.
1
17#pragma once
18
19#include "../IObserver.hxx"
20#include "../Event.hxx"
21
22#include <string>
23#include <memory>
24#include <unordered_map>
25#include <mutex>
26
38 * @details Содержит основные инфраструктурные сервисы, включая систему событий,
39 * механизм уведомлений, реализацию шаблона Observer и другие сквозные задачи,
40 * которые поддерживают операции СКВ.
41 */
42namespace svcs::services {
55enum class LogLevel {
63 DEBUG = 0,
64
69
72 INFO = 1,
73
77 *
78 * @russian
79 * @brief Потенциально проблемные ситуации.
80 */
81 WARN = 2,
82
90 ERROR = 3,
91
99 FATAL = 4
100};
101
115class Logger : public IObserver {
116private:
124 std::string name_;
125
133 LogLevel log_level_;
134
142 std::string pattern_;
143
151 static std::unordered_map<std::string, std::shared_ptr<Logger>> instances_;
152
160 static std::mutex instances_mutex_;
161
162public:
174 explicit Logger(const std::string& name);
175
183 ~Logger() override;
184
185 // -------------------------------------------------------------------------
186 // IObserver Methods
187 // -------------------------------------------------------------------------
188
200 void update(const Event& event) override;
201
215 void notify(const Event& event) override;
216
217 // -------------------------------------------------------------------------
218 // Static Singleton Access Methods
219 // -------------------------------------------------------------------------
220
234 static std::shared_ptr<Logger> getInstance(const std::string& name);
235
245 static void clearInstances();
246
247 // -------------------------------------------------------------------------
248 // Direct Logging Methods
249 // -------------------------------------------------------------------------
250
260 void debug(const std::string& message);
261
271 void info(const std::string& message);
272
282 void warn(const std::string& message);
283
293 void error(const std::string& message);
294
304 void fatal(const std::string& message);
305
306 // -------------------------------------------------------------------------
307 // Configuration & Utility
308 // -------------------------------------------------------------------------
309
319 void setLevel(LogLevel level);
320
330 [[nodiscard]] LogLevel getLevel() const;
331
341 void setPattern(const std::string& pattern);
342
350 void flush();
351
352private:
364 void log(LogLevel level, const std::string& message);
365
379 std::string formatMessage(LogLevel level, const std::string& message);
380
392 std::string levelToString(LogLevel level);
393};
394
395}
Defines the event structure used for the Observer pattern notifications.
Defines the interface for the Observer pattern component.
The Observer Interface (Abstract Base Class).
Definition IObserver.hxx:34
A thread-safe Singleton class responsible for logging system events.
Definition Logger.hxx:77
Logger(const std::string &name)
Constructor.
Definition Logger.cxx:27
void flush()
Forces all buffered log output to be written immediately (e.g., to disk).
Definition Logger.cxx:212
static void clearInstances()
Clears all registered Singleton instances.
Definition Logger.cxx:206
void warn(const std::string &message)
Logs a message at the WARN level.
Definition Logger.cxx:105
void fatal(const std::string &message)
Logs a message at the FATAL level.
Definition Logger.cxx:117
void update(const Event &event) override
Method called by the Subject to notify the observer with an event.
Definition Logger.cxx:39
~Logger() override
Destructor.
Definition Logger.cxx:33
void notify(const Event &event) override
Method called by the subject to notify the observer.
Definition Logger.cxx:74
void debug(const std::string &message)
Logs a message at the DEBUG level.
Definition Logger.cxx:93
void setPattern(const std::string &pattern)
Sets the output formatting pattern for messages.
Definition Logger.cxx:201
LogLevel getLevel() const
Retrieves the current minimum log level.
Definition Logger.cxx:197
static std::shared_ptr< Logger > getInstance(const std::string &name)
Retrieves the Singleton instance of the Logger for the given name.
Definition Logger.cxx:79
void error(const std::string &message)
Logs a message at the ERROR level.
Definition Logger.cxx:111
void setLevel(LogLevel level)
Sets the minimum log level for this instance.
Definition Logger.cxx:193
void info(const std::string &message)
Logs a message at the INFO level.
Definition Logger.cxx:99
Service layer components and infrastructure services.
LogLevel
Defines the severity levels for log messages.
Definition Logger.hxx:38
@ FATAL
Very severe error events that will likely cause the application to abort.
Definition Logger.hxx:67
@ WARN
Potentially harmful situations.
Definition Logger.hxx:55
@ INFO
General flow information.
Definition Logger.hxx:49
@ ERROR
Error events that might still allow the application to continue.
Definition Logger.hxx:61
@ DEBUG
Detailed information for debugging.
Definition Logger.hxx:43
Structure describing an event published by the VCS core.
Definition Event.hxx:30