Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Logger.cpp
//------------------------------------------------------------------------------
// $Id: Logger.cpp,v 1.8 2012/05/23 02:52:25 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 2001,2005 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.
//------------------------------------------------------------------------------
#include <stdarg.h>
#include <iostream> // for WIN32 hacking
#include "assa/Connector.h"
#include "assa/INETAddress.h"
#include "assa/IPv4Socket.h"
#include "assa/FileLogger.h"
#include "assa/StdOutLogger.h"
#include "assa/RemoteLogger.h"
#include "assa/AutoPtr.h"
#include "assa/Logger.h"
using namespace ASSA;
ASSA_DECL_SINGLETON(Logger);
// Internal storage
static const int TMPBUF_SZ = 4096;
// Logger's member functions
int
Logger::
log_close (void)
{
int ret = 0;
if (m_impl) {
ret = m_impl->log_close ();
delete m_impl;
m_impl = 0;
}
return ret;
}
int
Logger::
log_open (u_long groups_)
{
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_);
}
int
Logger::
log_open (const char* logfname_, u_long groups_, u_long maxsize_)
{
if (m_impl != NULL) {
return -1;
}
m_impl = new FileLogger;
return m_impl->log_open (logfname_, groups_, maxsize_);
}
int
Logger::
log_open (const std::string& logsvraddr_,
const char* logfname_,
u_long groups_,
u_long maxsize_,
Reactor* reactor_)
{
{
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;
}
int
Logger::
log_msg (u_long g_, const char* fmt_, ...)
{
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_func (u_long g_, marker_t type_)
{
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_);
}
AutoPtr.h
AutoPtr is a local implementation of STL's auto_ptr that makes dynamic memory handling a bit easier.
Connector.h
A generic pattern for establishing connection with TCP/IP servers.
FileLogger.h
Implemention of a Logger as a disk-based file.
INETAddress.h
An incapsulation of TCP/UDP Internet Protocol socket address structure.
IPv4Socket.h
Class IPv4Socket covers domain types AF_INET and AF_UNIX.
TMPBUF_SZ
static const int TMPBUF_SZ
Definition Logger.cpp:33
Logger.h
An abstraction to message logging facility.
u_long
unsigned long u_long
Definition Logger_Impl.h:41
RemoteLogger.h
A proxy connection class with logging server, assa-logd.
ASSA_DECL_SINGLETON
#define ASSA_DECL_SINGLETON(K)
ASSA_DECL_SINGLETON macro inserts static member declarations mandated by the Singleton class.
Definition Singleton.h:82
StdOutLogger.h
StdOutLogger implements Logger as standard output.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::FileLogger
Definition FileLogger.h:31
ASSA::INETAddress
Definition INETAddress.h:27
ASSA::Logger_Impl::log_close
virtual int log_close(void)=0
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::log_open
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition Logger_Impl.h:264
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
Definition Logger.h:46
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_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::log_func
int log_func(u_long g_, marker_t type_)
Definition Logger.cpp:230
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
ASSA::Logger::m_context
stack< string > m_context
Logger implementation.
Definition Logger.h:129
ASSA::Reactor
Definition Reactor.h:58
ASSA::RemoteLogger
Definition RemoteLogger.h:41
ASSA::StdOutLogger
Definition StdOutLogger.h:33
ASSA::TimeVal
Definition TimeVal.h:31
ASSA
Definition Acceptor.h:40
ASSA::marker_t
marker_t
Definition LogMask.h:67
ASSA::Group
Group
Definition LogMask.h:25