From abfb9ad048177e6b84360a18ae13fb7f365a6b3e Mon Sep 17 00:00:00 2001 From: Simon Symeonidis Date: Sat, 30 Dec 2023 14:35:48 -0500 Subject: [PATCH] Implement poorman's log rotating This implements a very simple circular buffer that will take the last N lines in a log file, and keep those instead. Right now the maximum N is hardcoded, but the code is written in a fashion that we can revisit some of this, and add toggles where we see fit (for example in a settings dialog or so). I tested out the functionality this way: $ yes some-log-line | nl | head -5000 > log.txt And then running PTE2 with a max log size of 1000, I noticed the logfile like so: $ head -10 log.txt 4001 some-log-line 4002 some-log-line 4003 some-log-line 4004 some-log-line 4005 some-log-line 4006 some-log-line 4007 some-log-line 4008 some-log-line 4009 some-log-line 4010 some-log-line and the bottom: $ tail -10 log.txt 2023-12-30T14:38:32Z: [debug]: finding translations for locale: 2023-12-30T14:38:32Z: [debug]: locale: en-US 2023-12-30T14:38:32Z: [debug]: locale: en 2023-12-30T14:38:32Z: [debug]: locale: en-Latn-US 2023-12-30T14:38:32Z: [debug]: - checking: /home/psyomn/.local/share/powertab/powertabeditor/translations 2023-12-30T14:38:32Z: [debug]: - checking: /usr/local/share/powertab/powertabeditor/translations 2023-12-30T14:38:32Z: [debug]: - checking: /usr/share/powertab/powertabeditor/translations 2023-12-30T14:38:32Z: [debug]: - checking: /home/psyomn/programming/cc/fork/powertabeditor/build/bin/data/translations 2023-12-30T14:38:32Z: [debug]: - checking: /usr/share/qt/translations 2023-12-30T14:38:32Z: [debug]: loaded qt base translations from /usr/share/qt/translations/qtbase_en.qm and last checks: $ nl log.txt | head -1 1 4001 some-log-line $ nl log.txt | tail -1 1011 2023-12-30T14:38:32Z: [debug]: loaded qt base translations from /usr/share/qt/translations/qtbase_en.qm --- source/util/log.cpp | 37 +++++++++++++++++++++++++++++++++++++ source/util/log.h | 8 ++------ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/source/util/log.cpp b/source/util/log.cpp index 9db0dc35..72a89fdc 100644 --- a/source/util/log.cpp +++ b/source/util/log.cpp @@ -25,10 +25,47 @@ std::filesystem::path logPath; std::mutex lock; +void trim(const unsigned int count) +{ + /* a poorman's circular buffer that keeps the last 'count' log lines. */ + std::vector v(count); + + std::ifstream input(logPath); + std::size_t index = 0; + std::string s; + while (std::getline(input, s)) { + v[index % count] = s; + index++; + } + + input.close(); + + // required because of len/cap discrepancy in vector used as a circular + // buffer. + const auto upto = count < index ? index : count; + + std::ofstream output(logPath, std::ios_base::out); + if (output.good()) { + for (size_t i = 0; i < upto; ++i) + output << v[(index + i) % count] << std::endl; + } else { + std::cerr << "could not open log file for trimming" << std::endl; + } + output.close(); +} + void init(enum Level lvl, std::filesystem::path lp) { FilterLevel = lvl; logPath = lp; + + // keep only the last X lines + trim(1000); + + if (!logFile) { + const auto mode = std::ios_base::out | std::ios_base::app; + logFile = std::ofstream(logPath, mode); + } } std::string all() diff --git a/source/util/log.h b/source/util/log.h index 91e7af1d..0c65aa4a 100644 --- a/source/util/log.h +++ b/source/util/log.h @@ -24,8 +24,8 @@ #include #include #include -#include #include +#include /* TODO: fmtlib: replace with std, when all compilers support `format'. */ #include @@ -59,6 +59,7 @@ void init(enum Level lvl, std::filesystem::path logPath); */ std::string all(); + template void backend(enum Level level, std::string_view fmt, Args&&... args) { @@ -94,11 +95,6 @@ void backend(enum Level level, std::string_view fmt, Args&&... args) << fmt::vformat(fmt, fmt::make_format_args(args...)) << std::endl; - if (!logFile) { - const auto mode = std::ios_base::out | std::ios_base::app; - logFile = std::ofstream(logPath, mode); - } - if (logFile.value().good()) { logFile.value() << timestamp_now_str_fn() << ": "