From 012a2802e00a7b34e47c8c1098dc093031044ce9 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 22 Apr 2024 18:44:25 +0100 Subject: Protocols: implement protoLog --- src/debug/Log.cpp | 35 +++++++++++++++++++++++++++++++++++ src/debug/Log.hpp | 32 +++++--------------------------- 2 files changed, 40 insertions(+), 27 deletions(-) (limited to 'src/debug') diff --git a/src/debug/Log.cpp b/src/debug/Log.cpp index 66c430d5..32d235e1 100644 --- a/src/debug/Log.cpp +++ b/src/debug/Log.cpp @@ -32,3 +32,38 @@ void Debug::wlrLog(wlr_log_importance level, const char* fmt, va_list args) { if (!disableStdout) std::cout << output << "\n"; } + +void Debug::log(LogLevel level, std::string str) { + if (level == TRACE && !trace) + return; + + if (shuttingDown) + return; + + switch (level) { + case LOG: str = "[LOG] " + str; break; + case WARN: str = "[WARN] " + str; break; + case ERR: str = "[ERR] " + str; break; + case CRIT: str = "[CRITICAL] " + str; break; + case INFO: str = "[INFO] " + str; break; + case TRACE: str = "[TRACE] " + str; break; + default: break; + } + + rollingLog += str + "\n"; + if (rollingLog.size() > ROLLING_LOG_SIZE) + rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE); + + if (!disableLogs || !**disableLogs) { + // log to a file + std::ofstream ofs; + ofs.open(logFile, std::ios::out | std::ios::app); + ofs << str << "\n"; + + ofs.close(); + } + + // log it to the stdout too. + if (!disableStdout) + std::cout << str << "\n"; +} \ No newline at end of file diff --git a/src/debug/Log.hpp b/src/debug/Log.hpp index a43d6fee..f1dd3eab 100644 --- a/src/debug/Log.hpp +++ b/src/debug/Log.hpp @@ -31,6 +31,10 @@ namespace Debug { inline std::string rollingLog = ""; // rolling log contains the ROLLING_LOG_SIZE tail of the log void init(const std::string& IS); + + // + void log(LogLevel level, std::string str); + template void log(LogLevel level, std::format_string fmt, Args&&... args) { if (level == TRACE && !trace) @@ -41,16 +45,6 @@ namespace Debug { std::string logMsg = ""; - switch (level) { - case LOG: logMsg += "[LOG] "; break; - case WARN: logMsg += "[WARN] "; break; - case ERR: logMsg += "[ERR] "; break; - case CRIT: logMsg += "[CRITICAL] "; break; - case INFO: logMsg += "[INFO] "; break; - case TRACE: logMsg += "[TRACE] "; break; - default: break; - } - // print date and time to the ofs if (disableTime && !**disableTime) { #ifndef _LIBCPP_VERSION @@ -58,7 +52,6 @@ namespace Debug { #else auto c = std::chrono::hh_mm_ss{std::chrono::system_clock::now() - std::chrono::floor(std::chrono::system_clock::now())}; logMsg += std::format("{:%H}:{:%M}:{:%S}", c.hours(), c.minutes(), c.subseconds()); - #endif } @@ -69,22 +62,7 @@ namespace Debug { // 3. this is actually what std::format in stdlib does logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); - rollingLog += logMsg + "\n"; - if (rollingLog.size() > ROLLING_LOG_SIZE) - rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE); - - if (!disableLogs || !**disableLogs) { - // log to a file - std::ofstream ofs; - ofs.open(logFile, std::ios::out | std::ios::app); - ofs << logMsg << "\n"; - - ofs.close(); - } - - // log it to the stdout too. - if (!disableStdout) - std::cout << logMsg << "\n"; + log(level, logMsg); } void wlrLog(wlr_log_importance level, const char* fmt, va_list args); -- cgit v1.2.3