arcslog.h
1 /*
2  name: include/arcs/arcslog.h
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #ifndef __ARCSLOG_H__
30 #define __ARCSLOG_H__
31 
32 #include <QList>
33 #include <QDataStream>
34 #include <QTextStream>
35 #include <arcs/arcsdll.h>
36 
37 
38 #define ARCS_SOURCE __FILE__":"+QString::number(__LINE__)
39 
48 {
49 public:
51  enum Level {
52  NONE,
57  };
58 
65  ARCSLogEvent(QString src=QString::null, Level lvl=NONE, QString msg=QString::null);
66 
68  QString getSource() const { return source; }
70  Level getLevel() const { return level; }
72  QString getMessage() const{ return message; }
73 
74 private:
75  QString source;
76  Level level;
77  QString message;
78 };
79 
80 
81 QDataStream &operator<< (QDataStream &out, const ARCSLogEvent & event);
82 QDataStream &operator>> (QDataStream &in, ARCSLogEvent & event);
83 
85 class ARCSLogSource :public QString
86 {
87 public:
88  ARCSLogSource(const char* str, int num) :
89  QString(QString(str)+":"+QString::number(num)) {}
90 };
91 
92 
93 #ifdef WIN32
94 #include <windows.h>
95 #endif
96 
97 
98 #ifdef ERROR
99 #undef ERROR
100 #endif
101 
102 /*********************************************************************************/
103 // ARCSColorLog Family
104 /*********************************************************************************/
105 
115 {
116 public:
118  enum Colors { NORMAL=0, GREEN, BLUE, YELLOW, RED};
119  ARCSColorLog(Colors color=NORMAL) { this->color = color; }
120  virtual ~ARCSColorLog() {}
121  friend std::ostream& operator<<(std::ostream& os, const ARCSColorLog& cl);
122  friend QTextStream& operator<<(QTextStream& ts, const ARCSColorLog& cl);
123  //static ARCSColorLog levels[];
124 
125 protected:
126  virtual void log(std::ostream& ) const;
127  virtual void log(QTextStream& ) const;
128  Colors getColor() const { return color; }
129 
130 private:
131  Colors color;
132 };
133 
134 std::ostream& operator<<(std::ostream& os, const ARCSColorLog& cl);
135 QTextStream& operator<<(QTextStream& ts, const ARCSColorLog& cl);
136 
137 #ifdef WIN32
138 class ARCSWindowsColorLog : public ARCSColorLog {
140 public:
141  ARCSWindowsColorLog(Colors color=NORMAL) : ARCSColorLog(color) {}
142 
143 
144 protected:
145  virtual void log(std::ostream& os) const;
146 
147 private:
148  static WORD normalAttribute;
149  static WORD getNormalAttribute();
150  static WORD attributes[];
151 };
152 
153 #else
154 class ARCSUnixColorLog : public ARCSColorLog {
156 public:
157  ARCSUnixColorLog(Colors color=NORMAL) : ARCSColorLog(color) {}
158 
159 protected:
160  virtual void log(std::ostream& os) const;
161 
162 private:
163  static const char* normalAttribute;
164  static const char* attributes[];
165 };
166 #endif
167 
168 
171 public:
172  ARCSHTMLColorLog(Colors color=NORMAL) : ARCSColorLog(color) {}
173 
174 protected:
175  virtual void log(std::ostream& os) const;
176  virtual void log(QTextStream& ts) const;
177 
178 private:
179  static const char* normalAttribute;
180  static const char* attributes[];
181 };
182 
183 /******************************************************************************/
184 // ARCSLog Class
185 /******************************************************************************/
197 class DLL_POINT ARCSLog
198 {
199 private:
200  ARCSLog();
201  static ARCSLog* instance;
202 
203  QList<ARCSLogEvent> logEvents;
204  ARCSLogEvent::Level display;
205  ARCSColorLog* levels[5];
206  QTextStream* textStream;
207 
208  void refreshMode();
209 
210 public:
212  enum LogModes { NONE, CONSOLE, HTML };
214  static ARCSLog* getInstance();
215 
216  void startCapture() {
217  sCapture.clear();
218  tsCapture.setString(&sCapture);
219  setTextStream(&tsCapture);
220  }
221  QString endCapture() { setTextStream(); return sCapture; }
222 
223 
224  void setTextStream(QTextStream* ts=0) {
225  textStream = ts; logMode = HTML; refreshMode(); }
226  void setLogMode(LogModes mode) { logMode = mode; refreshMode(); }
227  void log(ARCSLogEvent evt);
228  static void logInformation(QString source, QString message)
229  {
230  getInstance()->log(ARCSLogEvent(source,ARCSLogEvent::INFORMATION,message));
231  }
232 
233  static void logWarning(QString source, QString message)
234  {
235  getInstance()->log(ARCSLogEvent(source,ARCSLogEvent::WARNING,message));
236  }
237 
238  static void logError(QString source, QString message)
239  {
240  getInstance()->log(ARCSLogEvent(source,ARCSLogEvent::ERROR,message));
241  }
242 
243  static void logCritical(QString source, QString message)
244  {
245  getInstance()->log(ARCSLogEvent(source,ARCSLogEvent::CRITICAL,message));
246  }
247 
248  static const char* interp[];
249  void setDisplayLevel(ARCSLogEvent::Level lvl) { display = lvl; }
250 
251 private:
252  LogModes logMode;
253 
254  QTextStream tsCapture;
255  QString sCapture;
256 
257 };
258 
259 
260 #endif // __ARCSLOG_H__
ARCSLogEvent(QString src=QString::null, Level lvl=NONE, QString msg=QString::null)
Constructor of the event.
Definition: arcslog.cpp:32
not a proper level
Definition: arcslog.h:52
QString getSource() const
returns the source of the event
Definition: arcslog.h:68
specialized log colorizer for Unix
Definition: arcslog.h:155
A special QString subclass.
Definition: arcslog.h:85
General purpose logging class.
Definition: arcslog.h:197
class that describes a log event
Definition: arcslog.h:47
for informational purpose
Definition: arcslog.h:53
Level getLevel() const
returns the criticality level of the event
Definition: arcslog.h:70
QString getMessage() const
returns the message associated to the event
Definition: arcslog.h:72
Colors
< List of colors
Definition: arcslog.h:118
LogModes
logging modes
Definition: arcslog.h:212
specialized log colorizer for html output
Definition: arcslog.h:170
this is a critical error, it should make the engine stop
Definition: arcslog.h:56
this is an error but will not cause troubles to the engine
Definition: arcslog.h:55
generic class for coloring log event
Definition: arcslog.h:114
Level
Level of logging.
Definition: arcslog.h:51
this may be an error
Definition: arcslog.h:54