Fawkes API  Fawkes Development Version
cache.h
1 
2 /***************************************************************************
3  * cache.h - Fawkes cache logger
4  *
5  * Created: Wed Feb 11 22:54:23 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
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. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_LOGGING_CACHE_H_
25 #define __UTILS_LOGGING_CACHE_H_
26 
27 #include <logging/logger.h>
28 #include <ctime>
29 
30 #include <string>
31 #include <list>
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 class Mutex;
39 
40 class CacheLogger : public Logger
41 {
42  public:
43  CacheLogger(unsigned int num_entries = 20, LogLevel log_level = LL_DEBUG);
44  virtual ~CacheLogger();
45 
46  virtual void log_debug(const char *component, const char *format, ...);
47  virtual void log_info(const char *component, const char *format, ...);
48  virtual void log_warn(const char *component, const char *format, ...);
49  virtual void log_error(const char *component, const char *format, ...);
50 
51  virtual void vlog_debug(const char *component, const char *format, va_list va);
52  virtual void vlog_info(const char *component, const char *format, va_list va);
53  virtual void vlog_warn(const char *component, const char *format, va_list va);
54  virtual void vlog_error(const char *component, const char *format, va_list va);
55 
56  virtual void log_debug(const char *component, Exception &e);
57  virtual void log_info(const char *component, Exception &e);
58  virtual void log_warn(const char *component, Exception &e);
59  virtual void log_error(const char *component, Exception &e);
60 
61  virtual void tlog_debug(struct timeval *t, const char *component, const char *format, ...);
62  virtual void tlog_info(struct timeval *t, const char *component, const char *format, ...);
63  virtual void tlog_warn(struct timeval *t, const char *component, const char *format, ...);
64  virtual void tlog_error(struct timeval *t, const char *component, const char *format, ...);
65 
66  virtual void tlog_debug(struct timeval *t, const char *component, Exception &e);
67  virtual void tlog_info(struct timeval *t, const char *component, Exception &e);
68  virtual void tlog_warn(struct timeval *t, const char *component, Exception &e);
69  virtual void tlog_error(struct timeval *t, const char *component, Exception &e);
70 
71  virtual void vtlog_debug(struct timeval *t, const char *component,
72  const char *format, va_list va);
73  virtual void vtlog_info(struct timeval *t, const char *component,
74  const char *format, va_list va);
75  virtual void vtlog_warn(struct timeval *t, const char *component,
76  const char *format, va_list va);
77  virtual void vtlog_error(struct timeval *t, const char *component,
78  const char *format, va_list va);
79 
80  /** Cache entry struct. */
81  typedef struct {
82  LogLevel log_level; /**< log level */
83  std::string component; /**< component */
84  struct timeval time; /**< raw time */
85  std::string timestr; /**< Time encoded as string */
86  std::string message; /**< Message */
87  } CacheEntry;
88 
89  /** Get messages.
90  * @return reference to message list
91  */
92  std::list<CacheEntry> & get_messages();
93 
94  /** Clear messages. */
95  void clear();
96 
97  unsigned int size() const;
98  void set_size(unsigned int new_size);
99 
100  void lock();
101  void unlock();
102 
103  private:
104  void push_message(LogLevel ll, const char *component, const char *format,
105  va_list va);
106  void push_message(LogLevel ll, const char *component, Exception &e);
107  void tlog_push_message(LogLevel ll, struct timeval *t, const char *component,
108  const char *format, va_list va);
109  void tlog_push_message(LogLevel ll, struct timeval *t, const char *component,
110  Exception &);
111 
112 
113  private:
114  struct ::tm *now_s;
115  Mutex *mutex;
116 
117  std::list<CacheEntry> __messages;
118  unsigned int __num_entries;
119  unsigned int __max_num_entries;
120 
121 };
122 
123 
124 } // end namespace fawkes
125 
126 #endif
LogLevel
Log level.
Definition: logger.h:45
void lock()
Lock cache logger, no new messages can be added.
Definition: cache.cpp:113
LogLevel log_level
Minimum log level.
Definition: logger.h:128
virtual void vlog_info(const char *component, const char *format, va_list va)
Log informational message.
Definition: cache.cpp:206
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Log debug message for specific time.
Definition: cache.cpp:413
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Log warning message for specific time.
Definition: cache.cpp:371
void clear()
Clear messages.
Definition: cache.cpp:75
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Log informational message for specific time.
Definition: cache.cpp:419
CacheLogger(unsigned int num_entries=20, LogLevel log_level=LL_DEBUG)
Constructor.
Definition: cache.cpp:51
Fawkes library namespace.
std::string timestr
Time encoded as string.
Definition: cache.h:85
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Log error message for specific time.
Definition: cache.cpp:431
Logging Cache.
Definition: cache.h:40
virtual ~CacheLogger()
Destructor.
Definition: cache.cpp:62
LogLevel log_level
log level
Definition: cache.h:82
virtual void log_debug(const char *component, const char *format,...)
Log debug message.
Definition: cache.cpp:224
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Log informational message for specific time.
Definition: cache.cpp:362
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Log debug message for specific time.
Definition: cache.cpp:353
virtual void vlog_error(const char *component, const char *format, va_list va)
Log error message.
Definition: cache.cpp:218
void unlock()
Unlock cache logger.
Definition: cache.cpp:120
Base class for exceptions in Fawkes.
Definition: exception.h:36
void set_size(unsigned int new_size)
Set maximum number of log entries in cache.
Definition: cache.cpp:98
virtual void log_info(const char *component, const char *format,...)
Log informational message.
Definition: cache.cpp:233
debug output, relevant only when tracking down problems
Definition: logger.h:46
std::string message
Message.
Definition: cache.h:86
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Log error message for specific time.
Definition: cache.cpp:380
unsigned int size() const
Get maximum number of log entries in cache.
Definition: cache.cpp:88
Cache entry struct.
Definition: cache.h:81
virtual void log_error(const char *component, const char *format,...)
Log error message.
Definition: cache.cpp:251
virtual void vlog_debug(const char *component, const char *format, va_list va)
Log debug message.
Definition: cache.cpp:200
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: cache.cpp:242
std::string component
component
Definition: cache.h:83
virtual void vlog_warn(const char *component, const char *format, va_list va)
Log warning message.
Definition: cache.cpp:212
Mutex mutual exclusion lock.
Definition: mutex.h:32
std::list< CacheEntry > & get_messages()
Get messages.
Definition: cache.cpp:69
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Log warning message for specific time.
Definition: cache.cpp:425
Interface for logging.
Definition: logger.h:34