/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /** @file * @brief provides logging functions. * * @copyright © Copyright 2007-2008 by Intra2net AG */ #ifndef __I2N_COMMON_LOGFUNC_HPP__ #define __I2N_COMMON_LOGFUNC_HPP__ #include #include #include #include #include #include #include #include "source_track_basics.hpp" /* ** */ namespace I2n { namespace Logger { /** * wrapper class for syslog facility constants: * * @note we don't use an enum here since we import the values from * the appropriate system header files internally. */ struct Facility { static const int AuthPriv; static const int Cron; static const int Daemon; static const int Kern; static const int Mail; static const int News; static const int Syslog; static const int User; static const int UUCP; static const int Local0; static const int Local1; static const int Local2; static const int Local3; static const int Local4; static const int Local5; static const int Local6; static const int Local7; int m_facility; Facility(int facility = Facility::User) : m_facility(facility) {} operator int() const { return m_facility; } }; // eo Facility /** * struct for log level constants. */ struct LogLevel { enum { Emergency = 0, Fatal= Emergency, Alert, Critical, Error, Warning, Notice, Info, Debug, _LogLevel_END }; int m_level; LogLevel(int level= Warning) : m_level(level) {} operator int() const { return m_level; } }; // eo struct LogLevel /** * part logger. * Provides a logging object to be used within a module (part). */ class PartLogger { public: class LogHelper { public: virtual ~LogHelper(); template< typename T > std::ostream& operator << (T v) { if (StreamPtr.get()) { return *StreamPtr << v; } throw std::logic_error("pointer vanished"); } // eo operator <<(T) protected: friend class PartLogger; LogHelper(PartLogger& logger, int level, const SourceLocation& loc); LogHelper(const LogHelper& helper); protected: PartLogger& Logger; int Level; SourceLocation Location; mutable std::auto_ptr< std::ostringstream > StreamPtr; }; // eo class LogHelper public: PartLogger( const std::string& part ); PartLogger( const SourceLocation& loc ); virtual ~PartLogger(); void log(int level, const std::string &msg, bool keep_unsafe_chars=false); void fatal(const std::string& msg); void alert(const std::string& msg); void critical(const std::string& msg); void error(const std::string& msg); void warning(const std::string& msg); void notice(const std::string& msg); void info(const std::string& msg); void debug(const std::string& msg); LogHelper fatal(const SourceLocation& loc= SourceLocation()); LogHelper alert(const SourceLocation& loc= SourceLocation()); LogHelper critical(const SourceLocation& loc= SourceLocation()); LogHelper error(const SourceLocation& loc= SourceLocation()); LogHelper warning(const SourceLocation& loc= SourceLocation()); LogHelper notice(const SourceLocation& loc= SourceLocation()); LogHelper info(const SourceLocation& loc= SourceLocation()); LogHelper debug(const SourceLocation& loc= SourceLocation()); void set_keep_unsafe_chars(bool _keep_unsafe_chars); bool get_keep_unsafe_chars(); protected: std::string Part; bool KeepUnsafeChars; }; // eo class PartLogger typedef boost::shared_ptr< PartLogger > PartLoggerPtr; extern PartLogger GlobalLogger; /* ** functions to define the way the logging is done. ** They work globally; i.e. they determine the loggin for the whole program! */ void enable_syslog( const std::string& name, Facility facility= Facility::User ); void enable_syslog( Facility facility ); void enable_syslog( bool enable= true ); void enable_stderr_log(bool enable= true ); void enable_log_file( const std::string& name ); void enable_log_file( bool enable= true ); bool is_logging_to_file(); std::string get_log_file_name(); void reopen(); int set_log_level( int level ); int get_log_level(); bool has_log_level(int level); std::string get_log_level_string(); /* ** char* versions .... *sigh* ** ( are required, since the compiler doesn't automatically select the const std::string& versions... *big*sigh* ) */ inline void enable_syslog( const char* name, Facility facility= Facility::User ) { enable_syslog( std::string(name), facility); } inline void enable_log_file( const char* name) { enable_log_file( std::string(name) ); } } // eo namespace Logger } // eo namespace I2n #endif