Loading...
Searching...
No Matches
Logger_Impl.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Logger_Impl.h
//------------------------------------------------------------------------------
// $Id: Logger_Impl.h,v 1.12 2012/05/21 03:20:39 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 2001 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_IMPL_H
#define LOGGER_IMPL_H
#include <errno.h>
#include <cstdarg>
#include <string>
#include <stdio.h>
#if defined(sun)
#include <sys/varargs.h> // va_list
#endif
#if defined (__CYGWIN32__) || defined (__NetBSD__) || defined (WIN32) || defined (__GLIBC__)
# include <stdarg.h>
#endif
#if defined(WIN32)
# include <winsock2.h> /* select(3) */
#endif
/* Also defined in winsock.h, winsock2.h, gmon.h and in cygwin's sys/types
*/
#if !defined ( _BSDTYPES_DEFINED )
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
typedef unsigned long u_long;
#define _BSDTYPES_DEFINED
#endif /* ! def _BSDTYPES_DEFINED */
using std::string;
using std::ostream;
#include "assa/LogMask.h"
#if defined (WIN32)
typedef SOCKET handler_t;
#define BAD_HANDLER INVALID_SOCKET
#define EINPROGRESS WSAEINPROGRESS /* A blocking Winsock call is in
* progress, or the service provider
* is still process a callback function.
*/
#define EWOULDBLOCK WSAEWOULDBLOCK /* The socket is marked as nonblocking
* and the connection cannot be completed
* immediately.
*/
#define EISCONN WSAEISCONN
#define ENOTSOCK WSAENOTSOCK /* The descriptor is not a socket.
*/
#define ECONNREFUSED WSAECONNREFUSED /* The attempt to connect was
* forcefully rejected.
*/
#define ETIMEDOUT WSAETIMEDOUT /* An attempt to connect timed out
* without establishing connection.
*/
#else /*--- POSIX ---*/
#define BAD_HANDLER -1
typedef int handler_t;
#endif // ifdef WIN32
namespace ASSA {
class Reactor;
//---------------------------------------------------------------------------
// Utilities that don't fit anywhere else
//---------------------------------------------------------------------------
inline bool is_valid_handler (handler_t socket_)
{
return (socket_ != BAD_HANDLER);
}
inline void disable_handler (handler_t& socket_)
{
socket_ = BAD_HANDLER;
}
inline int get_errno ()
{
int myerrno;
#if defined (WIN32)
myerrno = WSAGetLastError ();
#else
myerrno = errno;
#endif
return myerrno;
}
inline void set_errno (int new_errno_)
{
#if defined (WIN32)
WSASetLastError (new_errno_);
#else
errno = new_errno_;
#endif
}
//---------------------------------------------------------------------------
// Class Logger_Impl
//---------------------------------------------------------------------------
class Logger_Impl {
public:
static const unsigned int LOGGER_MAXLINE = 6660;
public:
Logger_Impl ();
virtual ~Logger_Impl () { /* empty */ }
void enable_group (Group g_) { m_groups |= g_; }
void disable_group (Group g_) { m_groups &= ~g_; }
void enable_groups (u_long g_) { m_groups |= g_; }
void disable_groups (u_long g_) { m_groups &= ~g_; }
void enable_all_groups (void) { m_groups = ASSA::ALL; }
void disable_all_groups (void) { m_groups = 0; }
bool group_enabled (Group g_) const { return (m_groups & g_); }
void enable_timestamp (void) { m_tmflg = true; }
void disable_timestamp (void) { m_tmflg = false; }
bool timestamp_enabled (void) const { return m_tmflg; }
void set_timezone (int zone_) { m_tz = zone_; }
void set_indent_step (u_short step_) { m_indent_step = step_; }
u_short get_indent_step (void) const { return m_indent_step; }
virtual int log_open (u_long groups_);
virtual int log_open (const char* logfname_,
u_long groups_,
u_long maxsize_);
virtual int log_open (const char* appname_,
const char* logfname_,
u_long groups_,
u_long maxsize_,
Reactor* reactor_);
virtual int log_close (void) = 0;
virtual void log_resync (void) { /* empty */ }
virtual int log_msg (Group g_,
size_t indent_level_,
const string& func_name_,
size_t expected_sz_,
const char* fmt_,
va_list) = 0;
virtual int log_func (Group g_,
size_t indent_level_,
const string& func_name_,
marker_t type_) = 0;
protected:
virtual u_short add_timestamp (ostream& sink_);
virtual u_short indent_func_name (ostream& sink_,
const string& funcname_,
size_t indent_level_,
marker_t type_);
char* format_msg (size_t expected_sz_,
const char* fmt_,
va_list vap_,
bool& release_);
protected:
static char m_msgbuf [LOGGER_MAXLINE];
u_short m_indent_step;
u_long m_groups;
string m_logfname;
bool m_tmflg;
int m_tz;
};
inline
Logger_Impl::
Logger_Impl ()
: m_indent_step (1),
m_groups (0),
m_tmflg (false),
m_tz (1)
{
/* no-op */
}
inline int
Logger_Impl::
log_open (u_long /* groups_ */)
{
errno = ENOSYS;
return -1;
}
inline int
Logger_Impl::
log_open (const char*, /* logfname_ */
u_long, /* groups_ */
u_long /* maxsize_ */)
{
errno = ENOSYS;
return -1;
}
inline int
Logger_Impl::
log_open (const char*, /* appname_ */
const char*, /* logfname_ */
u_long, /* groups_ */
u_long, /* maxsize_ */
Reactor* /* reactor_ */)
{
errno = ENOSYS;
return -1;
}
} // end namespace ASSA
#endif /* LOGGER_IMPL_H */
LogMask.h
Bit mask used by Logger.
BAD_HANDLER
#define BAD_HANDLER
Sort out WIN32/mingw oddities.
Definition Logger_Impl.h:81
u_long
unsigned long u_long
Definition Logger_Impl.h:41
u_short
unsigned short u_short
Definition Logger_Impl.h:39
handler_t
int handler_t
Definition Logger_Impl.h:82
u_int
unsigned int u_int
Definition Logger_Impl.h:40
u_char
unsigned char u_char
Definition Logger_Impl.h:38
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::Logger_Impl
Definition Logger_Impl.h:141
ASSA::Logger_Impl::format_msg
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
Definition Logger_Impl.cpp:86
ASSA::Logger_Impl::indent_func_name
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition Logger_Impl.cpp:54
ASSA::Logger_Impl::log_close
virtual int log_close(void)=0
ASSA::Logger_Impl::group_enabled
bool group_enabled(Group g_) const
Definition Logger_Impl.h:164
ASSA::Logger_Impl::enable_all_groups
void enable_all_groups(void)
Definition Logger_Impl.h:161
ASSA::Logger_Impl::m_indent_step
u_short m_indent_step
Indentation step.
Definition Logger_Impl.h:236
ASSA::Logger_Impl::m_tz
int m_tz
Timezone: 0-GMT, 1-Local.
Definition Logger_Impl.h:248
ASSA::Logger_Impl::m_groups
u_long m_groups
Enabled groups.
Definition Logger_Impl.h:239
ASSA::Logger_Impl::log_resync
virtual void log_resync(void)
Definition Logger_Impl.h:190
ASSA::Logger_Impl::timestamp_enabled
bool timestamp_enabled(void) const
Definition Logger_Impl.h:168
ASSA::Logger_Impl::disable_all_groups
void disable_all_groups(void)
Definition Logger_Impl.h:162
ASSA::Logger_Impl::m_logfname
string m_logfname
Log file name.
Definition Logger_Impl.h:242
ASSA::Logger_Impl::m_msgbuf
static char m_msgbuf[LOGGER_MAXLINE]
Static buffer for formatted message.
Definition Logger_Impl.h:233
ASSA::Logger_Impl::set_indent_step
void set_indent_step(u_short step_)
Definition Logger_Impl.h:171
ASSA::Logger_Impl::~Logger_Impl
virtual ~Logger_Impl()
Definition Logger_Impl.h:153
ASSA::Logger_Impl::enable_timestamp
void enable_timestamp(void)
Definition Logger_Impl.h:166
ASSA::Logger_Impl::log_msg
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)=0
ASSA::Logger_Impl::disable_groups
void disable_groups(u_long g_)
Definition Logger_Impl.h:159
ASSA::Logger_Impl::get_indent_step
u_short get_indent_step(void) const
Definition Logger_Impl.h:172
ASSA::Logger_Impl::disable_timestamp
void disable_timestamp(void)
Definition Logger_Impl.h:167
ASSA::Logger_Impl::enable_group
void enable_group(Group g_)
Definition Logger_Impl.h:155
ASSA::Logger_Impl::LOGGER_MAXLINE
static const unsigned int LOGGER_MAXLINE
Maximum length of the formatted message.
Definition Logger_Impl.h:149
ASSA::Logger_Impl::set_timezone
void set_timezone(int zone_)
Definition Logger_Impl.h:169
ASSA::Logger_Impl::log_open
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition Logger_Impl.h:264
ASSA::Logger_Impl::disable_group
void disable_group(Group g_)
Definition Logger_Impl.h:156
ASSA::Logger_Impl::log_func
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)=0
ASSA::Logger_Impl::add_timestamp
virtual u_short add_timestamp(ostream &sink_)
Definition Logger_Impl.cpp:35
ASSA::Logger_Impl::enable_groups
void enable_groups(u_long g_)
Definition Logger_Impl.h:158
ASSA::Logger_Impl::m_tmflg
bool m_tmflg
Timestamp on/off flag.
Definition Logger_Impl.h:245
ASSA::Logger_Impl::Logger_Impl
Logger_Impl()
Definition Logger_Impl.h:253
ASSA::Reactor
Definition Reactor.h:58
ASSA
Definition Acceptor.h:40
ASSA::set_errno
void set_errno(int new_errno_)
Set error number in a portable way.
Definition Logger_Impl.h:128
ASSA::marker_t
marker_t
Definition LogMask.h:67
ASSA::Group
Group
Definition LogMask.h:25
ASSA::ALL
@ ALL
All messages: library + application
Definition LogMask.h:62
ASSA::is_valid_handler
bool is_valid_handler(handler_t socket_)
Detect socket() error in a portable way.
Definition Logger_Impl.h:100
ASSA::disable_handler
void disable_handler(handler_t &socket_)
Set socket descriptor to invalid value in a portable way.
Definition Logger_Impl.h:108
ASSA::get_errno
int get_errno()
Fetch error number in a portable way.
Definition Logger_Impl.h:115