libassa 3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // Logger_Impl.h 00004 //------------------------------------------------------------------------------ 00005 // $Id: Logger_Impl.h,v 1.9 2006/07/20 02:30:54 vlg Exp $ 00006 //------------------------------------------------------------------------------ 00007 // Copyright (c) 2001 by Vladislav Grinchenko 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Library General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2 of the License, or (at your option) any later version. 00013 //------------------------------------------------------------------------------ 00014 #ifndef LOGGER_IMPL_H 00015 #define LOGGER_IMPL_H 00016 00017 #include <errno.h> 00018 #include <cstdarg> 00019 #include <string> 00020 00021 #if defined(sun) 00022 #include <sys/varargs.h> // va_list 00023 #endif 00024 00025 #if defined (__CYGWIN32__) || defined (__NetBSD__) || defined (WIN32) 00026 # include <stdarg.h> 00027 #endif 00028 00029 #if defined(WIN32) 00030 # include <winsock2.h> /* select(3) */ 00031 #endif 00032 00033 /* Also defined in winsock.h, winsock2.h, gmon.h and in cygwin's sys/types 00034 */ 00035 #if !defined ( _BSDTYPES_DEFINED ) 00036 00037 typedef unsigned char u_char; 00038 typedef unsigned short u_short; 00039 typedef unsigned int u_int; 00040 typedef unsigned long u_long; 00041 00042 #define _BSDTYPES_DEFINED 00043 00044 #endif /* ! def _BSDTYPES_DEFINED */ 00045 00046 using std::string; 00047 using std::ostream; 00048 00049 #include "assa/LogMask.h" 00050 00053 #if defined (WIN32) 00054 00055 typedef SOCKET handler_t; 00056 #define BAD_HANDLER INVALID_SOCKET 00057 00060 #define EINPROGRESS WSAEINPROGRESS /* A blocking Winsock call is in 00061 * progress, or the service provider 00062 * is still process a callback function. 00063 */ 00064 #define EWOULDBLOCK WSAEWOULDBLOCK /* The socket is marked as nonblocking 00065 * and the connection cannot be completed 00066 * immediately. 00067 */ 00068 #define EISCONN WSAEISCONN 00069 00070 #define ENOTSOCK WSAENOTSOCK /* The descriptor is not a socket. 00071 */ 00072 #define ECONNREFUSED WSAECONNREFUSED /* The attempt to connect was 00073 * forcefully rejected. 00074 */ 00075 #define ETIMEDOUT WSAETIMEDOUT /* An attempt to connect timed out 00076 * without establishing connection. 00077 */ 00078 #else /*--- POSIX ---*/ 00079 00080 #define BAD_HANDLER -1 00081 typedef int handler_t; 00082 00083 #endif // ifdef WIN32 00084 00085 00086 namespace ASSA { 00087 00088 class Reactor; 00089 00090 //--------------------------------------------------------------------------- 00091 // Utilities that don't fit anywhere else 00092 //--------------------------------------------------------------------------- 00093 00099 inline bool is_valid_handler (handler_t socket_) 00100 { 00101 return (socket_ != BAD_HANDLER); 00102 } 00103 00107 inline void disable_handler (handler_t& socket_) 00108 { 00109 socket_ = BAD_HANDLER; 00110 } 00111 00114 inline int get_errno () 00115 { 00116 int myerrno; 00117 #if defined (WIN32) 00118 myerrno = WSAGetLastError (); 00119 #else 00120 myerrno = errno; 00121 #endif 00122 return myerrno; 00123 } 00124 00127 inline void set_errno (int new_errno_) 00128 { 00129 #if defined (WIN32) 00130 WSASetLastError (new_errno_); 00131 #else 00132 errno = new_errno_; 00133 #endif 00134 } 00135 00136 //--------------------------------------------------------------------------- 00137 // Class Logger_Impl 00138 //--------------------------------------------------------------------------- 00139 00140 class Logger_Impl { 00141 public: 00148 static const unsigned int LOGGER_MAXLINE = 6660; 00149 00150 public: 00151 Logger_Impl (); 00152 virtual ~Logger_Impl () { /* empty */ } 00153 00154 void enable_group (Group g_) { m_groups |= g_; } 00155 void disable_group (Group g_) { m_groups &= ~g_; } 00156 00157 void enable_groups (u_long g_) { m_groups |= g_; } 00158 void disable_groups (u_long g_) { m_groups &= ~g_; } 00159 00160 void enable_all_groups (void) { m_groups = ASSA::ALL; } 00161 void disable_all_groups (void) { m_groups = 0; } 00162 00163 bool group_enabled (Group g_) const { return (m_groups & g_); } 00164 00165 void enable_timestamp (void) { m_tmflg = true; } 00166 void disable_timestamp (void) { m_tmflg = false; } 00167 bool timestamp_enabled (void) const { return m_tmflg; } 00168 void set_timezone (int zone_) { m_tz = zone_; } 00169 00170 void set_indent_step (u_short step_) { m_indent_step = step_; } 00171 u_short get_indent_step (void) const { return m_indent_step; } 00172 00174 virtual int log_open (u_long groups_); 00175 00177 virtual int log_open (const char* logfname_, 00178 u_long groups_, 00179 u_long maxsize_); 00180 00182 virtual int log_open (const char* appname_, 00183 const char* logfname_, 00184 u_long groups_, 00185 u_long maxsize_, 00186 Reactor* reactor_); 00187 00188 virtual int log_close (void) = 0; 00189 virtual void log_resync (void) { /* empty */ } 00190 00191 virtual int log_msg (Group g_, 00192 size_t indent_level_, 00193 const string& func_name_, 00194 size_t expected_sz_, 00195 const char* fmt_, 00196 va_list) = 0; 00197 00198 virtual int log_func (Group g_, 00199 size_t indent_level_, 00200 const string& func_name_, 00201 marker_t type_) = 0; 00202 protected: 00203 virtual u_short add_timestamp (ostream& sink_); 00204 virtual u_short indent_func_name (ostream& sink_, 00205 const string& funcname_, 00206 size_t indent_level_, 00207 marker_t type_); 00208 00225 char* format_msg (size_t expected_sz_, 00226 const char* fmt_, 00227 va_list vap_, 00228 bool& release_); 00229 00230 protected: 00232 static char m_msgbuf [LOGGER_MAXLINE]; 00233 00235 u_short m_indent_step; 00236 00238 u_long m_groups; 00239 00241 string m_logfname; 00242 00244 bool m_tmflg; 00245 00247 int m_tz; 00248 }; 00249 00250 inline 00251 Logger_Impl:: 00252 Logger_Impl () 00253 : m_indent_step (1), 00254 m_groups (0), 00255 m_tmflg (false), 00256 m_tz (1) 00257 { 00258 /* no-op */ 00259 } 00260 00261 inline int 00262 Logger_Impl:: 00263 log_open (u_long /* groups_ */) 00264 { 00265 errno = ENOSYS; 00266 return -1; 00267 } 00268 00269 inline int 00270 Logger_Impl:: 00271 log_open (const char*, /* logfname_ */ 00272 u_long, /* groups_ */ 00273 u_long /* maxsize_ */) 00274 { 00275 errno = ENOSYS; 00276 return -1; 00277 } 00278 00279 inline int 00280 Logger_Impl:: 00281 log_open (const char*, /* appname_ */ 00282 const char*, /* logfname_ */ 00283 u_long, /* groups_ */ 00284 u_long, /* maxsize_ */ 00285 Reactor* /* reactor_ */) 00286 { 00287 errno = ENOSYS; 00288 return -1; 00289 } 00290 00291 } // end namespace ASSA 00292 00293 #endif /* LOGGER_IMPL_H */