Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

log.hh

00001 /*
00002 *  Name:      log.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Definitons for logging C++ programs
00005 *  Date:      $Date: 2003/08/08 23:30:34 $
00006 *  Revision:  $Revision: 1.2 $
00007 *
00008 *  Copyright (C) 2000-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_UTIL_LOGGING_LOG__
00027 #define _MPCL_UTIL_LOGGING_LOG__
00028 
00029 #include <iomanip>
00030 #include <iostream>
00031 #include <vector>
00032 #include "../../exceptions.hh"
00033 #include "../../system/all.hh"
00034 #include "../../text/string.hh"
00035 #include "../general-functions.hh"
00036 
00037 
00039 namespace mpcl
00040 {
00041 
00043   namespace util
00044   {
00045 
00047     namespace logging
00048     {
00049 
00050       using system::ISystem;
00051       using text::TString;
00052 
00054       class TBaseLog;
00055 
00057       template <typename TChar, typename TTraits>
00058       class TLog;
00059 
00061       typedef TBaseLog& (*MManipulator) (TBaseLog&);
00062 
00064       template <typename TChar, typename TTraits>
00065       TLog<TChar, TTraits>& header (TLog<TChar, TTraits>& rtBASE_LOG);
00066 
00068 
00072       TBaseLog& emerg (TBaseLog& rtBASE_LOG);   
00073       TBaseLog& alert (TBaseLog& rtBASE_LOG);   
00074       TBaseLog& crit (TBaseLog& rtBASE_LOG);    
00075       TBaseLog& err (TBaseLog& rtBASE_LOG);     
00076       TBaseLog& warning (TBaseLog& rtBASE_LOG); 
00077       TBaseLog& notice (TBaseLog& rtBASE_LOG);  
00078       TBaseLog& info (TBaseLog& rtBASE_LOG);    
00079       TBaseLog& debug (TBaseLog& rtBASE_LOG);   
00080 
00081 
00083 
00087       TBaseLog& authpriv (TBaseLog& rtBASE_LOG); 
00088       TBaseLog& cron (TBaseLog& rtBASE_LOG);     
00089       TBaseLog& daemon (TBaseLog& rtBASE_LOG);   
00090       TBaseLog& kern (TBaseLog& rtBASE_LOG);     
00091       TBaseLog& local0 (TBaseLog& rtBASE_LOG);   
00092       TBaseLog& local1 (TBaseLog& rtBASE_LOG);   
00093       TBaseLog& local2 (TBaseLog& rtBASE_LOG);   
00094       TBaseLog& local3 (TBaseLog& rtBASE_LOG);   
00095       TBaseLog& local4 (TBaseLog& rtBASE_LOG);   
00096       TBaseLog& local5 (TBaseLog& rtBASE_LOG);   
00097       TBaseLog& local6 (TBaseLog& rtBASE_LOG);   
00098       TBaseLog& local7 (TBaseLog& rtBASE_LOG);   
00099       TBaseLog& lpr (TBaseLog& rtBASE_LOG);      
00100       TBaseLog& mail (TBaseLog& rtBASE_LOG);     
00101       TBaseLog& news (TBaseLog& rtBASE_LOG);     
00102       TBaseLog& user (TBaseLog& rtBASE_LOG);     
00103       TBaseLog& uucp (TBaseLog& rtBASE_LOG);     
00104 
00105 
00107       class TBaseLog
00108       {
00109 
00110         public:
00111 
00116           typedef
00117             ISystem::ELogFacility
00118             ELogFacility;
00119 
00124           typedef
00125             ISystem::ELogLevel
00126             ELogLevel;
00127 
00128 
00129         public:
00130 
00132           virtual void setFacility (ELogFacility eLOG_FACILITY) = 0;
00133 
00135           virtual void setLevel (ELogLevel eLOG_LEVEL) = 0;
00136 
00137 
00138         public:
00139 
00140           //
00141           //  S E L E C T O R S
00142           //
00143 
00145           virtual ELogLevel getLevel (void) const = 0;
00146 
00148           virtual ELogFacility getFacility (void) const = 0;
00149 
00150       };  // class TBaseLog
00151 
00152 
00157       template < typename TChar                             ,
00158                  typename TTraits = std::char_traits<TChar> >
00159       class TLogStreambuf                           :
00160         public TBaseLog                             ,
00161         public std::basic_streambuf<TChar, TTraits>
00162       {
00163 
00164         private:
00165 
00167           typedef
00168             std::vector<std::basic_ostream<TChar, TTraits>*>
00169             TOstreamVector;
00170 
00171 
00172         private:
00173 
00175           ELogFacility   eLogFacility;
00176 
00178           ELogLevel   eLogLevel;
00179 
00181           TOstreamVector   tOstreamVector;
00182 
00184           TString   yLine;
00185 
00186 
00187         public:
00188 
00189           //
00190           //  C O N S T R U C T O R S
00191           //
00192 
00194           TLogStreambuf (void)                                      :
00195             TBaseLog                             ()                 ,
00196             std::basic_streambuf<TChar, TTraits> ()                 ,
00197             eLogFacility                         (ISystem::eUser)   ,
00198             eLogLevel                            (ISystem::eNotice) ,
00199             tOstreamVector                       ()                 ,
00200             yLine                                ()                 {}
00201 
00208           void also (std::basic_ostream<TChar, TTraits>& rtOSTREAM)
00209           {
00210             tOstreamVector.push_back (&rtOSTREAM);
00211           }
00212 
00220           void erase (const std::basic_ostream<TChar, TTraits>& rktOSTREAM)
00221             throw (TNotFoundException);
00222 
00229           int overflow (int iCHAR = EOF);
00230 
00232           void setFacility (ELogFacility eLOG_FACILITY)
00233           {
00234             eLogFacility = eLOG_FACILITY;
00235           }
00236 
00238           void setLevel (ELogLevel eLOG_LEVEL)
00239           {
00240             eLogLevel = eLOG_LEVEL;
00241           }
00242 
00252           std::streamsize xsputn (const char* pkcSTRING, std::streamsize zSIZE);
00253 
00254 
00255         public:
00256 
00257           //
00258           //  S E L E C T O R S
00259           //
00260 
00262           ELogLevel getLevel (void) const
00263           {
00264             return eLogLevel;
00265           }
00266 
00268           ELogFacility getFacility (void) const
00269           {
00270             return eLogFacility;
00271           }
00272 
00273       };  // class TLogStreambuf
00274 
00281       template < typename TChar                             ,
00282                  typename TTraits = std::char_traits<TChar> >
00283       class TLog                                  :
00284         public TBaseLog                           ,
00285         public std::basic_ostream<TChar, TTraits>
00286       {
00287 
00288         public:
00289 
00291           using TBaseLog::ELogLevel;
00292 
00293 
00294         private:
00295 
00297           TLogStreambuf<TChar, TTraits>   tLogStreambuf;
00298 
00299 
00300         public:
00301 
00302           //
00303           //  C O N S T R U C T O R S
00304           //
00305 
00307           TLog (void)                                           :
00308             TBaseLog                           ()               ,
00309             std::basic_ostream<TChar, TTraits> (&tLogStreambuf) ,
00310             tLogStreambuf                      ()               {}
00311 
00318           TLog& also (std::basic_ostream<TChar, TTraits>& rtOSTREAM)
00319           {
00320             tLogStreambuf.also (rtOSTREAM);
00321             return *this;
00322           }
00323 
00329           template <typename TItem>
00330           void debug (TItem tITEM)
00331           {
00332             ELogLevel      eCurrentLogLevel    = getLevel();
00333             ELogFacility   eCurrentLogFacility = getFacility();
00334 
00335             setLevel (ISystem::eDebug);
00336             setFacility (ISystem::eUser);
00337             *this << header << tITEM << std::endl;
00338             setLevel (eCurrentLogLevel);
00339             setFacility (eCurrentLogFacility);
00340           }
00341 
00348           TLog& erase (const std::basic_ostream<TChar, TTraits>& rktOSTREAM)
00349           {
00350             tLogStreambuf.erase (rktOSTREAM);
00351             return *this;
00352           }
00353 
00360           TLog& operator << (MManipulator mMANIP_FUNCTION)
00361           {
00362             (*mMANIP_FUNCTION) (*this);
00363             return *this;
00364           }
00365 
00372           TLog& operator << (TLog& (*mMANIP_FUNCTION) (TLog&))
00373           {
00374             return (*mMANIP_FUNCTION) (*this);
00375           }
00376 
00382           void setFacility (ELogFacility eLOG_FACILITY)
00383           {
00384             tLogStreambuf.setFacility (eLOG_FACILITY);
00385           }
00386 
00392           void setLevel (ELogLevel eLOG_LEVEL)
00393           {
00394             tLogStreambuf.setLevel (eLOG_LEVEL);
00395           }
00396 
00397 
00398         public:
00399 
00400           //
00401           //  S E L E C T O R S
00402           //
00403 
00405           ELogLevel getLevel (void) const
00406           {
00407             return tLogStreambuf.getLevel();
00408           }
00409 
00411           ELogFacility getFacility (void) const
00412           {
00413             return tLogStreambuf.getFacility();
00414           }
00415 
00416       };  // class TLog
00417 
00418 
00419       //
00420       //  E X P O R T E D   V A R I A B L E S
00421       //
00422 
00424       extern TLog<char>   tLog;
00425 
00426     }  // namespace logging
00427 
00428   }  // namespace util
00429 
00430 }  // namespace mpcl
00431 
00432 
00433 //
00434 //  E X P O R T E D   F U N C T I O N S
00435 //
00436 
00437 template <typename TChar, typename TTraits>
00438 mpcl::util::logging::TLog<TChar, TTraits>& mpcl::util::logging::
00439 header (TLog<TChar, TTraits>& rtBASE_LOG)
00440 {
00441 
00442   using system::ISystem;
00443 
00444   switch (rtBASE_LOG.getLevel())
00445   {
00446     case ISystem::eEmerg:   rtBASE_LOG << "#EMERG: ";   break;
00447     case ISystem::eAlert:   rtBASE_LOG << "#ALERT: ";   break;
00448     case ISystem::eCrit:    rtBASE_LOG << "#CRIT: ";    break;
00449     case ISystem::eErr:     rtBASE_LOG << "#ERR: ";     break;
00450     case ISystem::eWarning: rtBASE_LOG << "#WARNING: "; break;
00451     case ISystem::eNotice:  rtBASE_LOG << "#NOTICE: ";  break;
00452     case ISystem::eInfo:    rtBASE_LOG << "#INFO: ";    break;
00453     case ISystem::eDebug:   rtBASE_LOG << "#DEBUG: ";   break;
00454   }
00455   return rtBASE_LOG;
00456 
00457 }  // header()
00458 
00459 
00460 //
00461 //  C O N S T R U C T O R S
00462 //
00463 
00464 template <typename TChar, typename TTraits>
00465 void mpcl::util::logging::TLogStreambuf<TChar, TTraits>::
00466 erase (const std::basic_ostream<TChar, TTraits>& rktOSTREAM)
00467   throw (TNotFoundException)
00468 {
00469  
00470   typename TOstreamVector::iterator   I;
00471 
00472   I = std::find (tOstreamVector.begin(), tOstreamVector.end(), &rktOSTREAM);
00473   if ( I == tOstreamVector.end() )
00474   {
00475     throw TNotFoundException ("stream not found", __FILE__, __LINE__);
00476   }
00477   else
00478   {
00479     tOstreamVector.erase (I);
00480   }
00481 
00482 }  // TLogStreambuf::erase()
00483 
00484 
00485 template <typename TChar, typename TTraits>
00486 int mpcl::util::logging::TLogStreambuf<TChar, TTraits>::
00487 overflow (int iCHAR)
00488 {
00489 
00490   using std::for_each;
00491   using std::isprint;
00492   using std::string;
00493   using system::tSystem;
00494   using text::Format;
00495 
00496   switch (iCHAR)
00497   {
00498     case '\x0':
00499     case EOF:
00500     {
00501       break;
00502     }
00503     case '\n':
00504     {
00505       tSystem.writeLogEntry (yLine, eLogLevel, eLogFacility);
00506       for_each ( tOstreamVector.begin()                                              ,
00507                  tOstreamVector.end()                                                ,
00508                  MAppend<std::basic_ostream<TChar, TTraits>*, string> (yLine + '\n') );
00509       yLine.erase();
00510       break;
00511     }
00512     default:
00513     {
00514       if ( isprint (iCHAR) )
00515       {
00516         yLine += iCHAR;
00517       }
00518       else
00519       {
00520         yLine += Format ("\\x%x", (unsigned int) iCHAR);
00521       }
00522       break;
00523     }
00524   }
00525   return iCHAR;
00526 
00527 }  // TLogStreambuf::overflow()
00528 
00529 
00530 template <typename TChar, typename TTraits>
00531 std::streamsize mpcl::util::logging::TLogStreambuf<TChar, TTraits>::
00532 xsputn (const char* pkcSTRING, std::streamsize zSIZE)
00533 {
00534 
00535   using std::basic_ostream;
00536   using std::for_each;
00537   using std::isprint;
00538   using std::streamsize;
00539   using std::string;
00540   using system::tSystem;
00541   using text::Format;
00542 
00543   if ( !pkcSTRING )
00544   {
00545     throw TConstraintException ("null string", __FILE__, __LINE__);
00546   }
00547   else
00548   {
00549     for (streamsize I = 0; ( ( I < zSIZE ) && *pkcSTRING ) ;++I, ++pkcSTRING)
00550     {
00551       if ( *pkcSTRING != '\n' )
00552       {
00553         if ( isprint (*pkcSTRING) )
00554         {
00555           yLine += *pkcSTRING;
00556         }
00557         else
00558         {
00559           yLine += Format ("\\x%x", (int) *pkcSTRING);
00560         }
00561       }
00562       else
00563       {
00564         tSystem.writeLogEntry (yLine, eLogLevel, eLogFacility);
00565         for_each ( tOstreamVector.begin()                                         ,
00566                    tOstreamVector.end()                                           ,
00567                    MAppend<basic_ostream<TChar, TTraits>*, string> (yLine + '\n') );
00568         yLine.erase();
00569       }
00570     }
00571   }
00572   return zSIZE;
00573 
00574 }  // TLogStreambuf::xsputn()
00575 
00576 
00577 #endif  // not _MPCL_UTIL_LOGGING_LOG__

Generated on Mon Oct 13 02:35:23 2003 for MPCL by doxygen1.2.18