libassa 3.5.1
Loading...
Searching...
No Matches
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
23using std::string;
24using std::stack;
25
26//ASSA headers
27
28#include "assa/Logger_Impl.h"
29#include "assa/Singleton.h"
30#include "assa/MemDump.h"
31
32namespace ASSA { // @start namespace ASSA
33
39class Reactor;
40
41/******************************************************************************
42 Class Logger
43******************************************************************************/
44
45class Logger : public Singleton<Logger>
46{
47public:
48 Logger () : m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
49 ~Logger () { this->log_close (); }
50
51public:
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
71
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
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_,
120
121 void log_resync (void);
122 int log_close (void);
123
124 int log_msg (u_long g_, const char* fmt_, ...);
126
127private:
130 std::string m_app_name;
131};
132
136#define LOGGER ASSA::Logger::get_instance()
137
138/*******************************************************************************
139 Inline functions
140*******************************************************************************/
141
142inline void
145{
146 if (m_impl) {
148 }
149}
150
151inline void
154{
155 if (m_impl) {
157 }
158}
159
160inline void
163{
164 if (m_impl) {
166 }
167}
168
169inline void
172{
173 if (m_impl) {
175 }
176}
177
178inline bool
180group_enabled (Group g_) const
181{
182 return (m_impl) ? m_impl->group_enabled (g_) : false;
183}
184
185inline void
188{
189 if (m_impl) {
191 }
192}
193
194inline void
197{
198 if (m_impl) {
200 }
201}
202
203inline void
205enable_timestamp (void)
206{
207 if (m_impl) {
209 }
210}
211
212inline void
215{
216 if (m_impl) {
218 }
219}
220
221inline bool
223timestamp_enabled (void) const
224{
225 return (m_impl) ? m_impl->timestamp_enabled () : false;
226}
227
228inline void
231{
232 if (m_impl) {
234 }
235}
236
237inline void
239log_resync (void)
240{
241 if (m_impl) {
242 m_impl->log_resync ();
243 }
244}
245
246inline void
248sign_on (const string& func_name_)
249{
250 m_context.push (func_name_);
251}
252
253inline void
255sign_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
297namespace 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
328 {
329 return LOGGER->log_open (groups_);
330 }
331
344 inline int open_log_server (const std::string& logsvraddr_,
345 const char* logfname_,
348 u_long maxsize_ = 10485760)
349 {
350 int ret = LOGGER->log_open (logsvraddr_, logfname_, groups_,
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{
381public:
382 DiagnosticContext (const char* fn_, u_long mask_ = TRACE);
384
385private:
388
389private:
390 const char* m_fname;
392};
393
394inline
396DiagnosticContext (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
405inline
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.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
Class DiagnosticContext tracks who deep a function is in the calling stack.
Definition Logger.h:380
DiagnosticContext & operator=(const DiagnosticContext &)
const char * m_fname
Definition Logger.h:390
DiagnosticContext(const char *fn_, u_long mask_=TRACE)
Definition Logger.h:396
DiagnosticContext(const DiagnosticContext &)
bool group_enabled(Group g_) const
void enable_all_groups(void)
virtual void log_resync(void)
bool timestamp_enabled(void) const
void disable_all_groups(void)
void enable_timestamp(void)
void disable_groups(u_long g_)
void disable_timestamp(void)
void enable_group(Group g_)
void set_timezone(int zone_)
void disable_group(Group g_)
void enable_groups(u_long g_)
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
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