aboutsummaryrefslogtreecommitdiff
path: root/src/Logger
diff options
context:
space:
mode:
authorDark1-dev <shansarkar272@gmail.com>2023-03-01 21:32:53 +0600
committerGitHub <noreply@github.com>2023-03-01 21:32:53 +0600
commitde57bc38217c09a0ae4a143f631896652368ecc3 (patch)
tree635e3f429a5a6e00744b6817533615e41c6db5e2 /src/Logger
parent60a301a93b6057bb2c54ac04a7c38c38389037b3 (diff)
downloadMasterserver-Qt5-de57bc38217c09a0ae4a143f631896652368ecc3.tar.gz
Masterserver-Qt5-de57bc38217c09a0ae4a143f631896652368ecc3.zip
Add files via upload
Diffstat (limited to 'src/Logger')
-rw-r--r--src/Logger/cyclelogfile.cpp48
-rw-r--r--src/Logger/initlog.cpp37
-rw-r--r--src/Logger/logevent.cpp20
-rw-r--r--src/Logger/logger.cpp10
-rw-r--r--src/Logger/logger.h46
-rw-r--r--src/Logger/logprimitive.h12
-rw-r--r--src/Logger/openlogfile.cpp21
-rw-r--r--src/Logger/writelogfile.cpp18
8 files changed, 212 insertions, 0 deletions
diff --git a/src/Logger/cyclelogfile.cpp b/src/Logger/cyclelogfile.cpp
new file mode 100644
index 0000000..cb0266e
--- /dev/null
+++ b/src/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/src/Logger/initlog.cpp b/src/Logger/initlog.cpp
new file mode 100644
index 0000000..d852c86
--- /dev/null
+++ b/src/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<QString, int> 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/src/Logger/logevent.cpp b/src/Logger/logevent.cpp
new file mode 100644
index 0000000..9d1858c
--- /dev/null
+++ b/src/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/src/Logger/logger.cpp b/src/Logger/logger.cpp
new file mode 100644
index 0000000..3bf7def
--- /dev/null
+++ b/src/Logger/logger.cpp
@@ -0,0 +1,10 @@
+#include "logger.h"
+
+Logger::Logger()
+{
+}
+
+void Logger::stop()
+{
+ closeLogFile();
+}
diff --git a/src/Logger/logger.h b/src/Logger/logger.h
new file mode 100644
index 0000000..9880c44
--- /dev/null
+++ b/src/Logger/logger.h
@@ -0,0 +1,46 @@
+#ifndef LOGGER_H
+#define LOGGER_H
+
+#include <QDir>
+#include <QFile>
+#include <QDateTime>
+#include <QTextStream>
+
+#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/src/Logger/logprimitive.h b/src/Logger/logprimitive.h
new file mode 100644
index 0000000..1630566
--- /dev/null
+++ b/src/Logger/logprimitive.h
@@ -0,0 +1,12 @@
+#ifndef LOGPRIMITIVE_H
+#define LOGPRIMITIVE_H
+
+#include <QTextStream>
+
+inline QTextStream& logPrimitive()
+{
+ static QTextStream r{stdout};
+ return r;
+}
+
+#endif // LOGPRIMITIVE_H
diff --git a/src/Logger/openlogfile.cpp b/src/Logger/openlogfile.cpp
new file mode 100644
index 0000000..349d058
--- /dev/null
+++ b/src/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/src/Logger/writelogfile.cpp b/src/Logger/writelogfile.cpp
new file mode 100644
index 0000000..954b5b2
--- /dev/null
+++ b/src/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;
+}