cxxtools.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003, Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_LOG_CXXTOOLS_H
30 #define CXXTOOLS_LOG_CXXTOOLS_H
31 
32 #include <string>
33 #include <iostream>
34 
35 #define _cxxtools_log_enabled(impl, level) \
36  (getLogger ## impl() != 0 && getLogger ## impl()->isEnabled(::cxxtools::Logger::level))
37 
38 #define _cxxtools_log(impl, level, displaylevel, expr) \
39  do { \
40  ::cxxtools::Logger* _cxxtools_logger = getLogger ## impl(); \
41  if (_cxxtools_logger != 0 && _cxxtools_logger->isEnabled(::cxxtools::Logger::level)) \
42  { \
43  ::cxxtools::LogMessage _cxxtools_logMessage(_cxxtools_logger, displaylevel); \
44  _cxxtools_logMessage.out() << expr; \
45  _cxxtools_logMessage.finish(); \
46  } \
47  } while (false)
48 
49 #define _cxxtools_log_if(impl, level, displaylevel, cond, expr) \
50  do { \
51  ::cxxtools::Logger* _cxxtools_logger = getLogger ## impl(); \
52  if (_cxxtools_logger != 0 && _cxxtools_logger->isEnabled(::cxxtools::Logger::level) && (cond)) \
53  { \
54  ::cxxtools::LogMessage _cxxtools_logMessage(_cxxtools_logger, displaylevel); \
55  _cxxtools_logMessage.out() << expr; \
56  _cxxtools_logMessage.finish(); \
57  } \
58  } while (false)
59 
60 #define log_fatal_enabled() _cxxtools_log_enabled(_default, LOG_FATAL)
61 #define log_error_enabled() _cxxtools_log_enabled(_default, LOG_ERROR)
62 #define log_warn_enabled() _cxxtools_log_enabled(_default, LOG_WARN)
63 #define log_info_enabled() _cxxtools_log_enabled(_default, LOG_INFO)
64 #define log_debug_enabled() _cxxtools_log_enabled(_default, LOG_DEBUG)
65 #define log_fine_enabled() _cxxtools_log_enabled(_default, LOG_FINE)
66 #define log_finer_enabled() _cxxtools_log_enabled(_default, LOG_FINER)
67 #define log_finest_enabled() _cxxtools_log_enabled(_default, LOG_FINEST)
68 #define log_trace_enabled() _cxxtools_log_enabled(_default, LOG_TRACE)
69 
70 #define log_fatal_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_FATAL)
71 #define log_error_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_ERROR)
72 #define log_warn_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_WARN)
73 #define log_info_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_INFO)
74 #define log_debug_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_DEBUG)
75 #define log_fine_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_FINE)
76 #define log_finer_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_FINER)
77 #define log_finest_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_FINEST)
78 #define log_trace_to_enabled(impl) _cxxtools_log_enabled(impl, LOG_TRACE)
79 
80 #define log_fatal(expr) _cxxtools_log(_default, LOG_FATAL, "FATAL", expr)
81 #define log_error(expr) _cxxtools_log(_default, LOG_ERROR, "ERROR", expr)
82 #define log_warn(expr) _cxxtools_log(_default, LOG_WARN, "WARN", expr)
83 #define log_info(expr) _cxxtools_log(_default, LOG_INFO, "INFO", expr)
84 #define log_debug(expr) _cxxtools_log(_default, LOG_DEBUG, "DEBUG", expr)
85 #define log_fine(expr) _cxxtools_log(_default, LOG_FINE, "FINE", expr)
86 #define log_finer(expr) _cxxtools_log(_default, LOG_FINER, "FINER", expr)
87 #define log_finest(expr) _cxxtools_log(_default, LOG_FINEST, "FINEST", expr)
88 
89 #define log_fatal_to(impl, expr) _cxxtools_log(impl, LOG_FATAL, "FATAL", expr)
90 #define log_error_to(impl, expr) _cxxtools_log(impl, LOG_ERROR, "ERROR", expr)
91 #define log_warn_to(impl, expr) _cxxtools_log(impl, LOG_WARN, "WARN", expr)
92 #define log_info_to(impl, expr) _cxxtools_log(impl, LOG_INFO, "INFO", expr)
93 #define log_debug_to(impl, expr) _cxxtools_log(impl, LOG_DEBUG, "DEBUG", expr)
94 #define log_fine_to(impl, expr) _cxxtools_log(impl, LOG_FINE, "FINE", expr)
95 #define log_finer_to(impl, expr) _cxxtools_log(impl, LOG_FINER, "FINER", expr)
96 #define log_finest_to(impl, expr) _cxxtools_log(impl, LOG_FINEST, "FINEST", expr)
97 
98 #define log_fatal_if(cond, expr) _cxxtools_log_if(_default, LOG_FATAL, "FATAL", cond, expr)
99 #define log_error_if(cond, expr) _cxxtools_log_if(_default, LOG_ERROR, "ERROR", cond, expr)
100 #define log_warn_if(cond, expr) _cxxtools_log_if(_default, LOG_WARN, "WARN", cond, expr)
101 #define log_info_if(cond, expr) _cxxtools_log_if(_default, LOG_INFO, "INFO", cond, expr)
102 #define log_debug_if(cond, expr) _cxxtools_log_if(_default, LOG_DEBUG, "DEBUG", cond, expr)
103 #define log_fine_if(cond, expr) _cxxtools_log_if(_default, LOG_FINE, "FINE", cond, expr)
104 #define log_finer_if(cond, expr) _cxxtools_log_if(_default, LOG_FINER, "FINER", cond, expr)
105 #define log_finest_if(cond, expr) _cxxtools_log_if(_default, LOG_FINEST, "FINEST", cond, expr)
106 
107 #define log_fatal_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_FATAL, "FATAL", cond, expr)
108 #define log_error_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_ERROR, "ERROR", cond, expr)
109 #define log_warn_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_WARN, "WARN", cond, expr)
110 #define log_info_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_INFO, "INFO", cond, expr)
111 #define log_debug_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_DEBUG, "DEBUG", cond, expr)
112 #define log_fine_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_FINE, "FINE", cond, expr)
113 #define log_finer_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_FINER, "FINER", cond, expr)
114 #define log_finest_to_if(impl, cond, expr) _cxxtools_log_if(impl, LOG_FINEST, "FINEST", cond, expr)
115 
116 #define log_trace_to(impl, expr) \
117  ::cxxtools::LogTracer _cxxtools_tracer; \
118  do { \
119  ::cxxtools::Logger* _cxxtools_logger = getLogger ## impl(); \
120  if (_cxxtools_logger != 0 && _cxxtools_logger->isEnabled(::cxxtools::Logger::LOG_TRACE)) \
121  { \
122  _cxxtools_tracer.setLogger(_cxxtools_logger); \
123  _cxxtools_tracer.out() << expr; \
124  _cxxtools_tracer.enter(); \
125  } \
126  } while (false)
127 
128 #define log_trace(expr) log_trace_to(_default, expr)
129 
130 #define log_define_instance(instance, category) \
131  static ::cxxtools::Logger* getLogger ## instance() \
132  { \
133  static cxxtools::Logger* logger = 0; \
134  if (!::cxxtools::LogManager::isEnabled()) \
135  return 0; \
136  if (logger == 0) \
137  logger = ::cxxtools::LogManager::getInstance().getLogger(category); \
138  return logger; \
139  }
140 
141 #define log_define(category) log_define_instance(_default, category)
142 
143 #define log_init_cxxtools ::cxxtools::LogManager::logInit
144 #define log_init log_init_cxxtools
145 
146 namespace cxxtools
147 {
148  class SerializationInfo;
149 
151  //
152  class Logger
153  {
154  Logger(const Logger&);
155  Logger& operator=(const Logger&);
156 
157  public:
159  LOG_FATAL = 0x01,
160  LOG_ERROR = 0x02,
161  LOG_WARN = 0x04,
162  LOG_INFO = 0x08,
163  LOG_DEBUG = 0x10,
164  LOG_FINE = 0x10,
165  LOG_FINER = 0x20,
166  LOG_FINEST = 0x40,
167  LOG_TRACE = 0x80
168  };
169 
173  LOG_LEVEL_WARN = (LOG_WARN << 1) - 1,
174  LOG_LEVEL_INFO = (LOG_INFO << 1) - 1,
176  LOG_LEVEL_FINE = (LOG_FINE << 1) - 1,
180  };
181 
182  private:
183  std::string category;
184  int flags;
185 
186  public:
188  Logger(const std::string& c, int f)
189  : category(c), flags(f)
190  { }
191 
192  const std::string& getCategory() const
193  { return category; }
194  bool isEnabled(log_flag_type l) const
195  { return (flags & l) != 0; }
197  { return static_cast<log_level_type>(flags); }
198  int getLogFlags() const
199  { return flags; }
200  void setLogFlags(int f)
201  { flags = f; }
202  };
203 
205  //
207  {
208  public:
209  class Impl;
210 
211  private:
212  friend class Impl;
213  Impl* _impl;
214 
215  public:
245  static int strToLogFlags(const std::string& level);
246 
248 
252 
254 
255  Impl* impl() { return _impl; }
256  const Impl* impl() const { return _impl; }
257 
258  int rootFlags() const;
259  int logFlags(const std::string& category) const;
260 
261  // setter
262  void setRootFlags(int flags);
264  { setRootFlags(static_cast<int>(level)); }
267  void setRootLevel(const std::string& level)
268  { setRootFlags(strToLogFlags(level)); }
269 
270  void setLogFlags(const std::string& category, int flags);
271  void setLogLevel(const std::string& category, log_level_type level)
272  { setLogFlags(category, static_cast<int>(level)); };
276  void setLogLevel(const std::string& category, const std::string& level);
277 
278  void setFile(const std::string& fname);
279  void setFile(const std::string& fname, unsigned maxfilesize, unsigned maxbackupindex);
280  void setLoghost(const std::string& host, unsigned short port, bool broadcast = false);
281  void setStdout();
282  void setStderr();
283  };
284 
285  void operator>>= (const SerializationInfo& si, LogConfiguration& logConfiguration);
286  void operator<<= (SerializationInfo& si, const LogConfiguration& logConfiguration);
287 
289  //
291  {
292  public:
293  class Impl;
294 
295  friend class Impl;
296 
298  LogManager();
299  static bool _enabled;
300 
301  LogManager(const LogManager&);
303 
304  public:
305  ~LogManager();
306 
307  Impl* impl() { return _impl; }
308  const Impl* impl() const { return _impl; }
309 
310  static LogManager& getInstance();
311  static void logInit();
312  static void logInit(const std::string& fname);
313  static void logInit(const SerializationInfo& si);
314  static void logInit(const LogConfiguration& config);
315 
316  void configure(const LogConfiguration& config);
318 
319  Logger* getLogger(const std::string& category);
320  static bool isEnabled()
321  { return _enabled; }
322 
323  int rootFlags() const;
324  int logFlags(const std::string& category) const;
325  };
326 
328  //
330  {
331  public:
332  class Impl;
333 
334  private:
335  Impl* _impl;
336 
337  LogMessage(const LogMessage&);
338  LogMessage& operator=(const LogMessage&);
339 
340  public:
341  LogMessage(Logger* logger, const char* level);
342  LogMessage(Logger* logger, Logger::log_level_type level);
343  ~LogMessage();
344 
345  Impl* impl() { return _impl; }
346  const Impl* impl() const { return _impl; }
347 
348  std::ostream& out();
349  std::string str() const;
350 
351  void finish();
352  };
353 
355  //
356  class LogTracer
357  {
358  public:
359  class Impl;
360 
361  private:
362  Impl* _impl;
363 
364  LogTracer(const LogTracer&);
365  LogTracer& operator=(const LogTracer&);
366 
367  public:
368  LogTracer();
369  ~LogTracer();
370 
371  Impl* impl() { return _impl; }
372  const Impl* impl() const { return _impl; }
373 
374  void setLogger(Logger* l);
375  std::ostream& out();
376  void enter();
377  void exit();
378  };
379 
380 }
381 
382 #endif // LOG_CXXTOOLS_H