libassa  3.5.1
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 
24 using namespace ASSA;
25 
26 /*******************************************************************************
27  Class member functions
28 *******************************************************************************/
30 RemoteLogger () :
31  m_state (closed),
32  m_recursive_call (false)
33 {
34  // no-op
35 }
36 
37 int
39 open ()
40 {
41  return 0;
42 }
43 
44 int
46 log_open (const char* appname_,
47  const char* logfname_,
48  u_long groups_,
49  u_long maxsize_,
50  Reactor* reactor_)
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  }
60  m_logfname = logfname_;
61  m_groups = groups_;
62  m_reactor = reactor_;
63 
64  m_reactor->registerIOHandler (this, get_stream ().getHandler(),
66 
71 
74  size_t len = sizeof (maxsize_) +
75  Socket::xdr_length (appname_) +
76  Socket::xdr_length (logfname_);
77 
80  get_stream () << 1234567890 << SIGN_ON << len
81  << maxsize_ << appname_ << logfname_ << ASSA::flush;
82  m_state = opened;
83  m_recursive_call = false;
84  return 0;
85 }
86 
87 int
89 log_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 
103 int
105 handle_close (int fd_)
106 {
107  m_state = closed;
108  m_logfname.empty ();
109  return 0;
110 }
111 
112 void
114 log_resync (void)
115 {
116  if (m_state == opened) {
117  m_recursive_call = true;
118  get_stream () << ASSA::flush;
119  m_recursive_call = false;
120  }
121 }
122 
123 int
125 log_msg (Group groups_,
126  size_t indent_level_,
127  const string& func_name_,
128  size_t expected_sz_,
129  const char* fmt_,
130  va_list msg_list_)
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;
143  add_timestamp (os);
144  indent_func_name (os, func_name_, indent_level_, FUNC_MSG);
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 
173 int
175 log_func (Group groups_,
176  size_t indent_level_,
177  const string& func_name_,
178  marker_t type_)
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;
191  add_timestamp (os);
192  indent_func_name (os, func_name_, indent_level_, type_);
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.
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
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition: Logger_Impl.cpp:54
bool group_enabled(Group g_) const
Definition: Logger_Impl.h:164
u_long m_groups
Enabled groups.
Definition: Logger_Impl.h:239
string m_logfname
Log file name.
Definition: Logger_Impl.h:242
virtual u_short add_timestamp(ostream &sink_)
Definition: Logger_Impl.cpp:35
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)
Reactor * m_reactor
Definition: RemoteLogger.h:86
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.
Definition: RemoteLogger.h:89
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
Definition: Acceptor.h:40
@ READ_EVENT
Notify when there will be at least 1 byte available for reading from IO channel without blocking .
Definition: EventHandler.h:36
Socket & flush(Socket &os_)
flush manipulator.
Definition: Socket.h:587
marker_t
Definition: LogMask.h:67
@ FUNC_ENTRY
Definition: LogMask.h:69
@ FUNC_MSG
Definition: LogMask.h:68
Group
Definition: LogMask.h:25