1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// -*- 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;
}
/**
Here is an interesting twist introduced by remote logging server:
Setting errno bits puts us in an unexpected situation.
Asynchronous connection establishment reliest on examening errno to see
if it was set by the system call, connect(), to EINPROGRESS.
If it was, and thus connect() failed, IPv4Socket's connect() would log it
as an error with EL() macro. When client is configured to
log its messages to the server, call to EL() would result into
the call to log_msg(), and because connection to the server
has not yet been established, m_impl is set to 0 and errno
is reset to EPERM. When stack unwinds itself, Connector's connect()
fails because Connector's connectServiceHandler() returned
errno different from expected EINPROGRESS (it is EPERM)!
*/
/** From vprintf(3S) manpage:
*
* int vsnprintf (char* buf, size_t cnt, const char* fmt, va_list argptr);
*
* "The vsnprintf() function returns the number of characters
* formatted, that is, then number of characters that would have
* been written to the buffer if it were large enough. It returns
* a negative value if an output error was encountered."
*
* However, mingw relies on WIN32 implementation of the function
* which is inherently broken (not C99 compliant).
* From MSD reference:
*
* "vsnprint returns the number of characters written if the
* the number of characters to write is less than or equal to 'cnt';
* if the number of characters to write is greater than 'cnt',
* RETURN -1 indicating that output has been truncated. The return
* value does not include the terminating null, if one is written.
*/
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;
}
/** Estimate message size
*/
#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;
/** Fromat and write message to the log
*/
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);
/**
* For extra debuggin
*
if (ret < 0) {
va_start (ap, fmt_);
vsnprintf (tmpbuf, TMPBUF_SZ -1, fmt_, ap);
std::cout << "m_impl->log_msg()=-1 message:\n" << tmpbuf
<< std::flush;
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_);
}
|