Skip to content

Commit 162ab39

Browse files
committed
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
1 parent 3bd7772 commit 162ab39

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

source/util/log.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,44 @@ std::filesystem::path logPath;
2525

2626
std::mutex lock;
2727

28+
void trim(const unsigned int count)
29+
{
30+
std::ifstream input(logPath);
31+
std::string s;
32+
33+
/* a poorman's circular buffer that keeps the last 'count' log lines. */
34+
std::vector<std::string> v(count);
35+
36+
std::size_t index = 0;
37+
while (std::getline(input, s)) {
38+
v[index % count] = s;
39+
index++;
40+
}
41+
42+
input.close();
43+
44+
std::ofstream output(logPath, std::ios_base::out);
45+
if (output.good()) {
46+
for (size_t i = 0; i < count; ++i)
47+
output << v[(index + i) % count] << std::endl;
48+
} else {
49+
std::cerr << "could not open log file for trimming" << std::endl;
50+
}
51+
output.close();
52+
}
53+
2854
void init(enum Level lvl, std::filesystem::path lp)
2955
{
3056
FilterLevel = lvl;
3157
logPath = lp;
58+
59+
// keep only the last X lines
60+
trim(1000);
61+
62+
if (!logFile) {
63+
const auto mode = std::ios_base::out | std::ios_base::app;
64+
logFile = std::ofstream(logPath, mode);
65+
}
3266
}
3367

3468
std::string all()

source/util/log.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <sstream>
2525
#include <filesystem>
2626
#include <optional>
27-
#include <vector>
2827
#include <mutex>
28+
#include <vector>
2929

3030
/* TODO: fmtlib: replace with std, when all compilers support `format'. */
3131
#include <fmt/core.h>
@@ -59,6 +59,7 @@ void init(enum Level lvl, std::filesystem::path logPath);
5959
*/
6060
std::string all();
6161

62+
6263
template <typename... Args>
6364
void backend(enum Level level, std::string_view fmt, Args&&... args)
6465
{
@@ -94,11 +95,6 @@ void backend(enum Level level, std::string_view fmt, Args&&... args)
9495
<< fmt::vformat(fmt, fmt::make_format_args(args...))
9596
<< std::endl;
9697

97-
if (!logFile) {
98-
const auto mode = std::ios_base::out | std::ios_base::app;
99-
logFile = std::ofstream(logPath, mode);
100-
}
101-
10298
if (logFile.value().good()) {
10399
logFile.value()
104100
<< timestamp_now_str_fn() << ": "

0 commit comments

Comments
 (0)