summaryrefslogtreecommitdiffstats
path: root/src/util/consoleLogger.cpp
blob: 9189543448755f3b6c40cddfb89d77e8d7e2f07e (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 # Copyright (c) 2009, 2010 - OpenSLX Project, Computer Center University of
 # Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # -----------------------------------------------------------------------------
 # consoleLogger.cpp
 #  - ???.
 # -----------------------------------------------------------------------------
 */

#include "consoleLogger.h"

#ifdef never
template<class T>
void ConsoleLogger::addListener(T* who, void (T :: *func)(QString))
{
    LogNotify<T> *test = new LogNotify<T>(who, func);
    _CBLog.push_back(dynamic_cast<LogNotifyEntry*>(test));
}

template<class T>
void ConsoleLogger::removeListener(T* who, void (T :: *func)(QString))
{
    if (_CBLog.size())
    {
        for (std::list<LogNotifyEntry*>::iterator it = _CBLog.begin(); it != _CBLog.end(); it++)
        {
            LogNotify<T> *probe = dynamic_cast<LogNotify<T>*>((*it));
            if (probe)
            {
                if (probe->getCallee() == who && probe->getCB() == func)
                {
                    _CBLog.remove((*it));
                }
            }
        }
    }
//    LogNotify<T> *test = new LogNotify<T>(who, func);
    //  _CBLog.push_back(dynamic_cast<LogNotifyEntry*>(test));
}

#endif
int ConsoleLogger::writeLine(QString message, LOG_LEVEL level)
{
    if (message.size() == 0)
        return -1;

    ConsoleEntry consoleEntry(_getTimeStamp(), message, level);
    _announceWrite(consoleEntry);    // tell the listeners!
    _writeLine2File(consoleEntry.getLine());
    _log.push_back(consoleEntry);

    return _log.size();
}

int ConsoleLogger::writeline(const char* message, LOG_LEVEL level)
{
    if (message)
    {
        if (strlen(message) > 0)
            return writeLine(QString(message), level);
    }
    return -1;
}

int ConsoleLogger::writeError(QString error)
{
    return writeLine(error, LOG_ERROR);
}

int ConsoleLogger::writeError(const char* error)
{
    return writeLine(error, LOG_ERROR);
}

int ConsoleLogger::writeTerminal(QString terminal)
{
    // printing the line on the console happens further down the line!
    return writeLine(terminal, LOG_TERMINAL);
}

int ConsoleLogger::writeTerminal(const char* terminal)
{
    if (terminal)
        if (strlen(terminal) > 0)
            return writeTerminal(QString(terminal));

    terminal = NULL;
    return -1;
}

int ConsoleLogger::writeNetwork(QString network)
{
    // printing the line on the console happens further down the line!
    return writeLine(network, LOG_NETWORK);
}

int ConsoleLogger::writeNetwork(const char* network)
{
    if (network)
        if (strlen(network) > 0)
            return writeNetwork(QString(network));

    network = NULL;
    return -1;
}

int ConsoleLogger::writeChat(QString chat)
{
	return writeLine(chat, LOG_CHAT);
}

int ConsoleLogger::writeChat(const char* chat)
{
	if (chat)
		if (strlen(chat) > 0)
			return writeChat(QString(chat));

	chat = NULL;
	return -1;
}

void ConsoleLogger::getLine(int lineNum, char* line)
{
    QString tmp;
    getLine(lineNum, &tmp);
    if (tmp.size() > 0)
    {
        line = new char[_log[lineNum].getLine().size()+1];
        if (line)
        {
            memcpy(line, _log[lineNum].getLine().toUtf8().data(), _log[lineNum].getLine().size());
            if (strlen(line) > 0)
                return;
        }
    }
    line = NULL;

}

void ConsoleLogger::getLine(int lineNum, QString* line)
{
    if (lineNum <= int(_log.size()))
    {
        if (lineNum > 0)
            lineNum--;
        else if (lineNum < 0)
        {
            line = NULL;
            return;
        }
    }
    else
    {
        line->clear();
        line = NULL;
    }
}

void ConsoleLogger::setLogPath(QString logPath)
{
    getLogger()->_logPath = logPath;
    getLogger()->_prepareLog();
}

void ConsoleLogger::setLogName(QString logName)
{
    getLogger()->_logName = logName;
    getLogger()->_prepareLog();
}

void ConsoleLogger::dumpLog(DUMP_MODE mode)
{
    if (mode == DUMP_FILE || (DUMP_FILE_CONCAT && !_fileRead))
        getLogger()->_writeLog();
    if (mode == DUMP_FILE_CONCAT && _fileRead)
        getLogger()->_writeLog(true);
    if (mode == DUMP_ALL_LISTENERS)
    {
        for (int i = 0; i <int(_log.size()); i++)
        {
            _announceWrite(_log[i]);
        }
    }
}

int ConsoleLogger::count = 0;

ConsoleLogger* ConsoleLogger::_logger;

ConsoleLogger* ConsoleLogger::getLogger()
{
    if (!_logger)
    {
        return ConsoleLogger::_logger = new ConsoleLogger();
    }
    else
        return ConsoleLogger::_logger;
}

ConsoleLogger::ConsoleLogger()
        :       _logName(""),
        _logPath(getPolicyFilePath(QString(""))),
        _fileRead(false),
        entryDispatcher()
{
//    _prepareLog();
}

ConsoleLogger::~ConsoleLogger()
{
    // doesnt work!?
    _writeLog();
}

void ConsoleLogger::_prepareLog()
{
    _logFile.close();
    _logFileGood = false;
    _readLog();

    createPolicyDir();
    QString fullpath;
    fullpath.append(_logPath);
    //TODO: handle wether path/ or path were entered?
    fullpath.append(_logName);
    _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::app);
    if (_logFile.good())
        _logFileGood = true;
    else
        qDebug("ERROR: Logfile ( %s ) not accessible/found. Logs will not be available.", qPrintable(_logPath));

    _logFile.close();
}

void ConsoleLogger::_writeLine2File(QString line)
{
    if (_logFileGood)
    {
        QString fullpath;
        fullpath.append(_logPath);
        //TODO: handle wether path/ or path were entered?
        fullpath.append(_logName);
        _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::app);

        if (_logFile.good()) // one can never be too sure
        {
            _logFile.write(line.toUtf8().data(), line.size());
        }

        _logFile.close();
    }

}

// overwrites the file and dumps the complete log archive of this session
void ConsoleLogger::_writeLog(bool concat)
{
    if (_logFileGood)
    {
        QString fullpath;
        fullpath.append(_logPath);
        //TODO: handle wether path/ or path were entered?
        fullpath.append(_logName);

        std::vector<ConsoleEntry> *tmpLog = NULL;

        if (concat)
        {
            std::vector<ConsoleEntry> newLog = _prev_log;
            newLog.insert(newLog.end(), _log.begin(), _log.end());
            tmpLog = &newLog;
        }
        else
        {
            tmpLog = &_log;
        }
        _logFile.open(fullpath.toUtf8().data(), std::ofstream::out | std::ofstream::trunc);

        if (_logFile.good()) // one can never be too sure
        {
            for (int i = 0; i < int(tmpLog->size()); i++)
            {
                _logFile.write(tmpLog->at(i).getLine().toUtf8().data(), tmpLog->at(i).getLine().size());
            }
        }

        _logFile.close();
    }
}

void ConsoleLogger::_announceWrite(ConsoleEntry consoleEntry)
{

    entryDispatcher.fire(consoleEntry);
}

void ConsoleLogger::_readLog(QString path)
{
    // todo, read the file, filter the entries and recombine them if possible etc..
    // then save it all to _log_prev.
    // then set _readFile to true, so the dump will concat our current log to the old and THEN overwrite the whole file
    _prev_log.clear();
    _fileRead = false;
}

QString ConsoleLogger::_getTimeStamp()
{
    time_t rawtime;
    tm * ptm;
    time ( &rawtime );

    //ptm = gmtime ( &rawtime );
    ptm = localtime ( &rawtime );

    QString tmpStr;
    if (ptm->tm_mday < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(ptm->tm_mday));
    tmpStr.push_back('.');
    if (ptm->tm_mon < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(int(ptm->tm_mon) +1));
    tmpStr.push_back('.');
    int year = (ptm->tm_year % 100);
    if (year < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(year));
    tmpStr.push_back(' ');
    if (ptm->tm_hour < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(ptm->tm_hour));
    tmpStr.push_back(':');
    if (ptm->tm_min < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(ptm->tm_min));
    tmpStr.push_back(':');
    if (ptm->tm_sec < 10)
        tmpStr.append(QString("0"));
    tmpStr.append(int2String(ptm->tm_sec));

    return tmpStr;
}