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 <chrono>
6#include <fstream>
7#include <iostream>
8#include <memory>
9#include <set>
10#include <string>
11#include <vector>
12#include <utility>
13
14#include "absl/strings/str_format.h"
15#include "absl/strings/str_cat.h"
16#include "app/core/features.h"
17#include "absl/strings/string_view.h"
18
19namespace yaze {
20namespace util {
21
28
38 public:
39 // Singleton access
40 static LogManager& instance();
41
42 // Deleted constructors for singleton pattern
43 LogManager(const LogManager&) = delete;
44 void operator=(const LogManager&) = delete;
45
53 void configure(LogLevel level, const std::string& file_path,
54 const std::set<std::string>& categories);
55
62 void log(LogLevel level, absl::string_view category,
63 absl::string_view message);
64
68 void SetLogLevel(LogLevel level) { min_level_.store(level); }
69 LogLevel GetLogLevel() const { return min_level_.load(); }
70
76 bool IsDebugEnabled() const { return min_level_.load() == LogLevel::YAZE_DEBUG; }
77
78 private:
79 LogManager();
81
82 // Configuration state
83 std::atomic<LogLevel> min_level_;
84 std::set<std::string> enabled_categories_;
85 std::atomic<bool> all_categories_enabled_;
86
87 // Output sink
88 std::ofstream log_stream_;
89 std::string log_file_path_;
90};
91
92// --- Public Logging Macros ---
93// These macros provide a convenient and efficient API for logging.
94// The `do-while(0)` loop ensures they behave like a single statement.
95// The level check avoids the cost of string formatting if the message won't be
96// logged.
97
98#define LOG(level, category, format, ...) \
99 do { \
100 yaze::util::LogManager::instance().log( \
101 level, category, absl::StrFormat(format, ##__VA_ARGS__)); \
102 } while (0)
103
104#define LOG_DEBUG(category, format, ...) \
105 LOG(yaze::util::LogLevel::YAZE_DEBUG, category, format, ##__VA_ARGS__)
106#define LOG_INFO(category, format, ...) \
107 LOG(yaze::util::LogLevel::INFO, category, format, ##__VA_ARGS__)
108#define LOG_WARN(category, format, ...) \
109 LOG(yaze::util::LogLevel::WARNING, category, format, ##__VA_ARGS__)
110#define LOG_ERROR(category, format, ...) \
111 LOG(yaze::util::LogLevel::ERROR, category, format, ##__VA_ARGS__)
112#define LOG_FATAL(category, format, ...) \
113 LOG(yaze::util::LogLevel::FATAL, category, format, ##__VA_ARGS__)
114
115template <typename... Args>
116inline void logf(const absl::FormatSpec<Args...>& format, Args&&... args) {
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:37
std::ofstream log_stream_
Definition log.h:88
static LogManager & instance()
Definition log.cc:29
void configure(LogLevel level, const std::string &file_path, const std::set< std::string > &categories)
Configures the logging system.
Definition log.cc:43
std::string log_file_path_
Definition log.h:89
void DisableDebugLogging()
Definition log.h:75
void operator=(const LogManager &)=delete
void EnableDebugLogging()
Toggle debug logging on/off at runtime.
Definition log.h:74
LogLevel GetLogLevel() const
Definition log.h:69
std::atomic< bool > all_categories_enabled_
Definition log.h:85
std::atomic< LogLevel > min_level_
Definition log.h:83
void SetLogLevel(LogLevel level)
Runtime log level control (for debug card)
Definition log.h:68
std::set< std::string > enabled_categories_
Definition log.h:84
void log(LogLevel level, absl::string_view category, absl::string_view message)
The primary logging function.
Definition log.cc:74
LogManager(const LogManager &)=delete
bool IsDebugEnabled() const
Definition log.h:76
LogLevel
Defines the severity levels for log messages. This allows for filtering messages based on their impor...
Definition log.h:27
void logf(const absl::FormatSpec< Args... > &format, Args &&... args)
Definition log.h:116
Main namespace for the application.