summaryrefslogtreecommitdiffstats
path: root/src/CRSMLogging.cpp
blob: 733f49c3612863cade071744624434502bac7a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#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(response)
    {
        log("Response: " + message.trimmed() + "\n", userLogsFolder + client.toString());
    }
    else if(command)
    {
        log("Command:  " + 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, true) + " " + message.trimmed() + "\n", modLogsFolder + modName);
    }
    else
    {
        log("Command:  " + client.toString(true, true) + " " + message.trimmed() + "\n", modLogsFolder + modName);
    }
}

void CRSMLogging::commandLog(const QString &message, const ClientInfo &client, bool response)
{
    if(response)
    {
        log("Response: " + message.trimmed() + "\n", cmdLogsFolder + client.toString());
        log("Response: " + client.toString(true, true) + " " + message.trimmed() + "\n", cmdsLogFile);
    }
    else
    {
        log("Command:  " + message + "\n", cmdLogsFolder + client.toString());
        log("Command:  " + client.toString(true, true) + " " + message + "\n", cmdsLogFile);
    }
    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 ? "*" : ">"));
}