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
41namespace Pt {
42
43class Settings;
44
45namespace System {
46
216class PT_SYSTEM_API Logger : protected Pt::NonCopyable
217{
218 friend class LogManager;
219
220 public:
227 Logger(const std::string& name);
228
235 Logger(const char* name);
236
240
243 static void loadPlugin(const std::string& sym, const Path& path);
244
247 static void registerPlugin(Plugin<LogChannel>& plugin);
248
252
261 static void init(const std::string& file);
262
264 static void initTargets()
265 { init("log.properties"); }
266
267 // TODO: init with Settings::ConstEntry, so a section in the
268 // application settings can be used.
269
277 static void init(const Settings& settings);
278
306 static void setPattern(const std::string& pattern);
307
316 static void setLogLevel(const std::string& target, LogLevel level)
317 { LogTarget::get(target).setLogLevel(level); }
318
326 static void setChannel(const std::string& target, const std::string& url)
327 { LogTarget::get(target).setChannel(url); }
328
330 static void reset(const std::string& target)
331 { LogTarget::get(target).reset(); }
332
335 bool enabled(LogLevel level) const
336 {
337 return level <= _target->logLevel();
338 }
339
342 bool enabled(const LogRecord& record) const
343 {
344 return record.logLevel() <= _target->logLevel();
345 }
346
353 void log(const LogRecord& record, bool always = false)
354 {
355 if( always || this->enabled( record.logLevel() ) )
356 {
357 _target->log( record );
358 }
359 }
360
362 LogTarget& target() const
363 { return *_target; }
364
365 protected:
367 Logger(LogTarget& t)
368 : _target( &t )
369 {}
370
371 private:
372 LogTarget& initLogger(const std::string& name);
373
374 private:
376 LogTarget* _target;
377};
378
423{
424 public:
435 LogMessage(Logger& logger, const LogLevel& level, bool always = false)
436 : _record(level)
437 , _logger(&logger)
438 , _enabled(always)
439 { }
440
443 {}
444
446 const LogRecord& record() const
447 { return _record; }
448
450 void log()
451 {
452 _logger->log( _record, _enabled );
453 _enabled = false;
454 }
455
457 bool enabled() const
458 { return _logger->enabled( _record.logLevel() ); }
459
461 operator bool() const
462 { return enabled(); }
463
465 bool operator!() const
466 { return ! enabled(); }
467
469 template <typename T>
470 LogMessage& operator<<(const T& value)
471 {
472 _record << value;
473 return *this;
474 }
475
477 LogMessage& operator<<( std::ostream& (*op)(std::ostream&) )
478 {
479 _record << op;
480 return *this;
481 }
482
484 LogMessage& operator<<( std::ios& (*op)(std::ios&) )
485 {
486 _record << op;
487 return *this;
488 }
489
491 LogMessage& operator<<( std::ios_base& (*op)(std::ios_base&) )
492 {
493 _record << op;
494 return *this;
495 }
496
499 {
500 return pf(*this);
501 }
502
503 private:
505 LogRecord _record;
506
508 Logger* _logger;
509
511 bool _enabled;
512};
513
519{
520 msg.log();
521 return msg;
522}
523
524struct LoggerStaticInit
525{
526 template <typename F>
527 LoggerStaticInit(F initfunc)
528 { initfunc(); }
529};
530
531} // namespace System
532
533} // namespace Pt
534
536#define PT_LOGGER_BEGIN_IMPL(logger, level) \
537 if( ! logger.enabled( Pt::System::level ) ) \
538 ; \
539 else Pt::System::LogMessage(static_cast<Pt::System::Logger&>(logger), Pt::System::level, true)
540
541#ifdef NLOG
542 #define PT_LOG_INIT(file)
543 #define PT_LOG_DEFINE_IMPL(instance, category)
544 #define PT_LOG_TO_IMPL(instance, level, message)
545 #define PT_LOGGER_LOG_IMPL(logger, level, expr)
546#else
548 #define PT_LOG_INIT(settings) Pt::System::Logger::init(settings);
549
551 #define PT_LOG_DEFINE_IMPL(instance, category) \
552 inline static Pt::System::Logger& instance() \
553 { \
554 static Pt::System::Logger instance##_instance(category); \
555 return instance##_instance; \
556 } \
557 static const Pt::System::LoggerStaticInit instance##_static_init( &instance );
558
560 #define PT_LOG_TO_IMPL(instance, level, expr) PT_LOGGER_LOG_IMPL(instance(), level, expr)
561
563 #define PT_LOGGER_LOG_IMPL(logger, level, expr) PT_LOGGER_BEGIN_IMPL(logger, level) << expr << Pt::System::endlog
564#endif
565
566#define PT_LOG_DEFINE(category) PT_LOG_DEFINE_IMPL(static_logger, category)
567#define PT_LOG_FATAL(expr) PT_LOG_TO_IMPL(static_logger, Fatal, expr)
568#define PT_LOG_ERROR(expr) PT_LOG_TO_IMPL(static_logger, Error, expr)
569#define PT_LOG_WARN(expr) PT_LOG_TO_IMPL(static_logger, Warn, expr)
570#define PT_LOG_INFO(expr) PT_LOG_TO_IMPL(static_logger, Info, expr)
571#define PT_LOG_DEBUG(expr) PT_LOG_TO_IMPL(static_logger, Debug, expr)
572#define PT_LOG_TRACE(expr) PT_LOG_TO_IMPL(static_logger, Trace, expr)
573
574#define PT_LOG_DEFINE_INSTANCE(instance, category) PT_LOG_DEFINE_IMPL(instance, category)
575#define PT_LOG_FATAL_TO(instance, expr) PT_LOG_TO_IMPL(instance, Fatal, expr)
576#define PT_LOG_ERROR_TO(instance, expr) PT_LOG_TO_IMPL(instance, Error, expr)
577#define PT_LOG_WARN_TO(instance, expr) PT_LOG_TO_IMPL(instance, Warn, expr)
578#define PT_LOG_INFO_TO(instance, expr) PT_LOG_TO_IMPL(instance, Info, expr)
579#define PT_LOG_DEBUG_TO(instance, expr) PT_LOG_TO_IMPL(instance, Debug, expr)
580#define PT_LOG_TRACE_TO(instance, expr) PT_LOG_TO_IMPL(instance, Trace, expr)
581
582#define PT_LOGGER_LOG_FATAL(logger, expr) PT_LOGGER_LOG_IMPL(logger, Fatal, expr)
583#define PT_LOGGER_LOG_ERROR(logger, expr) PT_LOGGER_LOG_IMPL(logger, Error, expr)
584#define PT_LOGGER_LOG_WARN(logger, expr) PT_LOGGER_LOG_IMPL(logger, Warn, expr)
585#define PT_LOGGER_LOG_INFO(logger, expr) PT_LOGGER_LOG_IMPL(logger, Info, expr)
586#define PT_LOGGER_LOG_DEBUG(logger, expr) PT_LOGGER_LOG_IMPL(logger, Debug, expr)
587#define PT_LOGGER_LOG_TRACE(logger, expr) PT_LOGGER_LOG_IMPL(logger, Trace, expr)
588
589#define PT_LOGGER_BEGIN_FATAL(logger) PT_LOGGER_BEGIN_IMPL(logger, Fatal)
590#define PT_LOGGER_BEGIN_ERROR(logger) PT_LOGGER_BEGIN_IMPL(logger, Error)
591#define PT_LOGGER_BEGIN_WARN(logger) PT_LOGGER_BEGIN_IMPL(logger, Warn)
592#define PT_LOGGER_BEGIN_INFO(logger) PT_LOGGER_BEGIN_IMPL(logger, Info)
593#define PT_LOGGER_BEGIN_DEBUG(logger) PT_LOGGER_BEGIN_IMPL(logger, Debug)
594#define PT_LOGGER_BEGIN_TRACE(logger) PT_LOGGER_BEGIN_IMPL(logger, Trace)
595
596#endif // Pt_System_Logger_h
Base class that disables copy construction and assignment.
Definition NonCopyable.h:59
Hierarchical application settings loaded from text.
Definition Settings.h:135
LogMessage & operator<<(std::ios_base &(*op)(std::ios_base &))
Applies op to the log record.
Definition Logger.h:491
LogMessage & operator<<(LogMessage &(*pf)(LogMessage &))
Applies a manipulator to the log message.
Definition Logger.h:498
const LogRecord & record() const
Returns the underlying log record.
Definition Logger.h:446
bool operator!() const
Returns true if the record's log level is disabled for the target.
Definition Logger.h:465
LogMessage & operator<<(std::ios &(*op)(std::ios &))
Applies op to the log record.
Definition Logger.h:484
LogMessage(Logger &logger, const LogLevel &level, bool always=false)
Constructs a log message for a logger.
Definition Logger.h:435
void log()
Sends the message's log record to the logger.
Definition Logger.h:450
LogMessage & operator<<(std::ostream &(*op)(std::ostream &))
Applies op to the log record.
Definition Logger.h:477
LogMessage & operator<<(const T &value)
Appends value to the log record.
Definition Logger.h:470
LogMessage & endlog(LogMessage &msg)
Manipulator to end and send a log-message.
Definition Logger.h:518
~LogMessage()
Destructor.
Definition Logger.h:442
bool enabled() const
Returns true if the record's log level is enabled for the target.
Definition Logger.h:457
Log records can be added to a log.
Definition LogRecord.h:94
LogLevel logLevel() const
Returns the severity level.
Definition LogRecord.h:118
Target of log-messages.
Definition LogTarget.h:63
static LogTarget & get(const std::string &name)
Get a target from the logging manager.
Writes log records to a target.
Definition Logger.h:217
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:326
~Logger()
Destructor.
bool enabled(const LogRecord &record) const
Returns true if the log level is enabled for the target.
Definition Logger.h:342
static void registerPlugin(Plugin< LogChannel > &plugin)
Registers a log channel plugin.
static void init(const std::string &file)
Initialize logging targets with a settings file.
Logger(const char *name)
Constructs a new logger for a target and log-level.
static void reset(const std::string &target)
Resets log level and channel.
Definition Logger.h:330
static void loadPlugin(const std::string &sym, const Path &path)
Loads log channel plugins from a library.
Logger(const std::string &name)
Constructs a new logger for a target and log-level.
static void setLogLevel(const std::string &target, LogLevel level)
Sets the log-level of the target and its children.
Definition Logger.h:316
static void init(const Settings &settings)
Initialize logging targets with a settings.
static void unregisterPlugin(Plugin< LogChannel > &plugin)
Unregisters a log channel plugin.
bool enabled(LogLevel level) const
Returns true if the log level is enabled for the target.
Definition Logger.h:335
static void setPattern(const std::string &pattern)
Set the pattern for log records.
void log(const LogRecord &record, bool always=false)
Write a log record to the target.
Definition Logger.h:353
Represents a path in the file-system.
Definition Path.h:48
Interface for plugins.
Definition Plugin.h:98
LogLevel
Severity of the log-message.
Definition LogLevel.h:41
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33