36 constexpr const char *
RESET =
"\033[0m";
40 constexpr const char *
LOG_RED =
"\033[31m";
42 constexpr const char *
CYAN =
"\033[36m";
60 std::string path(filePath);
61 size_t pos = path.find_last_of(
"/\\");
62 return (pos == std::string::npos) ? path : path.substr(pos + 1);
69 auto now = std::chrono::system_clock::now();
70 auto nowTime = std::chrono::system_clock::to_time_t(now);
71 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
75 localtime_s(&timeInfo, &nowTime);
77 timeInfo = *std::localtime(&nowTime);
80 std::ostringstream oss;
81 oss << std::put_time(&timeInfo,
"%H:%M:%S");
82 oss <<
'.' << std::setfill(
'0') << std::setw(3) << ms.count();
143 static void log(
Level level,
const char *file,
int line,
const std::string &message) {
147 std::lock_guard<std::mutex> lock(
_mutex);
154 std::ostringstream oss;
155 oss << timeColor <<
"[" <<
getTimestamp() <<
"]" << resetColor <<
" ";
156 oss << levelColor <<
"[" <<
getLevelString(level) <<
"]" << resetColor <<
" ";
157 oss << fileColor <<
"[" <<
getBasename(file) <<
":" << line <<
"]" << resetColor <<
" ";
160 std::cout << oss.str() << std::endl;
166 template <
typename... Args>
167 static void logf(
Level level,
const char *file,
int line, Args &&...args) {
171 std::ostringstream oss;
172 (oss << ... << std::forward<Args>(args));
173 log(level, file, line, oss.str());
180#define LOG_DEBUG(...) logger::Logger::logf(logger::Level::DEBUG, __FILE__, __LINE__, __VA_ARGS__)
181#define LOG_INFO(...) logger::Logger::logf(logger::Level::INFO, __FILE__, __LINE__, __VA_ARGS__)
182#define LOG_WARNING(...) logger::Logger::logf(logger::Level::WARNING, __FILE__, __LINE__, __VA_ARGS__)
183#define LOG_ERROR(...) logger::Logger::logf(logger::Level::ERROR, __FILE__, __LINE__, __VA_ARGS__)
184#define LOG_CRITICAL(...) logger::Logger::logf(logger::Level::CRITICAL, __FILE__, __LINE__, __VA_ARGS__)
187#define LOG_D(...) LOG_DEBUG(__VA_ARGS__)
188#define LOG_I(...) LOG_INFO(__VA_ARGS__)
189#define LOG_W(...) LOG_WARNING(__VA_ARGS__)
190#define LOG_E(...) LOG_ERROR(__VA_ARGS__)
Thread-safe logging system with timestamps and source location.
static std::string getTimestamp()
Get current timestamp with milliseconds.
static void logf(Level level, const char *file, int line, Args &&...args)
Log a message with formatted arguments.
static void setColors(bool enable)
Enable or disable colored output.
static const char * getLevelString(Level level)
Get string representation of log level.
static void log(Level level, const char *file, int line, const std::string &message)
Log a message with source location.
static std::string getBasename(const char *filePath)
Extract basename from file path.
static void setLevel(Level level)
Set minimum log level (messages below this level are ignored)
static const char * getLevelColor(Level level)
Get color for log level.
static bool _enableColors
constexpr const char * CYAN
constexpr const char * LOG_GRAY
constexpr const char * LOG_RED
constexpr const char * BOLD_RED
constexpr const char * LOG_GREEN
constexpr const char * RESET
constexpr const char * BOLD_WHITE
constexpr const char * LOG_YELLOW
Level
Log severity levels.