libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Private Attributes
ASSA::Logger Class Reference

#include <Logger.h>

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

List of all members.

Public Member Functions

 Logger ()
 ~Logger ()
void set_app_name (const std::string &appname_)
 Set application name.
void enable_group (Group g_)
 Enable logging for group g_.
void disable_group (Group g_)
 Disable logging for group g_.
void enable_groups (u_long groups_)
 Enable logging for groups_.
void disable_groups (u_long groups_)
 Disable logging for groups_.
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.
void disable_timestamp (void)
bool timestamp_enabled (void) const
void set_timezone (int zone)
 0 - GMT, 1 - LOCAL
void sign_on (const string &func_name_)
void sign_off (void)
int log_open (u_long groups_)
 Write log messages to standard output.
int log_open (const char *logfname_, u_long groups_, u_long maxsize_)
 Write log messages to the logfile.
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.
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:
int log_func (u_long g_, marker_t type_)

Private Attributes

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

Additional Inherited Members

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

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.

: m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
ASSA::Logger::~Logger ( )
inline

Definition at line 49 of file Logger.h.

References log_close().

{ this->log_close (); }

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.

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
void ASSA::Logger::disable_timestamp ( void  )
inline

Definition at line 214 of file Logger.h.

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

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
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.

{
if (m_impl) {
}
}
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.

{
return (m_impl) ? m_impl->group_enabled (g_) : false;
}
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().

{
int ret = 0;
if (m_impl) {
ret = m_impl->log_close ();
delete m_impl;
m_impl = 0;
}
return ret;
}
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.

{
std::string empty_str;
if (m_impl == NULL) {
return -1;
}
return m_impl->log_func (static_cast<Group> (g_),
m_context.size (),
m_context.size () ? m_context.top () : empty_str,
type_);
}
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.

{
va_list ap;
va_list ap2;
string empty_str;
size_t expected_sz = 0;
static char tmpbuf[TMPBUF_SZ];
char* bufp = tmpbuf;
int len = TMPBUF_SZ;
int ret;
if (m_impl == NULL) {
return -1;
}
#if defined(WIN32)
va_copy (ap2, ap);
ret = vsnprintf (bufp, len-1, fmt_, ap2);
va_end (ap2);
if (ret == -1 || ret >= len)
{
len *= 2;
bufp = new char [len];
while (1) {
va_copy (ap2, ap);
ret = vsnprintf (bufp, len-1, fmt_, ap2);
va_end (ap2);
if (ret > -1 && ret < len) {
delete [] bufp;
break;
}
len *= 2;
delete [] bufp;
bufp = new char [len];
// std::cout << "Logger::log_func : malloc(" << len << ")\n"
// << std::flush;
}
}
#else /* POSIX, C99 compliant */
char c;
va_start (ap, fmt_);
ret = ::vsnprintf (&c, 1, fmt_, ap);
va_end (ap);
#endif
expected_sz = ret + 1;
// std::cout << "Logger::log_func : expected_sz = "
// << expected_sz << "\n" << std::flush;
va_start (ap, fmt_);
ret = m_impl->log_msg (static_cast<Group> (g_),
m_context.size (),
m_context.size () ? m_context.top () : empty_str,
expected_sz,
fmt_,
ap);
va_end (ap);
return ret;
}
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.

{
if (m_impl != NULL) {
std::cerr << "Logger::log_open - Implementation already exist"
return -1;
}
return m_impl->log_open (groups_);
}
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.

{
if (m_impl != NULL) {
return -1;
}
return m_impl->log_open (logfname_, groups_, maxsize_);
}
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().

{
{
TimeVal tv (10.0);
INETAddress addr (logsvraddr_.c_str ());
if (addr.bad ()) {
return -1;
}
log_connector.open (tv);
if (log_connector.connect (lsp.get (), addr) < 0) {
delete m_impl;
m_impl = NULL;
return -1;
}
m_impl = lsp.release ();
}
int ret = m_impl->log_open (m_app_name.c_str (), logfname_,
groups_, maxsize_, reactor_);
return ret;
}
void ASSA::Logger::log_resync ( void  )
inline

Definition at line 239 of file Logger.h.

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

{
if (m_impl) {
}
}
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.

{ m_app_name = appname_; }
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().

{
if (m_impl) {
m_impl->set_timezone (zone_);
}
}
void ASSA::Logger::sign_off ( void  )
inline

Definition at line 255 of file Logger.h.

References m_context.

{
if (!m_context.empty ()) {
m_context.pop ();
}
}
void ASSA::Logger::sign_on ( const string &  func_name_)
inline

Definition at line 248 of file Logger.h.

References m_context.

{
m_context.push (func_name_);
}
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().

{
return (m_impl) ? m_impl->timestamp_enabled () : false;
}

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: