ai-dev-template / cpp /include /utils /logger.hpp
Jordandevlog's picture
Upload cpp/include/utils/logger.hpp
89e64cd verified
Raw
History Blame Contribute Delete
1.37 kB
/**
* @file logger.hpp
* @brief Structured logging using modern C++23 std::format.
*/
#pragma once
#include <string>
#include <format>
#include <iostream>
#include <chrono>
#include <source_location>
namespace app::utils {
enum class LogLevel {
Debug,
Info,
Warn,
Error
};
class Logger {
public:
explicit Logger(std::string name) : name_(std::move(name)) {}
template<typename... Args>
void info(std::format_string<Args...> fmt, Args&&... args,
std::source_location loc = std::source_location::current()) {
log(LogLevel::Info, std::format(fmt, std::forward<Args>(args)...), loc);
}
template<typename... Args>
void error(std::format_string<Args...> fmt, Args&&... args,
std::source_location loc = std::source_location::current()) {
log(LogLevel::Error, std::format(fmt, std::forward<Args>(args)...), loc);
}
template<typename... Args>
void debug(std::format_string<Args...> fmt, Args&&... args,
std::source_location loc = std::source_location::current()) {
log(LogLevel::Debug, std::format(fmt, std::forward<Args>(args)...), loc);
}
private:
void log(LogLevel level, const std::string& message, const std::source_location& loc);
std::string name_;
};
Logger& get_logger(const std::string& name);
} // namespace app::utils