Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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