Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Logger.h
//------------------------------------------------------------------------------
// Copyright (c) 2001 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef LOGGER_H
#define LOGGER_H
//System headers
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <stack>
using std::string;
using std::stack;
//ASSA headers
#include "assa/Logger_Impl.h"
#include "assa/Singleton.h"
#include "assa/MemDump.h"
namespace ASSA { // @start namespace ASSA
class Reactor;
/******************************************************************************
Class Logger
******************************************************************************/
class Logger : public Singleton<Logger>
{
public:
Logger () : m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
~Logger () { this->log_close (); }
public:
void set_app_name (const std::string& appname_) { m_app_name = appname_; }
void enable_group (Group g_);
void disable_group (Group g_);
void enable_groups (u_long groups_);
void disable_groups (u_long groups_);
void enable_all_groups (void);
void disable_all_groups (void);
bool group_enabled (Group g_) const;
void enable_timestamp (void);
void disable_timestamp (void);
bool timestamp_enabled (void) const;
void set_timezone (int zone);
void sign_on (const string& func_name_);
void sign_off (void);
int log_open (u_long groups_);
int log_open (const char* logfname_, u_long groups_, u_long maxsize_);
int log_open (const std::string& logsvr_, const char* logfname_,
u_long groups_, u_long maxsize_, Reactor* reactor_);
void log_resync (void);
int log_close (void);
int log_msg (u_long g_, const char* fmt_, ...);
int log_func (u_long g_, marker_t type_);
private:
Logger_Impl* m_impl;
stack<string> m_context;
std::string m_app_name;
};
#define LOGGER ASSA::Logger::get_instance()
/*******************************************************************************
Inline functions
*******************************************************************************/
inline void
Logger::
enable_group (Group g_)
{
if (m_impl) {
m_impl->enable_group (g_);
}
}
inline void
Logger::
disable_group (Group g_)
{
if (m_impl) {
m_impl->disable_group (g_);
}
}
inline void
Logger::
enable_groups (u_long g_)
{
if (m_impl) {
m_impl->enable_groups (g_);
}
}
inline void
Logger::
disable_groups (u_long g_)
{
if (m_impl) {
m_impl->disable_groups (g_);
}
}
inline bool
Logger::
group_enabled (Group g_) const
{
return (m_impl) ? m_impl->group_enabled (g_) : false;
}
inline void
Logger::
enable_all_groups (void)
{
if (m_impl) {
m_impl->enable_all_groups ();
}
}
inline void
Logger::
disable_all_groups (void)
{
if (m_impl) {
m_impl->disable_all_groups ();
}
}
inline void
Logger::
enable_timestamp (void)
{
if (m_impl) {
m_impl->enable_timestamp ();
}
}
inline void
Logger::
disable_timestamp (void)
{
if (m_impl) {
m_impl->disable_timestamp ();
}
}
inline bool
Logger::
timestamp_enabled (void) const
{
return (m_impl) ? m_impl->timestamp_enabled () : false;
}
inline void
Logger::
set_timezone (int zone_)
{
if (m_impl) {
m_impl->set_timezone (zone_);
}
}
inline void
Logger::
log_resync (void)
{
if (m_impl) {
m_impl->log_resync ();
}
}
inline void
Logger::
sign_on (const string& func_name_)
{
m_context.push (func_name_);
}
inline void
Logger::
sign_off (void)
{
if (!m_context.empty ()) {
m_context.pop ();
}
}
/*******************************************************************************
Macro definition shortcuts
*******************************************************************************/
#if defined (ASSA_NLOGGING)
# define DL(X) do {} while (0)
#else
# define DL(X) \
do { \
LOGGER->log_msg X; \
} while (0)
#endif
#if defined (ASSA_NLOGGING)
# define EL(X) do {} while (0)
#else
# define EL(X) \
do { \
LOGGER->log_msg X; \
LOGGER->log_msg(ASSA::ASSAERR,"errno: %d \"%s\"\n", errno, strerror(errno)); \
} while (0)
#endif
/*******************************************************************************
Namespace Log provides only the most plausable interface.
Other public functions can be reached via LOGGER
*******************************************************************************/
namespace Log { // @start namespace Log
inline void set_app_name (const std::string& appname_)
{
LOGGER->set_app_name (appname_);
}
inline int open_log_file (const char* logfname_,
u_long groups_ = ALL,
u_long maxsize_ = 10485760)
{
return LOGGER->log_open (logfname_, groups_, maxsize_);
}
inline int open_log_stdout (u_long groups_ = ALL)
{
return LOGGER->log_open (groups_);
}
inline int open_log_server (const std::string& logsvraddr_,
const char* logfname_,
Reactor* reactor_,
u_long groups_ = ASSA::ALL,
u_long maxsize_ = 10485760)
{
int ret = LOGGER->log_open (logsvraddr_, logfname_, groups_,
maxsize_, reactor_);
return ret;
}
inline void log_resync (void) { LOGGER->log_resync (); }
inline int log_close (void) { return LOGGER->log_close (); }
inline void set_gmt_timezone (void) { LOGGER->set_timezone (0); }
inline void enable_timestamp (void) { LOGGER->enable_timestamp (); }
inline void disable_timestamp (void) { LOGGER->disable_timestamp (); }
} // @end namespace Log
//------------------------------------------------------------------------------
// DiagnosticContext
//------------------------------------------------------------------------------
class DiagnosticContext
{
public:
DiagnosticContext (const char* fn_, u_long mask_ = TRACE);
~DiagnosticContext ();
private:
DiagnosticContext (const DiagnosticContext&);
DiagnosticContext& operator= (const DiagnosticContext&);
private:
const char* m_fname;
u_long m_mask;
};
inline
DiagnosticContext::
DiagnosticContext (const char* fn_, u_long mask_)
: m_fname (fn_), m_mask (mask_)
{
if (LOGGER->group_enabled ((ASSA::Group) m_mask)) {
LOGGER->sign_on (m_fname);
LOGGER->log_func (m_mask, FUNC_ENTRY);
}
}
inline
DiagnosticContext::
~DiagnosticContext ()
{
if (LOGGER->group_enabled ((ASSA::Group) m_mask)) {
LOGGER->log_func (m_mask, FUNC_EXIT);
LOGGER->sign_off ();
}
}
#define trace(s) ASSA::DiagnosticContext tRaCeR(s);
#define trace_with_mask(s, m) ASSA::DiagnosticContext tRaCeR(s, m);
} // @end namespace ASSA
#endif /* LOGGER_H */
LOGGER
#define LOGGER
A shortcut to locate a singleton object of class Logger.
Definition Logger.h:136
Logger_Impl.h
u_long
unsigned long u_long
Definition Logger_Impl.h:41
MemDump.h
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
Singleton.h
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
ASSA::DiagnosticContext::operator=
DiagnosticContext & operator=(const DiagnosticContext &)
ASSA::DiagnosticContext::m_fname
const char * m_fname
Definition Logger.h:390
ASSA::DiagnosticContext::m_mask
u_long m_mask
Definition Logger.h:391
ASSA::DiagnosticContext::DiagnosticContext
DiagnosticContext(const char *fn_, u_long mask_=TRACE)
Definition Logger.h:396
ASSA::DiagnosticContext::DiagnosticContext
DiagnosticContext(const DiagnosticContext &)
ASSA::DiagnosticContext::~DiagnosticContext
~DiagnosticContext()
Definition Logger.h:407
ASSA::Logger_Impl
Definition Logger_Impl.h:141
ASSA::Logger::sign_on
void sign_on(const string &func_name_)
Definition Logger.h:248
ASSA::Logger::log_msg
int log_msg(u_long g_, const char *fmt_,...)
Here is an interesting twist introduced by remote logging server:
Definition Logger.cpp:144
ASSA::Logger::log_resync
void log_resync(void)
Definition Logger.h:239
ASSA::Logger::disable_timestamp
void disable_timestamp(void)
Definition Logger.h:214
ASSA::Logger::~Logger
~Logger()
Definition Logger.h:49
ASSA::Logger::log_open
int log_open(u_long groups_)
Write log messages to standard output.
Definition Logger.cpp:53
ASSA::Logger::log_close
int log_close(void)
Definition Logger.cpp:39
ASSA::Logger::enable_timestamp
void enable_timestamp(void)
Add optional timezone: GMT vs. Local.
Definition Logger.h:205
ASSA::Logger::log_func
int log_func(u_long g_, marker_t type_)
Definition Logger.cpp:230
ASSA::Logger::set_timezone
void set_timezone(int zone)
0 - GMT, 1 - LOCAL
Definition Logger.h:230
ASSA::Logger::disable_groups
void disable_groups(u_long groups_)
Disable logging for groups_.
Definition Logger.h:171
ASSA::Logger::set_app_name
void set_app_name(const std::string &appname_)
Set application name.
Definition Logger.h:55
ASSA::Logger::disable_group
void disable_group(Group g_)
Disable logging for group g_.
Definition Logger.h:153
ASSA::Logger::enable_group
void enable_group(Group g_)
Enable logging for group g_.
Definition Logger.h:144
ASSA::Logger::enable_all_groups
void enable_all_groups(void)
Definition Logger.h:187
ASSA::Logger::m_app_name
std::string m_app_name
Stack of all contexts.
Definition Logger.h:130
ASSA::Logger::enable_groups
void enable_groups(u_long groups_)
Enable logging for groups_.
Definition Logger.h:162
ASSA::Logger::group_enabled
bool group_enabled(Group g_) const
Definition Logger.h:180
ASSA::Logger::m_impl
Logger_Impl * m_impl
Definition Logger.h:128
ASSA::Logger::sign_off
void sign_off(void)
Definition Logger.h:255
ASSA::Logger::m_context
stack< string > m_context
Logger implementation.
Definition Logger.h:129
ASSA::Logger::timestamp_enabled
bool timestamp_enabled(void) const
Definition Logger.h:223
ASSA::Logger::disable_all_groups
void disable_all_groups(void)
Definition Logger.h:196
ASSA::Logger::Logger
Logger()
Definition Logger.h:48
ASSA::Reactor
Definition Reactor.h:58
ASSA::Singleton< Logger >::Singleton
Singleton()
Definition Singleton.h:57
ASSA::Log
Definition Logger.h:297
ASSA::Log::open_log_file
int open_log_file(const char *logfname_, u_long groups_=ALL, u_long maxsize_=10485760)
Open log file.
Definition Logger.h:319
ASSA::Log::disable_timestamp
void disable_timestamp(void)
Disable timestamp logging.
Definition Logger.h:368
ASSA::Log::enable_timestamp
void enable_timestamp(void)
Enable timestamp logging.
Definition Logger.h:365
ASSA::Log::log_close
int log_close(void)
Close logging stream.
Definition Logger.h:359
ASSA::Log::log_resync
void log_resync(void)
Resynchronize logging stream after Fork.
Definition Logger.h:356
ASSA::Log::set_app_name
void set_app_name(const std::string &appname_)
Set application name.
Definition Logger.h:305
ASSA::Log::open_log_server
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
ASSA::Log::open_log_stdout
int open_log_stdout(u_long groups_=ALL)
Write log message to standard output.
Definition Logger.h:327
ASSA::Log::set_gmt_timezone
void set_gmt_timezone(void)
Set timezone to GMT.
Definition Logger.h:362
ASSA
Definition Acceptor.h:40
ASSA::marker_t
marker_t
Definition LogMask.h:67
ASSA::FUNC_EXIT
@ FUNC_EXIT
Definition LogMask.h:70
ASSA::Group
Group
Definition LogMask.h:25
ASSA::TRACE
@ TRACE
Function call trace.
Definition LogMask.h:26
ASSA::ALL
@ ALL
All messages: library + application.
Definition LogMask.h:62