xrootd
XrdClLog.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef __XRD_CL_LOG_HH__
00020 #define __XRD_CL_LOG_HH__
00021 
00022 #include <stdarg.h>
00023 #include <string>
00024 #include <map>
00025 #include <stdint.h>
00026 
00027 #include "XrdCl/XrdClOptimizers.hh"
00028 
00029 #include "XrdSys/XrdSysPthread.hh"
00030 
00031 namespace XrdCl
00032 {
00033   //----------------------------------------------------------------------------
00035   //----------------------------------------------------------------------------
00036   class LogOut
00037   {
00038     public:
00039       virtual ~LogOut() {}
00040 
00041       //------------------------------------------------------------------------
00045       //------------------------------------------------------------------------
00046       virtual void Write( const std::string &message ) = 0;
00047   };
00048 
00049   //----------------------------------------------------------------------------
00051   //----------------------------------------------------------------------------
00052   class LogOutFile: public LogOut
00053   {
00054     public:
00055       LogOutFile(): pFileDes(-1) {};
00056       virtual ~LogOutFile() { Close(); };
00057 
00058       //------------------------------------------------------------------------
00060       //------------------------------------------------------------------------
00061       bool Open( const std::string &fileName );
00062 
00063       //------------------------------------------------------------------------
00065       //------------------------------------------------------------------------
00066       void Close();
00067       virtual void Write( const std::string &message );
00068     private:
00069       int pFileDes;
00070   };
00071 
00072   //----------------------------------------------------------------------------
00074   //----------------------------------------------------------------------------
00075   class LogOutCerr: public LogOut
00076   {
00077     public:
00078       virtual void Write( const std::string &message );
00079     private:
00080       XrdSysMutex pMutex;
00081   };
00082 
00083   //----------------------------------------------------------------------------
00085   //----------------------------------------------------------------------------
00086   class Log
00087   {
00088     public:
00089       //------------------------------------------------------------------------
00091       //------------------------------------------------------------------------
00092       enum LogLevel
00093       {
00094         NoMsg       = 0,  
00095         ErrorMsg    = 1,  
00096         WarningMsg  = 2,  
00097         InfoMsg     = 3,  
00098         DebugMsg    = 4,  
00099         DumpMsg     = 5   
00100       };
00101 
00102       //------------------------------------------------------------------------
00104       //------------------------------------------------------------------------
00105       Log(): pLevel( NoMsg ), pTopicMaxLength( 18 )
00106       {
00107         pOutput = new LogOutCerr();
00108         int maxMask = (int)DumpMsg+1;
00109         for( int i = 0; i < maxMask; ++i )
00110           pMask[i] = 0xffffffffffffffffULL;
00111       }
00112 
00113       //------------------------------------------------------------------------
00114       // Destructor
00115       //------------------------------------------------------------------------
00116       ~Log()
00117       {
00118         delete pOutput;
00119       }
00120 
00121       //------------------------------------------------------------------------
00123       //------------------------------------------------------------------------
00124       void Error( uint64_t topic, const char *format, ... )
00125       {
00126         if( unlikely( pLevel < ErrorMsg ) )
00127           return;
00128 
00129         if( unlikely( (topic & pMask[ErrorMsg]) == 0 ) )
00130           return;
00131 
00132         va_list argList;
00133         va_start( argList, format );
00134         Say( ErrorMsg, topic, format, argList );
00135         va_end( argList );
00136       }
00137 
00138       //------------------------------------------------------------------------
00140       //------------------------------------------------------------------------
00141       void Warning( uint64_t topic, const char *format, ... )
00142       {
00143         if( unlikely( pLevel < WarningMsg ) )
00144           return;
00145 
00146         if( unlikely( (topic & pMask[WarningMsg]) == 0 ) )
00147           return;
00148 
00149         va_list argList;
00150         va_start( argList, format );
00151         Say( WarningMsg, topic, format, argList );
00152         va_end( argList );
00153       }
00154 
00155       //------------------------------------------------------------------------
00157       //------------------------------------------------------------------------
00158       void Info( uint64_t topic, const char *format, ... )
00159       {
00160         if( likely( pLevel < InfoMsg ) )
00161           return;
00162 
00163         if( unlikely( (topic & pMask[InfoMsg]) == 0 ) )
00164           return;
00165 
00166         va_list argList;
00167         va_start( argList, format );
00168         Say( InfoMsg, topic, format, argList );
00169         va_end( argList );
00170       }
00171 
00172       //------------------------------------------------------------------------
00174       //------------------------------------------------------------------------
00175       void Debug( uint64_t topic, const char *format, ... )
00176       {
00177         if( likely( pLevel < DebugMsg ) )
00178           return;
00179 
00180         if( unlikely( (topic & pMask[DebugMsg]) == 0 ) )
00181           return;
00182 
00183         va_list argList;
00184         va_start( argList, format );
00185         Say( DebugMsg, topic, format, argList );
00186         va_end( argList );
00187       }
00188 
00189       //------------------------------------------------------------------------
00191       //------------------------------------------------------------------------
00192       void Dump( uint64_t topic, const char *format, ... )
00193       {
00194         if( likely( pLevel < DumpMsg ) )
00195           return;
00196 
00197         if( unlikely( (topic & pMask[DumpMsg]) == 0 ) )
00198           return;
00199 
00200         va_list argList;
00201         va_start( argList, format );
00202         Say( DumpMsg, topic, format, argList );
00203         va_end( argList );
00204       }
00205 
00206       //------------------------------------------------------------------------
00213       //------------------------------------------------------------------------
00214       void Say( LogLevel level, uint64_t topic, const char *format, va_list list );
00215 
00216       //------------------------------------------------------------------------
00218       //------------------------------------------------------------------------
00219       void SetLevel( LogLevel level )
00220       {
00221         pLevel = level;
00222       }
00223 
00224       //------------------------------------------------------------------------
00226       //------------------------------------------------------------------------
00227       void SetLevel( const std::string &level )
00228       {
00229         LogLevel lvl;
00230         if( StringToLogLevel( level, lvl ) )
00231           pLevel = lvl;
00232       }
00233 
00234       //------------------------------------------------------------------------
00236       //------------------------------------------------------------------------
00237       void SetOutput( LogOut *output )
00238       {
00239         delete pOutput;
00240         pOutput = output;
00241       }
00242 
00243       //------------------------------------------------------------------------
00245       //------------------------------------------------------------------------
00246       void SetMask( LogLevel level, uint64_t mask )
00247       {
00248         pMask[level] = mask;
00249       }
00250 
00251       //------------------------------------------------------------------------
00253       //------------------------------------------------------------------------
00254       void SetMask( const std::string &level, uint64_t mask )
00255       {
00256         LogLevel lvl;
00257         if( StringToLogLevel( level, lvl ) )
00258           pMask[lvl] = mask;
00259       }
00260 
00261       //------------------------------------------------------------------------
00263       //------------------------------------------------------------------------
00264       void SetTopicName( uint64_t topic, std::string name );
00265 
00266       //------------------------------------------------------------------------
00268       //------------------------------------------------------------------------
00269       LogLevel GetLevel() const
00270       {
00271         return pLevel;
00272       }
00273 
00274     private:
00275       typedef std::map<uint64_t, std::string> TopicMap;
00276       std::string LogLevelToString( LogLevel level );
00277       bool StringToLogLevel( const std::string &strLevel, LogLevel &level );
00278       std::string TopicToString( uint64_t topic );
00279 
00280       LogLevel    pLevel;
00281       uint64_t    pMask[DumpMsg+1];
00282       LogOut     *pOutput;
00283       TopicMap    pTopicMap;
00284       uint32_t    pTopicMaxLength;
00285   };
00286 }
00287 
00288 #endif // __XRD_CL_LOG_HH__