Skip to content

Commit

Permalink
Added automatic logging to user dir and command line args "nolog" and…
Browse files Browse the repository at this point in the history
… "log=<path>".
  • Loading branch information
kipp committed Jul 24, 2023
1 parent cfb267e commit b97b8d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 12 additions & 2 deletions Qitom/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,19 @@ int main(int argc, char *argv[])
//it is possible to redirect all Qt messages sent via qDebug, qWarning... to the logfile itomlog.txt.
//This option is enabled via the argument log passed to the executable.
ito::Logger* logger = nullptr;
if (args.contains("log", Qt::CaseInsensitive))
if (!args.contains("nolog", Qt::CaseInsensitive))
{
logger = new ito::Logger("itomlog.txt");
QString logFileDir = "";
QStringListIterator i(args);
while (i.hasNext())
{
QString arg = i.next();
if (arg.startsWith("log="))
{
logFileDir = arg.mid(4);
}
}
logger = new ito::Logger("itomlog.txt", logFileDir, 5 * 1024 * 1024, 2);
ito::AppManagement::setLogger((QObject*)logger);
}

Expand Down
7 changes: 5 additions & 2 deletions common/itomLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ class ITOMCOMMONQT_EXPORT Logger : public QObject
/**
* @brief Construct a new Logger object
*
* @param logFile the log will be written to this file
* @param logFileName the name of the log file
* @param fileSizeBytes the file size in bytes above which a new file will be created
* @param backupCount the number of old log files to be kept
* @param logFileDir the log will be written to this directory; a user directory is used if not
* given
*/
Logger(QString logFile, int fileSizeBytes = 0, int backupCount = 0);
Logger(
QString logFileName, QString logFileDir = "", int fileSizeBytes = 0, int backupCount = 0);
~Logger();

public slots:
Expand Down
10 changes: 8 additions & 2 deletions common/sources/itomLog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../itomLog.h"
#include <qdatetime.h>
#include <qdir.h>
#include <qstandardpaths.h>


namespace ito {
Expand All @@ -11,9 +12,14 @@ QVector<Logger*> Logger::s_instances = QVector<Logger*>();


//----------------------------------------------------------------------------------------------------------------------------------
Logger::Logger(QString logFile, int fileSizeBytes, int backupCount)
Logger::Logger(QString logFileName, QString logFileDir, int fileSizeBytes, int backupCount)
{
this->m_logFile.setFileName(logFile);
if (logFileDir.isEmpty())
{
logFileDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
logFileDir += "/qitom/log";
}
this->m_logFile.setFileName(logFileDir + "/" + logFileName);
this->initFiles(fileSizeBytes, backupCount);
this->m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);

Expand Down

0 comments on commit b97b8d5

Please sign in to comment.