| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 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. |
| 13 | |
| 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. |
| 16 | |
| 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. |
| 19 | */ |
| 20 | /** @file |
| 21 | * @brief provides logging functions. |
| 22 | * |
| 23 | * @copyright © Copyright 2007-2008 by Intra2net AG |
| 24 | */ |
| 25 | |
| 26 | #ifndef __I2N_COMMON_LOGFUNC_HPP__ |
| 27 | #define __I2N_COMMON_LOGFUNC_HPP__ |
| 28 | |
| 29 | |
| 30 | #include <string> |
| 31 | #include <ostream> |
| 32 | #include <sstream> |
| 33 | #include <memory> |
| 34 | #include <exception> |
| 35 | #include <stdexcept> |
| 36 | |
| 37 | #include <boost/shared_ptr.hpp> |
| 38 | |
| 39 | #include "source_track_basics.hpp" |
| 40 | |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | ** |
| 45 | */ |
| 46 | |
| 47 | namespace I2n |
| 48 | { |
| 49 | namespace Logger |
| 50 | { |
| 51 | |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * wrapper class for syslog facility constants: |
| 56 | * |
| 57 | * @note we don't use an enum here since we import the values from |
| 58 | * the appropriate system header files internally. |
| 59 | */ |
| 60 | struct Facility |
| 61 | { |
| 62 | |
| 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; |
| 80 | |
| 81 | int m_facility; |
| 82 | |
| 83 | Facility(int facility = Facility::User) : m_facility(facility) {} |
| 84 | |
| 85 | operator int() const { return m_facility; } |
| 86 | }; // eo Facility |
| 87 | |
| 88 | /** |
| 89 | * struct for log level constants. |
| 90 | */ |
| 91 | struct LogLevel |
| 92 | { |
| 93 | enum { |
| 94 | Emergency = 0, Fatal= Emergency, |
| 95 | Alert, |
| 96 | Critical, |
| 97 | Error, |
| 98 | Warning, |
| 99 | Notice, |
| 100 | Info, |
| 101 | Debug, |
| 102 | _LogLevel_END |
| 103 | }; |
| 104 | |
| 105 | int m_level; |
| 106 | |
| 107 | LogLevel(int level= Warning) : m_level(level) {} |
| 108 | |
| 109 | operator int() const { return m_level; } |
| 110 | }; // eo struct LogLevel |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * part logger. |
| 115 | * Provides a logging object to be used within a module (part). |
| 116 | */ |
| 117 | class PartLogger |
| 118 | { |
| 119 | public: |
| 120 | |
| 121 | class LogHelper |
| 122 | { |
| 123 | public: |
| 124 | virtual ~LogHelper(); |
| 125 | |
| 126 | template< typename T > |
| 127 | std::ostream& operator << (T v) |
| 128 | { |
| 129 | if (StreamPtr.get()) |
| 130 | { |
| 131 | return *StreamPtr << v; |
| 132 | } |
| 133 | throw std::logic_error("pointer vanished"); |
| 134 | } // eo operator <<(T) |
| 135 | |
| 136 | protected: |
| 137 | friend class PartLogger; |
| 138 | |
| 139 | LogHelper(PartLogger& logger, int level, const SourceLocation& loc); |
| 140 | LogHelper(const LogHelper& helper); |
| 141 | |
| 142 | protected: |
| 143 | |
| 144 | PartLogger& Logger; |
| 145 | int Level; |
| 146 | SourceLocation Location; |
| 147 | mutable std::auto_ptr< std::ostringstream > StreamPtr; |
| 148 | }; // eo class LogHelper |
| 149 | |
| 150 | |
| 151 | public: |
| 152 | PartLogger( const std::string& part ); |
| 153 | PartLogger( const SourceLocation& loc ); |
| 154 | virtual ~PartLogger(); |
| 155 | |
| 156 | void log(int level, const std::string &msg, bool keep_unsafe_chars=false); |
| 157 | |
| 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); |
| 166 | |
| 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()); |
| 175 | |
| 176 | void set_keep_unsafe_chars(bool _keep_unsafe_chars); |
| 177 | bool get_keep_unsafe_chars(); |
| 178 | |
| 179 | protected: |
| 180 | |
| 181 | std::string Part; |
| 182 | bool KeepUnsafeChars; |
| 183 | |
| 184 | }; // eo class PartLogger |
| 185 | |
| 186 | typedef boost::shared_ptr< PartLogger > PartLoggerPtr; |
| 187 | |
| 188 | |
| 189 | extern PartLogger GlobalLogger; |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | ** functions to define the way the logging is done. |
| 194 | ** They work globally; i.e. they determine the loggin for the whole program! |
| 195 | */ |
| 196 | |
| 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 ); |
| 200 | |
| 201 | void enable_stderr_log(bool enable= true ); |
| 202 | |
| 203 | void enable_log_file( const std::string& name ); |
| 204 | void enable_log_file( bool enable= true ); |
| 205 | |
| 206 | bool is_logging_to_file(); |
| 207 | std::string get_log_file_name(); |
| 208 | |
| 209 | void reopen(); |
| 210 | |
| 211 | |
| 212 | int set_log_level( int level ); |
| 213 | int get_log_level(); |
| 214 | bool has_log_level(int level); |
| 215 | |
| 216 | std::string get_log_level_string(); |
| 217 | |
| 218 | |
| 219 | /* |
| 220 | ** char* versions .... *sigh* |
| 221 | ** ( are required, since the compiler doesn't automatically select the const std::string& versions... *big*sigh* ) |
| 222 | */ |
| 223 | |
| 224 | inline void enable_syslog( const char* name, Facility facility= Facility::User ) |
| 225 | { |
| 226 | enable_syslog( std::string(name), facility); |
| 227 | } |
| 228 | |
| 229 | inline void enable_log_file( const char* name) { enable_log_file( std::string(name) ); } |
| 230 | |
| 231 | } // eo namespace Logger |
| 232 | } // eo namespace I2n |
| 233 | |
| 234 | #endif |