libassa 3.5.1
Loading...
Searching...
No Matches
RemoteLogger.cpp
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// RemoteLogger.cpp
4//------------------------------------------------------------------------------
5// $Id: RemoteLogger.cpp,v 1.3 2006/07/26 00:27:32 vlg Exp $
6//------------------------------------------------------------------------------
7// Copyright (c) 2003 by Vladislav Grinchenko
8//
9// This program is free software; you can redistribute it and/or
10// modify it under the terms of the GNU General Public License
11// as published by the Free Software Foundation; either version
12// 2 of the License, or (at your option) any later version.
13//------------------------------------------------------------------------------
14// Date:
15//------------------------------------------------------------------------------
16
17#include <iostream>
18#include <sstream>
19
20#include "Reactor.h"
21#include "Socket.h"
22#include "RemoteLogger.h"
23
24using namespace ASSA;
25
26/*******************************************************************************
27 Class member functions
28*******************************************************************************/
30RemoteLogger () :
31 m_state (closed),
32 m_recursive_call (false)
33{
34 // no-op
35}
36
37int
39open ()
40{
41 return 0;
42}
43
44int
46log_open (const char* appname_,
47 const char* logfname_,
51{
52 if (m_recursive_call) {
53 return 0;
54 }
55 m_recursive_call = true;
56
57 if (m_state == opened) {
58 return 0;
59 }
63
64 m_reactor->registerIOHandler (this, get_stream ().getHandler(),
66
71
74 size_t len = sizeof (maxsize_) +
77
80 get_stream () << 1234567890 << SIGN_ON << len
83 m_recursive_call = false;
84 return 0;
85}
86
87int
89log_close (void)
90{
94 if (m_state == opened) {
95 m_recursive_call = true;
96 get_stream () << 1234567890 << SIGN_OFF << 0 << ASSA::flush;
98 m_recursive_call = false;
99 }
100 return 0;
101}
102
103int
105handle_close (int fd_)
106{
107 m_state = closed;
108 m_logfname.empty ();
109 return 0;
110}
111
112void
114log_resync (void)
115{
116 if (m_state == opened) {
117 m_recursive_call = true;
119 m_recursive_call = false;
120 }
121}
122
123int
126 size_t indent_level_,
127 const string& func_name_,
128 size_t expected_sz_,
129 const char* fmt_,
131{
132 if (m_recursive_call) {
133 return 0;
134 }
135 if (m_state == closed) {
136 return -1;
137 }
138 if (!group_enabled (groups_)) {
139 return 0;
140 }
141
142 std::ostringstream os;
145
146 bool release = false;
147 char* msgbuf_ptr = format_msg (expected_sz_, fmt_, msg_list_, release);
148 if (msgbuf_ptr == NULL) {
149 return -1; // failed to format
150 }
151
152 os << msgbuf_ptr;
153
154 if (release) {
155 delete [] msgbuf_ptr;
156 }
157
160 if (get_stream ()) {
161 m_recursive_call = true;
162 Assure_exit (os.str ().length () != 0);
163 get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
164 << os.str () << ASSA::flush;
165 m_recursive_call = false;
166 }
167 else {
168 m_state = closed;
169 }
170 return 0;
171}
172
173int
176 size_t indent_level_,
177 const string& func_name_,
179{
180 if (m_recursive_call) {
181 return 0;
182 }
183 if (m_state == closed) {
184 return -1;
185 }
186 if (! group_enabled (groups_)) {
187 return 0;
188 }
189
190 std::ostringstream os;
193 os << ((type_ == FUNC_ENTRY) ? "---v---\n" : "---^---\n");
194
197 if (get_stream ().good ()) {
198 m_recursive_call = true;
199 get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
200 << os.str () << ASSA::flush;
201 m_recursive_call = false;
202 }
203 else {
204 m_state = closed;
205 }
206
207 return 0;
208}
209
210
#define Assure_exit(exp_)
Macro that makes program exit if assert fails.
Definition Assure.h:39
unsigned long u_long
Definition Logger_Impl.h:41
An implementation of Reactor pattern.
A proxy connection class with logging server, assa-logd.
Abstraction of socket data type.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
bool group_enabled(Group g_) const
u_long m_groups
Enabled groups.
string m_logfname
Log file name.
virtual u_short add_timestamp(ostream &sink_)
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition Reactor.cpp:93
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
virtual int open()
Called by Connector upon establishing connection.
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.
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)
virtual void log_resync(void)
Flush output buffer.
virtual int log_close(void)
Close connection to the assa-logd.
bool m_recursive_call
If true, recursive call is in progress.
virtual int handle_close(int fd_)
Called by Reactor when we close connection with log_close()
ASSA::IPv4Socket & get_stream()
Return referenct to underlying PEER_STREAM.
static size_t xdr_length(const std::string &s_)
Give the true length of the XDR-encoded STL string.
Definition Socket.h:343
bool turnOptionOff(opt_t opt_)
Disable socket option.
Definition Socket.cpp:204
@ nonblocking
Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK).
Definition Socket.h:115
bool good() const
Indicates no error on the socket.
Definition Socket.h:315
@ READ_EVENT
Notify when there will be at least 1 byte available for reading from IO channel without blocking .
marker_t
Definition LogMask.h:67
@ FUNC_ENTRY
Definition LogMask.h:69
@ FUNC_MSG
Definition LogMask.h:68
Group
Definition LogMask.h:25
Socket & flush(Socket &os_)
flush manipulator.
Definition Socket.h:587