libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 *******************************************************************************/
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
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