Logger.h
1 /*
2  * Copyright (C) 2005-2010 by Dr. Marc Boris Duerner
3  * Copyright (C) 2010-2010 by Aloysius Indrayanto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef Pt_System_Logger_h
31 #define Pt_System_Logger_h
32 
33 #include <Pt/System/Api.h>
34 #include <Pt/System/LogLevel.h>
35 #include <Pt/System/LogTarget.h>
36 #include <Pt/System/LogRecord.h>
37 #include <Pt/NonCopyable.h>
38 #include <string>
39 
40 namespace Pt {
41 
42 class Settings;
43 
44 namespace System {
45 
95 class PT_SYSTEM_API Logger : protected Pt::NonCopyable
96 {
97  friend class LogManager;
98 
99  public:
106  Logger(const std::string& name);
107 
114  Logger(const char* name);
115 
118  ~Logger();
119 
128  static void init(const std::string& file);
129 
131  static void initTargets()
132  { init("log.properties"); }
133 
134  // TODO: init with Settings::ConstEntry, so a section in the
135  // application settings can be used.
136 
144  static void init(const Settings& settings);
145 
173  static void setPattern(const std::string& pattern);
174 
183  static void setLogLevel(const std::string& target, LogLevel level)
184  { LogTarget::get(target).setLogLevel(level); }
185 
193  static void setChannel(const std::string& target, const std::string& url)
194  { LogTarget::get(target).setChannel(url); }
195 
198  bool enabled(LogLevel level) const
199  {
200  return level <= _target->logLevel();
201  }
202 
205  bool enabled(const LogRecord& record) const
206  {
207  return record.logLevel() <= _target->logLevel();
208  }
209 
216  void log(const LogRecord& record, bool always = false)
217  {
218  if( always || this->enabled( record.logLevel() ) )
219  {
220  _target->log( record );
221  }
222  }
223 
225  LogTarget& target() const
226  { return *_target; }
227 
228  protected:
230  Logger(LogTarget& t)
231  : _target( &t )
232  {}
233 
234  private:
235  LogTarget& initLogger(const std::string& name);
236 
237  private:
239  LogTarget* _target;
240 };
241 
280 class LogMessage : protected Pt::NonCopyable
281 {
282  public:
293  LogMessage(Logger& logger, const LogLevel& level, bool always = false)
294  : _record(level)
295  , _logger(&logger)
296  , _enabled(always)
297  { }
298 
301  {}
302 
304  const LogRecord& record() const
305  { return _record; }
306 
308  void log()
309  {
310  _logger->log( _record, _enabled );
311  _enabled = false;
312  }
313 
315  bool enabled() const
316  { return _logger->enabled( _record.logLevel() ); }
317 
319  operator bool() const
320  { return enabled(); }
321 
323  bool operator!() const
324  { return ! enabled(); }
325 
327  template <typename T>
328  LogMessage& operator<<(const T& value)
329  {
330  _record << value;
331  return *this;
332  }
333 
335  LogMessage& operator<<( std::ostream& (*op)(std::ostream&) )
336  {
337  _record << op;
338  return *this;
339  }
340 
342  LogMessage& operator<<( std::ios& (*op)(std::ios&) )
343  {
344  _record << op;
345  return *this;
346  }
347 
349  LogMessage& operator<<( std::ios_base& (*op)(std::ios_base&) )
350  {
351  _record << op;
352  return *this;
353  }
354 
357  {
358  return pf(*this);
359  }
360 
361  private:
363  LogRecord _record;
364 
366  Logger* _logger;
367 
369  bool _enabled;
370 };
371 
377 {
378  msg.log();
379  return msg;
380 }
381 
382 struct LoggerStaticInit
383 {
384  template <typename F>
385  LoggerStaticInit(F initfunc)
386  { initfunc(); }
387 };
388 
389 } // namespace System
390 
391 } // namespace Pt
392 
394 #define PT_LOGGER_BEGIN_IMPL(logger, level) \
395  if( ! logger.enabled( Pt::System::level ) ) \
396  ; \
397  else Pt::System::LogMessage(static_cast<Pt::System::Logger&>(logger), Pt::System::level, true)
398 
399 #ifdef NLOG
400  #define PT_LOG_INIT(file)
401  #define PT_LOG_DEFINE_IMPL(instance, category)
402  #define PT_LOG_TO_IMPL(instance, level, message)
403  #define PT_LOGGER_LOG_IMPL(logger, level, expr)
404 #else
405  #define PT_LOG_INIT(settings) Pt::System::Logger::init(settings);
407 
409  #define PT_LOG_DEFINE_IMPL(instance, category) \
410  inline static Pt::System::Logger& instance() \
411  { \
412  static Pt::System::Logger instance##_instance(category); \
413  return instance##_instance; \
414  } \
415  static const Pt::System::LoggerStaticInit instance##_static_init( &instance );
416 
418  #define PT_LOG_TO_IMPL(instance, level, expr) PT_LOGGER_LOG_IMPL(instance(), level, expr)
419 
421  #define PT_LOGGER_LOG_IMPL(logger, level, expr) PT_LOGGER_BEGIN_IMPL(logger, level) << expr << Pt::System::endlog
422 #endif
423 
424 #define PT_LOG_DEFINE(category) PT_LOG_DEFINE_IMPL(static_logger, category)
425 #define PT_LOG_FATAL(expr) PT_LOG_TO_IMPL(static_logger, Fatal, expr)
426 #define PT_LOG_ERROR(expr) PT_LOG_TO_IMPL(static_logger, Error, expr)
427 #define PT_LOG_WARN(expr) PT_LOG_TO_IMPL(static_logger, Warn, expr)
428 #define PT_LOG_INFO(expr) PT_LOG_TO_IMPL(static_logger, Info, expr)
429 #define PT_LOG_DEBUG(expr) PT_LOG_TO_IMPL(static_logger, Debug, expr)
430 #define PT_LOG_TRACE(expr) PT_LOG_TO_IMPL(static_logger, Trace, expr)
431 
432 #define PT_LOG_DEFINE_INSTANCE(instance, category) PT_LOG_DEFINE_IMPL(instance, category)
433 #define PT_LOG_FATAL_TO(instance, expr) PT_LOG_TO_IMPL(instance, Fatal, expr)
434 #define PT_LOG_ERROR_TO(instance, expr) PT_LOG_TO_IMPL(instance, Error, expr)
435 #define PT_LOG_WARN_TO(instance, expr) PT_LOG_TO_IMPL(instance, Warn, expr)
436 #define PT_LOG_INFO_TO(instance, expr) PT_LOG_TO_IMPL(instance, Info, expr)
437 #define PT_LOG_DEBUG_TO(instance, expr) PT_LOG_TO_IMPL(instance, Debug, expr)
438 #define PT_LOG_TRACE_TO(instance, expr) PT_LOG_TO_IMPL(instance, Trace, expr)
439 
440 #define PT_LOGGER_LOG_FATAL(logger, expr) PT_LOGGER_LOG_IMPL(logger, Fatal, expr)
441 #define PT_LOGGER_LOG_ERROR(logger, expr) PT_LOGGER_LOG_IMPL(logger, Error, expr)
442 #define PT_LOGGER_LOG_WARN(logger, expr) PT_LOGGER_LOG_IMPL(logger, Warn, expr)
443 #define PT_LOGGER_LOG_INFO(logger, expr) PT_LOGGER_LOG_IMPL(logger, Info, expr)
444 #define PT_LOGGER_LOG_DEBUG(logger, expr) PT_LOGGER_LOG_IMPL(logger, Debug, expr)
445 #define PT_LOGGER_LOG_TRACE(logger, expr) PT_LOGGER_LOG_IMPL(logger, Trace, expr)
446 
447 #define PT_LOGGER_BEGIN_FATAL(logger) PT_LOGGER_BEGIN_IMPL(logger, Fatal)
448 #define PT_LOGGER_BEGIN_ERROR(logger) PT_LOGGER_BEGIN_IMPL(logger, Error)
449 #define PT_LOGGER_BEGIN_WARN(logger) PT_LOGGER_BEGIN_IMPL(logger, Warn)
450 #define PT_LOGGER_BEGIN_INFO(logger) PT_LOGGER_BEGIN_IMPL(logger, Info)
451 #define PT_LOGGER_BEGIN_DEBUG(logger) PT_LOGGER_BEGIN_IMPL(logger, Debug)
452 #define PT_LOGGER_BEGIN_TRACE(logger) PT_LOGGER_BEGIN_IMPL(logger, Trace)
453 
454 #endif // Pt_System_Logger_h
Log records can be added to a log.
Definition: LogRecord.h:90
Protects derived classes from being copied.
Definition: NonCopyable.h:54
bool enabled(const LogRecord &record) const
Returns true if the log level is enabled for the target.
Definition: Logger.h:205
const LogRecord & record() const
Returns the underlying log record.
Definition: Logger.h:304
LogLevel
Severity of the log-message.
Definition: LogLevel.h:41
LogLevel logLevel() const
Returns the severity level.
Definition: LogRecord.h:115
static void setLogLevel(const std::string &target, LogLevel level)
Sets the log-level of the target and its children.
Definition: Logger.h:183
static LogTarget & get(const std::string &name)
Get a target from the logging manager.
void setChannel(const std::string &url)
Sets the channel to be used by this target.
bool operator!() const
Returns true if the record's log level is disabled for the target.
Definition: Logger.h:323
LogMessage & endlog(LogMessage &msg)
Manipulator to end and send a log-message.
Definition: Logger.h:376
static void setChannel(const std::string &target, const std::string &url)
Sets the channel to be used by the target and its children.
Definition: Logger.h:193
Target of log-messages.
Definition: LogTarget.h:62
LogMessage & operator<<(LogMessage &(*pf)(LogMessage &))
Applies a manipulator to the log message.
Definition: Logger.h:356
void setLogLevel(LogLevel level)
Sets the log-level of the target and its children.
LogMessage & operator<<(std::ostream &(*op)(std::ostream &))
Applies op to the log record.
Definition: Logger.h:335
LogMessage(Logger &logger, const LogLevel &level, bool always=false)
Constructs a log message for a logger.
Definition: Logger.h:293
bool enabled(LogLevel level) const
Returns true if the log level is enabled for the target.
Definition: Logger.h:198
LogMessage & operator<<(const T &value)
Appends value to the log record.
Definition: Logger.h:328
LogMessage & operator<<(std::ios_base &(*op)(std::ios_base &))
Applies op to the log record.
Definition: Logger.h:349
~LogMessage()
Destructor.
Definition: Logger.h:300
void log(const LogRecord &record, bool always=false)
Write a log record to the target.
Definition: Logger.h:216
bool enabled() const
Returns true if the record's log level is enabled for the target.
Definition: Logger.h:315
Writes log records to a target.
Definition: Logger.h:95
void log()
Sends the message's log record to the logger.
Definition: Logger.h:308
Store application settings.
Definition: Settings.h:172
LogMessage & operator<<(std::ios &(*op)(std::ios &))
Applies op to the log record.
Definition: Logger.h:342
Logs records with a logger.
Definition: Logger.h:280