Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
LogCategory.cpp
Go to the documentation of this file.
1
7
9#include <algorithm>
10#include <cctype>
11#include <fstream>
12#include <nlohmann/json.hpp>
13#include <plog/Log.h>
14
15namespace EngineCore
16{
17
18// ============================================================================
19// LogVerbosity helpers
20// ============================================================================
21
22LogVerbosity verbosityFromString(const std::string& str)
23{
24 // Convert to lowercase for case-insensitive comparison
25 std::string lower = str;
26 std::transform(lower.begin(), lower.end(), lower.begin(),
27 [](unsigned char c) { return std::tolower(c); });
28
29 if (lower == "fatal" || lower == "off") return LogVerbosity::Fatal;
30 if (lower == "error") return LogVerbosity::Error;
31 if (lower == "warning" || lower == "warn") return LogVerbosity::Warning;
32 if (lower == "info") return LogVerbosity::Info;
33 if (lower == "debug") return LogVerbosity::Debug;
34 if (lower == "verbose" || lower == "all") return LogVerbosity::Verbose;
35
36 // Default to Info if not recognized
37 return LogVerbosity::Info;
38}
39
40// ============================================================================
41// LogCategory
42// ============================================================================
43
44LogCategory::LogCategory(const char* name, LogVerbosity defaultVerbosity, LogVerbosity compileTimeVerbosity)
45 : name_(name)
46 , runtimeVerbosity_(defaultVerbosity)
47 , compileTimeVerbosity_(compileTimeVerbosity)
48{
49 // Auto-register with the manager
51}
52
53// ============================================================================
54// LogCategoryManager
55// ============================================================================
56
58{
59 static LogCategoryManager instance;
60 return instance;
61}
62
64{
65 std::lock_guard<std::mutex> lock(mutex_);
66 categories_[category->getName()] = category;
67}
68
70{
71 std::lock_guard<std::mutex> lock(mutex_);
72 auto it = categories_.find(name);
73 return (it != categories_.end()) ? it->second : nullptr;
74}
75
76bool LogCategoryManager::setVerbosity(const std::string& categoryName, LogVerbosity verbosity)
77{
78 std::lock_guard<std::mutex> lock(mutex_);
79 auto it = categories_.find(categoryName);
80 if (it != categories_.end()) {
81 it->second->setVerbosity(verbosity);
82 return true;
83 }
84 return false;
85}
86
88{
89 std::lock_guard<std::mutex> lock(mutex_);
90 defaultVerbosity_ = verbosity;
91 for (auto& [name, category] : categories_) {
92 category->setVerbosity(verbosity);
93 }
94}
95
96std::vector<LogCategory*> LogCategoryManager::getAllCategories() const
97{
98 std::lock_guard<std::mutex> lock(mutex_);
99 std::vector<LogCategory*> result;
100 result.reserve(categories_.size());
101 for (const auto& [name, category] : categories_) {
102 result.push_back(category);
103 }
104 return result;
105}
106
107bool LogCategoryManager::loadConfig(const std::string& configPath)
108{
109 try {
110 std::ifstream file(configPath);
111 if (!file.is_open()) {
112 PLOGW << "[LogCategoryManager] Could not open config file: " << configPath;
113 return false;
114 }
115
116 nlohmann::json config;
117 file >> config;
118
119 if (!config.contains("LogCategories")) {
120 PLOGW << "[LogCategoryManager] Config file missing 'LogCategories' section";
121 return false;
122 }
123
124 const auto& categories = config["LogCategories"];
125
126 // Check for default verbosity
127 if (categories.contains("Default")) {
128 std::string defaultStr = categories["Default"].get<std::string>();
130 PLOGI << "[LogCategoryManager] Default verbosity set to: " << defaultStr;
131 }
132
133 // Apply per-category settings
134 std::lock_guard<std::mutex> lock(mutex_);
135 for (auto& [name, category] : categories_) {
136 if (categories.contains(name)) {
137 std::string verbStr = categories[name].get<std::string>();
138 LogVerbosity verbosity = verbosityFromString(verbStr);
139 category->setVerbosity(verbosity);
140 PLOGD << "[LogCategoryManager] " << name << " verbosity set to: " << verbStr;
141 } else {
142 // Apply default to categories not explicitly listed
143 category->setVerbosity(defaultVerbosity_);
144 }
145 }
146
147 PLOGI << "[LogCategoryManager] Loaded config from: " << configPath;
148 return true;
149 }
150 catch (const std::exception& e) {
151 PLOGE << "[LogCategoryManager] Failed to load config: " << e.what();
152 return false;
153 }
154}
155
156bool LogCategoryManager::saveConfig(const std::string& configPath) const
157{
158 try {
159 nlohmann::json config;
160 nlohmann::json& categories = config["LogCategories"];
161
162 categories["Default"] = verbosityToString(defaultVerbosity_);
163
164 {
165 std::lock_guard<std::mutex> lock(mutex_);
166 for (const auto& [name, category] : categories_) {
167 categories[name] = verbosityToString(category->getVerbosity());
168 }
169 }
170
171 std::ofstream file(configPath);
172 if (!file.is_open()) {
173 PLOGE << "[LogCategoryManager] Could not open config file for writing: " << configPath;
174 return false;
175 }
176
177 file << config.dump(2); // Pretty print with 2-space indent
178 PLOGI << "[LogCategoryManager] Saved config to: " << configPath;
179 return true;
180 }
181 catch (const std::exception& e) {
182 PLOGE << "[LogCategoryManager] Failed to save config: " << e.what();
183 return false;
184 }
185}
186
187} // namespace EngineCore
void setAllVerbosity(LogVerbosity verbosity)
Set verbosity for all categories.
std::unordered_map< std::string, LogCategory * > categories_
LogCategory * findCategory(const std::string &name)
Find a category by name.
std::vector< LogCategory * > getAllCategories() const
Get all registered categories (for debug UI, listing, etc.)
bool loadConfig(const std::string &configPath)
Load verbosity settings from a JSON config file.
bool setVerbosity(const std::string &categoryName, LogVerbosity verbosity)
bool saveConfig(const std::string &configPath) const
Save current verbosity settings to a JSON config file.
static LogCategoryManager & get()
Get the singleton instance.
void registerCategory(LogCategory *category)
Register a category (called automatically by DEFINE_LOG_CATEGORY)
Represents a single log category with verbosity control.
Definition LogCategory.h:89
std::atomic< LogVerbosity > runtimeVerbosity_
const char * getName() const
Get the category name.
LogCategory(const char *name, LogVerbosity defaultVerbosity, LogVerbosity compileTimeVerbosity)
Construct a log category.
const LogVerbosity compileTimeVerbosity_
Log category system implementation.
LogVerbosity verbosityFromString(const std::string &str)
Parse verbosity from string (case-insensitive)
LogVerbosity
Log verbosity levels, ordered from most to least severe.
Definition LogCategory.h:28
@ Warning
Potential issue.
Definition LogCategory.h:31
@ Info
Important state changes.
Definition LogCategory.h:32
@ Fatal
Unrecoverable error, may crash.
Definition LogCategory.h:29
@ Error
Recoverable error.
Definition LogCategory.h:30
@ Debug
Development debugging.
Definition LogCategory.h:33
@ Verbose
Detailed tracing.
Definition LogCategory.h:34
constexpr const char * verbosityToString(LogVerbosity v)
Convert verbosity enum to string for display/config.
Definition LogCategory.h:44