libassa  3.5.1
Public Member Functions | Private Attributes | List of all members
ASSA::Logger Class Reference

#include <Logger.h>

Inheritance diagram for ASSA::Logger:
ASSA::Singleton< Logger >

Public Member Functions

 Logger ()
 
 ~Logger ()
 
void set_app_name (const std::string &appname_)
 Set application name. More...
 
void enable_group (Group g_)
 Enable logging for group g_. More...
 
void disable_group (Group g_)
 Disable logging for group g_. More...
 
void enable_groups (u_long groups_)
 Enable logging for groups_. More...
 
void disable_groups (u_long groups_)
 Disable logging for groups_. More...
 
void enable_all_groups (void)
 
void disable_all_groups (void)
 
bool group_enabled (Group g_) const
 
void enable_timestamp (void)
 Add optional timezone: GMT vs. Local. More...
 
void disable_timestamp (void)
 
bool timestamp_enabled (void) const
 
void set_timezone (int zone)
 0 - GMT, 1 - LOCAL More...
 
void sign_on (const string &func_name_)
 
void sign_off (void)
 
int log_open (u_long groups_)
 Write log messages to standard output. More...
 
int log_open (const char *logfname_, u_long groups_, u_long maxsize_)
 Write log messages to the logfile. More...
 
int log_open (const std::string &logsvr_, const char *logfname_, u_long groups_, u_long maxsize_, Reactor *reactor_)
 Write log messages to the log server assa-logd. More...
 
void log_resync (void)
 
int log_close (void)
 
int log_msg (u_long g_, const char *fmt_,...)
 Here is an interesting twist introduced by remote logging server: More...
 
int log_func (u_long g_, marker_t type_)
 

Private Attributes

Logger_Implm_impl
 
stack< string > m_context
 Logger implementation. More...
 
std::string m_app_name
 Stack of all contexts. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from ASSA::Singleton< Logger >
static Loggerget_instance ()
 Return an instance of templated class T. More...
 
- Protected Member Functions inherited from ASSA::Singleton< Logger >
 Singleton ()
 Protected Constructor. More...
 
virtual ~Singleton ()
 Virtual Destructor. More...
 

Detailed Description

Definition at line 45 of file Logger.h.

Constructor & Destructor Documentation

ASSA::Logger::Logger ( )
inline

Definition at line 48 of file Logger.h.

48 : m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
std::string m_app_name
Stack of all contexts.
Definition: Logger.h:130
Logger_Impl * m_impl
Definition: Logger.h:128
ASSA::Logger::~Logger ( )
inline

Definition at line 49 of file Logger.h.

References log_close().

49 { this->log_close (); }
int log_close(void)
Definition: Logger.cpp:39

Member Function Documentation

void ASSA::Logger::disable_all_groups ( void  )
inline

Definition at line 196 of file Logger.h.

References ASSA::Logger_Impl::disable_all_groups(), and m_impl.

197 {
198  if (m_impl) {
200  }
201 }
void disable_all_groups(void)
Definition: Logger_Impl.h:162
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::disable_group ( Group  g_)
inline

Disable logging for group g_.

Parameters
g_Group to disable

Definition at line 153 of file Logger.h.

References ASSA::Logger_Impl::disable_group(), and m_impl.

154 {
155  if (m_impl) {
156  m_impl->disable_group (g_);
157  }
158 }
void disable_group(Group g_)
Definition: Logger_Impl.h:156
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::disable_groups ( u_long  groups_)
inline

Disable logging for groups_.

Parameters
groups_Groups to disable

Definition at line 171 of file Logger.h.

References ASSA::Logger_Impl::disable_groups(), and m_impl.

172 {
173  if (m_impl) {
174  m_impl->disable_groups (g_);
175  }
176 }
void disable_groups(u_long g_)
Definition: Logger_Impl.h:159
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::disable_timestamp ( void  )
inline

Definition at line 214 of file Logger.h.

References ASSA::Logger_Impl::disable_timestamp(), and m_impl.

215 {
216  if (m_impl) {
218  }
219 }
void disable_timestamp(void)
Definition: Logger_Impl.h:167
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::enable_all_groups ( void  )
inline

Definition at line 187 of file Logger.h.

References ASSA::Logger_Impl::enable_all_groups(), and m_impl.

188 {
189  if (m_impl) {
191  }
192 }
void enable_all_groups(void)
Definition: Logger_Impl.h:161
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::enable_group ( Group  g_)
inline

Enable logging for group g_.

Parameters
g_Group to enable

Definition at line 144 of file Logger.h.

References ASSA::Logger_Impl::enable_group(), and m_impl.

145 {
146  if (m_impl) {
147  m_impl->enable_group (g_);
148  }
149 }
Logger_Impl * m_impl
Definition: Logger.h:128
void enable_group(Group g_)
Definition: Logger_Impl.h:155
void ASSA::Logger::enable_groups ( u_long  groups_)
inline

Enable logging for groups_.

Parameters
groups_Groups to enable

Definition at line 162 of file Logger.h.

References ASSA::Logger_Impl::enable_groups(), and m_impl.

163 {
164  if (m_impl) {
165  m_impl->enable_groups (g_);
166  }
167 }
void enable_groups(u_long g_)
Definition: Logger_Impl.h:158
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::enable_timestamp ( void  )
inline

Add optional timezone: GMT vs. Local.

Definition at line 205 of file Logger.h.

References ASSA::Logger_Impl::enable_timestamp(), and m_impl.

206 {
207  if (m_impl) {
209  }
210 }
void enable_timestamp(void)
Definition: Logger_Impl.h:166
Logger_Impl * m_impl
Definition: Logger.h:128
bool ASSA::Logger::group_enabled ( Group  g_) const
inline

Definition at line 180 of file Logger.h.

References ASSA::Logger_Impl::group_enabled(), and m_impl.

181 {
182  return (m_impl) ? m_impl->group_enabled (g_) : false;
183 }
bool group_enabled(Group g_) const
Definition: Logger_Impl.h:164
Logger_Impl * m_impl
Definition: Logger.h:128
int Logger::log_close ( void  )

Definition at line 39 of file Logger.cpp.

References ASSA::Logger_Impl::log_close(), and m_impl.

Referenced by ~Logger().

40 {
41  int ret = 0;
42 
43  if (m_impl) {
44  ret = m_impl->log_close ();
45  delete m_impl;
46  m_impl = 0;
47  }
48  return ret;
49 }
virtual int log_close(void)=0
Logger_Impl * m_impl
Definition: Logger.h:128
int Logger::log_func ( u_long  g_,
marker_t  type_ 
)

Definition at line 230 of file Logger.cpp.

References ASSA::Logger_Impl::log_func(), m_context, and m_impl.

231 {
232  std::string empty_str;
233 
234  if (m_impl == NULL) {
235  return -1;
236  }
237 
238  return m_impl->log_func (static_cast<Group> (g_),
239  m_context.size (),
240  m_context.size () ? m_context.top () : empty_str,
241  type_);
242 }
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)=0
Logger_Impl * m_impl
Definition: Logger.h:128
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
int Logger::log_msg ( u_long  g_,
const char *  fmt_,
  ... 
)

Here is an interesting twist introduced by remote logging server:

Setting errno bits puts us in an unexpected situation. Asynchronous connection establishment reliest on examening errno to see if it was set by the system call, connect(), to EINPROGRESS. If it was, and thus connect() failed, IPv4Socket's connect() would log it as an error with EL() macro. When client is configured to log its messages to the server, call to EL() would result into the call to log_msg(), and because connection to the server has not yet been established, m_impl is set to 0 and errno is reset to EPERM. When stack unwinds itself, Connector's connect() fails because Connector's connectServiceHandler() returned errno different from expected EINPROGRESS (it is EPERM)!From vprintf(3S) manpage:

int vsnprintf (char* buf, size_t cnt, const char* fmt, va_list argptr);

"The vsnprintf() function returns the number of characters formatted, that is, then number of characters that would have been written to the buffer if it were large enough. It returns a negative value if an output error was encountered."

However, mingw relies on WIN32 implementation of the function which is inherently broken (not C99 compliant). From MSD reference:

"vsnprint returns the number of characters written if the the number of characters to write is less than or equal to 'cnt'; if the number of characters to write is greater than 'cnt', RETURN -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written.

Estimate message size

Fromat and write message to the log

For extra debuggin

if (ret < 0) { va_start (ap, fmt_); vsnprintf (tmpbuf, TMPBUF_SZ -1, fmt_, ap); std::cout << "m_impl->log_msg()=-1 message:\n" << tmpbuf << std::flush; va_end (ap); }

Definition at line 144 of file Logger.cpp.

References ASSA::Logger_Impl::log_msg(), m_context, m_impl, and TMPBUF_SZ.

145 {
146  va_list ap;
147  va_list ap2;
148  string empty_str;
149  size_t expected_sz = 0;
150 
151  static char tmpbuf[TMPBUF_SZ];
152  char* bufp = tmpbuf;
153  int len = TMPBUF_SZ;
154  int ret;
155 
156  if (m_impl == NULL) {
157  return -1;
158  }
159 
162 #if defined(WIN32)
163 
164  va_copy (ap2, ap);
165  ret = vsnprintf (bufp, len-1, fmt_, ap2);
166  va_end (ap2);
167 
168  if (ret == -1 || ret >= len)
169  {
170  len *= 2;
171  bufp = new char [len];
172  while (1) {
173  va_copy (ap2, ap);
174  ret = vsnprintf (bufp, len-1, fmt_, ap2);
175  va_end (ap2);
176  if (ret > -1 && ret < len) {
177  delete [] bufp;
178  break;
179  }
180  len *= 2;
181  delete [] bufp;
182  bufp = new char [len];
183 // std::cout << "Logger::log_func : malloc(" << len << ")\n"
184 // << std::flush;
185  }
186  }
187 
188 #else /* POSIX, C99 compliant */
189 
190  char c;
191  va_start (ap, fmt_);
192  ret = ::vsnprintf (&c, 1, fmt_, ap);
193  va_end (ap);
194 
195 #endif
196 
197  expected_sz = ret + 1;
198 
199 // std::cout << "Logger::log_func : expected_sz = "
200 // << expected_sz << "\n" << std::flush;
201 
204  va_start (ap, fmt_);
205  ret = m_impl->log_msg (static_cast<Group> (g_),
206  m_context.size (),
207  m_context.size () ? m_context.top () : empty_str,
208  expected_sz,
209  fmt_,
210  ap);
211  va_end (ap);
212 
225  return ret;
226 }
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)=0
static const int TMPBUF_SZ
Definition: Logger.cpp:33
Logger_Impl * m_impl
Definition: Logger.h:128
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
int Logger::log_open ( u_long  groups_)

Write log messages to standard output.

Returns
0 on success, -1 on error with errno set

Definition at line 53 of file Logger.cpp.

References ASSA::endl(), ASSA::Logger_Impl::log_open(), and m_impl.

54 {
55  if (m_impl != NULL) {
56  std::cerr << "Logger::log_open - Implementation already exist"
57  << std::endl;
58  return -1;
59  }
60  m_impl = new StdOutLogger;
61  return m_impl->log_open (groups_);
62 }
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition: Logger_Impl.h:264
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602
Logger_Impl * m_impl
Definition: Logger.h:128
int Logger::log_open ( const char *  logfname_,
u_long  groups_,
u_long  maxsize_ 
)

Write log messages to the logfile.

Parameters
logfname_Name of the logfile
groups_Logging masks enabled
maxsize_Maximum size of the logfile
Returns
0 on success, -1 on error with errno set

Definition at line 66 of file Logger.cpp.

References ASSA::Logger_Impl::log_open(), and m_impl.

67 {
68  if (m_impl != NULL) {
69  return -1;
70  }
71  m_impl = new FileLogger;
72  return m_impl->log_open (logfname_, groups_, maxsize_);
73 }
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition: Logger_Impl.h:264
Logger_Impl * m_impl
Definition: Logger.h:128
int Logger::log_open ( const std::string &  logsvr_,
const char *  logfname_,
u_long  groups_,
u_long  maxsize_,
Reactor reactor_ 
)

Write log messages to the log server assa-logd.

Parameters
logsvr_Address of assa-logd (assalogd@hostname)
logfname_Name of the logfile
groups_Logging masks enabled
maxsize_Maximum size of the logfile
reactor_Pointer to the Reactor to use for communication.
Returns
: 0 on success; -1 on error with errno set

Definition at line 77 of file Logger.cpp.

References ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::connect(), ASSA::AutoPtr< X >::get(), ASSA::Logger_Impl::log_open(), m_app_name, m_impl, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open(), and ASSA::AutoPtr< X >::release().

82 {
83  {
84  TimeVal tv (10.0);
85  INETAddress addr (logsvraddr_.c_str ());
86  if (addr.bad ()) {
87  return -1;
88  }
89 
92  log_connector.open (tv);
93 
94  if (log_connector.connect (lsp.get (), addr) < 0) {
95  delete m_impl;
96  m_impl = NULL;
97  return -1;
98  }
99 
100  m_impl = lsp.release ();
101  }
102  int ret = m_impl->log_open (m_app_name.c_str (), logfname_,
103  groups_, maxsize_, reactor_);
104  return ret;
105 }
Connector is a template class for initialization of communication services.
Definition: Connector.h:63
virtual int connect(SERVICE_HANDLER *sh_, Address &addr_, int protocol_=AF_INET)
Define strategy for establishing connection.
Definition: Connector.h:245
std::string m_app_name
Stack of all contexts.
Definition: Logger.h:130
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition: Logger_Impl.h:264
AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit...
Definition: AutoPtr.h:49
Logger_Impl * m_impl
Definition: Logger.h:128
virtual int open(const TimeVal &tv_=TimeVal(5.0), ConnectMode mode_=sync, Reactor *r_=(Reactor *) NULL)
Configure Connector.
Definition: Connector.h:223
void ASSA::Logger::log_resync ( void  )
inline

Definition at line 239 of file Logger.h.

References ASSA::Logger_Impl::log_resync(), and m_impl.

240 {
241  if (m_impl) {
242  m_impl->log_resync ();
243  }
244 }
virtual void log_resync(void)
Definition: Logger_Impl.h:190
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::set_app_name ( const std::string &  appname_)
inline

Set application name.

This should be the first call made to the newly created Logger object.

Definition at line 55 of file Logger.h.

References m_app_name.

55 { m_app_name = appname_; }
std::string m_app_name
Stack of all contexts.
Definition: Logger.h:130
void ASSA::Logger::set_timezone ( int  zone)
inline

0 - GMT, 1 - LOCAL

Definition at line 230 of file Logger.h.

References m_impl, and ASSA::Logger_Impl::set_timezone().

231 {
232  if (m_impl) {
233  m_impl->set_timezone (zone_);
234  }
235 }
void set_timezone(int zone_)
Definition: Logger_Impl.h:169
Logger_Impl * m_impl
Definition: Logger.h:128
void ASSA::Logger::sign_off ( void  )
inline

Definition at line 255 of file Logger.h.

References m_context.

256 {
257  if (!m_context.empty ()) {
258  m_context.pop ();
259  }
260 }
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
void ASSA::Logger::sign_on ( const string &  func_name_)
inline

Definition at line 248 of file Logger.h.

References m_context.

249 {
250  m_context.push (func_name_);
251 }
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
bool ASSA::Logger::timestamp_enabled ( void  ) const
inline

Definition at line 223 of file Logger.h.

References m_impl, and ASSA::Logger_Impl::timestamp_enabled().

224 {
225  return (m_impl) ? m_impl->timestamp_enabled () : false;
226 }
bool timestamp_enabled(void) const
Definition: Logger_Impl.h:168
Logger_Impl * m_impl
Definition: Logger.h:128

Member Data Documentation

std::string ASSA::Logger::m_app_name
private

Stack of all contexts.

Definition at line 130 of file Logger.h.

Referenced by log_open(), and set_app_name().

stack<string> ASSA::Logger::m_context
private

Logger implementation.

Definition at line 129 of file Logger.h.

Referenced by log_func(), log_msg(), sign_off(), and sign_on().

Logger_Impl* ASSA::Logger::m_impl
private

The documentation for this class was generated from the following files: