diff options
| author | Markus Mittendrein <git@maxmitti.tk> | 2015-11-10 21:48:29 +0100 |
|---|---|---|
| committer | Markus Mittendrein <git@maxmitti.tk> | 2015-11-10 21:48:29 +0100 |
| commit | e4ccb9c3e8baae8632d52ec7158f64eabc2b4d76 (patch) | |
| tree | e3917f7036eacf720940e52337961b56022c6047 /src/CRSMLogging.cpp | |
| parent | 7ceb033275b40e72142db6395aa4fc5c5f669cd3 (diff) | |
| download | manager-e4ccb9c3e8baae8632d52ec7158f64eabc2b4d76.tar.gz manager-e4ccb9c3e8baae8632d52ec7158f64eabc2b4d76.zip | |
Add logging
Diffstat (limited to 'src/CRSMLogging.cpp')
| -rw-r--r-- | src/CRSMLogging.cpp | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/src/CRSMLogging.cpp b/src/CRSMLogging.cpp new file mode 100644 index 0000000..ca1badd --- /dev/null +++ b/src/CRSMLogging.cpp @@ -0,0 +1,192 @@ +#include "CRSMLogging.hpp" +#include "crsm.hpp" + +#include <QDateTime> +#include <QDir> + +CRSMLogging::CRSMLogging() +{ + +} + +QString CRSMLogging::setLogFolder(const QString &folder) +{ + logFolder = folder; + QDir logDir(logFolder); + if(!logDir.mkpath(folder)) + { + return "Warning: Could not create Logging.Folder.\n"; + } + + if(!mainLogName.isEmpty()) + { + setMainLog(mainLogName); + } + + foreach(QFile* file, logFiles) + { + file->close(); + delete file; + } + logFiles.clear(); + + if(!QDir(logFolder + cmdLogsFolder).exists() && !logDir.mkdir(cmdLogsFolder)) + { + return "Warning: Could not create Folder for command-logs.\n"; + } + + if(!QDir(logFolder + ircLogsFolder).exists() && !logDir.mkdir(ircLogsFolder)) + { + return "Warning: Could not create Folder for irc-logs.\n"; + } + + if(!QDir(logFolder + modLogsFolder).exists() && !logDir.mkdir(modLogsFolder)) + { + return "Warning: Could not create Folder for moderator-logs.\n"; + } + + if(!QDir(logFolder + userLogsFolder).exists() && !logDir.mkdir(userLogsFolder)) + { + return "Warning: Could not create Folder for user-logs.\n"; + } + + return ""; +} + +QString CRSMLogging::setMainLog(const QString &fileName) +{ + mainLogName = fileName; + mainLog.close(); + mainLog.setFileName(logFolder + fileName); + if(!mainLog.open(QFile::WriteOnly | QFile::Append | QFile::Unbuffered)) + { + return "Warning: Could not open MainLog-file for writing: " + mainLog.errorString() + "\n"; + } + return ""; +} + +void CRSMLogging::setTimestampFormat(const QString &format) +{ + timestampFormat = format; +} + +void CRSMLogging::log(const QString &message, const QString logFile) +{ + QFile* file = (logFile.isEmpty()) ? &mainLog : logFiles.value(logFile, nullptr); + if(file == nullptr) + { + file = logFiles[logFile] = new QFile; + file->setFileName(logFolder + logFile); + file->open(QFile::WriteOnly | QFile::Append | QFile::Unbuffered); + } + if(file->isOpen()) + { + file->write(QDateTime::currentDateTime().toString(timestampFormat).toUtf8()); + file->write(message.toUtf8()); + } +} + +void CRSMLogging::clonkUserLog(const QString &message, const ClientInfo &client, bool action, bool command, bool response) +{ + if(action) + { + log("Action: " + message + "\n", userLogsFolder + client.toString()); + } + else if(command) + { + log("Command: " + message + "\n", userLogsFolder + client.toString()); + } + else if(response) + { + log("Response: " + message + "\n", userLogsFolder + client.toString()); + } + else + { + log("Message: " + message + "\n", userLogsFolder + client.toString()); + } +} + +void CRSMLogging::modLog(const QString &message, const ClientInfo &client, const QString &modName, bool response) +{ + if(response) + { + log("Response: " + client.toString(true) + " " + message + "\n", modLogsFolder + modName); + } + else + { + log("Command: " + client.toString(true) + " " + message + "\n", modLogsFolder + modName); + } +} + +void CRSMLogging::commandLog(const QString &message, const ClientInfo &client, bool response) +{ + if(response) + { + log("Response: " + message.trimmed() + "\n", cmdLogsFolder + client.toString()); + } + else + { + log("Command: " + message + "\n", cmdLogsFolder + client.toString()); + } + if(client.interface == Clonk) + { + clonkUserLog(message, client, false, true, response); + } + else if(client.interface == IRC) + { + ircUserLog(message.trimmed(), client, false, response ? "¡Response!" : "¿Command?", false, false); + } +} + +void CRSMLogging::ircLog(const QString& message, const QString &nick, bool query, QString channel, bool action, bool notice, bool response) +{ + if(query) + { + if(notice) + { + log("Notice: " + message + "\n", ircLogsFolder + nick); + } + else if(action) + { + log("Action: " + message + "\n", ircLogsFolder + nick); + } + else if(response) + { + log("Response: " + message + "\n", ircLogsFolder + nick); + } + else + { + log("Message: " + message + "\n", ircLogsFolder + nick); + } + } + else + { + log(formatIrcNick(nick, true, "", action, notice) + " " + message + "\n", ircLogsFolder + channel); + } +} + +void CRSMLogging::ircUserLog(const QString &message, const ClientInfo &client, bool query, const QString& channel, bool action, bool notice) +{ + log(formatIrcNick(client.nick, query, channel, action, notice) + " " + message + "\n", userLogsFolder + client.toString()); +} + +void CRSMLogging::clonkChatLog(const QString &message) +{ + log(message + "\n", clonkChatLogFile); +} + +void CRSMLogging::clonkLog(const QString &message) +{ + log(message + "\n", clonkLogFile); +} + +void CRSMLogging::scenLog(const ScenarioSettings &scen) +{ + log(scen.name + (scen.league ? " league" : " no league") + " by " + scen.wishClient.toString() + "\n", scensLogFile); +} + +QString CRSMLogging::formatIrcNick(const QString &nick, bool noChannel, const QString &channel, bool action, bool notice) +{ + return (notice ? "-" : (action ? "*" : "<")) + (noChannel ? "" : channel + ":") + nick + (notice ? "-" : (action ? "*" : ">")); +} + |
