diff options
| author | Vaxry <vaxry@vaxry.net> | 2023-11-14 20:06:04 +0000 |
|---|---|---|
| committer | Vaxry <vaxry@vaxry.net> | 2023-11-14 20:06:04 +0000 |
| commit | e195e51c1bd4b17398ec6964f30751b6a54bbb44 (patch) | |
| tree | 31fc8158f4964943c90df98f02d8459fe37b321e /src/debug/Log.hpp | |
| parent | e8469f8b1bb184a08351ee3cc3fcc4d0638eb90b (diff) | |
logging: move to an internal rolling log buffer
disables logging to the logfile by default
Diffstat (limited to 'src/debug/Log.hpp')
| -rw-r--r-- | src/debug/Log.hpp | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/debug/Log.hpp b/src/debug/Log.hpp index 085465c9..659e2db4 100644 --- a/src/debug/Log.hpp +++ b/src/debug/Log.hpp @@ -7,9 +7,11 @@ #include "../includes.hpp" #include "../helpers/MiscFunctions.hpp" -#define LOGMESSAGESIZE 1024 +#define LOGMESSAGESIZE 1024 +#define ROLLING_LOG_SIZE 4096 -enum LogLevel { +enum LogLevel +{ NONE = -1, LOG = 0, WARN, @@ -26,12 +28,11 @@ namespace Debug { inline bool disableStdout = false; inline bool trace = false; + inline std::string rollingLog = ""; // rolling log contains the ROLLING_LOG_SIZE tail of the log + void init(const std::string& IS); template <typename... Args> void log(LogLevel level, std::format_string<Args...> fmt, Args&&... args) { - if (disableLogs && *disableLogs) - return; - if (level == TRACE && !trace) return; @@ -47,10 +48,6 @@ namespace Debug { default: break; } - // log to a file - std::ofstream ofs; - ofs.open(logFile, std::ios::out | std::ios::app); - // print date and time to the ofs if (disableTime && !*disableTime) { #ifndef _LIBCPP_VERSION @@ -69,9 +66,18 @@ namespace Debug { // 3. this is actually what std::format in stdlib does logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); - ofs << logMsg << "\n"; + 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(); + ofs.close(); + } // log it to the stdout too. if (!disableStdout) |
