00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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>
00022 #endif
00023
00024 #if defined (__CYGWIN32__) || defined (__NetBSD__) || defined (WIN32)
00025 # include <stdarg.h>
00026 #endif
00027
00028 #if defined(WIN32)
00029 # include <winsock2.h>
00030 #endif
00031
00032
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
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
00060
00061
00062
00063 #define EWOULDBLOCK WSAEWOULDBLOCK
00064
00065
00066
00067 #define EISCONN WSAEISCONN
00068
00069 #define ENOTSOCK WSAENOTSOCK
00070
00071 #define ECONNREFUSED WSAECONNREFUSED
00072
00073
00074 #define ETIMEDOUT WSAETIMEDOUT
00075
00076
00077 #else
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
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
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 () { }
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) { }
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
00258 }
00259
00260 inline int
00261 Logger_Impl::
00262 log_open (u_long )
00263 {
00264 errno = ENOSYS;
00265 return -1;
00266 }
00267
00268 inline int
00269 Logger_Impl::
00270 log_open (const char*,
00271 u_long,
00272 u_long )
00273 {
00274 errno = ENOSYS;
00275 return -1;
00276 }
00277
00278 inline int
00279 Logger_Impl::
00280 log_open (const char*,
00281 const char*,
00282 u_long,
00283 u_long,
00284 Reactor* )
00285 {
00286 errno = ENOSYS;
00287 return -1;
00288 }
00289
00290 }
00291
00292 #endif