libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
28 using namespace ASSA;
29 
31 
32 // Internal storage
33 static const int TMPBUF_SZ = 4096;
34 
35 // Logger's member functions
36 
37 int
39 log_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 
51 int
53 log_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 
64 int
66 log_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 
75 int
77 log_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 
142 int
143 Logger::
144 log_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 
228 int
229 Logger::
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