yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1#ifndef YAZE_UTIL_LOG_H
2#define YAZE_UTIL_LOG_H
3
4#include <atomic>
5#include <fstream>
6#include <set>
7#include <string>
8#include <utility>
9
10#include "absl/strings/str_cat.h"
11#include "absl/strings/str_format.h"
12#include "absl/strings/string_view.h"
13#include "core/features.h"
14
15namespace yaze {
16namespace util {
17
24
34 public:
35 // Singleton access
36 static LogManager& instance();
37
38 // Deleted constructors for singleton pattern
39 LogManager(const LogManager&) = delete;
40 void operator=(const LogManager&) = delete;
41
49 void configure(LogLevel level, const std::string& file_path,
50 const std::set<std::string>& categories);
51
58 void log(LogLevel level, absl::string_view category,
59 absl::string_view message);
60
64 void SetLogLevel(LogLevel level) { min_level_.store(level); }
65 LogLevel GetLogLevel() const { return min_level_.load(); }
66
72 bool IsDebugEnabled() const {
73 return min_level_.load() == LogLevel::YAZE_DEBUG;
74 }
75
76 private:
77 LogManager();
79
80 // Configuration state
81 std::atomic<LogLevel> min_level_;
82 std::set<std::string> enabled_categories_;
83 std::set<std::string> disabled_categories_;
84 std::atomic<bool> all_categories_enabled_;
85
86 // Output sink
87 std::ofstream log_stream_;
88 std::string log_file_path_;
89};
90
91// --- Public Logging Macros ---
92// These macros provide a convenient and efficient API for logging.
93// The `do-while(0)` loop ensures they behave like a single statement.
94// The level check avoids the cost of string formatting if the message won't be
95// logged.
96
97#define LOG(level, category, format, ...) \
98 do { \
99 yaze::util::LogManager::instance().log( \
100 level, category, absl::StrFormat(format, ##__VA_ARGS__)); \
101 } while (0)
102
103#define LOG_DEBUG(category, format, ...) \
104 LOG(yaze::util::LogLevel::YAZE_DEBUG, category, format, ##__VA_ARGS__)
105#define LOG_INFO(category, format, ...) \
106 LOG(yaze::util::LogLevel::INFO, category, format, ##__VA_ARGS__)
107#define LOG_WARN(category, format, ...) \
108 LOG(yaze::util::LogLevel::WARNING, category, format, ##__VA_ARGS__)
109#define LOG_ERROR(category, format, ...) \
110 LOG(yaze::util::LogLevel::ERROR, category, format, ##__VA_ARGS__)
111#define LOG_FATAL(category, format, ...) \
112 LOG(yaze::util::LogLevel::FATAL, category, format, ##__VA_ARGS__)
113
114template <typename... Args>
115inline void logf(const absl::FormatSpec<Args...>& format, Args&&... args) {
117 LogLevel::INFO, "General",
118 absl::StrFormat(format, std::forward<Args>(args)...));
119}
120
121inline void logf(absl::string_view message) {
122 LogManager::instance().log(LogLevel::INFO, "General", message);
123}
124
125} // namespace util
126} // namespace yaze
127
128#endif // YAZE_UTIL_LOG_H
A singleton that manages all logging configuration and output.
Definition log.h:33
std::ofstream log_stream_
Definition log.h:87
static LogManager & instance()
Definition log.cc:31
void configure(LogLevel level, const std::string &file_path, const std::set< std::string > &categories)
Configures the logging system.
Definition log.cc:45
std::string log_file_path_
Definition log.h:88
void DisableDebugLogging()
Definition log.h:71
void operator=(const LogManager &)=delete
void EnableDebugLogging()
Toggle debug logging on/off at runtime.
Definition log.h:70
LogLevel GetLogLevel() const
Definition log.h:65
std::atomic< bool > all_categories_enabled_
Definition log.h:84
std::atomic< LogLevel > min_level_
Definition log.h:81
std::set< std::string > disabled_categories_
Definition log.h:83
void SetLogLevel(LogLevel level)
Runtime log level control (for debug card)
Definition log.h:64
std::set< std::string > enabled_categories_
Definition log.h:82
void log(LogLevel level, absl::string_view category, absl::string_view message)
The primary logging function.
Definition log.cc:97
LogManager(const LogManager &)=delete
bool IsDebugEnabled() const
Definition log.h:72
LogLevel
Defines the severity levels for log messages. This allows for filtering messages based on their impor...
Definition log.h:23
void logf(const absl::FormatSpec< Args... > &format, Args &&... args)
Definition log.h:115