libassa  3.5.1
Logger_Impl.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Logger_Impl.h
4 //------------------------------------------------------------------------------
5 // $Id: Logger_Impl.h,v 1.12 2012/05/21 03:20:39 vlg Exp $
6 //------------------------------------------------------------------------------
7 // Copyright (c) 2001 Vladislav Grinchenko
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 #ifndef LOGGER_IMPL_H
15 #define LOGGER_IMPL_H
16 
17 #include <errno.h>
18 #include <cstdarg>
19 #include <string>
20 #include <stdio.h>
21 
22 #if defined(sun)
23 #include <sys/varargs.h> // va_list
24 #endif
25 
26 #if defined (__CYGWIN32__) || defined (__NetBSD__) || defined (WIN32) || defined (__GLIBC__)
27 # include <stdarg.h>
28 #endif
29 
30 #if defined(WIN32)
31 # include <winsock2.h> /* select(3) */
32 #endif
33 
34 /* Also defined in winsock.h, winsock2.h, gmon.h and in cygwin's sys/types
35 */
36 #if !defined ( _BSDTYPES_DEFINED )
37 
38 typedef unsigned char u_char;
39 typedef unsigned short u_short;
40 typedef unsigned int u_int;
41 typedef unsigned long u_long;
42 
43 #define _BSDTYPES_DEFINED
44 
45 #endif /* ! def _BSDTYPES_DEFINED */
46 
47 using std::string;
48 using std::ostream;
49 
50 #include "assa/LogMask.h"
51 
54 #if defined (WIN32)
55 
56 typedef SOCKET handler_t;
57 #define BAD_HANDLER INVALID_SOCKET
58 
61 #define EINPROGRESS WSAEINPROGRESS /* A blocking Winsock call is in
62  * progress, or the service provider
63  * is still process a callback function.
64  */
65 #define EWOULDBLOCK WSAEWOULDBLOCK /* The socket is marked as nonblocking
66  * and the connection cannot be completed
67  * immediately.
68  */
69 #define EISCONN WSAEISCONN
70 
71 #define ENOTSOCK WSAENOTSOCK /* The descriptor is not a socket.
72  */
73 #define ECONNREFUSED WSAECONNREFUSED /* The attempt to connect was
74  * forcefully rejected.
75  */
76 #define ETIMEDOUT WSAETIMEDOUT /* An attempt to connect timed out
77  * without establishing connection.
78  */
79 #else /*--- POSIX ---*/
80 
81 #define BAD_HANDLER -1
82 typedef int handler_t;
83 
84 #endif // ifdef WIN32
85 
86 
87 namespace ASSA {
88 
89 class Reactor;
90 
91 //---------------------------------------------------------------------------
92 // Utilities that don't fit anywhere else
93 //---------------------------------------------------------------------------
94 
100  inline bool is_valid_handler (handler_t socket_)
101  {
102  return (socket_ != BAD_HANDLER);
103  }
104 
108  inline void disable_handler (handler_t& socket_)
109  {
110  socket_ = BAD_HANDLER;
111  }
112 
115  inline int get_errno ()
116  {
117  int myerrno;
118 #if defined (WIN32)
119  myerrno = WSAGetLastError ();
120 #else
121  myerrno = errno;
122 #endif
123  return myerrno;
124  }
125 
128  inline void set_errno (int new_errno_)
129  {
130 #if defined (WIN32)
131  WSASetLastError (new_errno_);
132 #else
133  errno = new_errno_;
134 #endif
135  }
136 
137 //---------------------------------------------------------------------------
138 // Class Logger_Impl
139 //---------------------------------------------------------------------------
140 
141 class Logger_Impl {
142 public:
149  static const unsigned int LOGGER_MAXLINE = 6660;
150 
151 public:
152  Logger_Impl ();
153  virtual ~Logger_Impl () { /* empty */ }
154 
155  void enable_group (Group g_) { m_groups |= g_; }
156  void disable_group (Group g_) { m_groups &= ~g_; }
157 
158  void enable_groups (u_long g_) { m_groups |= g_; }
159  void disable_groups (u_long g_) { m_groups &= ~g_; }
160 
162  void disable_all_groups (void) { m_groups = 0; }
163 
164  bool group_enabled (Group g_) const { return (m_groups & g_); }
165 
166  void enable_timestamp (void) { m_tmflg = true; }
167  void disable_timestamp (void) { m_tmflg = false; }
168  bool timestamp_enabled (void) const { return m_tmflg; }
169  void set_timezone (int zone_) { m_tz = zone_; }
170 
171  void set_indent_step (u_short step_) { m_indent_step = step_; }
172  u_short get_indent_step (void) const { return m_indent_step; }
173 
175  virtual int log_open (u_long groups_);
176 
178  virtual int log_open (const char* logfname_,
179  u_long groups_,
180  u_long maxsize_);
181 
183  virtual int log_open (const char* appname_,
184  const char* logfname_,
185  u_long groups_,
186  u_long maxsize_,
187  Reactor* reactor_);
188 
189  virtual int log_close (void) = 0;
190  virtual void log_resync (void) { /* empty */ }
191 
192  virtual int log_msg (Group g_,
193  size_t indent_level_,
194  const string& func_name_,
195  size_t expected_sz_,
196  const char* fmt_,
197  va_list) = 0;
198 
199  virtual int log_func (Group g_,
200  size_t indent_level_,
201  const string& func_name_,
202  marker_t type_) = 0;
203 protected:
204  virtual u_short add_timestamp (ostream& sink_);
205  virtual u_short indent_func_name (ostream& sink_,
206  const string& funcname_,
207  size_t indent_level_,
208  marker_t type_);
209 
226  char* format_msg (size_t expected_sz_,
227  const char* fmt_,
228  va_list vap_,
229  bool& release_);
230 
231 protected:
233  static char m_msgbuf [LOGGER_MAXLINE];
234 
237 
240 
242  string m_logfname;
243 
245  bool m_tmflg;
246 
248  int m_tz;
249 };
250 
251 inline
253 Logger_Impl ()
254  : m_indent_step (1),
255  m_groups (0),
256  m_tmflg (false),
257  m_tz (1)
258 {
259  /* no-op */
260 }
261 
262 inline int
264 log_open (u_long /* groups_ */)
265 {
266  errno = ENOSYS;
267  return -1;
268 }
269 
270 inline int
272 log_open (const char*, /* logfname_ */
273  u_long, /* groups_ */
274  u_long /* maxsize_ */)
275 {
276  errno = ENOSYS;
277  return -1;
278 }
279 
280 inline int
282 log_open (const char*, /* appname_ */
283  const char*, /* logfname_ */
284  u_long, /* groups_ */
285  u_long, /* maxsize_ */
286  Reactor* /* reactor_ */)
287 {
288  errno = ENOSYS;
289  return -1;
290 }
291 
292 } // end namespace ASSA
293 
294 #endif /* LOGGER_IMPL_H */
Bit mask used by Logger.
#define BAD_HANDLER
Sort out WIN32/mingw oddities.
Definition: Logger_Impl.h:81
unsigned long u_long
Definition: Logger_Impl.h:41
unsigned short u_short
Definition: Logger_Impl.h:39
int handler_t
Definition: Logger_Impl.h:82
unsigned int u_int
Definition: Logger_Impl.h:40
unsigned char u_char
Definition: Logger_Impl.h:38
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
Definition: Logger_Impl.cpp:86
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition: Logger_Impl.cpp:54
virtual int log_close(void)=0
bool group_enabled(Group g_) const
Definition: Logger_Impl.h:164
void enable_all_groups(void)
Definition: Logger_Impl.h:161
u_short m_indent_step
Indentation step.
Definition: Logger_Impl.h:236
int m_tz
Timezone: 0-GMT, 1-Local.
Definition: Logger_Impl.h:248
u_long m_groups
Enabled groups.
Definition: Logger_Impl.h:239
virtual void log_resync(void)
Definition: Logger_Impl.h:190
bool timestamp_enabled(void) const
Definition: Logger_Impl.h:168
void disable_all_groups(void)
Definition: Logger_Impl.h:162
string m_logfname
Log file name.
Definition: Logger_Impl.h:242
static char m_msgbuf[LOGGER_MAXLINE]
Static buffer for formatted message.
Definition: Logger_Impl.h:233
void set_indent_step(u_short step_)
Definition: Logger_Impl.h:171
virtual ~Logger_Impl()
Definition: Logger_Impl.h:153
void enable_timestamp(void)
Definition: Logger_Impl.h:166
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)=0
void disable_groups(u_long g_)
Definition: Logger_Impl.h:159
u_short get_indent_step(void) const
Definition: Logger_Impl.h:172
void disable_timestamp(void)
Definition: Logger_Impl.h:167
void enable_group(Group g_)
Definition: Logger_Impl.h:155
static const unsigned int LOGGER_MAXLINE
Maximum length of the formatted message.
Definition: Logger_Impl.h:149
void set_timezone(int zone_)
Definition: Logger_Impl.h:169
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition: Logger_Impl.h:264
void disable_group(Group g_)
Definition: Logger_Impl.h:156
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)=0
virtual u_short add_timestamp(ostream &sink_)
Definition: Logger_Impl.cpp:35
void enable_groups(u_long g_)
Definition: Logger_Impl.h:158
bool m_tmflg
Timestamp on/off flag.
Definition: Logger_Impl.h:245
Definition: Acceptor.h:40
void set_errno(int new_errno_)
Set error number in a portable way.
Definition: Logger_Impl.h:128
marker_t
Definition: LogMask.h:67
Group
Definition: LogMask.h:25
@ ALL
All messages: library + application
Definition: LogMask.h:62
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition: Logger_Impl.h:100
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition: Logger_Impl.h:108
int get_errno()
Fetch error number in a portable way.
Definition: Logger_Impl.h:115