Fawkes API  Fawkes Development Version
file.cpp
1 
2 /***************************************************************************
3  * file.cpp - Fawkes file logger
4  *
5  * Created: Tue Jan 16 16:56:49 2007
6  * Copyright 2006-2007 Tim Niemueller [www.niemueller.de]
7  * 2007 Daniel Beck
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <logging/file.h>
26 #include <utils/system/file.h>
27 
28 #include <core/threading/mutex.h>
29 
30 #include <cstdlib>
31 #include <sys/time.h>
32 #include <sys/stat.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <cerrno>
36 
37 namespace fawkes {
38 
39 /** @class FileLogger <logging/file.h>
40  * Interface for logging to a specified file.
41  * The FileLogger will pipe all output into the given file. The
42  * output will be prepended by a single character which determines the
43  * type of output (E for error, W for warning, etc.).
44  *
45  */
46 
47 /** Constructor.
48  * @param filename the name of the log-file
49  * @param log_level minimum log level
50  */
51 FileLogger::FileLogger(const char* filename, LogLevel log_level)
52  : Logger(log_level)
53 {
54  int fd = open(filename, O_RDWR | O_CREAT | O_APPEND,
55  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
56  if (fd == -1) {
57  throw Exception(errno, "Failed to open log file %s", filename);
58  }
59  log_file = fdopen(fd, "a");
60 
61  now_s = (struct tm *)malloc(sizeof(struct tm));
62 
63  mutex = new Mutex();
64 }
65 
66 
67 /** Destructor. */
69 {
70  free(now_s);
71  fclose(log_file);
72  delete mutex;
73 }
74 
75 
76 void
77 FileLogger::log_debug(const char *component, const char *format, ...)
78 {
79  va_list arg;
80  va_start(arg, format);
81  vlog_debug(component, format, arg);
82  va_end(arg);
83 }
84 
85 
86 void
87 FileLogger::log_info(const char *component, const char *format, ...)
88 {
89  va_list arg;
90  va_start(arg, format);
91  vlog_info(component, format, arg);
92  va_end(arg);
93 }
94 
95 
96 void
97 FileLogger::log_warn(const char *component, const char *format, ...)
98 {
99  va_list arg;
100  va_start(arg, format);
101  vlog_warn(component, format, arg);
102  va_end(arg);
103 }
104 
105 
106 void
107 FileLogger::log_error(const char *component, const char *format, ...)
108 {
109  va_list arg;
110  va_start(arg, format);
111  vlog_error(component, format, arg);
112  va_end(arg);
113 }
114 
115 
116 void
117 FileLogger::log_debug(const char *component, Exception &e)
118 {
119  if ( log_level <= LL_DEBUG ) {
120  struct timeval now;
121  gettimeofday(&now, NULL);
122  mutex->lock();
123  localtime_r(&now.tv_sec, now_s);
124  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
125  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "D", now_s->tm_hour,
126  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
127  fprintf(log_file, "%s", *i);
128  fprintf(log_file, "\n");
129  }
130  fflush(log_file);
131  mutex->unlock();
132  }
133 }
134 
135 
136 void
137 FileLogger::log_info(const char *component, Exception &e)
138 {
139  if ( log_level <= LL_INFO ) {
140  struct timeval now;
141  gettimeofday(&now, NULL);
142  mutex->lock();
143  localtime_r(&now.tv_sec, now_s);
144  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
145  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "I", now_s->tm_hour,
146  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
147  fprintf(log_file, "%s", *i);
148  fprintf(log_file, "\n");
149  }
150  fflush(log_file);
151  mutex->unlock();
152  }
153 }
154 
155 
156 void
157 FileLogger::log_warn(const char *component, Exception &e)
158 {
159  if ( log_level <= LL_WARN ) {
160  struct timeval now;
161  gettimeofday(&now, NULL);
162  mutex->lock();
163  localtime_r(&now.tv_sec, now_s);
164  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
165  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "W", now_s->tm_hour,
166  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
167  fprintf(log_file, "%s", *i);
168  fprintf(log_file, "\n");
169  }
170  fflush(log_file);
171  mutex->unlock();
172  }
173 }
174 
175 
176 void
177 FileLogger::log_error(const char *component, Exception &e)
178 {
179  if ( log_level <= LL_ERROR ) {
180  struct timeval now;
181  gettimeofday(&now, NULL);
182  mutex->lock();
183  localtime_r(&now.tv_sec, now_s);
184  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
185  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "E", now_s->tm_hour,
186  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
187  fprintf(log_file, "%s", *i);
188  fprintf(log_file, "\n");
189  }
190  fflush(log_file);
191  mutex->unlock();
192  }
193 }
194 
195 
196 void
197 FileLogger::vlog_debug(const char* component, const char* format, va_list va)
198 {
199  if (log_level <= LL_DEBUG ) {
200  struct timeval now;
201  gettimeofday(&now, NULL);
202  mutex->lock();
203  localtime_r(&now.tv_sec, now_s);
204  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "D", now_s->tm_hour,
205  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
206  vfprintf(log_file, format, va);
207  fprintf(log_file, "\n");
208  fflush(log_file);
209  mutex->unlock();
210  }
211 }
212 
213 
214 void
215 FileLogger::vlog_info(const char *component, const char *format, va_list va)
216 {
217  if (log_level <= LL_INFO ) {
218  struct timeval now;
219  gettimeofday(&now, NULL);
220  mutex->lock();
221  localtime_r(&now.tv_sec, now_s);
222  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "I", now_s->tm_hour,
223  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
224  vfprintf(log_file, format, va);
225  fprintf(log_file, "\n");
226  fflush(log_file);
227  mutex->unlock();
228  }
229 }
230 
231 
232 void
233 FileLogger::vlog_warn(const char *component, const char *format, va_list va)
234 {
235  if (log_level <= LL_WARN ) {
236  struct timeval now;
237  gettimeofday(&now, NULL);
238  mutex->lock();
239  localtime_r(&now.tv_sec, now_s);
240  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "W", now_s->tm_hour,
241  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
242  vfprintf(log_file, format, va);
243  fprintf(log_file, "\n");
244  fflush(log_file);
245  mutex->unlock();
246  }
247 }
248 
249 
250 void
251 FileLogger::vlog_error(const char *component, const char *format, va_list va)
252 {
253  if (log_level <= LL_ERROR ) {
254  struct timeval now;
255  gettimeofday(&now, NULL);
256  mutex->lock();
257  localtime_r(&now.tv_sec, now_s);
258  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "E", now_s->tm_hour,
259  now_s->tm_min, now_s->tm_sec, (long)now.tv_usec, component);
260  vfprintf(log_file, format, va);
261  fprintf(log_file, "\n");
262  fflush(log_file);
263  mutex->unlock();
264  }
265 }
266 
267 
268 void
269 FileLogger::tlog_debug(struct timeval *t, const char *component, const char *format, ...)
270 {
271  va_list arg;
272  va_start(arg, format);
273  vtlog_debug(t, component, format, arg);
274  va_end(arg);
275 }
276 
277 
278 void
279 FileLogger::tlog_info(struct timeval *t, const char *component, const char *format, ...)
280 {
281  va_list arg;
282  va_start(arg, format);
283  vtlog_info(t, component, format, arg);
284  va_end(arg);
285 }
286 
287 
288 void
289 FileLogger::tlog_warn(struct timeval *t, const char *component, const char *format, ...)
290 {
291  va_list arg;
292  va_start(arg, format);
293  vtlog_warn(t, component, format, arg);
294  va_end(arg);
295 }
296 
297 
298 void
299 FileLogger::tlog_error(struct timeval *t, const char *component, const char *format, ...)
300 {
301  va_list arg;
302  va_start(arg, format);
303  vtlog_error(t, component, format, arg);
304  va_end(arg);
305 }
306 
307 
308 void
309 FileLogger::tlog_debug(struct timeval *t, const char *component, Exception &e)
310 {
311  if ( log_level <= LL_DEBUG ) {
312  mutex->lock();
313  localtime_r(&t->tv_sec, now_s);
314  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
315  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "D", now_s->tm_hour,
316  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
317  fprintf(log_file, "%s", *i);
318  fprintf(log_file, "\n");
319  }
320  fflush(log_file);
321  mutex->unlock();
322  }
323 }
324 
325 
326 void
327 FileLogger::tlog_info(struct timeval *t, const char *component, Exception &e)
328 {
329  if ( log_level <= LL_INFO ) {
330  mutex->lock();
331  localtime_r(&t->tv_sec, now_s);
332  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
333  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "I", now_s->tm_hour,
334  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
335  fprintf(log_file, "%s", *i);
336  fprintf(log_file, "\n");
337  }
338  fflush(log_file);
339  mutex->unlock();
340  }
341 }
342 
343 
344 void
345 FileLogger::tlog_warn(struct timeval *t, const char *component, Exception &e)
346 {
347  if ( log_level <= LL_WARN ) {
348  mutex->lock();
349  localtime_r(&t->tv_sec, now_s);
350  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
351  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "W", now_s->tm_hour,
352  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
353  fprintf(log_file, "%s", *i);
354  fprintf(log_file, "\n");
355  }
356  fflush(log_file);
357  mutex->unlock();
358  }
359 }
360 
361 
362 void
363 FileLogger::tlog_error(struct timeval *t, const char *component, Exception &e)
364 {
365  if ( log_level <= LL_ERROR ) {
366  mutex->lock();
367  localtime_r(&t->tv_sec, now_s);
368  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
369  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s [EXCEPTION]: ", "E", now_s->tm_hour,
370  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
371  fprintf(log_file, "%s", *i);
372  fprintf(log_file, "\n");
373  }
374  fflush(log_file);
375  mutex->unlock();
376  }
377 }
378 
379 
380 void
381 FileLogger::vtlog_debug(struct timeval *t, const char* component, const char* format, va_list va)
382 {
383  if (log_level <= LL_DEBUG ) {
384  mutex->lock();
385  localtime_r(&t->tv_sec, now_s);
386  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "D", now_s->tm_hour,
387  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
388  vfprintf(log_file, format, va);
389  fprintf(log_file, "\n");
390  fflush(log_file);
391  mutex->unlock();
392  }
393 }
394 
395 
396 void
397 FileLogger::vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
398 {
399  if (log_level <= LL_INFO ) {
400  mutex->lock();
401  localtime_r(&t->tv_sec, now_s);
402  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "I", now_s->tm_hour,
403  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
404  vfprintf(log_file, format, va);
405  fprintf(log_file, "\n");
406  fflush(log_file);
407  mutex->unlock();
408  }
409 }
410 
411 
412 void
413 FileLogger::vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
414 {
415  if (log_level <= LL_WARN ) {
416  mutex->lock();
417  localtime_r(&t->tv_sec, now_s);
418  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "W", now_s->tm_hour,
419  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
420  vfprintf(log_file, format, va);
421  fprintf(log_file, "\n");
422  fflush(log_file);
423  mutex->unlock();
424  }
425 }
426 
427 
428 void
429 FileLogger::vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
430 {
431  if (log_level <= LL_ERROR ) {
432  mutex->lock();
433  localtime_r(&t->tv_sec, now_s);
434  fprintf(log_file, "%s %02d:%02d:%02d.%06ld %s: ", "E", now_s->tm_hour,
435  now_s->tm_min, now_s->tm_sec, (long)t->tv_usec, component);
436  vfprintf(log_file, format, va);
437  fprintf(log_file, "\n");
438  fflush(log_file);
439  mutex->unlock();
440  }
441 }
442 
443 
444 } // end namespace fawkes
LogLevel
Log level.
Definition: logger.h:45
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Log informational message for specific time.
Definition: file.cpp:279
LogLevel log_level
Minimum log level.
Definition: logger.h:128
informational output about normal procedures
Definition: logger.h:47
Fawkes library namespace.
void unlock()
Unlock the mutex.
Definition: mutex.cpp:135
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: file.cpp:107
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Log debug message for specific time.
Definition: file.cpp:269
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:48
FileLogger(const char *filename, LogLevel min_level=LL_DEBUG)
Constructor.
Definition: file.cpp:51
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Log debug message for specific time.
Definition: file.cpp:381
Message iterator for exceptions.
Definition: exception.h:72
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:51
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: file.cpp:197
iterator end()
Get end iterator for messages.
Definition: exception.cpp:717
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: file.cpp:215
virtual ~FileLogger()
Destructor.
Definition: file.cpp:68
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Log warning message for specific time.
Definition: file.cpp:289
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: file.cpp:233
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Log error message for specific time.
Definition: file.cpp:299
iterator begin()
Get iterator for messages.
Definition: exception.cpp:700
debug output, relevant only when tracking down problems
Definition: logger.h:46
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: file.cpp:97
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: file.cpp:251
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Log informational message for specific time.
Definition: file.cpp:397
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Log warning message for specific time.
Definition: file.cpp:413
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Log error message for specific time.
Definition: file.cpp:429
void lock()
Lock this mutex.
Definition: mutex.cpp:89
Mutex mutual exclusion lock.
Definition: mutex.h:32
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: file.cpp:77
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: file.cpp:87
Interface for logging.
Definition: logger.h:34