From 91251d7771fff13f86e27c7ccb783b84ae71048f Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Tue, 9 Apr 2024 03:45:01 +0000 Subject: [PATCH] feat support log write --- src/sled/log/log.cc | 11 +++++++++++ src/sled/log/log.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/sled/log/log.cc b/src/sled/log/log.cc index 60311eb..08c592b 100644 --- a/src/sled/log/log.cc +++ b/src/sled/log/log.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace sled { @@ -97,6 +98,8 @@ GetCurrentUTCTime() } static LogLevel g_log_level = LogLevel::kTrace; +static std::string g_log_file_name; +static std::ofstream g_log_stream; void SetLogLevel(LogLevel level) @@ -104,6 +107,13 @@ SetLogLevel(LogLevel level) g_log_level = level; } +void +SetLogFileName(const char *file_name) +{ + g_log_file_name = file_name; + g_log_stream.open(file_name); +} + static std::atomic g_current_id(0); static std::atomic g_request_id(0); @@ -138,6 +148,7 @@ Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int Waiter waiter(g_request_id.fetch_add(1), g_current_id); waiter.wait(); + if (g_log_stream.is_open()) { g_log_stream << msg << std::endl; } std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix() << std::endl; } diff --git a/src/sled/log/log.h b/src/sled/log/log.h index 2bbcedd..267c648 100644 --- a/src/sled/log/log.h +++ b/src/sled/log/log.h @@ -58,6 +58,7 @@ enum class LogLevel { kFatal = 5, }; void SetLogLevel(LogLevel level); +void SetLogFileName(const char *file_name); void Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int line, const char *func_name, ...);