diff --git a/src/blackmisc/filelogger.cpp b/src/blackmisc/filelogger.cpp new file mode 100644 index 000000000..221cc1d43 --- /dev/null +++ b/src/blackmisc/filelogger.cpp @@ -0,0 +1,100 @@ +/* Copyright (C) 2015 + * Swift Project Community / Contributors + * + * This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "filelogger.h" +#include +#include +#include + +namespace BlackMisc +{ + + CFileLogger::CFileLogger(QObject *parent) : CFileLogger(QCoreApplication::applicationName(), QString(), parent) + { + } + + CFileLogger::CFileLogger(const QString &applicationName, const QString &logPath, QObject *parent) : + QObject(parent), + m_logSubscriber(this, &CFileLogger::ps_writeStatusMessageToFile), + m_logFile(this), + m_applicationName(applicationName), + m_logPath(logPath) + { + removeOldLogFiles(); + if (!m_logPath.isEmpty() && !m_logPath.endsWith('/')) { m_logPath += '/'; } + m_logFile.setFileName(getFullFileName()); + m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); + m_stream.setDevice(&m_logFile); + m_stream.setCodec("UTF-8"); + writeHeaderToFile(); + } + + CFileLogger::~CFileLogger() + { + if (m_logFile.isOpen()) + { + writeContentToFile(QStringLiteral("Application stopped.")); + m_logFile.close(); + } + } + + void CFileLogger::changeLogPattern(const CLogPattern &pattern) + { + m_logSubscriber.changeSubscription(pattern); + } + + void CFileLogger::ps_writeStatusMessageToFile(const BlackMisc::CStatusMessage &statusMessage) + { + QString finalContent = QDateTime::currentDateTime().toString(QStringLiteral("hh:mm:ss ")); + finalContent += statusMessage.getHumanReadableCategory(); + finalContent += " "; + finalContent += statusMessage.getSeverityAsString(); + finalContent += ": "; + finalContent += statusMessage.getMessage(); + writeContentToFile(finalContent); + } + + QString CFileLogger::getFullFileName() + { + QString filename; + Q_ASSERT(!m_applicationName.isEmpty()); + if (!m_logPath.isEmpty()) filename += m_logPath; + filename += m_applicationName; + filename += QLatin1String("_"); + filename += QDateTime::currentDateTime().toString(QStringLiteral("yyMMddhhmmss")); + filename += QLatin1String(".log"); + return filename; + } + + void CFileLogger::removeOldLogFiles() + { + QString nameFilter(m_applicationName); + nameFilter += QLatin1String("*.log"); + QDir dir(m_logPath, nameFilter, QDir::Name, QDir::Files); + + for (const auto &logFile : dir.entryList()) + { + if (QFileInfo(logFile).lastModified().daysTo(QDateTime::currentDateTime()) > 7 ) + { + dir.remove(logFile); + } + } + } + + void CFileLogger::writeHeaderToFile() + { + QString header(QStringLiteral("Application started.")); + writeContentToFile(header); + } + + void CFileLogger::writeContentToFile(const QString &content) + { + m_stream << content << endl; + } +} diff --git a/src/blackmisc/filelogger.h b/src/blackmisc/filelogger.h new file mode 100644 index 000000000..54f52505e --- /dev/null +++ b/src/blackmisc/filelogger.h @@ -0,0 +1,71 @@ +/* Copyright (C) 2015 + * swift Project Community / Contributors + * + * This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKMISC_FILELOGGER_H +#define BLACKMISC_FILELOGGER_H + +//! \file + +#include "blackmiscexport.h" +#include "loghandler.h" +#include "statusmessage.h" +#include +#include + +namespace BlackMisc +{ + + //! Class to write log messages to file + class BLACKMISC_EXPORT CFileLogger : public QObject + { + Q_OBJECT + + public: + + //! Constructor. + //! Filename defaults to QCoreApplication::applicationName() and path to "." + CFileLogger(QObject *parent = nullptr); + + /*! + * Constructor + * \param applicationName Use the applications name without any extension. + * A timestamp and extension will be added automatically. + * \param logPath Path the log files is written to. If you leave this empty, the + * file will be written in the working directory of the binary. + * \param parent QObject parent + */ + CFileLogger(const QString &applicationName, const QString &logPath, QObject *parent = nullptr); + + //! Destructor. + ~CFileLogger(); + + //! Change the log pattern. Call this method at least once to start logging + void changeLogPattern(const CLogPattern &pattern); + + private slots: + + void ps_writeStatusMessageToFile(const BlackMisc::CStatusMessage &statusMessage); + + private: + + QString getFullFileName(); + void removeOldLogFiles(); + + void writeHeaderToFile(); + void writeContentToFile(const QString &content); + + CLogSubscriber m_logSubscriber; + QFile m_logFile; + QTextStream m_stream; + QString m_applicationName; + QString m_logPath; //!< Empty by default. Hence the working directory "." is used + }; +} + +#endif