libassa 3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // Logger.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (c) 2001 by Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 #ifndef LOGGER_H 00013 #define LOGGER_H 00014 00015 //System headers 00016 #include <sys/types.h> 00017 #include <string.h> 00018 #include <stdio.h> 00019 00020 #include <string> 00021 #include <stack> 00022 00023 using std::string; 00024 using std::stack; 00025 00026 //ASSA headers 00027 00028 #include "assa/Logger_Impl.h" 00029 #include "assa/Singleton.h" 00030 #include "assa/MemDump.h" 00031 00032 namespace ASSA { // @start namespace ASSA 00033 00039 class Reactor; 00040 00041 /****************************************************************************** 00042 Class Logger 00043 ******************************************************************************/ 00044 00045 class Logger : public Singleton<Logger> 00046 { 00047 public: 00048 Logger () : m_impl (NULL), m_app_name ("zombie") { /* no-op */ } 00049 ~Logger () { this->log_close (); } 00050 00051 public: 00055 void set_app_name (const std::string& appname_) { m_app_name = appname_; } 00056 00060 void enable_group (Group g_); 00061 00065 void disable_group (Group g_); 00066 00070 void enable_groups (u_long groups_); 00071 00075 void disable_groups (u_long groups_); 00076 00077 void enable_all_groups (void); 00078 void disable_all_groups (void); 00079 00080 bool group_enabled (Group g_) const; 00081 00083 void enable_timestamp (void); 00084 void disable_timestamp (void); 00085 bool timestamp_enabled (void) const; 00086 00088 void set_timezone (int zone); 00089 00090 void sign_on (const string& func_name_); 00091 void sign_off (void); 00092 00096 int log_open (u_long groups_); 00097 00106 int log_open (const char* logfname_, u_long groups_, u_long maxsize_); 00107 00118 int log_open (const std::string& logsvr_, const char* logfname_, 00119 u_long groups_, u_long maxsize_, Reactor* reactor_); 00120 00121 void log_resync (void); 00122 int log_close (void); 00123 00124 int log_msg (u_long g_, const char* fmt_, ...); 00125 int log_func (u_long g_, marker_t type_); 00126 00127 private: 00128 Logger_Impl* m_impl; 00129 stack<string> m_context; 00130 std::string m_app_name; 00131 }; 00132 00136 #define LOGGER ASSA::Logger::get_instance() 00137 00138 /******************************************************************************* 00139 Inline functions 00140 *******************************************************************************/ 00141 00142 inline void 00143 Logger:: 00144 enable_group (Group g_) 00145 { 00146 if (m_impl) { 00147 m_impl->enable_group (g_); 00148 } 00149 } 00150 00151 inline void 00152 Logger:: 00153 disable_group (Group g_) 00154 { 00155 if (m_impl) { 00156 m_impl->disable_group (g_); 00157 } 00158 } 00159 00160 inline void 00161 Logger:: 00162 enable_groups (u_long g_) 00163 { 00164 if (m_impl) { 00165 m_impl->enable_groups (g_); 00166 } 00167 } 00168 00169 inline void 00170 Logger:: 00171 disable_groups (u_long g_) 00172 { 00173 if (m_impl) { 00174 m_impl->disable_groups (g_); 00175 } 00176 } 00177 00178 inline bool 00179 Logger:: 00180 group_enabled (Group g_) const 00181 { 00182 return (m_impl) ? m_impl->group_enabled (g_) : false; 00183 } 00184 00185 inline void 00186 Logger:: 00187 enable_all_groups (void) 00188 { 00189 if (m_impl) { 00190 m_impl->enable_all_groups (); 00191 } 00192 } 00193 00194 inline void 00195 Logger:: 00196 disable_all_groups (void) 00197 { 00198 if (m_impl) { 00199 m_impl->disable_all_groups (); 00200 } 00201 } 00202 00203 inline void 00204 Logger:: 00205 enable_timestamp (void) 00206 { 00207 if (m_impl) { 00208 m_impl->enable_timestamp (); 00209 } 00210 } 00211 00212 inline void 00213 Logger:: 00214 disable_timestamp (void) 00215 { 00216 if (m_impl) { 00217 m_impl->disable_timestamp (); 00218 } 00219 } 00220 00221 inline bool 00222 Logger:: 00223 timestamp_enabled (void) const 00224 { 00225 return (m_impl) ? m_impl->timestamp_enabled () : false; 00226 } 00227 00228 inline void 00229 Logger:: 00230 set_timezone (int zone_) 00231 { 00232 if (m_impl) { 00233 m_impl->set_timezone (zone_); 00234 } 00235 } 00236 00237 inline void 00238 Logger:: 00239 log_resync (void) 00240 { 00241 if (m_impl) { 00242 m_impl->log_resync (); 00243 } 00244 } 00245 00246 inline void 00247 Logger:: 00248 sign_on (const string& func_name_) 00249 { 00250 m_context.push (func_name_); 00251 } 00252 00253 inline void 00254 Logger:: 00255 sign_off (void) 00256 { 00257 if (!m_context.empty ()) { 00258 m_context.pop (); 00259 } 00260 } 00261 00262 /******************************************************************************* 00263 Macro definition shortcuts 00264 *******************************************************************************/ 00265 00270 #if defined (ASSA_NLOGGING) 00271 # define DL(X) do {} while (0) 00272 #else 00273 # define DL(X) \ 00274 do { \ 00275 LOGGER->log_msg X; \ 00276 } while (0) 00277 #endif 00278 00282 #if defined (ASSA_NLOGGING) 00283 # define EL(X) do {} while (0) 00284 #else 00285 # define EL(X) \ 00286 do { \ 00287 LOGGER->log_msg X; \ 00288 LOGGER->log_msg(ASSA::ASSAERR,"errno: %d \"%s\"\n", errno, strerror(errno)); \ 00289 } while (0) 00290 #endif 00291 00292 /******************************************************************************* 00293 Namespace Log provides only the most plausable interface. 00294 Other public functions can be reached via LOGGER 00295 *******************************************************************************/ 00296 00297 namespace Log { // @start namespace Log 00298 00305 inline void set_app_name (const std::string& appname_) 00306 { 00307 LOGGER->set_app_name (appname_); 00308 } 00309 00319 inline int open_log_file (const char* logfname_, 00320 u_long groups_ = ALL, 00321 u_long maxsize_ = 10485760) 00322 { 00323 return LOGGER->log_open (logfname_, groups_, maxsize_); 00324 } 00325 00327 inline int open_log_stdout (u_long groups_ = ALL) 00328 { 00329 return LOGGER->log_open (groups_); 00330 } 00331 00344 inline int open_log_server (const std::string& logsvraddr_, 00345 const char* logfname_, 00346 Reactor* reactor_, 00347 u_long groups_ = ASSA::ALL, 00348 u_long maxsize_ = 10485760) 00349 { 00350 int ret = LOGGER->log_open (logsvraddr_, logfname_, groups_, 00351 maxsize_, reactor_); 00352 return ret; 00353 } 00354 00356 inline void log_resync (void) { LOGGER->log_resync (); } 00357 00359 inline int log_close (void) { return LOGGER->log_close (); } 00360 00362 inline void set_gmt_timezone (void) { LOGGER->set_timezone (0); } 00363 00365 inline void enable_timestamp (void) { LOGGER->enable_timestamp (); } 00366 00368 inline void disable_timestamp (void) { LOGGER->disable_timestamp (); } 00369 00370 } // @end namespace Log 00371 00372 //------------------------------------------------------------------------------ 00373 // DiagnosticContext 00374 //------------------------------------------------------------------------------ 00375 00379 class DiagnosticContext 00380 { 00381 public: 00382 DiagnosticContext (const char* fn_, u_long mask_ = TRACE); 00383 ~DiagnosticContext (); 00384 00385 private: 00386 DiagnosticContext (const DiagnosticContext&); 00387 DiagnosticContext& operator= (const DiagnosticContext&); 00388 00389 private: 00390 const char* m_fname; 00391 u_long m_mask; 00392 }; 00393 00394 inline 00395 DiagnosticContext:: 00396 DiagnosticContext (const char* fn_, u_long mask_) 00397 : m_fname (fn_), m_mask (mask_) 00398 { 00399 if (LOGGER->group_enabled ((ASSA::Group) m_mask)) { 00400 LOGGER->sign_on (m_fname); 00401 LOGGER->log_func (m_mask, FUNC_ENTRY); 00402 } 00403 } 00404 00405 inline 00406 DiagnosticContext:: 00407 ~DiagnosticContext () 00408 { 00409 if (LOGGER->group_enabled ((ASSA::Group) m_mask)) { 00410 LOGGER->log_func (m_mask, FUNC_EXIT); 00411 LOGGER->sign_off (); 00412 } 00413 } 00414 00429 #define trace(s) ASSA::DiagnosticContext tRaCeR(s); 00430 00437 #define trace_with_mask(s, m) ASSA::DiagnosticContext tRaCeR(s, m); 00438 00439 } // @end namespace ASSA 00440 00441 #endif /* LOGGER_H */