forked from wiglot/LogView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.h
executable file
·139 lines (103 loc) · 2.31 KB
/
logger.h
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
//
// C++ Interface: logpool
//
// Description:
//
//
// Author: Wagner de Melo Reck,,, <wagner@wiglotron>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef LOGGERLOGPOOL_H
#define LOGGERLOGPOOL_H
#include <QObject>
class QFile;
class QTextStream;
namespace Logger {
/**
@author Wagner de Melo Reck,,, <wagner@wiglotron>
*/
enum LogType {LOG, WARNING, FATAL};
class LogMessage:public QObject {
Q_OBJECT
public:
LogMessage(){ }
~LogMessage(){ }
void setMessage ( const QString& theValue ){_message = theValue; }
QString message() const{return _message; }
void setType ( const LogType& theValue ){_type = theValue;}
LogType type() const{return _type;}
private:
QString _message;
LogType _type;
};
class LogPool : public QObject
{
Q_OBJECT
private:
static LogPool * self;
QList <LogMessage*> messages;
LogPool(){ }
~LogPool(){ foreach(LogMessage* msg, messages) delete msg; }
signals:
void newLog(LogMessage*);
void newLog();
public:
inline static LogPool * instance(){
if (LogPool::self == 0)
LogPool::self = new LogPool;
return LogPool::self;
}
static int size(){
return LogPool::instance()->messages.size();
}
static LogMessage * getLog(int index){
if (index < 0)
throw (QString("Index Is Negative."));
if (index >= LogPool::size())
throw (QString("Index Out of bounds."));
return LogPool::instance()->messages[index];
}
static void clearLogs(){
if (LogPool::self != 0){
delete LogPool::self;
LogPool::self = 0;
}
}
static void log(LogType type, QString msg){
LogMessage * l = new LogMessage;
l->setType(type);
l->setMessage(msg);
LogPool::instance()->messages.append(l);
emit LogPool::instance()->newLog();
emit LogPool::instance()->newLog(l);
}
static void warning(QString msg){
LogPool::log(Logger::WARNING, msg);
}
static void log(QString msg){
LogPool::log(Logger::LOG, msg);
}
static void fatal(QString msg){
LogPool::log(Logger::FATAL, msg);
}
};
class LogFile : public QObject{
Q_OBJECT
public:
LogFile(QString fileName = "logFile.log");
~LogFile();
void saveAtEach(int value = 0);
public slots:
void appendLog(LogMessage* log);
private:
QFile * file;
QTextStream * textStream;
int _saveAtEach;
int _stored;
int _actual;
void writeLog(LogMessage * log);
};
}
#endif