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/System/Plugin.h>
38 #include <Pt/NonCopyable.h>
39 #include <string>
40 
41 namespace Pt {
42 
43 class Settings;
44 
45 namespace System {
46 
96 class PT_SYSTEM_API Logger : protected Pt::NonCopyable
97 {
98  friend class LogManager;
99 
100  public:
107  Logger(const std::string& name);
108 
115  Logger(const char* name);
116 
120 
123  static void loadPlugin(const std::string& sym, const Path& path);
124 
127  static void registerPlugin(Plugin<LogChannel>& plugin);
128 
131  static void unregisterPlugin(Plugin<LogChannel>& plugin);
132 
141  static void init(const std::string& file);
142 
144  static void initTargets()
145  { init("log.properties"); }
146 
147  // TODO: init with Settings::ConstEntry, so a section in the
148  // application settings can be used.
149 
157  static void init(const Settings& settings);
158 
186  static void setPattern(const std::string& pattern);
187 
196  static void setLogLevel(const std::string& target, LogLevel level)
197  { LogTarget::get(target).setLogLevel(level); }
198 
206  static void setChannel(const std::string& target, const std::string& url)
207  { LogTarget::get(target).setChannel(url); }
208 
210  static void reset(const std::string& target)
211  { LogTarget::get(target).reset(); }
212 
215  bool enabled(LogLevel level) const
216  {
217  return level <= _target->logLevel();
218  }
219 
222  bool enabled(const LogRecord& record) const
223  {
224  return record.logLevel() <= _target->logLevel();
225  }
226 
233  void log(const LogRecord& record, bool always = false)
234  {
235  if( always || this->enabled( record.logLevel() ) )
236  {
237  _target->log( record );
238  }
239  }
240 
242  LogTarget& target() const
243  { return *_target; }
244 
245  protected:
247  Logger(LogTarget& t)
248  : _target( &t )
249  {}
250 
251  private:
252  LogTarget& initLogger(const std::string& name);
253 
254  private:
256  LogTarget* _target;
257 };
258 
297 class LogMessage : protected Pt::NonCopyable
298 {
299  public:
310  LogMessage(Logger& logger, const LogLevel& level, bool always = false)
311  : _record(level)
312  , _logger(&logger)
313  , _enabled(always)
314  { }
315 
318  {}
319 
321  const LogRecord& record() const
322  { return _record; }
323 
325  void log()
326  {
327  _logger->log( _record, _enabled );
328  _enabled = false;
329  }
330 
332  bool enabled() const
333  { return _logger->enabled( _record.logLevel() ); }
334 
336  operator bool() const
337  { return enabled(); }
338 
340  bool operator!() const
341  { return ! enabled(); }
342 
344  template <typename T>
345  LogMessage& operator<<(const T& value)
346  {
347  _record << value;
348  return *this;
349  }
350 
352  LogMessage& operator<<( std::ostream& (*op)(std::ostream&) )
353  {
354  _record << op;
355  return *this;
356  }
357 
359  LogMessage& operator<<( std::ios& (*op)(std::ios&) )
360  {
361  _record << op;
362  return *this;
363  }
364 
366  LogMessage& operator<<( std::ios_base& (*op)(std::ios_base&) )
367  {
368  _record << op;
369  return *this;
370  }
371 
374  {
375  return pf(*this);
376  }
377 
378  private:
380  LogRecord _record;
381 
383  Logger* _logger;
384 
386  bool _enabled;
387 };
388 
394 {
395  msg.log();
396  return msg;
397 }
398 
399 struct LoggerStaticInit
400 {
401  template <typename F>
402  LoggerStaticInit(F initfunc)
403  { initfunc(); }
404 };
405 
406 } // namespace System
407 
408 } // namespace Pt
409 
411 #define PT_LOGGER_BEGIN_IMPL(logger, level) \
412  if( ! logger.enabled( Pt::System::level ) ) \
413  ; \
414  else Pt::System::LogMessage(static_cast<Pt::System::Logger&>(logger), Pt::System::level, true)
415 
416 #ifdef NLOG
417  #define PT_LOG_INIT(file)
418  #define PT_LOG_DEFINE_IMPL(instance, category)
419  #define PT_LOG_TO_IMPL(instance, level, message)
420  #define PT_LOGGER_LOG_IMPL(logger, level, expr)
421 #else
422  #define PT_LOG_INIT(settings) Pt::System::Logger::init(settings);
424 
426  #define PT_LOG_DEFINE_IMPL(instance, category) \
427  inline static Pt::System::Logger& instance() \
428  { \
429  static Pt::System::Logger instance##_instance(category); \
430  return instance##_instance; \
431  } \
432  static const Pt::System::LoggerStaticInit instance##_static_init( &instance );
433 
435  #define PT_LOG_TO_IMPL(instance, level, expr) PT_LOGGER_LOG_IMPL(instance(), level, expr)
436 
438  #define PT_LOGGER_LOG_IMPL(logger, level, expr) PT_LOGGER_BEGIN_IMPL(logger, level) << expr << Pt::System::endlog
439 #endif
440 
441 #define PT_LOG_DEFINE(category) PT_LOG_DEFINE_IMPL(static_logger, category)
442 #define PT_LOG_FATAL(expr) PT_LOG_TO_IMPL(static_logger, Fatal, expr)
443 #define PT_LOG_ERROR(expr) PT_LOG_TO_IMPL(static_logger, Error, expr)
444 #define PT_LOG_WARN(expr) PT_LOG_TO_IMPL(static_logger, Warn, expr)
445 #define PT_LOG_INFO(expr) PT_LOG_TO_IMPL(static_logger, Info, expr)
446 #define PT_LOG_DEBUG(expr) PT_LOG_TO_IMPL(static_logger, Debug, expr)
447 #define PT_LOG_TRACE(expr) PT_LOG_TO_IMPL(static_logger, Trace, expr)
448 
449 #define PT_LOG_DEFINE_INSTANCE(instance, category) PT_LOG_DEFINE_IMPL(instance, category)
450 #define PT_LOG_FATAL_TO(instance, expr) PT_LOG_TO_IMPL(instance, Fatal, expr)
451 #define PT_LOG_ERROR_TO(instance, expr) PT_LOG_TO_IMPL(instance, Error, expr)
452 #define PT_LOG_WARN_TO(instance, expr) PT_LOG_TO_IMPL(instance, Warn, expr)
453 #define PT_LOG_INFO_TO(instance, expr) PT_LOG_TO_IMPL(instance, Info, expr)
454 #define PT_LOG_DEBUG_TO(instance, expr) PT_LOG_TO_IMPL(instance, Debug, expr)
455 #define PT_LOG_TRACE_TO(instance, expr) PT_LOG_TO_IMPL(instance, Trace, expr)
456 
457 #define PT_LOGGER_LOG_FATAL(logger, expr) PT_LOGGER_LOG_IMPL(logger, Fatal, expr)
458 #define PT_LOGGER_LOG_ERROR(logger, expr) PT_LOGGER_LOG_IMPL(logger, Error, expr)
459 #define PT_LOGGER_LOG_WARN(logger, expr) PT_LOGGER_LOG_IMPL(logger, Warn, expr)
460 #define PT_LOGGER_LOG_INFO(logger, expr) PT_LOGGER_LOG_IMPL(logger, Info, expr)
461 #define PT_LOGGER_LOG_DEBUG(logger, expr) PT_LOGGER_LOG_IMPL(logger, Debug, expr)
462 #define PT_LOGGER_LOG_TRACE(logger, expr) PT_LOGGER_LOG_IMPL(logger, Trace, expr)
463 
464 #define PT_LOGGER_BEGIN_FATAL(logger) PT_LOGGER_BEGIN_IMPL(logger, Fatal)
465 #define PT_LOGGER_BEGIN_ERROR(logger) PT_LOGGER_BEGIN_IMPL(logger, Error)
466 #define PT_LOGGER_BEGIN_WARN(logger) PT_LOGGER_BEGIN_IMPL(logger, Warn)
467 #define PT_LOGGER_BEGIN_INFO(logger) PT_LOGGER_BEGIN_IMPL(logger, Info)
468 #define PT_LOGGER_BEGIN_DEBUG(logger) PT_LOGGER_BEGIN_IMPL(logger, Debug)
469 #define PT_LOGGER_BEGIN_TRACE(logger) PT_LOGGER_BEGIN_IMPL(logger, Trace)
470 
471 #endif // Pt_System_Logger_h
Core module.
Definition: Callable.h:2
bool enabled(LogLevel level) const
Returns true if the log level is enabled for the target.
Definition: Logger.h:215
static void loadPlugin(const std::string &sym, const Path &path)
Loads log channel plugins from a library.
LogMessage & operator<<(LogMessage &(*pf)(LogMessage &))
Applies a manipulator to the log message.
Definition: Logger.h:373
LogMessage & operator<<(std::ios &(*op)(std::ios &))
Applies op to the log record.
Definition: Logger.h:359
static void reset(const std::string &target)
Resets log level and channel.
Definition: Logger.h:210
Target of log-messages.
Definition: LogTarget.h:63
void log()
Sends the message's log record to the logger.
Definition: Logger.h:325
Log records can be added to a log.
Definition: LogRecord.h:91
static void unregisterPlugin(Plugin< LogChannel > &plugin)
Unregisters a log channel plugin.
~LogMessage()
Destructor.
Definition: Logger.h:317
LogMessage & operator<<(std::ios_base &(*op)(std::ios_base &))
Applies op to the log record.
Definition: Logger.h:366
LogMessage & endlog(LogMessage &msg)
Manipulator to end and send a log-message.
Definition: Logger.h:393
void setLogLevel(LogLevel level)
Sets the log-level of the target and its children.
bool enabled(const LogRecord &record) const
Returns true if the log level is enabled for the target.
Definition: Logger.h:222
LogLevel logLevel() const
Returns the severity level.
Definition: LogRecord.h:115
void reset()
Resets log level and channel.
bool enabled() const
Returns true if the record's log level is enabled for the target.
Definition: Logger.h:332
LogMessage & operator<<(std::ostream &(*op)(std::ostream &))
Applies op to the log record.
Definition: Logger.h:352
bool operator!() const
Returns true if the record's log level is disabled for the target.
Definition: Logger.h:340
static void registerPlugin(Plugin< LogChannel > &plugin)
Registers a log channel plugin.
Store application settings.
Definition: Settings.h:173
~Logger()
Destructor.
Interface for plugins.
Definition: Plugin.h:81
const LogRecord & record() const
Returns the underlying log record.
Definition: Logger.h:321
Writes log records to a target.
Definition: Logger.h:97
static void setLogLevel(const std::string &target, LogLevel level)
Sets the log-level of the target and its children.
Definition: Logger.h:196
Logs records with a logger.
Definition: Logger.h:298
void log(const LogRecord &record, bool always=false)
Write a log record to the target.
Definition: Logger.h:233
static void init(const std::string &file)
Initialize logging targets with a settings file.
Represents a path in the file-system.
Definition: Path.h:48
LogMessage & operator<<(const T &value)
Appends value to the log record.
Definition: Logger.h:345
void setChannel(const std::string &url)
Sets the channel to be used by this target.
LogMessage(Logger &logger, const LogLevel &level, bool always=false)
Constructs a log message for a logger.
Definition: Logger.h:310
Logger(const std::string &name)
Constructs a new logger for a target and log-level.
static void setPattern(const std::string &pattern)
Set the pattern for log records.
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:206
Logger(const char *name)
Constructs a new logger for a target and log-level.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
static LogTarget & get(const std::string &name)
Get a target from the logging manager.
LogLevel
Severity of the log-message.
Definition: LogLevel.h:41
static void init(const Settings &settings)
Initialize logging targets with a settings.