NGL  6.5
The NCCA Graphics Library
Logger.cpp
Go to the documentation of this file.
1 #include "Logger.h"
2 #include <cstdarg>
3 #include <vector>
4 //#include <pthread.h>
5 //#include <QtCore/QMutexLocker>
6 #include <boost/format.hpp>
7 
8 
9 namespace ngl
10 {
11 // PIMPL Idiom to make lib cleaner
13 {
14 
15 public:
17  bool m_logFile;
23  char x;
24  std::ofstream m_file;
27  unsigned int m_lineNumberCount;
28  unsigned int m_pad;
30 
31  Impl(const std::string& _fname) noexcept;
32  void write(const std::string& _text) noexcept;
33  void writeLineNumber() noexcept;
34  void writeTimeStamp() noexcept;
35  std::string currentTime() noexcept;
36  void setColour(Colours c) noexcept;
37 };
38 
39 
40 Logger::Impl::Impl(const std::string& _fname) noexcept
41 : m_logFileAndConsole(false),
42  m_logFile(true),
43  m_logConsole(false),
44  m_timeStamp(true),
45  m_lineNumber(true),
46  m_disableColours(false),
47  m_colour(Colours::RESET),
48  m_logfileName(_fname),
49  m_lineNumberCount(0),
50  m_pad(4)
51 {
52 
53  m_file.open(m_logfileName.c_str());
54  if (!m_file.is_open())
55  {
56  std::cerr << "error opening log file for writing\n";
57  exit(EXIT_FAILURE);
58  }
59  Tee tee(std::cout, m_file);
60 
61  m_output.open(tee);
62  m_output.set_auto_close(true);
63 
64  if (!m_output.is_open())
65  {
66  std::cerr << "problem opening log stream tee\n";
67  exit(EXIT_FAILURE);
68  }
69  m_timeString = "%I:%M%p";
70 }
72 {
73  std::string timeStr;
74  time_t rawTime;
75  struct tm* timeinfo;
76  char buffer[80];
77  time(&rawTime);
78  timeStr = ctime(&rawTime);
79  timeinfo = localtime(&rawTime);
80  //without the newline character
81  strftime(buffer, 80, m_timeString.c_str(), timeinfo);
82  return buffer;
83 }
84 
85 
87 {
88  setColour(m_colour);
89  if (m_lineNumber == true)
90  {
91  std::string st = boost::str(boost::format("%%0%dd") % m_pad);
92  std::string t = boost::str(boost::format(st.c_str()) % ++m_lineNumberCount);
93  m_output << t << " ";
94  }
95 }
97 {
98  setColour(m_colour);
99 
100  if (m_timeStamp == true)
101  {
102  m_output << currentTime() << " ";
103  }
104 }
105 
106 void Logger::Impl::write(const std::string& _text) noexcept
107 {
108  setColour(m_colour);
109  m_output << _text;
110 }
111 
112 // from http://stackoverflow.com/questions/3585846/color-text-in-terminal-aplications-in-unix
114 {
115  if (m_disableColours) return;
116 
117  switch (c)
118  {
119  case Colours::CNORMAL: m_output << "\x1B[0m"; break;
120  case Colours::RED: m_output << "\x1B[31m"; break;
121  case Colours::GREEN: m_output << "\x1B[32m"; break;
122  case Colours::YELLOW: m_output << "\x1B[33m"; break;
123  case Colours::BLUE: m_output << "\x1B[34m"; break;
124  case Colours::MAGENTA: m_output << "\x1B[35m"; break;
125  case Colours::CYAN: m_output << "\x1B[36m"; break;
126  case Colours::WHITE: m_output << "\x1B[37m"; break;
127  case Colours::RESET: m_output << "\033[0m"; break;
128  }
129 }
130 
131 
132 Logger::Logger() noexcept
133 : m_impl(new Logger::Impl("output.log"))
134 {
135  m_impl->setColour(Colours::BLUE);
136  m_impl->m_output << "Logger started " << m_impl->currentTime() << "\n";
137  m_impl->setColour(Colours::RESET);
138 }
139 
140 Logger::Logger(const std::string& _fname) noexcept
141 : m_impl(new Logger::Impl(_fname))
142 {
143  m_impl->setColour(Colours::BLUE);
144  m_impl->m_output << "Logger started " << m_impl->currentTime() << "\n";
145  m_impl->setColour(Colours::RESET);
146 }
147 
149 {
150  m_impl->setColour(Colours::RESET);
151  m_impl->m_output << "\n";
152  m_impl->m_output.flush();
153  m_impl->m_output.close();
154 }
155 
156 void Logger::close() noexcept
157 {
158  m_impl->setColour(Colours::RESET);
159  m_impl->m_output << "\n";
160  m_impl->m_output.flush();
161  m_impl->m_output.close();
162 }
163 
164 
165 void Logger::logMessage(const char* fmt, ...) noexcept
166 {
167  // create a mutux to stop other threads accessing
168  // QMutex m;
169  // the locker will auto unlock when out of scope
170  //QMutexLocker locker(&m);
171 
172  // pthread_mutex_lock (&m_impl->m_mutex);
173  m_impl->writeLineNumber();
174  m_impl->writeTimeStamp();
175  char buffer[1024];
176  va_list args;
177  va_start(args, fmt);
178  vsprintf(buffer, fmt, args);
179  std::string text = buffer;
180  va_end(args);
181  m_impl->write(text);
182  fflush(stdout);
183  // pthread_mutex_unlock(&m_impl->m_mutex);
184 }
185 
186 void Logger::logError(const char* fmt, ...) noexcept
187 {
188  // create a mutux to stop other threads accessing
189  // QMutex m;
190  // the locker will auto unlock when out of scope
191  // QMutexLocker locker(&m);
192 
193  // pthread_mutex_lock (&m_impl->m_mutex);
194  m_impl->writeLineNumber();
195  m_impl->writeTimeStamp();
196  char buffer[1024];
197  va_list args;
198  va_start(args, fmt);
199  vsprintf(buffer, fmt, args);
200  m_impl->setColour(Colours::RED);
201  m_impl->m_output << "[ERROR] ";
202  std::string text = buffer;
203  va_end(args);
204  m_impl->write(text);
205  fflush(stdout);
206  // pthread_mutex_unlock(&m_impl->m_mutex);
207 }
208 
209 void Logger::logWarning(const char* fmt...) noexcept
210 {
211  // pthread_mutex_lock (&m_impl->m_mutex);
212  // create a mutux to stop other threads accessing
213  // QMutex m;
214  // the locker will auto unlock when out of scope
215  // QMutexLocker locker(&m);
216  m_impl->writeLineNumber();
217  m_impl->writeTimeStamp();
218  char buffer[1024];
219  va_list args;
220  va_start(args, fmt);
221  vsprintf(buffer, fmt, args);
222  m_impl->setColour(Colours::GREEN);
223  m_impl->m_output << "[Warning] ";
224  std::string text = buffer;
225  va_end(args);
226  m_impl->write(text);
227  fflush(stdout);
228  // pthread_mutex_unlock(&m_impl->m_mutex);
229 }
230 
231 
232 void Logger::enableLogToFile() noexcept
233 {
234  m_impl->m_logFile = true;
235 }
237 {
238  m_impl->m_logFile = false;
239 }
241 {
242  m_impl->m_logConsole = true;
243 }
245 {
246  m_impl->m_logConsole = false;
247 }
249 {
250  m_impl->m_logConsole = true;
251  m_impl->m_logFile = true;
252 }
254 {
255  m_impl->m_logConsole = false;
256  m_impl->m_logFile = false;
257 }
258 void Logger::setLogFile(const std::string& _fname) noexcept
259 {
260  // close the file
261  m_impl->m_output.flush();
262  m_impl->m_output.close();
263 
264  m_impl->m_file.close();
265  m_impl->m_logfileName = _fname;
266  m_impl->m_file.open(m_impl->m_logfileName.c_str());
267  if (!m_impl->m_file.is_open())
268  {
269  std::cerr << "error opening log file for writing\n";
270  exit(EXIT_FAILURE);
271  }
272  Tee tee(std::cout, m_impl->m_file);
273 
274  m_impl->m_output.open(tee);
275  m_impl->m_output.set_auto_close(true);
276 
277  if (!m_impl->m_output.is_open())
278  {
279  std::cerr << "problem opening log stream tee\n";
280  exit(EXIT_FAILURE);
281  }
282 }
283 void Logger::setColour(Colours _c) noexcept
284 {
285  m_impl->m_colour = _c;
286 }
288 {
289  m_impl->m_lineNumber = true;
290 }
292 {
293  m_impl->m_lineNumber = false;
294 }
295 void Logger::enableTimeStamp() noexcept
296 {
297  m_impl->m_timeStamp = true;
298 }
300 {
301  m_impl->m_timeStamp = false;
302 }
303 
304 void Logger::disableColours() noexcept
305 {
306  m_impl->m_disableColours = true;
307 }
308 
309 void Logger::enableColours() noexcept
310 {
311  m_impl->m_disableColours = false;
312 }
313 
314 void Logger::setLineNumberPad(unsigned int _i) noexcept
315 {
316  m_impl->m_pad = _i;
317 }
318 
319 boost::iostreams::stream<Logger::Tee>& Logger::cout() noexcept
320 {
321  return m_impl->m_output;
322 }
323 
324 //Fri Nov 21 12:20:09 2014
326 {
327  switch (_f)
328  {
329  case TimeFormat::TIME: m_impl->m_timeString = "%I:%M%p"; break;
330  case TimeFormat::TIMEDATE: m_impl->m_timeString = "%R %D"; break;
331  case TimeFormat::TIMEDATEDAY: m_impl->m_timeString = "%c"; break;
332  }
333 }
334 }
void logWarning(const char *fmt,...) noexcept
Definition: Logger.cpp:209
TeeStream m_output
Definition: Logger.cpp:29
unsigned int m_pad
Definition: Logger.cpp:28
void setColour(Colours _c) noexcept
Definition: Logger.cpp:283
std::string m_logfileName
Definition: Logger.cpp:26
std::unique_ptr< Impl > m_impl
Definition: Logger.h:54
void writeTimeStamp() noexcept
Definition: Logger.cpp:96
void logMessage(const char *fmt,...) noexcept
Definition: Logger.cpp:165
std::string currentTime() noexcept
Definition: Logger.cpp:71
const GLfloat * c
Definition: glew.h:16629
void writeLineNumber() noexcept
Definition: Logger.cpp:86
void enableColours() noexcept
Definition: Logger.cpp:309
boost::iostreams::tee_device< std::ostream, std::ofstream > Tee
Definition: Logger.h:45
void close() noexcept
Definition: Logger.cpp:156
bool m_disableColours
Definition: Logger.cpp:21
Definition: format.h:316
boost::iostreams::stream< Logger::Tee > & cout() noexcept
Definition: Logger.cpp:319
GLdouble GLdouble t
Definition: glew.h:1401
TimeFormat
Definition: Logger.h:17
boost::iostreams::stream< Tee > TeeStream
Definition: Logger.h:46
implementation files for RibExport class
Definition: AABB.cpp:22
void enableTimeStamp() noexcept
Definition: Logger.cpp:295
void enableLogToFileAndConsole() noexcept
Definition: Logger.cpp:248
std::ofstream m_file
Definition: Logger.cpp:24
Logger() noexcept
Definition: Logger.cpp:132
void logError(const char *fmt,...) noexcept
Definition: Logger.cpp:186
void setColour(Colours c) noexcept
Definition: Logger.cpp:113
void setTimeFormat(TimeFormat _f) noexcept
Definition: Logger.cpp:325
GLuint buffer
Definition: glew.h:1683
Impl(const std::string &_fname) noexcept
Definition: Logger.cpp:40
void write(const std::string &_text) noexcept
Definition: Logger.cpp:106
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1257
void disableLogToFileAndConsole() noexcept
Definition: Logger.cpp:253
unsigned int m_lineNumberCount
Definition: Logger.cpp:27
Colours
Definition: Logger.h:16
void disableLineNumbers() noexcept
Definition: Logger.cpp:291
void disableTimeStamp() noexcept
Definition: Logger.cpp:299
void disableLogToConsole() noexcept
Definition: Logger.cpp:244
void setLineNumberPad(unsigned int i) noexcept
Definition: Logger.cpp:314
void setLogFile(const std::string &_fname) noexcept
Definition: Logger.cpp:258
void disableLogToFile() noexcept
Definition: Logger.cpp:236
Definition: format.cc:82
std::string m_timeString
Definition: Logger.cpp:25
Colours m_colour
Definition: Logger.cpp:22
void disableColours() noexcept
Definition: Logger.cpp:304
void enableLogToConsole() noexcept
Definition: Logger.cpp:240
bool m_logFileAndConsole
Definition: Logger.cpp:16
void enableLineNumbers() noexcept
Definition: Logger.cpp:287
void enableLogToFile() noexcept
Definition: Logger.cpp:232
GLsizei const GLchar *const * string
Definition: glew.h:1847