QElectroTech 0.200.1-dev
Loading...
Searching...
No Matches
qetlogger.h
Go to the documentation of this file.
1/*
2 Copyright 2006-2026 The QElectroTech Team
3 This file is part of QElectroTech.
4
5 QElectroTech is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 QElectroTech is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef QETLOGGER_H
19#define QETLOGGER_H
20
21#include "logring.h"
22
23#include <QFile>
24#include <QMutex>
25#include <QString>
26#include <QtGlobal>
27
74{
75 public:
76 static constexpr qint64 kMaxFileBytes = 2 * 1024 * 1024; // 2 MiB per file
77 static constexpr int kRotationKeep = 4; // .1.log .. .4.log
78 static constexpr int kMaxMessageBytes = 4096; // per-message truncation
79
80 static QetLogger &instance();
81
85 void init();
86
91
93 void handleMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg);
94
98 void pruneOldLogFiles(int days);
99
101 QVector<QByteArray> ringSnapshot() const {return m_ring.snapshot();}
102
103 // --- Step 5: getting the data back out -------------------------
104
107 bool hasPendingCrashDump() const;
108
112 QByteArray pendingCrashDumpContents() const;
113
118
123 QByteArray buildDiagnosticsReport() const;
124
129 static QByteArray redact(const QByteArray &input);
130
131 private:
132 QetLogger() = default;
133 QetLogger(const QetLogger &) = delete;
134
136 void rotateLocked();
137 void writeToFile(const QByteArray &line, QtMsgType type);
138 QString rotatedPath(int index) const;
139 QString crashDumpPath() const;
140 QString currentLogFilePath() const;
141
142 static QByteArray sanitize(const QByteArray &input);
143 static QByteArray truncateMessage(const QByteArray &input, int max_bytes);
144 static QByteArray formatLine(QtMsgType type, const QMessageLogContext &context, const QByteArray &sanitized_msg);
145
146 bool m_disabled = false;
147
148 QString m_log_dir;
149 QString m_base_name; // e.g. "20260803", resolved once in init()
150
152 QFile m_file;
154 bool m_file_output_ok = false;
155
157};
158
159#endif // QETLOGGER_H
The LogRing class Fixed-capacity, always-on in-memory ring of the most recent log lines,...
Definition logring.h:51
bool m_disabled
Definition qetlogger.h:146
QVector< QByteArray > ringSnapshot() const
Snapshot of the in-memory ring, oldest first.
Definition qetlogger.h:101
bool ensureFileOpenLocked()
QetLogger::ensureFileOpenLocked Caller must hold m_file_mutex. Opens the current session's log file i...
Definition qetlogger.cpp:126
void pruneOldLogFiles(int days)
Definition qetlogger.cpp:331
static constexpr int kMaxMessageBytes
Definition qetlogger.h:78
static QetLogger & instance()
QetLogger::instance Function-local static: guaranteed constructed exactly once, in a thread-safe way,...
Definition qetlogger.cpp:82
QByteArray pendingCrashDumpContents() const
Definition qetlogger.cpp:369
static QByteArray sanitize(const QByteArray &input)
QetLogger::sanitize Escapes newlines, carriage returns and other control characters....
Definition qetlogger.cpp:224
static QByteArray formatLine(QtMsgType type, const QMessageLogContext &context, const QByteArray &sanitized_msg)
Definition qetlogger.cpp:268
bool hasPendingCrashDump() const
Definition qetlogger.cpp:360
void rotateLocked()
QetLogger::rotateLocked Caller must hold m_file_mutex. Shifts .3.log -> .4.log (dropping the previous...
Definition qetlogger.cpp:163
QFile m_file
Definition qetlogger.h:152
void init()
Definition qetlogger.cpp:88
QetLogger()=default
void handleMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
The function installed via qInstallMessageHandler() forwards here.
Definition qetlogger.cpp:307
void clearPendingCrashDump()
Definition qetlogger.cpp:378
QString currentLogFilePath() const
Definition qetlogger.cpp:115
QMutex m_file_mutex
Definition qetlogger.h:151
qint64 m_bytes_written_current_file
Definition qetlogger.h:153
QetLogger(const QetLogger &)=delete
static QByteArray redact(const QByteArray &input)
QetLogger::redact Replaces the user's home directory with "~" wherever it appears....
Definition qetlogger.cpp:422
bool m_file_output_ok
Definition qetlogger.h:154
static constexpr int kRotationKeep
Definition qetlogger.h:77
QByteArray buildDiagnosticsReport() const
Definition qetlogger.cpp:383
LogRing m_ring
Definition qetlogger.h:156
QString m_base_name
Definition qetlogger.h:149
void installCrashHandler()
Definition qetlogger.cpp:102
void writeToFile(const QByteArray &line, QtMsgType type)
Definition qetlogger.cpp:185
QString rotatedPath(int index) const
Definition qetlogger.cpp:152
static constexpr qint64 kMaxFileBytes
Definition qetlogger.h:76
QString crashDumpPath() const
Definition qetlogger.cpp:110
QString m_log_dir
Definition qetlogger.h:148
static QByteArray truncateMessage(const QByteArray &input, int max_bytes)
QetLogger::truncateMessage Caps a single message at max_bytes, appending a marker stating how many by...
Definition qetlogger.cpp:254