libassa 3.5.1
Logger.cpp
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// Logger.cpp
4//------------------------------------------------------------------------------
5// $Id: Logger.cpp,v 1.8 2012/05/23 02:52:25 vlg Exp $
6//------------------------------------------------------------------------------
7// Copyright (c) 2001,2005 by Vladislav Grinchenko
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Library General Public
11// License as published by the Free Software Foundation; either
12// version 2 of the License, or (at your option) any later version.
13//------------------------------------------------------------------------------
14
15#include <stdarg.h>
16
17#include <iostream> // for WIN32 hacking
18
19#include "assa/Connector.h"
20#include "assa/INETAddress.h"
21#include "assa/IPv4Socket.h"
22#include "assa/FileLogger.h"
23#include "assa/StdOutLogger.h"
24#include "assa/RemoteLogger.h"
25#include "assa/AutoPtr.h"
26#include "assa/Logger.h"
27
28using namespace ASSA;
29
31
32// Internal storage
33static const int TMPBUF_SZ = 4096;
34
35// Logger's member functions
36
37int
39log_close (void)
40{
41 int ret = 0;
42
43 if (m_impl) {
44 ret = m_impl->log_close ();
45 delete m_impl;
46 m_impl = 0;
47 }
48 return ret;
49}
50
51int
53log_open (u_long groups_)
54{
55 if (m_impl != NULL) {
56 std::cerr << "Logger::log_open - Implementation already exist"
57 << std::endl;
58 return -1;
59 }
60 m_impl = new StdOutLogger;
61 return m_impl->log_open (groups_);
62}
63
64int
66log_open (const char* logfname_, u_long groups_, u_long maxsize_)
67{
68 if (m_impl != NULL) {
69 return -1;
70 }
71 m_impl = new FileLogger;
72 return m_impl->log_open (logfname_, groups_, maxsize_);
73}
74
75int
77log_open (const std::string& logsvraddr_,
78 const char* logfname_,
79 u_long groups_,
80 u_long maxsize_,
81 Reactor* reactor_)
82{
83 {
84 TimeVal tv (10.0);
85 INETAddress addr (logsvraddr_.c_str ());
86 if (addr.bad ()) {
87 return -1;
88 }
89
92 log_connector.open (tv);
93
94 if (log_connector.connect (lsp.get (), addr) < 0) {
95 delete m_impl;
96 m_impl = NULL;
97 return -1;
98 }
99
100 m_impl = lsp.release ();
101 }
102 int ret = m_impl->log_open (m_app_name.c_str (), logfname_,
103 groups_, maxsize_, reactor_);
104 return ret;
105}
106
142int
144log_msg (u_long g_, const char* fmt_, ...)
145{
146 va_list ap;
147 va_list ap2;
148 string empty_str;
149 size_t expected_sz = 0;
150
151 static char tmpbuf[TMPBUF_SZ];
152 char* bufp = tmpbuf;
153 int len = TMPBUF_SZ;
154 int ret;
155
156 if (m_impl == NULL) {
157 return -1;
158 }
159
162#if defined(WIN32)
163
164 va_copy (ap2, ap);
165 ret = vsnprintf (bufp, len-1, fmt_, ap2);
166 va_end (ap2);
167
168 if (ret == -1 || ret >= len)
169 {
170 len *= 2;
171 bufp = new char [len];
172 while (1) {
173 va_copy (ap2, ap);
174 ret = vsnprintf (bufp, len-1, fmt_, ap2);
175 va_end (ap2);
176 if (ret > -1 && ret < len) {
177 delete [] bufp;
178 break;
179 }
180 len *= 2;
181 delete [] bufp;
182 bufp = new char [len];
183// std::cout << "Logger::log_func : malloc(" << len << ")\n"
184// << std::flush;
185 }
186 }
187
188#else /* POSIX, C99 compliant */
189
190 char c;
191 va_start (ap, fmt_);
192 ret = ::vsnprintf (&c, 1, fmt_, ap);
193 va_end (ap);
194
195#endif
196
197 expected_sz = ret + 1;
198
199// std::cout << "Logger::log_func : expected_sz = "
200// << expected_sz << "\n" << std::flush;
201
204 va_start (ap, fmt_);
205 ret = m_impl->log_msg (static_cast<Group> (g_),
206 m_context.size (),
207 m_context.size () ? m_context.top () : empty_str,
208 expected_sz,
209 fmt_,
210 ap);
211 va_end (ap);
212
225 return ret;
226}
227
228int
230log_func (u_long g_, marker_t type_)
231{
232 std::string empty_str;
233
234 if (m_impl == NULL) {
235 return -1;
236 }
237
238 return m_impl->log_func (static_cast<Group> (g_),
239 m_context.size (),
240 m_context.size () ? m_context.top () : empty_str,
241 type_);
242}
243
244
245
AutoPtr is a local implementation of STL's auto_ptr that makes dynamic memory handling a bit easier.
A generic pattern for establishing connection with TCP/IP servers.
Implemention of a Logger as a disk-based file.
An incapsulation of TCP/UDP Internet Protocol socket address structure.
Class IPv4Socket covers domain types AF_INET and AF_UNIX.
ASSA_DECL_SINGLETON(Logger)
static const int TMPBUF_SZ
Definition: Logger.cpp:33
An abstraction to message logging facility.
unsigned long u_long
Definition: Logger_Impl.h:41
A proxy connection class with logging server, assa-logd.
StdOutLogger implements Logger as standard output.
bool bad() const
Indicates whether there was error during address construction process i.e.
Definition: Address.h:80
AutoPtr is based on SGI implementation of a auto_ptr template that makes memory handling a little bit...
Definition: AutoPtr.h:49
X * release()
Give up the ownership of the memory.
Definition: AutoPtr.h:130
X * get() const
Get a raw memory pointer without changing ownership status.
Definition: AutoPtr.h:123
Connector is a template class for initialization of communication services.
Definition: Connector.h:64
virtual int open(const TimeVal &tv_=TimeVal(5.0), ConnectMode mode_=sync, Reactor *r_=(Reactor *) NULL)
Configure Connector.
Definition: Connector.h:223
virtual int connect(SERVICE_HANDLER *sh_, Address &addr_, int protocol_=AF_INET)
Define strategy for establishing connection.
Definition: Connector.h:245
virtual int log_close(void)=0
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)=0
virtual int log_open(u_long groups_)
Open StdErr Logger.
Definition: Logger_Impl.h:264
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)=0
int log_msg(u_long g_, const char *fmt_,...)
Here is an interesting twist introduced by remote logging server:
Definition: Logger.cpp:144
int log_open(u_long groups_)
Write log messages to standard output.
Definition: Logger.cpp:53
int log_func(u_long g_, marker_t type_)
Definition: Logger.cpp:230
std::string m_app_name
Stack of all contexts.
Definition: Logger.h:130
Logger_Impl * m_impl
Definition: Logger.h:128
stack< string > m_context
Logger implementation.
Definition: Logger.h:129
int log_close(void)
Close logging stream.
Definition: Logger.h:359
Definition: Acceptor.h:40
marker_t
Definition: LogMask.h:67
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602
Group
Definition: LogMask.h:25