30#ifndef Pt_System_Logger_h
31#define Pt_System_Logger_h
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>
218 friend class LogManager;
261 static void init(
const std::string& file);
264 static void initTargets()
265 {
init(
"log.properties"); }
326 static void setChannel(
const std::string& target,
const std::string& url)
330 static void reset(
const std::string& target)
337 return level <= _target->logLevel();
344 return record.
logLevel() <= _target->logLevel();
357 _target->log( record );
372 LogTarget& initLogger(
const std::string& name);
452 _logger->log( _record, _enabled );
458 {
return _logger->enabled( _record.logLevel() ); }
461 operator bool()
const
469 template <
typename T>
524struct LoggerStaticInit
526 template <
typename F>
527 LoggerStaticInit(F initfunc)
536#define PT_LOGGER_BEGIN_IMPL(logger, level) \
537 if( ! logger.enabled( Pt::System::level ) ) \
539 else Pt::System::LogMessage(static_cast<Pt::System::Logger&>(logger), Pt::System::level, true)
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)
548 #define PT_LOG_INIT(settings) Pt::System::Logger::init(settings);
551 #define PT_LOG_DEFINE_IMPL(instance, category) \
552 inline static Pt::System::Logger& instance() \
554 static Pt::System::Logger instance##_instance(category); \
555 return instance##_instance; \
557 static const Pt::System::LoggerStaticInit instance##_static_init( &instance );
560 #define PT_LOG_TO_IMPL(instance, level, expr) PT_LOGGER_LOG_IMPL(instance(), level, expr)
563 #define PT_LOGGER_LOG_IMPL(logger, level, expr) PT_LOGGER_BEGIN_IMPL(logger, level) << expr << Pt::System::endlog
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)
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)
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)
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)
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
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