Skip to content

Commit

Permalink
Implement poorman's log rotating
Browse files Browse the repository at this point in the history
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
  • Loading branch information
psyomn committed Dec 30, 2023
1 parent 3bd7772 commit 3961ac8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
37 changes: 37 additions & 0 deletions source/util/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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()
Expand Down
8 changes: 2 additions & 6 deletions source/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <sstream>
#include <filesystem>
#include <optional>
#include <vector>
#include <mutex>
#include <vector>

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


template <typename... Args>
void backend(enum Level level, std::string_view fmt, Args&&... args)
{
Expand Down Expand Up @@ -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() << ": "
Expand Down

0 comments on commit 3961ac8

Please sign in to comment.