libassa  3.5.1
Logger.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Logger.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 2001 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 #ifndef LOGGER_H
13 #define LOGGER_H
14 
15 //System headers
16 #include <sys/types.h>
17 #include <string.h>
18 #include <stdio.h>
19 
20 #include <string>
21 #include <stack>
22 
23 using std::string;
24 using std::stack;
25 
26 //ASSA headers
27 
28 #include "assa/Logger_Impl.h"
29 #include "assa/Singleton.h"
30 #include "assa/MemDump.h"
31 
32 namespace ASSA { // @start namespace ASSA
33 
39 class Reactor;
40 
41 /******************************************************************************
42  Class Logger
43 ******************************************************************************/
44 
45 class Logger : public Singleton<Logger>
46 {
47 public:
48  Logger () : m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
49  ~Logger () { this->log_close (); }
50 
51 public:
55  void set_app_name (const std::string& appname_) { m_app_name = appname_; }
56 
60  void enable_group (Group g_);
61 
65  void disable_group (Group g_);
66 
70  void enable_groups (u_long groups_);
71 
75  void disable_groups (u_long groups_);
76 
77  void enable_all_groups (void);
78  void disable_all_groups (void);
79 
80  bool group_enabled (Group g_) const;
81 
83  void enable_timestamp (void);
84  void disable_timestamp (void);
85  bool timestamp_enabled (void) const;
86 
88  void set_timezone (int zone);
89 
90  void sign_on (const string& func_name_);
91  void sign_off (void);
92 
96  int log_open (u_long groups_);
97 
106  int log_open (const char* logfname_, u_long groups_, u_long maxsize_);
107 
118  int log_open (const std::string& logsvr_, const char* logfname_,
119  u_long groups_, u_long maxsize_, Reactor* reactor_);
120 
121  void log_resync (void);
122  int log_close (void);
123 
124  int log_msg (u_long g_, const char* fmt_, ...);
125  int log_func (u_long g_, marker_t type_);
126 
127 private:
129  stack<string> m_context;
130  std::string m_app_name;
131 };
132 
136 #define LOGGER ASSA::Logger::get_instance()
137 
138 /*******************************************************************************
139  Inline functions
140 *******************************************************************************/
141 
142 inline void
144 enable_group (Group g_)
145 {
146  if (m_impl) {
147  m_impl->enable_group (g_);
148  }
149 }
150 
151 inline void
153 disable_group (Group g_)
154 {
155  if (m_impl) {
156  m_impl->disable_group (g_);
157  }
158 }
159 
160 inline void
162 enable_groups (u_long g_)
163 {
164  if (m_impl) {
165  m_impl->enable_groups (g_);
166  }
167 }
168 
169 inline void
172 {
173  if (m_impl) {
174  m_impl->disable_groups (g_);
175  }
176 }
177 
178 inline bool
180 group_enabled (Group g_) const
181 {
182  return (m_impl) ? m_impl->group_enabled (g_) : false;
183 }
184 
185 inline void
187 enable_all_groups (void)
188 {
189  if (m_impl) {
191  }
192 }
193 
194 inline void
196 disable_all_groups (void)
197 {
198  if (m_impl) {
200  }
201 }
202 
203 inline void
205 enable_timestamp (void)
206 {
207  if (m_impl) {
209  }
210 }
211 
212 inline void
214 disable_timestamp (void)
215 {
216  if (m_impl) {
218  }
219 }
220 
221 inline bool
223 timestamp_enabled (void) const
224 {
225  return (m_impl) ? m_impl->timestamp_enabled () : false;
226 }
227 
228 inline void
230 set_timezone (int zone_)
231 {
232  if (m_impl) {
233  m_impl->set_timezone (zone_);
234  }
235 }
236 
237 inline void
239 log_resync (void)
240 {
241  if (m_impl) {
242  m_impl->log_resync ();
243  }
244 }
245 
246 inline void
248 sign_on (const string& func_name_)
249 {
250  m_context.push (func_name_);
251 }
252 
253 inline void
255 sign_off (void)
256 {
257  if (!m_context.empty ()) {
258  m_context.pop ();
259  }
260 }
261 
262 /*******************************************************************************
263  Macro definition shortcuts
264 *******************************************************************************/
265 
270 #if defined (ASSA_NLOGGING)
271 # define DL(X) do {} while (0)
272 #else
273 # define DL(X) \
274  do { \
275  LOGGER->log_msg X; \
276  } while (0)
277 #endif
278 
282 #if defined (ASSA_NLOGGING)
283 # define EL(X) do {} while (0)
284 #else
285 # define EL(X) \
286  do { \
287  LOGGER->log_msg X; \
288  LOGGER->log_msg(ASSA::ASSAERR,"errno: %d \"%s\"\n", errno, strerror(errno)); \
289  } while (0)
290 #endif
291 
292 /*******************************************************************************
293  Namespace Log provides only the most plausable interface.
294  Other public functions can be reached via LOGGER
295 *******************************************************************************/
296 
297 namespace Log { // @start namespace Log
298 
305  inline void set_app_name (const std::string& appname_)
306  {
307  LOGGER->set_app_name (appname_);
308  }
309 
319  inline int open_log_file (const char* logfname_,
320  u_long groups_ = ALL,
321  u_long maxsize_ = 10485760)
322  {
323  return LOGGER->log_open (logfname_, groups_, maxsize_);
324  }
325 
327  inline int open_log_stdout (u_long groups_ = ALL)
328  {
329  return LOGGER->log_open (groups_);
330  }
331 
344  inline int open_log_server (const std::string& logsvraddr_,
345  const char* logfname_,
346  Reactor* reactor_,
347  u_long groups_ = ASSA::ALL,
348  u_long maxsize_ = 10485760)
349  {
350  int ret = LOGGER->log_open (logsvraddr_, logfname_, groups_,
351  maxsize_, reactor_);
352  return ret;
353  }
354 
356  inline void log_resync (void) { LOGGER->log_resync (); }
357 
359  inline int log_close (void) { return LOGGER->log_close (); }
360 
362  inline void set_gmt_timezone (void) { LOGGER->set_timezone (0); }
363 
365  inline void enable_timestamp (void) { LOGGER->enable_timestamp (); }
366 
368  inline void disable_timestamp (void) { LOGGER->disable_timestamp (); }
369 
370 } // @end namespace Log
371 
372 //------------------------------------------------------------------------------
373 // DiagnosticContext
374 //------------------------------------------------------------------------------
375 
380 {
381 public:
382  DiagnosticContext (const char* fn_, u_long mask_ = TRACE);
384 
385 private:
388 
389 private:
390  const char* m_fname;
392 };
393 
394 inline
396 DiagnosticContext (const char* fn_, u_long mask_)
397  : m_fname (fn_), m_mask (mask_)
398 {
399  if (LOGGER->group_enabled ((ASSA::Group) m_mask)) {
400  LOGGER->sign_on (m_fname);
401  LOGGER->log_func (m_mask, FUNC_ENTRY);
402  }
403 }
404 
405 inline
408 {
409  if (LOGGER->group_enabled ((ASSA::Group) m_mask)) {
410  LOGGER->log_func (m_mask, FUNC_EXIT);
411  LOGGER->sign_off ();
412  }
413 }
414 
429 #define trace(s) ASSA::DiagnosticContext tRaCeR(s);
430 
437 #define trace_with_mask(s, m) ASSA::DiagnosticContext tRaCeR(s, m);
438 
439 } // @end namespace ASSA
440 
441 #endif /* LOGGER_H */
#define LOGGER
A shortcut to locate a singleton object of class Logger.
Definition: Logger.h:136
unsigned long u_long
Definition: Logger_Impl.h:41
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
Class DiagnosticContext tracks who deep a function is in the calling stack.
Definition: Logger.h:380
const char * m_fname
Definition: Logger.h:390
DiagnosticContext(const char *fn_, u_long mask_=TRACE)
Definition: Logger.h:396
DiagnosticContext(const DiagnosticContext &)
DiagnosticContext & operator=(const DiagnosticContext &)
bool group_enabled(Group g_) const
Definition: Logger_Impl.h:164
void enable_all_groups(void)
Definition: Logger_Impl.h:161
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
void enable_timestamp(void)
Definition: Logger_Impl.h:166
void disable_groups(u_long g_)
Definition: Logger_Impl.h:159
void disable_timestamp(void)
Definition: Logger_Impl.h:167
void enable_group(Group g_)
Definition: Logger_Impl.h:155
void set_timezone(int zone_)
Definition: Logger_Impl.h:169
void disable_group(Group g_)
Definition: Logger_Impl.h:156
void enable_groups(u_long g_)
Definition: Logger_Impl.h:158
void sign_on(const string &func_name_)
Definition: Logger.h:248
int log_msg(u_long g_, const char *fmt_,...)
Here is an interesting twist introduced by remote logging server:
Definition: Logger.cpp:144
void log_resync(void)
Definition: Logger.h:239
void disable_timestamp(void)
Definition: Logger.h:214
int log_open(u_long groups_)
Write log messages to standard output.
Definition: Logger.cpp:53
int log_close(void)
Definition: Logger.cpp:39
void enable_timestamp(void)
Add optional timezone: GMT vs. Local.
Definition: Logger.h:205
int log_func(u_long g_, marker_t type_)
Definition: Logger.cpp:230
void set_timezone(int zone)
0 - GMT, 1 - LOCAL
Definition: Logger.h:230
void disable_groups(u_long groups_)
Disable logging for groups_.
Definition: Logger.h:171
void set_app_name(const std::string &appname_)
Set application name.
Definition: Logger.h:55
void disable_group(Group g_)
Disable logging for group g_.
Definition: Logger.h:153
void enable_group(Group g_)
Enable logging for group g_.
Definition: Logger.h:144
void enable_all_groups(void)
Definition: Logger.h:187
std::string m_app_name
Stack of all contexts.
Definition: Logger.h:130
void enable_groups(u_long groups_)
Enable logging for groups_.
Definition: Logger.h:162
bool group_enabled(Group g_) const
Definition: Logger.h:180
Logger_Impl * m_impl
Definition: Logger.h:128
void sign_off(void)
Definition: Logger.h:255
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
bool timestamp_enabled(void) const
Definition: Logger.h:223
void disable_all_groups(void)
Definition: Logger.h:196
int open_log_file(const char *logfname_, u_long groups_=ALL, u_long maxsize_=10485760)
Open log file.
Definition: Logger.h:319
void disable_timestamp(void)
Disable timestamp logging.
Definition: Logger.h:368
void enable_timestamp(void)
Enable timestamp logging.
Definition: Logger.h:365
int log_close(void)
Close logging stream.
Definition: Logger.h:359
void log_resync(void)
Resynchronize logging stream after Fork.
Definition: Logger.h:356
void set_app_name(const std::string &appname_)
Set application name.
Definition: Logger.h:305
int open_log_server(const std::string &logsvraddr_, const char *logfname_, Reactor *reactor_, u_long groups_=ASSA::ALL, u_long maxsize_=10485760)
Open connection with and write log message to the log server.
Definition: Logger.h:344
int open_log_stdout(u_long groups_=ALL)
Write log message to standard output.
Definition: Logger.h:327
void set_gmt_timezone(void)
Set timezone to GMT.
Definition: Logger.h:362
Definition: Acceptor.h:40
marker_t
Definition: LogMask.h:67
@ FUNC_ENTRY
Definition: LogMask.h:69
@ FUNC_EXIT
Definition: LogMask.h:70
Group
Definition: LogMask.h:25
@ TRACE
Function call trace
Definition: LogMask.h:26
@ ALL
All messages: library + application
Definition: LogMask.h:62