Logger.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                             Logger.cpp
00004 //------------------------------------------------------------------------------
00005 // $Id: Logger.cpp,v 1.6 2005/03/12 21:03:41 vlg Exp $
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2001,2005 by Vladislav Grinchenko
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Library General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 
00015 #include <stdarg.h>
00016 
00017 #include "assa/Connector.h"
00018 #include "assa/INETAddress.h"
00019 #include "assa/IPv4Socket.h"
00020 #include "assa/FileLogger.h"
00021 #include "assa/StdOutLogger.h"
00022 #include "assa/RemoteLogger.h"
00023 #include "assa/AutoPtr.h"
00024 #include "assa/Logger.h"
00025 
00026 using namespace ASSA;
00027 
00028 ASSA_DECL_SINGLETON(Logger);
00029 
00030 // Logger's member functions
00031 
00032 int 
00033 Logger::
00034 log_close (void)
00035 {
00036     int ret = 0;
00037 
00038     if (m_impl) {
00039         ret = m_impl->log_close ();
00040         delete m_impl;
00041         m_impl = 0;
00042     }
00043     return ret;
00044 }
00045 
00046 int 
00047 Logger::
00048 log_open (u_long groups_)
00049 {
00050     if (m_impl != NULL) {
00051         std::cerr << "Logger::log_open - Implementation already exist" 
00052                   << std::endl;
00053         return -1;
00054     }
00055     m_impl = new StdOutLogger;
00056     return m_impl->log_open (groups_);
00057 }
00058     
00059 int 
00060 Logger::
00061 log_open (const char* logfname_, u_long groups_, u_long maxsize_)
00062 {
00063     if (m_impl != NULL) {
00064         return -1;
00065     }
00066     m_impl = new FileLogger;
00067     return m_impl->log_open (logfname_, groups_, maxsize_);
00068 }
00069 
00070 int 
00071 Logger::
00072 log_open (const std::string& logsvraddr_,
00073           const char*          logfname_, 
00074           u_long                 groups_, 
00075           u_long                maxsize_,
00076           Reactor*              reactor_)
00077 {
00078     {
00079         TimeVal tv (10.0);
00080         INETAddress addr (logsvraddr_.c_str ());
00081         if (addr.bad ()) {
00082             return -1;
00083         }
00084         Connector <RemoteLogger, IPv4Socket> log_connector;
00085         AutoPtr<RemoteLogger> lsp (new RemoteLogger);
00086         log_connector.open (tv);
00087         if (log_connector.connect (lsp.get (), addr) < 0) {
00088             delete m_impl;
00089             m_impl = NULL;
00090             return -1;
00091         }
00092         m_impl = lsp.release ();
00093     }
00094     int ret =  m_impl->log_open (m_app_name.c_str (), logfname_, 
00095                                  groups_, maxsize_, reactor_);
00096     return ret;
00097 }
00098 
00115 int
00116 Logger::
00117 log_msg (u_long g_, const char* fmt_, ...)
00118 {
00119     if (m_impl == NULL) {
00120         return -1;
00121     }
00122     int ret;
00123     string empty_str;
00124     va_list ap;
00125     size_t expected_sz = 0;
00126     char c;
00127 
00134     va_start (ap, fmt_);
00135     expected_sz = ::vsnprintf (&c, 1, fmt_, ap) + 1;
00136     va_end (ap);
00137 
00138     va_start (ap, fmt_);
00139     ret = m_impl->log_msg (static_cast<Group> (g_), m_context.size (),
00140                            m_context.size () ? m_context.top () : empty_str,
00141                            expected_sz, fmt_, ap);
00142     va_end (ap);
00143     return ret;
00144 }
00145 
00146 int
00147 Logger::
00148 log_func (u_long g_, marker_t type_)
00149 {
00150     std::string empty_str;
00151     if (m_impl == NULL) {
00152         return -1;
00153     }
00154     return  m_impl->log_func (static_cast<Group> (g_), m_context.size (),
00155                               m_context.size () ? m_context.top () : empty_str,
00156                               type_);
00157 }
00158 
00159 
00160 

Generated on Mon Dec 19 15:59:00 2005 for libassa by  doxygen 1.4.5