From 3c7253d6cdc23aac36208fa87dc6571c7cb7c5ff Mon Sep 17 00:00:00 2001 From: Dark1-dev Date: Wed, 1 Mar 2023 21:33:55 +0600 Subject: Add files via upload --- Logger/cyclelogfile.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Logger/initlog.cpp | 37 +++++++++++++++++++++++++++++++++++++ Logger/logevent.cpp | 20 ++++++++++++++++++++ Logger/logger.cpp | 10 ++++++++++ Logger/logger.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Logger/logprimitive.h | 12 ++++++++++++ Logger/openlogfile.cpp | 21 +++++++++++++++++++++ Logger/writelogfile.cpp | 18 ++++++++++++++++++ 8 files changed, 212 insertions(+) create mode 100644 Logger/cyclelogfile.cpp create mode 100644 Logger/initlog.cpp create mode 100644 Logger/logevent.cpp create mode 100644 Logger/logger.cpp create mode 100644 Logger/logger.h create mode 100644 Logger/logprimitive.h create mode 100644 Logger/openlogfile.cpp create mode 100644 Logger/writelogfile.cpp (limited to 'Logger') diff --git a/Logger/cyclelogfile.cpp b/Logger/cyclelogfile.cpp new file mode 100644 index 0000000..cb0266e --- /dev/null +++ b/Logger/cyclelogfile.cpp @@ -0,0 +1,48 @@ +#include "logger.h" + +// if "now" matches the previous logfile name, no cycling needed. otherwise, determine new logfile name. +bool Logger::cycleLogFile() +{ + // calculate the expected log filename + QString newFilename = _logLabel; + + // get date to see if we need to cycle + QDate date(QDate::currentDate()); + + // when is it necessary to cycle? + switch ( _cyclePeriod ) + { + case 1: // yearly + newFilename += date.toString("-yyyy"); + break; + case 2: // monthly + newFilename += date.toString("-yyyy-MM"); + break; + case 3: // weekly + newFilename += date.toString("-yyyy") + "-week" + QString("%1").arg(date.weekNumber(), 2, 10, QChar('0')); + break; + case 4: // daily + newFilename += date.toString("-yyyy-MM-dd"); + break; + default:; // 0 = never + } + + // add log file extension + newFilename += ".log"; + + // if current and new filename are not the same, cycle log file + if (_logFileName != newFilename) + { + // cycle log file + _logFileName = newFilename; + + // close the existing/opened log file + closeLogFile(); + + // open the new log file (pass possible error to caller) + return openLogFile(); + } + + // else: no cycling necessary + return true; +} diff --git a/Logger/initlog.cpp b/Logger/initlog.cpp new file mode 100644 index 0000000..d852c86 --- /dev/null +++ b/Logger/initlog.cpp @@ -0,0 +1,37 @@ +#include "logger.h" + +bool Logger::init(const QString &applicationPath, + const SettingStructure &settings) +{ + // test if log directory exists + _logPath = applicationPath + "/" + _logDirectory; + QDir logDir(_logPath); + if ( ! logDir.mkpath(".") ) + { + QTextStream(stdout) << "Failed to access log directory at " + _logPath << endl + << "Please create the director and/or apply relevant permissions." << endl; + return false; + } + + // log cycling -- determine log cycling setting + const QHash logCycling = { + {"never", 0}, + {"yearly", 1}, + {"monthly", 2}, + {"weekly", 3}, + {"daily", 4} + }; + + // load log-specific settings + _cyclePeriod = logCycling.value( settings.LoggingSettings.cycle ); + _suppressLog = settings.LoggingSettings.suppressLog; + _suppressDisplay = settings.LoggingSettings.suppressDisplay; + + // cycling initialisation ("never" is also a cycle) + if ( ! cycleLogFile() ) + { + QTextStream(stdout) << "Failed to access log file " + _logFileName + " at " + _logPath << endl; + return false; + } + return true; +} diff --git a/Logger/logevent.cpp b/Logger/logevent.cpp new file mode 100644 index 0000000..9d1858c --- /dev/null +++ b/Logger/logevent.cpp @@ -0,0 +1,20 @@ +#include "logger.h" + +void Logger::logEvent(const QString &messageType, + const QString &message) +{ + // printing to display suppressed? + if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") ) + { + QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); + logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message.toLatin1()) << endl; + } + + // printing to logfile suppressed? + if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") ) + { + // write message to log + QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz")); + writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message.toLatin1()) ); + } +} diff --git a/Logger/logger.cpp b/Logger/logger.cpp new file mode 100644 index 0000000..3bf7def --- /dev/null +++ b/Logger/logger.cpp @@ -0,0 +1,10 @@ +#include "logger.h" + +Logger::Logger() +{ +} + +void Logger::stop() +{ + closeLogFile(); +} diff --git a/Logger/logger.h b/Logger/logger.h new file mode 100644 index 0000000..9880c44 --- /dev/null +++ b/Logger/logger.h @@ -0,0 +1,46 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include +#include +#include + +#include "Settings/settingstructure.h" +#include "Core/version.h" +#include "logprimitive.h" + +class Logger +{ +public: + Logger(); + bool init(const QString &applicationPath, + const SettingStructure &settings); + void stop(); + void logEvent(const QString &messageType, + const QString &message); + +private: + // path variables + const QString _logDirectory = "../log"; + const QString _logLabel = "MasterServer-" + SHORT_VER; + QString _logPath = ""; + + // local variables + int _cyclePeriod; + QString _logFileName; + QString _suppressLog; + QString _suppressDisplay; + + // file i/o + QFile _logFile; + +private: + bool cycleLogFile(); + bool openLogFile(); + void closeLogFile(); + bool writeLogFile(const QString message); + +}; + +#endif // LOGGER_H diff --git a/Logger/logprimitive.h b/Logger/logprimitive.h new file mode 100644 index 0000000..1630566 --- /dev/null +++ b/Logger/logprimitive.h @@ -0,0 +1,12 @@ +#ifndef LOGPRIMITIVE_H +#define LOGPRIMITIVE_H + +#include + +inline QTextStream& logPrimitive() +{ + static QTextStream r{stdout}; + return r; +} + +#endif // LOGPRIMITIVE_H diff --git a/Logger/openlogfile.cpp b/Logger/openlogfile.cpp new file mode 100644 index 0000000..349d058 --- /dev/null +++ b/Logger/openlogfile.cpp @@ -0,0 +1,21 @@ +#include "logger.h" + +bool Logger::openLogFile() +{ + // verify that there is no open logfile already + if ( _logFile.openMode() == QIODevice::NotOpen) + { + // set new log file + _logFile.setFileName(_logPath + "/" + _logFileName); + return _logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); + } + // else: + // file already open, can not get a lock + return false; +} + +void Logger::closeLogFile() +{ + // close file if open. if already closed, ignore. + _logFile.close(); +} diff --git a/Logger/writelogfile.cpp b/Logger/writelogfile.cpp new file mode 100644 index 0000000..954b5b2 --- /dev/null +++ b/Logger/writelogfile.cpp @@ -0,0 +1,18 @@ +#include "logger.h" + +bool Logger::writeLogFile(const QString message) +{ + // first see if we need to cycle the log file already + cycleLogFile(); + + // write to file + if ( _logFile.isOpen() ) + { + QTextStream logStream(&_logFile); + logStream << message << endl; + return true; + } + + logPrimitive() << "[log error]" << message; + return false; +} -- cgit v1.2.3