arcslog.cpp
1 /*
2  name: lib/arcslog.cpp
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 #include <arcs/arcslog.h>
30 #include <iostream>
31 
32 ARCSLogEvent::ARCSLogEvent(QString src, Level lvl, QString msg)
33 {
34  source = src;
35  level = lvl;
36  message = msg;
37 }
38 
39 
40 QDataStream &operator<< (QDataStream &out, const ARCSLogEvent & event)
41 {
42  out << event.getSource() << event.getLevel() << event.getMessage() << "\n" ;
43  return out;
44 }
45 
46 QDataStream &operator>> (QDataStream &in, ARCSLogEvent & event)
47 {
48  QString src;
49  QString msg;
50  int lvl;
51  in >> src >> lvl >> msg;
52  event=ARCSLogEvent(src,(ARCSLogEvent::Level)lvl,msg);
53  return in;
54 }
55 
56 /*********************************************************************************/
57 // class ARCSColor Log
58 /*********************************************************************************/
59 void ARCSColorLog::log(std::ostream &) const
60 {
61  // empty implementation by default.
62 }
63 void ARCSColorLog::log(QTextStream &) const
64 {
65  // empty implementation by default.
66 }
67 
68 
69 
70 #ifdef WIN32
71 WORD ARCSWindowsColorLog::getNormalAttribute()
72 {
73  CONSOLE_SCREEN_BUFFER_INFO csbi;
74  GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
75  return csbi.wAttributes;
76 }
77 
78 WORD ARCSWindowsColorLog::normalAttribute= ARCSWindowsColorLog::getNormalAttribute();
79 WORD ARCSWindowsColorLog::attributes[] = { normalAttribute ,
80  FOREGROUND_GREEN|FOREGROUND_INTENSITY,
81  FOREGROUND_BLUE|FOREGROUND_INTENSITY,
82  FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY,
83  FOREGROUND_RED|FOREGROUND_INTENSITY };
84 
85 void ARCSWindowsColorLog::log(std::ostream& os) const
86 {
87  HANDLE hd;
88  if (os == std::cout)
89  hd = GetStdHandle(STD_OUTPUT_HANDLE);
90  else
91  hd = GetStdHandle(STD_ERROR_HANDLE);
92  SetConsoleTextAttribute(hd,attributes[getColor()]);
93 }
94 
95 #define ARCSConsoleColorLog ARCSWindowsColorLog
96 
97 #else
98 const char* ARCSUnixColorLog::normalAttribute="\033[0m";
99 const char* ARCSUnixColorLog::attributes[] = { normalAttribute, "\033[32;1m", "\033[34;1m", "\033[33;1m", "\033[31;1m" };
100 
101 void ARCSUnixColorLog::log(std::ostream& os) const {
102  os << attributes[getColor()];
103 }
104 
105 #define ARCSConsoleColorLog ARCSUnixColorLog
106 
107 #endif
108 
109 const char* ARCSHTMLColorLog::normalAttribute="</font>";
110 const char* ARCSHTMLColorLog::attributes[] = { normalAttribute, "<font color=\"green\">", "<font color=\"blue\">", "<font color=\"orange\">",
111  "<font color=\"red\">"};
112 
113 void ARCSHTMLColorLog::log(std::ostream& os) const {
114  os << attributes[getColor()];
115 }
116 
117 void ARCSHTMLColorLog::log(QTextStream& ts) const {
118  ts << attributes[getColor()];
119 }
120 
121 std::ostream& operator<<(std::ostream& os, const ARCSColorLog& cl)
122 {
123  // small hack here because of internal ostream changes
124  if (os.rdbuf() != std::cout.rdbuf() && os.rdbuf() != std::cerr.rdbuf())
125  return os;
126 
127  cl.log(os);
128  return os;
129 }
130 
131 QTextStream& operator<<(QTextStream& ts, const ARCSColorLog& cl)
132 {
133  cl.log(ts);
134  return ts;
135 }
136 
137 
138 
139 /*********************************************************************************/
140 
141 ARCSLog* ARCSLog::instance = 0; //new ARCSLog();
142 
143 const char* ARCSLog::interp[] = { "NOR", "INF", "WRN" , "ERR" , "CRT" };
144 
145 ARCSLog::ARCSLog()
146 {
147  display = ARCSLogEvent::INFORMATION ;
148  logMode = CONSOLE;
149  levels[0] = 0;
150  refreshMode();
151  textStream = 0;
152 
153 }
154 
155 void ARCSLog::refreshMode()
156 {
157  if (levels[0])
158  for (int i=0; i < 5; i++)
159  delete levels[i];
160 
161  if (logMode == CONSOLE)
162  {
163  levels[0] = new ARCSConsoleColorLog(ARCSColorLog::NORMAL);
164  levels[1] = new ARCSConsoleColorLog(ARCSColorLog::GREEN);
165  levels[2] = new ARCSConsoleColorLog(ARCSColorLog::BLUE);
166  levels[3] = new ARCSConsoleColorLog(ARCSColorLog::YELLOW);
167  levels[4] = new ARCSConsoleColorLog(ARCSColorLog::RED);
168  }
169  if (logMode == HTML)
170  {
171  levels[0] = new ARCSHTMLColorLog(ARCSColorLog::NORMAL);
172  levels[1] = new ARCSHTMLColorLog(ARCSColorLog::GREEN);
173  levels[2] = new ARCSHTMLColorLog(ARCSColorLog::BLUE);
174  levels[3] = new ARCSHTMLColorLog(ARCSColorLog::YELLOW);
175  levels[4] = new ARCSHTMLColorLog(ARCSColorLog::RED);
176  }
177  if (logMode == NONE)
178  {
179  levels[0] = new ARCSColorLog(ARCSColorLog::NORMAL);
180  levels[1] = new ARCSColorLog(ARCSColorLog::GREEN);
181  levels[2] = new ARCSColorLog(ARCSColorLog::BLUE);
182  levels[3] = new ARCSColorLog(ARCSColorLog::YELLOW);
183  levels[4] = new ARCSColorLog(ARCSColorLog::RED);
184  }
185 
186 }
187 
189 {
190  if (!instance)
191  instance=new ARCSLog();
192 
193  return instance;
194 }
195 
196 
197 void ARCSLog::log(ARCSLogEvent evt)
198 {
199  logEvents.append(evt);
200  if (evt.getLevel() >= display)
201  {
202  if (textStream)
203  {
204  QTextStream & t = *textStream ;
205  t << *levels[evt.getLevel()]
206  << "[" << interp[evt.getLevel()] << "!" << evt.getSource() << "] "
207  << *levels[0]
208  << evt.getMessage() << endl;
209  if (logMode == HTML)
210  t << "<br/>";
211  }
212  else
213  {
214 
215  if (evt.getLevel() >= ARCSLogEvent::WARNING)
216  std::cerr << *levels[evt.getLevel()]
217  << "[" << interp[evt.getLevel()] << "!" << qPrintable(evt.getSource()) << "] "
218  << *levels[0]
219  << qPrintable(evt.getMessage()) << std::endl;
220  else
221  std::cout << *levels[evt.getLevel()]
222  << "[" << interp[evt.getLevel()] << "!" << qPrintable(evt.getSource()) << "] "
223  << *levels[0]
224  << qPrintable(evt.getMessage()) << std::endl;
225  }
226  }
227 
228 
229 }
ARCSLogEvent(QString src=QString::null, Level lvl=NONE, QString msg=QString::null)
Constructor of the event.
Definition: arcslog.cpp:32
QString getSource() const
returns the source of the event
Definition: arcslog.h:68
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
static ARCSLog * getInstance()
returns the only instance of the logging system that is available
Definition: arcslog.cpp:188
specialized log colorizer for html output
Definition: arcslog.h:170
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