Skip to content

Commit

Permalink
Merge pull request #138 from saeugetier/74-log-file
Browse files Browse the repository at this point in the history
add custom message handler
  • Loading branch information
saeugetier authored May 26, 2024
2 parents 714820d + 8da0619 commit e43333b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,72 @@
#include "fileio.h"
#include "filesystem.h"
#include "system.h"
#include <iostream>

//some constants to parameterize.
const qint64 LOG_FILE_LIMIT = 3000000;
const QString LOG_PATH = "/logs/";
const QString LOG_FILENAME = "qtbooth.log";

//thread safety
QMutex mutex;

void redirectDebugMessages(QtMsgType type, const QMessageLogContext & context, const QString & str)
{
//thread safety
mutex.lock();
QString txt;

//prepend timestamp to every message
QString datetime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss");
//prepend a log level label to every message
switch (type) {
case QtDebugMsg:
txt = QString("[Debug*] ");
break;
case QtWarningMsg:
txt = QString("[Warning] ");
break;
case QtInfoMsg:
txt = QString("[Info] ");
break;
case QtCriticalMsg:
txt = QString("[Critical] ");
break;
case QtFatalMsg:
txt = QString("[Fatal] ");
}

QDir dir;

QString filePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation) + LOG_PATH;

if (!dir.exists(filePath))
dir.mkpath(filePath); // You can check the success if needed

filePath = filePath + LOG_FILENAME;

QFile outFile(filePath);

//if file reached the limit, rotate to filename.1
if(outFile.size() > LOG_FILE_LIMIT){
//roll the log file.
QFile::remove(filePath + ".1");
QFile::rename(filePath, filePath + ".1");
QFile::resize(filePath, 0);
}

//write message
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << datetime << txt << context.file << str << endl;

std::cout << datetime.toStdString() << " - " << txt.toStdString() << " - " << context.file << "(" << context.line << ") - " << str.toStdString() << std::endl;

//close fd
outFile.close();
mutex.unlock();
}

int main(int argc, char *argv[])
{
Expand All @@ -27,6 +93,8 @@ int main(int argc, char *argv[])
QGuiApplication::setOrganizationDomain("github.com");
QGuiApplication::setApplicationName("qtbooth");

qInstallMessageHandler(redirectDebugMessages);

QFontDatabase fontDatabase;
if (fontDatabase.addApplicationFont(":/font/fontello/fontello.ttf") == -1)
qWarning() << "Failed to load fontello.ttf";
Expand Down

0 comments on commit e43333b

Please sign in to comment.