6 #include <QCoreApplication>
12 #include <QStringBuilder>
18 using namespace swift::config;
25 static const QString
applicationName = QFileInfo(QCoreApplication::applicationFilePath()).completeBaseName();
32 static const QString fileName =
applicationName() % QLatin1String(
"_") %
33 QDateTime::currentDateTimeUtc().toString(QStringLiteral(
"yyMMddhhmmss")) %
34 QLatin1String(
"_") % QString::number(QCoreApplication::applicationPid()) %
35 QLatin1String(
".log");
39 CFileLogger::CFileLogger(QObject *parent) : QObject(parent), m_logFile(this)
45 m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
46 m_stream.setDevice(&m_logFile);
47 m_stream.setEncoding(QStringConverter::Utf8);
55 if (m_logFile.isOpen())
58 writeContentToFile(QStringLiteral(
"Logging stops."));
67 if (statusMessage.
isEmpty()) {
return; }
68 if (!m_logFile.isOpen()) {
return; }
69 if (!m_logPattern.
match(statusMessage)) {
return; }
71 if (categories != m_previousCategories)
73 writeContentToFile(u
"\n[" % categories % u
']');
74 m_previousCategories = categories;
76 const QString finalContent(QDateTime::currentDateTime().toString(QStringLiteral(
"hh:mm:ss ")) %
79 writeContentToFile(finalContent);
88 void CFileLogger::removeOldLogFiles()
91 nameFilter += QLatin1String(
"*.log");
94 QDateTime now = QDateTime::currentDateTime();
95 for (
const auto &logFileInfo : dir.entryInfoList())
97 if (logFileInfo.lastModified().daysTo(now) > 7) { dir.remove(logFileInfo.fileName()); }
101 void CFileLogger::writeHeaderToFile()
104 m_stream <<
" version " << CBuildConfig::getVersionString();
105 m_stream <<
" running on " << QSysInfo::prettyProductName();
106 m_stream <<
" " << QSysInfo::currentCpuArchitecture() << Qt::endl;
108 m_stream <<
"Built from revision " << CBuildConfig::gitHeadSha1();
109 m_stream <<
" on " << CBuildConfig::buildDateAndTime() << Qt::endl;
111 m_stream <<
"Built with Qt " << QT_VERSION_STR;
112 m_stream <<
" and running with Qt " << qVersion();
113 m_stream <<
" " << QSysInfo::buildAbi() << Qt::endl;
115 m_stream <<
"Application started." << Qt::endl;
118 void CFileLogger::writeContentToFile(
const QString &content) { m_stream << content << Qt::endl; }
static QString getLogFileName()
Get the log file name.
~CFileLogger()
Destructor.
void writeStatusMessageToFile(const swift::misc::CStatusMessage &statusMessage)
Write single status message to file.
static QString getLogFilePath()
Get the log file path (including its name)
bool match(const CStatusMessage &message) const
Returns true if the given message matches this pattern.
bool isEmpty() const
Message empty.
Streamable status message, e.g.
QString getCategoriesAsString() const
Message categories as string.
const QString & getSeverityAsString() const
Severity as string.
QString getMessage() const
Message.
static const QString & logDirectory()
Directory for log files.
Free functions in swift::misc.
QString logFileName()
Get log file name.
QString applicationName()
Get application name.