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 
29 #include <ctime>
30 #include <list>
31 #include <string>
32 
33 namespace fawkes {
34 
35 class Mutex;
36 
37 class CacheLogger : public Logger
38 {
39 public:
40  CacheLogger(unsigned int num_entries = 20, LogLevel log_level = LL_DEBUG);
41  virtual ~CacheLogger();
42 
43  virtual void log_debug(const char *component, const char *format, ...);
44  virtual void log_info(const char *component, const char *format, ...);
45  virtual void log_warn(const char *component, const char *format, ...);
46  virtual void log_error(const char *component, const char *format, ...);
47 
48  virtual void vlog_debug(const char *component, const char *format, va_list va);
49  virtual void vlog_info(const char *component, const char *format, va_list va);
50  virtual void vlog_warn(const char *component, const char *format, va_list va);
51  virtual void vlog_error(const char *component, const char *format, va_list va);
52 
53  virtual void log_debug(const char *component, Exception &e);
54  virtual void log_info(const char *component, Exception &e);
55  virtual void log_warn(const char *component, Exception &e);
56  virtual void log_error(const char *component, Exception &e);
57 
58  virtual void tlog_debug(struct timeval *t, const char *component, const char *format, ...);
59  virtual void tlog_info(struct timeval *t, const char *component, const char *format, ...);
60  virtual void tlog_warn(struct timeval *t, const char *component, const char *format, ...);
61  virtual void tlog_error(struct timeval *t, const char *component, const char *format, ...);
62 
63  virtual void tlog_debug(struct timeval *t, const char *component, Exception &e);
64  virtual void tlog_info(struct timeval *t, const char *component, Exception &e);
65  virtual void tlog_warn(struct timeval *t, const char *component, Exception &e);
66  virtual void tlog_error(struct timeval *t, const char *component, Exception &e);
67 
68  virtual void
69  vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va);
70  virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va);
71  virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va);
72  virtual void
73  vtlog_error(struct timeval *t, const char *component, const char *format, va_list va);
74 
75  /** Cache entry struct. */
76  typedef struct
77  {
78  LogLevel log_level; /**< log level */
79  std::string component; /**< component */
80  struct timeval time; /**< raw time */
81  std::string timestr; /**< Time encoded as string */
82  std::string message; /**< Message */
83  } CacheEntry;
84 
85  /** Get messages.
86  * @return reference to message list
87  */
88  std::list<CacheEntry> &get_messages();
89 
90  /** Clear messages. */
91  void clear();
92 
93  unsigned int size() const;
94  void set_size(unsigned int new_size);
95 
96  void lock();
97  void unlock();
98 
99 private:
100  void push_message(LogLevel ll, const char *component, const char *format, va_list va);
101  void push_message(LogLevel ll, const char *component, Exception &e);
102  void tlog_push_message(LogLevel ll,
103  struct timeval *t,
104  const char * component,
105  const char * format,
106  va_list va);
107  void tlog_push_message(LogLevel ll, struct timeval *t, const char *component, Exception &);
108 
109 private:
110  struct ::tm *now_s;
111  Mutex * mutex;
112 
113  std::list<CacheEntry> messages_;
114  unsigned int num_entries_;
115  unsigned int max_num_entries_;
116 };
117 
118 } // end namespace fawkes
119 
120 #endif
fawkes::CacheLogger::tlog_warn
virtual void tlog_warn(struct timeval *t, const char *component, const char *format,...)
Definition: cache.cpp:393
fawkes::CacheLogger::vtlog_info
virtual void vtlog_info(struct timeval *t, const char *component, const char *format, va_list va)
Definition: cache.cpp:441
fawkes::Logger::log_level
LogLevel log_level
Minimum log level.
Definition: logger.h:126
fawkes::Mutex
Definition: mutex.h:38
fawkes::CacheLogger::clear
void clear()
Clear messages.
Definition: cache.cpp:77
fawkes::CacheLogger::vtlog_debug
virtual void vtlog_debug(struct timeval *t, const char *component, const char *format, va_list va)
Definition: cache.cpp:435
fawkes::CacheLogger::vlog_warn
virtual void vlog_warn(const char *component, const char *format, va_list va)
Definition: cache.cpp:221
fawkes::CacheLogger::size
unsigned int size() const
Get maximum number of log entries in cache.
Definition: cache.cpp:89
fawkes::CacheLogger::vlog_info
virtual void vlog_info(const char *component, const char *format, va_list va)
Definition: cache.cpp:215
fawkes::CacheLogger::log_debug
virtual void log_debug(const char *component, const char *format,...)
Definition: cache.cpp:233
fawkes::CacheLogger::tlog_debug
virtual void tlog_debug(struct timeval *t, const char *component, const char *format,...)
Definition: cache.cpp:375
fawkes::CacheLogger::CacheEntry
Cache entry struct.
Definition: cache.h:82
fawkes::Logger::LL_DEBUG
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
fawkes::CacheLogger::~CacheLogger
virtual ~CacheLogger()
Destructor.
Definition: cache.cpp:64
fawkes::CacheLogger::tlog_error
virtual void tlog_error(struct timeval *t, const char *component, const char *format,...)
Definition: cache.cpp:402
fawkes::CacheLogger::vtlog_warn
virtual void vtlog_warn(struct timeval *t, const char *component, const char *format, va_list va)
Definition: cache.cpp:447
fawkes
fawkes::CacheLogger::tlog_info
virtual void tlog_info(struct timeval *t, const char *component, const char *format,...)
Definition: cache.cpp:384
fawkes::CacheLogger::vtlog_error
virtual void vtlog_error(struct timeval *t, const char *component, const char *format, va_list va)
Definition: cache.cpp:453
fawkes::CacheLogger::log_error
virtual void log_error(const char *component, const char *format,...)
Definition: cache.cpp:260
fawkes::CacheLogger::lock
void lock()
Lock cache logger, no new messages can be added.
Definition: cache.cpp:112
fawkes::CacheLogger::set_size
void set_size(unsigned int new_size)
Set maximum number of log entries in cache.
Definition: cache.cpp:98
fawkes::CacheLogger::vlog_debug
virtual void vlog_debug(const char *component, const char *format, va_list va)
Definition: cache.cpp:209
fawkes::Logger::LogLevel
LogLevel
Log level.
Definition: logger.h:51
fawkes::CacheLogger::vlog_error
virtual void vlog_error(const char *component, const char *format, va_list va)
Definition: cache.cpp:227
fawkes::CacheLogger::CacheLogger
CacheLogger(unsigned int num_entries=20, LogLevel log_level=LL_DEBUG)
Constructor.
Definition: cache.cpp:54
fawkes::CacheLogger::log_warn
virtual void log_warn(const char *component, const char *format,...)
Definition: cache.cpp:251
fawkes::CacheLogger::log_info
virtual void log_info(const char *component, const char *format,...)
Definition: cache.cpp:242
fawkes::CacheLogger::get_messages
std::list< CacheEntry > & get_messages()
Get messages.
Definition: cache.cpp:71
fawkes::CacheLogger::unlock
void unlock()
Unlock cache logger.
Definition: cache.cpp:119
fawkes::Exception
Definition: exception.h:41