test_123/include/logger.hpp

75 lines
2.0 KiB
C++
Raw Normal View History

2026-05-16 04:54:48 +00:00
#ifndef MODULAR_LOGGER_HPP
#define MODULAR_LOGGER_HPP
#include <string>
#include <mutex>
#include <iostream>
#include <sstream>
#include <ctime>
#include <iomanip>
/// @brief Log severity levels.
enum class LogLevel {
DEBUG, ///< Detailed debug information
INFO, ///< General informational messages
WARN, ///< Warning messages
ERROR ///< Error messages
};
/**
* @brief Thread-safe logger with hierarchical level filtering.
*
* Provides logging at DEBUG, INFO, WARN, and ERROR levels.
* Each log entry is prefixed with a timestamp and level tag.
*/
class Logger {
public:
/**
* @brief Construct a Logger with an optional minimum level.
* @param level Minimum level to log (default: INFO).
*/
explicit Logger(LogLevel level = LogLevel::INFO);
/**
* @brief Set the minimum log level.
* @param level New minimum level.
*/
void setLevel(LogLevel level);
/**
* @brief Get the current minimum log level.
* @return Current LogLevel.
*/
LogLevel level() const;
/**
* @brief Log a message at the given level.
* @param level Severity level.
* @param msg Message text.
*
* If @p level is below the configured minimum, the message is discarded.
*/
void log(LogLevel level, const std::string& msg);
/// @brief Convenience: log at DEBUG level.
void debug(const std::string& msg);
/// @brief Convenience: log at INFO level.
void info(const std::string& msg);
/// @brief Convenience: log at WARN level.
void warn(const std::string& msg);
/// @brief Convenience: log at ERROR level.
void error(const std::string& msg);
private:
LogLevel level_;
std::mutex mutex_;
/// @brief Convert a LogLevel to its string representation.
static std::string levelToString(LogLevel level);
/// @brief Get current local time as a formatted string "[YYYY-MM-DD HH:MM:SS]".
static std::string currentTimestamp();
};
#endif // MODULAR_LOGGER_HPP