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 <string> 00019 00020 #if defined(sun) 00021 #include <sys/varargs.h> // va_list 00022 #endif 00023 00024 #if defined (__CYGWIN32__) || defined (__NetBSD__) || defined (WIN32) || defined(__GNUC__) 00025 # include <stdarg.h> 00026 #endif 00027 00028 #if defined(WIN32) 00029 # include <winsock2.h> /* select(3) */ 00030 #endif 00031 00032 /* Also defined in winsock.h, winsock2.h, gmon.h and in cygwin's sys/types 00033 */ 00034 #if !defined ( _BSDTYPES_DEFINED ) 00035 00036 typedef unsigned char u_char; 00037 typedef unsigned short u_short; 00038 typedef unsigned int u_int; 00039 typedef unsigned long u_long; 00040 00041 #define _BSDTYPES_DEFINED 00042 00043 #endif /* ! def _BSDTYPES_DEFINED */ 00044 00045 using std::string; 00046 using std::ostream; 00047 00048 #include "assa/LogMask.h" 00049 00052 #if defined (WIN32) 00053 00054 typedef SOCKET handler_t; 00055 #define BAD_HANDLER INVALID_SOCKET 00056 00059 #define EINPROGRESS WSAEINPROGRESS /* A blocking Winsock call is in 00060 * progress, or the service provider 00061 * is still process a callback function. 00062 */ 00063 #define EWOULDBLOCK WSAEWOULDBLOCK /* The socket is marked as nonblocking 00064 * and the connection cannot be completed 00065 * immediately. 00066 */ 00067 #define EISCONN WSAEISCONN 00068 00069 #define ENOTSOCK WSAENOTSOCK /* The descriptor is not a socket. 00070 */ 00071 #define ECONNREFUSED WSAECONNREFUSED /* The attempt to connect was 00072 * forcefully rejected. 00073 */ 00074 #define ETIMEDOUT WSAETIMEDOUT /* An attempt to connect timed out 00075 * without establishing connection. 00076 */ 00077 #else /*--- POSIX ---*/ 00078 00079 #define BAD_HANDLER -1 00080 typedef int handler_t; 00081 00082 #endif // ifdef WIN32 00083 00084 00085 namespace ASSA { 00086 00087 class Reactor; 00088 00089 //--------------------------------------------------------------------------- 00090 // Utilities that don't fit anywhere else 00091 //--------------------------------------------------------------------------- 00092 00098 inline bool is_valid_handler (handler_t socket_) 00099 { 00100 return (socket_ != BAD_HANDLER); 00101 } 00102 00106 inline void disable_handler (handler_t& socket_) 00107 { 00108 socket_ = BAD_HANDLER; 00109 } 00110 00113 inline int get_errno () 00114 { 00115 int myerrno; 00116 #if defined (WIN32) 00117 myerrno = WSAGetLastError (); 00118 #else 00119 myerrno = errno; 00120 #endif 00121 return myerrno; 00122 } 00123 00126 inline void set_errno (int new_errno_) 00127 { 00128 #if defined (WIN32) 00129 WSASetLastError (new_errno_); 00130 #else 00131 errno = new_errno_; 00132 #endif 00133 } 00134 00135 //--------------------------------------------------------------------------- 00136 // Class Logger_Impl 00137 //--------------------------------------------------------------------------- 00138 00139 class Logger_Impl { 00140 public: 00147 static const unsigned int LOGGER_MAXLINE = 6660; 00148 00149 public: 00150 Logger_Impl (); 00151 virtual ~Logger_Impl () { /* empty */ } 00152 00153 void enable_group (Group g_) { m_groups |= g_; } 00154 void disable_group (Group g_) { m_groups &= ~g_; } 00155 00156 void enable_groups (u_long g_) { m_groups |= g_; } 00157 void disable_groups (u_long g_) { m_groups &= ~g_; } 00158 00159 void enable_all_groups (void) { m_groups = ASSA::ALL; } 00160 void disable_all_groups (void) { m_groups = 0; } 00161 00162 bool group_enabled (Group g_) const { return (m_groups & g_); } 00163 00164 void enable_timestamp (void) { m_tmflg = true; } 00165 void disable_timestamp (void) { m_tmflg = false; } 00166 bool timestamp_enabled (void) const { return m_tmflg; } 00167 void set_timezone (int zone_) { m_tz = zone_; } 00168 00169 void set_indent_step (u_short step_) { m_indent_step = step_; } 00170 u_short get_indent_step (void) const { return m_indent_step; } 00171 00173 virtual int log_open (u_long groups_); 00174 00176 virtual int log_open (const char* logfname_, 00177 u_long groups_, 00178 u_long maxsize_); 00179 00181 virtual int log_open (const char* appname_, 00182 const char* logfname_, 00183 u_long groups_, 00184 u_long maxsize_, 00185 Reactor* reactor_); 00186 00187 virtual int log_close (void) = 0; 00188 virtual void log_resync (void) { /* empty */ } 00189 00190 virtual int log_msg (Group g_, 00191 size_t indent_level_, 00192 const string& func_name_, 00193 size_t expected_sz_, 00194 const char* fmt_, 00195 va_list) = 0; 00196 00197 virtual int log_func (Group g_, 00198 size_t indent_level_, 00199 const string& func_name_, 00200 marker_t type_) = 0; 00201 protected: 00202 virtual u_short add_timestamp (ostream& sink_); 00203 virtual u_short indent_func_name (ostream& sink_, 00204 const string& funcname_, 00205 size_t indent_level_, 00206 marker_t type_); 00207 00224 char* format_msg (size_t expected_sz_, 00225 const char* fmt_, 00226 va_list vap_, 00227 bool& release_); 00228 00229 protected: 00231 static char m_msgbuf [LOGGER_MAXLINE]; 00232 00234 u_short m_indent_step; 00235 00237 u_long m_groups; 00238 00240 string m_logfname; 00241 00243 bool m_tmflg; 00244 00246 int m_tz; 00247 }; 00248 00249 inline 00250 Logger_Impl:: 00251 Logger_Impl () 00252 : m_indent_step (1), 00253 m_groups (0), 00254 m_tmflg (false), 00255 m_tz (1) 00256 { 00257 /* no-op */ 00258 } 00259 00260 inline int 00261 Logger_Impl:: 00262 log_open (u_long /* groups_ */) 00263 { 00264 errno = ENOSYS; 00265 return -1; 00266 } 00267 00268 inline int 00269 Logger_Impl:: 00270 log_open (const char*, /* logfname_ */ 00271 u_long, /* groups_ */ 00272 u_long /* maxsize_ */) 00273 { 00274 errno = ENOSYS; 00275 return -1; 00276 } 00277 00278 inline int 00279 Logger_Impl:: 00280 log_open (const char*, /* appname_ */ 00281 const char*, /* logfname_ */ 00282 u_long, /* groups_ */ 00283 u_long, /* maxsize_ */ 00284 Reactor* /* reactor_ */) 00285 { 00286 errno = ENOSYS; 00287 return -1; 00288 } 00289 00290 } // end namespace ASSA 00291 00292 #endif /* LOGGER_IMPL_H */