libassa 3.5.1
Loading...
Searching...
No Matches
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
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
67{
68 if (m_impl != NULL) {
69 return -1;
70 }
71 m_impl = new FileLogger;
73}
74
75int
77log_open (const std::string& logsvraddr_,
78 const char* logfname_,
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_,
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,
209 fmt_,
210 ap);
211 va_end (ap);
212
225 return ret;
226}
227
228int
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.
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.
#define ASSA_DECL_SINGLETON(K)
ASSA_DECL_SINGLETON macro inserts static member declarations mandated by the Singleton class.
Definition Singleton.h:82
StdOutLogger implements Logger as standard output.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
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.
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_close(void)
Definition Logger.cpp:39
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
marker_t
Definition LogMask.h:67
Group
Definition LogMask.h:25