Loading...
Searching...
No Matches
ASSA::Logger Class Reference

`#include <Logger.h>`

Inheritance diagram for ASSA::Logger:

## 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_Impl * | m_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 Logger * | get_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

## Logger()

ASSA::Logger::Logger | ( |  | ) |

inline

Definition at line 48 of file Logger.h.

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

References m_app_name, and m_impl.

## ~Logger()

ASSA::Logger::~Logger | ( |  | ) |

inline

Definition at line 49 of file Logger.h.

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

References log_close().

## Member Function Documentation

## disable_all_groups()

void ASSA::Logger::disable_all_groups | ( | void |  | ) |

inline

Definition at line 195 of file Logger.h.

{
if (m_impl) {
m_impl->disable_all_groups ();
}
}

References m_impl.

## disable_group()

void ASSA::Logger::disable_group | ( | Group | g_ | ) |

inline

Disable logging for group g_.

Parameters

  g_ | Group to disable

Definition at line 152 of file Logger.h.

{
if (m_impl) {
m_impl->disable_group (g_);
}
}

References m_impl.

## disable_groups()

void ASSA::Logger::disable_groups | ( | u_long | groups_ | ) |

inline

Disable logging for groups_.

Parameters

  groups_ | Groups to disable

Definition at line 170 of file Logger.h.

{
if (m_impl) {
m_impl->disable_groups (g_);
}
}

References m_impl.

## disable_timestamp()

void ASSA::Logger::disable_timestamp | ( | void |  | ) |

inline

Definition at line 213 of file Logger.h.

{
if (m_impl) {
m_impl->disable_timestamp ();
}
}

References m_impl.

## enable_all_groups()

void ASSA::Logger::enable_all_groups | ( | void |  | ) |

inline

Definition at line 186 of file Logger.h.

{
if (m_impl) {
m_impl->enable_all_groups ();
}
}

References m_impl.

## enable_group()

void ASSA::Logger::enable_group | ( | Group | g_ | ) |

inline

Enable logging for group g_.

Parameters

  g_ | Group to enable

Definition at line 143 of file Logger.h.

{
if (m_impl) {
m_impl->enable_group (g_);
}
}

References m_impl.

## enable_groups()

void ASSA::Logger::enable_groups | ( | u_long | groups_ | ) |

inline

Enable logging for groups_.

Parameters

  groups_ | Groups to enable

Definition at line 161 of file Logger.h.

{
if (m_impl) {
m_impl->enable_groups (g_);
}
}

References m_impl.

## enable_timestamp()

void ASSA::Logger::enable_timestamp | ( | void |  | ) |

inline

Add optional timezone: GMT vs. Local.

Definition at line 204 of file Logger.h.

{
if (m_impl) {
m_impl->enable_timestamp ();
}
}

References m_impl.

## group_enabled()

bool ASSA::Logger::group_enabled | ( | Group | g_ | ) | const

inline

Definition at line 179 of file Logger.h.

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

References m_impl.

## log_close()

int Logger::log_close | ( | void |  | ) |

Definition at line 38 of file Logger.cpp.

{
int ret = 0;
if (m_impl) {
ret = m_impl->log_close ();
delete m_impl;
m_impl = 0;
}
return ret;
}

References m_impl.

Referenced by ~Logger().

## log_func()

int Logger::log_func | ( | u_long | g_,
|  | marker_t | type_ )

Definition at line 229 of file Logger.cpp.

{
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_);
}
ASSA::Logger::m_context
stack< string > m_context
Logger implementation.
Definition Logger.h:129
ASSA::Group
Group
Definition LogMask.h:25

References m_context, and m_impl.

## log_msg()

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 143 of file Logger.cpp.

{
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;
}
TMPBUF_SZ
static const int TMPBUF_SZ
Definition Logger.cpp:33

References m_context, m_impl, and TMPBUF_SZ.

## log_open() [1/3]

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 65 of file Logger.cpp.

{
if (m_impl != NULL) {
return -1;
}
m_impl = new FileLogger;
return m_impl->log_open (logfname_, groups_, maxsize_);
}

References m_impl.

## log_open() [2/3]

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 76 of file Logger.cpp.

{
{
TimeVal tv (10.0);
INETAddress addr (logsvraddr_.c_str ());
if (addr.bad ()) {
return -1;
}
Connector <RemoteLogger, IPv4Socket> log_connector;
AutoPtr<RemoteLogger> lsp (new RemoteLogger);
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;
}
ASSA::Connector::open
virtual int open(const TimeVal &tv_=TimeVal(5.0), ConnectMode mode_=sync, Reactor *r_=(Reactor *) NULL)
Configure Connector.
Definition Connector.h:223
ASSA::Connector::connect
virtual int connect(SERVICE_HANDLER *sh_, Address &addr_, int protocol_=AF_INET)
Define strategy for establishing connection.
Definition Connector.h:245

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

## log_open() [3/3]

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 52 of file Logger.cpp.

{
if (m_impl != NULL) {
std::cerr << "Logger::log_open - Implementation already exist"
<< std::endl;
return -1;
}
m_impl = new StdOutLogger;
return m_impl->log_open (groups_);
}

References m_impl.

## log_resync()

void ASSA::Logger::log_resync | ( | void |  | ) |

inline

Definition at line 238 of file Logger.h.

{
if (m_impl) {
m_impl->log_resync ();
}
}

References m_impl.

## set_app_name()

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.

{ m_app_name = appname_; }

References m_app_name.

## set_timezone()

void ASSA::Logger::set_timezone | ( | int | zone | ) |

inline

0 - GMT, 1 - LOCAL

Definition at line 229 of file Logger.h.

{
if (m_impl) {
m_impl->set_timezone (zone_);
}
}

References m_impl.

## sign_off()

void ASSA::Logger::sign_off | ( | void |  | ) |

inline

Definition at line 254 of file Logger.h.

{
if (!m_context.empty ()) {
m_context.pop ();
}
}

References m_context.

## sign_on()

void ASSA::Logger::sign_on | ( | const string & | func_name_ | ) |

inline

Definition at line 247 of file Logger.h.

{
m_context.push (func_name_);
}

References m_context.

## timestamp_enabled()

bool ASSA::Logger::timestamp_enabled | ( | void |  | ) | const

inline

Definition at line 222 of file Logger.h.

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

References m_impl.

## Member Data Documentation

## m_app_name

std::string ASSA::Logger::m_app_name

private

Stack of all contexts.

Definition at line 130 of file Logger.h.

Referenced by log_open(), Logger(), and set_app_name().

## m_context

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().

## m_impl

Logger_Impl* ASSA::Logger::m_impl

private

Definition at line 128 of file Logger.h.

Referenced by disable_all_groups(), disable_group(), disable_groups(), disable_timestamp(), enable_all_groups(), enable_group(), enable_groups(), enable_timestamp(), group_enabled(), log_close(), log_func(), log_msg(), log_open(), log_open(), log_open(), log_resync(), Logger(), set_timezone(), and timestamp_enabled().

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

- Logger.h
- Logger.cpp