Skip to content

Commit 1ba21e9

Browse files
authored
merge(codex): add macro for setting minimum log level
Add log level control
2 parents d9c847d + 6f7f5a8 commit 1ba21e9

6 files changed

Lines changed: 82 additions & 0 deletions

File tree

include/logit_cpp/logit/LogMacros.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,17 @@
971971
#define LOGIT_SET_TIME_OFFSET(logger_index, offset_ms) \
972972
logit::Logger::get_instance().set_timestamp_offset(logger_index, offset_ms)
973973

974+
/// \brief Sets minimal log level for a specific logger.
975+
/// \param logger_index Index of logger.
976+
/// \param level Minimum log level.
977+
#define LOGIT_SET_LOG_LEVEL_TO(logger_index, level) \
978+
logit::Logger::get_instance().set_log_level(logger_index, level)
979+
980+
/// \brief Sets minimal log level for all loggers.
981+
/// \param level Minimum log level.
982+
#define LOGIT_SET_LOG_LEVEL(level) \
983+
logit::Logger::get_instance().set_log_level(level)
984+
974985
/// \brief Checks if a logger is in single mode.
975986
/// \param logger_index Index of logger.
976987
/// \return True if in single mode, false otherwise.

include/logit_cpp/logit/Logger.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ namespace logit {
9494
}
9595
}
9696

97+
/// \brief Sets minimal log level for all loggers.
98+
/// \param level Minimum log level.
99+
void set_log_level(LogLevel level) {
100+
if (m_shutdown) return;
101+
std::lock_guard<std::mutex> lock(m_mutex);
102+
for (auto& strategy : m_loggers) {
103+
strategy.logger->set_log_level(level);
104+
}
105+
}
106+
107+
/// \brief Sets minimal log level for a specific logger.
108+
/// \param logger_index Index of logger.
109+
/// \param level Minimum log level.
110+
void set_log_level(int logger_index, LogLevel level) {
111+
if (m_shutdown) return;
112+
std::lock_guard<std::mutex> lock(m_mutex);
113+
if (logger_index >= 0 && logger_index < static_cast<int>(m_loggers.size())) {
114+
m_loggers[logger_index].logger->set_log_level(level);
115+
}
116+
}
117+
97118
/// \brief Checks whether a logger is in single mode.
98119
/// \param logger_index Index of logger.
99120
/// \return True if logger is in single mode, false otherwise.
@@ -118,12 +139,14 @@ namespace logit {
118139
if (record.logger_index >= 0 && record.logger_index < static_cast<int>(m_loggers.size())) {
119140
const auto& strategy = m_loggers[record.logger_index];
120141
if (!strategy.enabled) return;
142+
if (static_cast<int>(record.log_level) < static_cast<int>(strategy.logger->get_log_level())) return;
121143
strategy.logger->log(record, strategy.formatter->format(record));
122144
return;
123145
}
124146
for (const auto& strategy : m_loggers) {
125147
if (strategy.single_mode) continue;
126148
if (!strategy.enabled) continue;
149+
if (static_cast<int>(record.log_level) < static_cast<int>(strategy.logger->get_log_level())) continue;
127150
strategy.logger->log(record, strategy.formatter->format(record));
128151
}
129152
}

include/logit_cpp/logit/loggers/ConsoleLogger.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ namespace logit {
161161
return 0.0;
162162
}
163163

164+
/// \brief Sets the minimal log level for this logger.
165+
void set_log_level(LogLevel level) override {
166+
m_log_level = static_cast<int>(level);
167+
}
168+
169+
/// \brief Gets the minimal log level for this logger.
170+
LogLevel get_log_level() const override {
171+
return static_cast<LogLevel>(m_log_level.load());
172+
}
173+
164174
/// \brief Waits for all asynchronous tasks to complete.
165175
/// If asynchronous logging is enabled, waits for all pending log messages to be written.
166176
void wait() override {
@@ -179,6 +189,7 @@ namespace logit {
179189
mutable std::mutex m_mutex; ///< Mutex to protect console output
180190
Config m_config; ///< Configuration for the console logger.
181191
std::atomic<int64_t> m_last_log_ts = ATOMIC_VAR_INIT(0);
192+
std::atomic<int> m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE));
182193

183194
# if defined(_WIN32)
184195

include/logit_cpp/logit/loggers/FileLogger.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ namespace logit {
3434
std::string get_string_param(const LoggerParam&) const override { return {}; }
3535
int64_t get_int_param(const LoggerParam&) const override { return 0; }
3636
double get_float_param(const LoggerParam&) const override { return 0.0; }
37+
void set_log_level(LogLevel) override {}
38+
LogLevel get_log_level() const override { return LogLevel::LOG_LVL_TRACE; }
3739
void wait() override {}
3840

3941
private:
4042
void warn() const { std::cerr << "FileLogger is not supported under Emscripten" << std::endl; }
43+
std::atomic<int> m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE));
4144
};
4245

4346
#else
@@ -164,6 +167,16 @@ namespace logit {
164167
return 0.0;
165168
}
166169

170+
/// \brief Sets the minimal log level for this logger.
171+
void set_log_level(LogLevel level) override {
172+
m_log_level = static_cast<int>(level);
173+
}
174+
175+
/// \brief Gets the minimal log level for this logger.
176+
LogLevel get_log_level() const override {
177+
return static_cast<LogLevel>(m_log_level.load());
178+
}
179+
167180
/// \brief Waits for all asynchronous tasks to complete.
168181
void wait() override {
169182
if (!m_config.async) return;
@@ -179,6 +192,7 @@ namespace logit {
179192
std::string m_file_name; ///< Name of the currently open log file.
180193
int64_t m_current_date_ts = 0; ///< Timestamp of the current log file's date.
181194
std::atomic<int64_t> m_last_log_ts = ATOMIC_VAR_INIT(0); ///< Timestamp of the last log.
195+
std::atomic<int> m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE));
182196

183197
/// \brief Starts the logging process by initializing the file and directory.
184198
void start_logging() {

include/logit_cpp/logit/loggers/ILogger.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace logit {
2323

24+
2425
/// \interface ILogger
2526
/// \brief Interface for loggers that handle log message output.
2627
class ILogger {
@@ -54,6 +55,14 @@ namespace logit {
5455
/// \return A double representing the requested parameter.
5556
virtual double get_float_param(const LoggerParam& param) const = 0;
5657

58+
/// \brief Sets the minimal log level for this logger.
59+
/// \param level Minimum log level.
60+
virtual void set_log_level(LogLevel level) = 0;
61+
62+
/// \brief Gets the minimal log level for this logger.
63+
/// \return Current minimal log level.
64+
virtual LogLevel get_log_level() const = 0;
65+
5766
/// \brief Waits for all asynchronous logging operations to complete.
5867
///
5968
/// This pure virtual function must be implemented by derived logger classes.

include/logit_cpp/logit/loggers/UniqueFileLogger.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ namespace logit {
4040
std::string get_string_param(const LoggerParam&) const override { return {}; }
4141
int64_t get_int_param(const LoggerParam&) const override { return 0; }
4242
double get_float_param(const LoggerParam&) const override { return 0.0; }
43+
void set_log_level(LogLevel) override {}
44+
LogLevel get_log_level() const override { return LogLevel::LOG_LVL_TRACE; }
4345
void wait() override {}
4446

4547
private:
4648
void warn() const { std::cerr << "UniqueFileLogger is not supported under Emscripten" << std::endl; }
49+
std::atomic<int> m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE));
4750
};
4851

4952
#else
@@ -236,6 +239,16 @@ namespace logit {
236239
return 0.0;
237240
}
238241

242+
/// \brief Sets the minimal log level for this logger.
243+
void set_log_level(LogLevel level) override {
244+
m_log_level = static_cast<int>(level);
245+
}
246+
247+
/// \brief Gets the minimal log level for this logger.
248+
LogLevel get_log_level() const override {
249+
return static_cast<LogLevel>(m_log_level.load());
250+
}
251+
239252
/// \brief Waits for all asynchronous tasks to complete.
240253
void wait() override {
241254
if (!m_config.async) return;
@@ -268,6 +281,7 @@ namespace logit {
268281
std::unordered_map<std::thread::id, ThreadLogInfo> m_thread_log_info; ///< Map to store log information per thread.
269282

270283
std::atomic<int64_t> m_last_log_ts = ATOMIC_VAR_INIT(0); ///< Timestamp of the last log.
284+
std::atomic<int> m_log_level = ATOMIC_VAR_INIT(static_cast<int>(LogLevel::LOG_LVL_TRACE));
271285

272286

273287
/// \brief Starts the logging process by initializing the directory and removing old logs.

0 commit comments

Comments
 (0)