libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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
143 Logger::
145 {
146  if (m_impl) {
147  m_impl->enable_group (g_);
148  }
149 }
150 
151 inline void
152 Logger::
154 {
155  if (m_impl) {
156  m_impl->disable_group (g_);
157  }
158 }
159 
160 inline void
161 Logger::
163 {
164  if (m_impl) {
165  m_impl->enable_groups (g_);
166  }
167 }
168 
169 inline void
170 Logger::
172 {
173  if (m_impl) {
174  m_impl->disable_groups (g_);
175  }
176 }
177 
178 inline bool
179 Logger::
181 {
182  return (m_impl) ? m_impl->group_enabled (g_) : false;
183 }
184 
185 inline void
186 Logger::
188 {
189  if (m_impl) {
191  }
192 }
193 
194 inline void
195 Logger::
197 {
198  if (m_impl) {
200  }
201 }
202 
203 inline void
204 Logger::
206 {
207  if (m_impl) {
209  }
210 }
211 
212 inline void
213 Logger::
215 {
216  if (m_impl) {
218  }
219 }
220 
221 inline bool
222 Logger::
223 timestamp_enabled (void) const
224 {
225  return (m_impl) ? m_impl->timestamp_enabled () : false;
226 }
227 
228 inline void
229 Logger::
230 set_timezone (int zone_)
231 {
232  if (m_impl) {
233  m_impl->set_timezone (zone_);
234  }
235 }
236 
237 inline void
238 Logger::
239 log_resync (void)
240 {
241  if (m_impl) {
242  m_impl->log_resync ();
243  }
244 }
245 
246 inline void
247 Logger::
248 sign_on (const string& func_name_)
249 {
250  m_context.push (func_name_);
251 }
252 
253 inline void
254 Logger::
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 */