2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief provides logging functions.
23 * @copyright © Copyright 2007-2008 by Intra2net AG
26 #ifndef __I2N_COMMON_LOGFUNC_HPP__
27 #define __I2N_COMMON_LOGFUNC_HPP__
37 #include <boost/shared_ptr.hpp>
39 #include "source_track_basics.hpp"
55 * wrapper class for syslog facility constants:
57 * @note we don't use an enum here since we import the values from
58 * the appropriate system header files internally.
63 static const int AuthPriv;
64 static const int Cron;
65 static const int Daemon;
66 static const int Kern;
67 static const int Mail;
68 static const int News;
69 static const int Syslog;
70 static const int User;
71 static const int UUCP;
72 static const int Local0;
73 static const int Local1;
74 static const int Local2;
75 static const int Local3;
76 static const int Local4;
77 static const int Local5;
78 static const int Local6;
79 static const int Local7;
83 Facility(int facility = Facility::User) : m_facility(facility) {}
85 operator int() const { return m_facility; }
89 * struct for log level constants.
94 Emergency = 0, Fatal= Emergency,
107 LogLevel(int level= Warning) : m_level(level) {}
109 operator int() const { return m_level; }
110 }; // eo struct LogLevel
115 * Provides a logging object to be used within a module (part).
124 virtual ~LogHelper();
126 template< typename T >
127 std::ostream& operator << (T v)
131 return *StreamPtr << v;
133 throw std::logic_error("pointer vanished");
134 } // eo operator <<(T)
137 friend class PartLogger;
139 LogHelper(PartLogger& logger, int level, const SourceLocation& loc);
140 LogHelper(const LogHelper& helper);
146 SourceLocation Location;
147 mutable std::auto_ptr< std::ostringstream > StreamPtr;
148 }; // eo class LogHelper
152 PartLogger( const std::string& part );
153 PartLogger( const SourceLocation& loc );
154 virtual ~PartLogger();
156 void log(int level, const std::string &msg, bool keep_unsafe_chars=false);
158 void fatal(const std::string& msg);
159 void alert(const std::string& msg);
160 void critical(const std::string& msg);
161 void error(const std::string& msg);
162 void warning(const std::string& msg);
163 void notice(const std::string& msg);
164 void info(const std::string& msg);
165 void debug(const std::string& msg);
167 LogHelper fatal(const SourceLocation& loc= SourceLocation());
168 LogHelper alert(const SourceLocation& loc= SourceLocation());
169 LogHelper critical(const SourceLocation& loc= SourceLocation());
170 LogHelper error(const SourceLocation& loc= SourceLocation());
171 LogHelper warning(const SourceLocation& loc= SourceLocation());
172 LogHelper notice(const SourceLocation& loc= SourceLocation());
173 LogHelper info(const SourceLocation& loc= SourceLocation());
174 LogHelper debug(const SourceLocation& loc= SourceLocation());
176 void set_keep_unsafe_chars(bool _keep_unsafe_chars);
177 bool get_keep_unsafe_chars();
182 bool KeepUnsafeChars;
184 }; // eo class PartLogger
186 typedef boost::shared_ptr< PartLogger > PartLoggerPtr;
189 extern PartLogger GlobalLogger;
193 ** functions to define the way the logging is done.
194 ** They work globally; i.e. they determine the loggin for the whole program!
197 void enable_syslog( const std::string& name, Facility facility= Facility::User );
198 void enable_syslog( Facility facility );
199 void enable_syslog( bool enable= true );
201 void enable_stderr_log(bool enable= true );
203 void enable_log_file( const std::string& name );
204 void enable_log_file( bool enable= true );
206 bool is_logging_to_file();
207 std::string get_log_file_name();
212 int set_log_level( int level );
214 bool has_log_level(int level);
216 std::string get_log_level_string();
220 ** char* versions .... *sigh*
221 ** ( are required, since the compiler doesn't automatically select the const std::string& versions... *big*sigh* )
224 inline void enable_syslog( const char* name, Facility facility= Facility::User )
226 enable_syslog( std::string(name), facility);
229 inline void enable_log_file( const char* name) { enable_log_file( std::string(name) ); }
231 } // eo namespace Logger
232 } // eo namespace I2n