Loading...
Searching...
No Matches
RemoteLogger.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// RemoteLogger.cpp
//------------------------------------------------------------------------------
// $Id: RemoteLogger.cpp,v 1.3 2006/07/26 00:27:32 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 2003 by Vladislav Grinchenko
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Date:
//------------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include "Reactor.h"
#include "Socket.h"
#include "RemoteLogger.h"
using namespace ASSA;
/*******************************************************************************
Class member functions
*******************************************************************************/
RemoteLogger::
RemoteLogger () :
m_state (closed),
m_recursive_call (false)
{
// no-op
}
int
RemoteLogger::
open ()
{
return 0;
}
int
RemoteLogger::
log_open (const char* appname_,
const char* logfname_,
u_long groups_,
u_long maxsize_,
Reactor* reactor_)
{
if (m_recursive_call) {
return 0;
}
m_recursive_call = true;
if (m_state == opened) {
return 0;
}
m_logfname = logfname_;
m_groups = groups_;
m_reactor = reactor_;
m_reactor->registerIOHandler (this, get_stream ().getHandler(),
ASSA::READ_EVENT);
get_stream ().turnOptionOff (Socket::nonblocking);
size_t len = sizeof (maxsize_) +
Socket::xdr_length (appname_) +
Socket::xdr_length (logfname_);
get_stream () << 1234567890 << SIGN_ON << len
<< maxsize_ << appname_ << logfname_ << ASSA::flush;
m_state = opened;
m_recursive_call = false;
return 0;
}
int
RemoteLogger::
log_close (void)
{
if (m_state == opened) {
m_recursive_call = true;
get_stream () << 1234567890 << SIGN_OFF << 0 << ASSA::flush;
m_reactor->removeHandler (this, READ_EVENT);
m_recursive_call = false;
}
return 0;
}
int
RemoteLogger::
handle_close (int fd_)
{
m_state = closed;
m_logfname.empty ();
return 0;
}
void
RemoteLogger::
log_resync (void)
{
if (m_state == opened) {
m_recursive_call = true;
get_stream () << ASSA::flush;
m_recursive_call = false;
}
}
int
RemoteLogger::
log_msg (Group groups_,
size_t indent_level_,
const string& func_name_,
size_t expected_sz_,
const char* fmt_,
va_list msg_list_)
{
if (m_recursive_call) {
return 0;
}
if (m_state == closed) {
return -1;
}
if (!group_enabled (groups_)) {
return 0;
}
std::ostringstream os;
add_timestamp (os);
indent_func_name (os, func_name_, indent_level_, FUNC_MSG);
bool release = false;
char* msgbuf_ptr = format_msg (expected_sz_, fmt_, msg_list_, release);
if (msgbuf_ptr == NULL) {
return -1; // failed to format
}
os << msgbuf_ptr;
if (release) {
delete [] msgbuf_ptr;
}
if (get_stream ()) {
m_recursive_call = true;
Assure_exit (os.str ().length () != 0);
get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
<< os.str () << ASSA::flush;
m_recursive_call = false;
}
else {
m_state = closed;
}
return 0;
}
int
RemoteLogger::
log_func (Group groups_,
size_t indent_level_,
const string& func_name_,
marker_t type_)
{
if (m_recursive_call) {
return 0;
}
if (m_state == closed) {
return -1;
}
if (! group_enabled (groups_)) {
return 0;
}
std::ostringstream os;
add_timestamp (os);
indent_func_name (os, func_name_, indent_level_, type_);
os << ((type_ == FUNC_ENTRY) ? "---v---\n" : "---^---\n");
if (get_stream ().good ()) {
m_recursive_call = true;
get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
<< os.str () << ASSA::flush;
m_recursive_call = false;
}
else {
m_state = closed;
}
return 0;
}
Assure_exit
#define Assure_exit(exp_)
Macro that makes program exit if assert fails.
Definition Assure.h:39
u_long
unsigned long u_long
Definition Logger_Impl.h:41
Reactor.h
An implementation of Reactor pattern.
RemoteLogger.h
A proxy connection class with logging server, assa-logd.
Socket.h
Abstraction of socket data type.
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
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::group_enabled
bool group_enabled(Group g_) const
Definition Logger_Impl.h:164
ASSA::Logger_Impl::m_groups
u_long m_groups
Enabled groups.
Definition Logger_Impl.h:239
ASSA::Logger_Impl::m_logfname
string m_logfname
Log file name.
Definition Logger_Impl.h:242
ASSA::Logger_Impl::add_timestamp
virtual u_short add_timestamp(ostream &sink_)
Definition Logger_Impl.cpp:35
ASSA::Reactor
Definition Reactor.h:58
ASSA::Reactor::registerIOHandler
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition Reactor.cpp:93
ASSA::Reactor::removeHandler
bool removeHandler(EventHandler *eh_, EventType et_=ALL_EVENTS)
Remove Event handler from reactor for either all I/O events or timeout event or both.
Definition Reactor.cpp:173
ASSA::RemoteLogger::RemoteLogger
RemoteLogger()
Definition RemoteLogger.cpp:30
ASSA::RemoteLogger::open
virtual int open()
Called by Connector upon establishing connection.
Definition RemoteLogger.cpp:39
ASSA::RemoteLogger::log_open
virtual int log_open(const char *appname_, const char *logfname_, u_long groups_, u_long maxsize_, Reactor *reactor_)
Calling results into sending SIGN-ON message.
Definition RemoteLogger.cpp:46
ASSA::RemoteLogger::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)
Definition RemoteLogger.cpp:125
ASSA::RemoteLogger::closed
@ closed
Definition RemoteLogger.h:82
ASSA::RemoteLogger::opened
@ opened
Definition RemoteLogger.h:82
ASSA::RemoteLogger::m_reactor
Reactor * m_reactor
Definition RemoteLogger.h:86
ASSA::RemoteLogger::log_func
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)
Definition RemoteLogger.cpp:175
ASSA::RemoteLogger::log_resync
virtual void log_resync(void)
Flush output buffer.
Definition RemoteLogger.cpp:114
ASSA::RemoteLogger::log_close
virtual int log_close(void)
Close connection to the assa-logd.
Definition RemoteLogger.cpp:89
ASSA::RemoteLogger::m_recursive_call
bool m_recursive_call
If true, recursive call is in progress.
Definition RemoteLogger.h:89
ASSA::RemoteLogger::m_state
state_t m_state
Definition RemoteLogger.h:85
ASSA::RemoteLogger::handle_close
virtual int handle_close(int fd_)
Called by Reactor when we close connection with log_close()
Definition RemoteLogger.cpp:105
ASSA::RemoteLogger::LOG_MSG
@ LOG_MSG
Definition RemoteLogger.h:83
ASSA::RemoteLogger::SIGN_OFF
@ SIGN_OFF
Definition RemoteLogger.h:83
ASSA::RemoteLogger::SIGN_ON
@ SIGN_ON
Definition RemoteLogger.h:83
ASSA::ServiceHandler< ASSA::IPv4Socket >::get_stream
ASSA::IPv4Socket & get_stream()
Return referenct to underlying PEER_STREAM.
Definition ServiceHandler.h:100
ASSA::Socket::xdr_length
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition Socket.h:343
ASSA::Socket::turnOptionOff
bool turnOptionOff(opt_t opt_)
Disable socket option.
Definition Socket.cpp:204
ASSA::Socket::nonblocking
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition Socket.h:115
ASSA::Socket::good
bool good() const
Indicates no error on the socket.
Definition Socket.h:315
ASSA
Definition Acceptor.h:40
ASSA::READ_EVENT
@ READ_EVENT
Notify when there will be at least 1 byte available for reading from IO channel without blocking .
Definition EventHandler.h:36
ASSA::marker_t
marker_t
Definition LogMask.h:67
ASSA::FUNC_ENTRY
@ FUNC_ENTRY
Definition LogMask.h:69
ASSA::FUNC_MSG
@ FUNC_MSG
Definition LogMask.h:68
ASSA::Group
Group
Definition LogMask.h:25
ASSA::flush
Socket & flush(Socket &os_)
flush manipulator.
Definition Socket.h:587