core/log: add timestamps to log files

This commit is contained in:
outfoxxed 2024-08-07 13:40:37 -07:00
parent 38ba3fff24
commit 7c7326ec52
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
3 changed files with 25 additions and 5 deletions

View file

@ -55,6 +55,6 @@ void FileLogger::init() {
} }
void FileLogger::onMessage(const LogMessage& msg) { void FileLogger::onMessage(const LogMessage& msg) {
LogManager::formatMessage(this->fileStream, msg, false); LogManager::formatMessage(this->fileStream, msg, false, true);
this->fileStream << Qt::endl; this->fileStream << Qt::endl;
} }

View file

@ -21,7 +21,7 @@ void LogManager::messageHandler(
auto* self = LogManager::instance(); auto* self = LogManager::instance();
LogManager::formatMessage(self->stdoutStream, message, self->colorLogs); LogManager::formatMessage(self->stdoutStream, message, self->colorLogs, false);
self->stdoutStream << Qt::endl; self->stdoutStream << Qt::endl;
emit self->logMessage(message); emit self->logMessage(message);
@ -32,7 +32,17 @@ LogManager* LogManager::instance() {
return instance; return instance;
} }
void LogManager::formatMessage(QTextStream& stream, const LogMessage& msg, bool color) { void LogManager::formatMessage(
QTextStream& stream,
const LogMessage& msg,
bool color,
bool timestamp
) {
if (timestamp) {
if (color) stream << "\033[90m";
stream << msg.time.toString("yyyy-MM-dd hh:mm:ss.zzz");
}
if (color) { if (color) {
switch (msg.type) { switch (msg.type) {
case QtDebugMsg: stream << "\033[34m DEBUG"; break; case QtDebugMsg: stream << "\033[34m DEBUG"; break;
@ -62,4 +72,6 @@ void LogManager::formatMessage(QTextStream& stream, const LogMessage& msg, bool
if (color && msg.type != QtFatalMsg) stream << "\033[0m"; if (color && msg.type != QtFatalMsg) stream << "\033[0m";
stream << ": " << msg.body; stream << ": " << msg.body;
if (color && msg.type == QtFatalMsg) stream << "\033[0m";
} }

View file

@ -2,18 +2,26 @@
#include <utility> #include <utility>
#include <qdatetime.h>
#include <qlogging.h> #include <qlogging.h>
#include <qobject.h> #include <qobject.h>
#include <qtextstream.h> #include <qtextstream.h>
#include <qtmetamacros.h> #include <qtmetamacros.h>
struct LogMessage { struct LogMessage {
explicit LogMessage(QtMsgType type, const char* category, QByteArray body) explicit LogMessage(
QtMsgType type,
const char* category,
QByteArray body,
QDateTime time = QDateTime::currentDateTime()
)
: type(type) : type(type)
, time(std::move(time))
, category(category) , category(category)
, body(std::move(body)) {} , body(std::move(body)) {}
QtMsgType type; QtMsgType type;
QDateTime time;
const char* category; const char* category;
QByteArray body; QByteArray body;
}; };
@ -24,7 +32,7 @@ class LogManager: public QObject {
public: public:
static LogManager* instance(); static LogManager* instance();
static void formatMessage(QTextStream& stream, const LogMessage& msg, bool color); static void formatMessage(QTextStream& stream, const LogMessage& msg, bool color, bool timestamp);
signals: signals:
void logMessage(LogMessage msg); void logMessage(LogMessage msg);