libzypp  10.5.0
Debug.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00013 #warning ZYPP_BASE_DEBUG_H included
00014 #ifndef ZYPP_BASE_DEBUG_H
00015 #define ZYPP_BASE_DEBUG_H
00016 
00017 #include <iosfwd>
00018 //#include <sstream>
00019 //#include <string>
00020 #include "zypp/base/Logger.h"
00021 #include "zypp/base/String.h"
00022 #include "zypp/ExternalProgram.h"
00023 #include "zypp/base/ProvideNumericId.h"
00024 
00026 namespace zypp
00027 { 
00028 
00029   namespace debug
00030   { 
00031 
00035 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
00036 
00038     inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
00039     {
00040       static std::string mypid( str::numstring( getpid() ) );
00041       const char* argv[] =
00042       {
00043         "ps",
00044         "v",
00045         mypid.c_str(),
00046         NULL
00047       };
00048       ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
00049 
00050       str << "MEMUSAGE " << msg << std::endl;
00051       for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
00052           str << line;
00053 
00054       prog.close();
00055       return str;
00056     }
00057 
00059 
00066     struct TraceCADBase
00067     {
00068       enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
00069       std::string _ident;
00070     };
00071 
00073     inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
00074     {
00075       switch( obj )
00076         {
00077         case TraceCADBase::CTOR:     return str << "CTOR";
00078         case TraceCADBase::COPYCTOR: return str << "COPYCTOR";
00079         case TraceCADBase::ASSIGN:   return str << "ASSIGN";
00080         case TraceCADBase::DTOR:     return str << "DTOR";
00081         case TraceCADBase::PING:     return str << "PING";
00082         }
00083       return str;
00084     }
00085 
00107     template<class _Tp>
00108       struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
00109                       , public TraceCADBase
00110       {
00111         static unsigned long & _totalTraceCAD()
00112         { static unsigned long _val = 0;
00113           return _val; }
00114 
00115         TraceCAD()
00116         { _ident = __PRETTY_FUNCTION__;
00117           ++_totalTraceCAD();
00118           traceCAD( CTOR, *this, *this ); }
00119 
00120         TraceCAD( const TraceCAD & rhs )
00121         { ++_totalTraceCAD();
00122           traceCAD( COPYCTOR, *this, rhs ); }
00123 
00124         TraceCAD & operator=( const TraceCAD & rhs )
00125         { traceCAD( ASSIGN, *this, rhs ); return *this; }
00126 
00127         virtual ~TraceCAD()
00128         { --_totalTraceCAD();
00129           traceCAD( DTOR, *this, *this ); }
00130 
00131         void _PING() const
00132         { traceCAD( PING, *this, *this ); }
00133       };
00134 
00136     template<class _Tp>
00137       inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
00138       { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
00139                    << ") [" << &obj << "] "; }
00140 
00144     template<class _Tp>
00145       void traceCAD( TraceCADBase::What what_r,
00146                      const TraceCAD<_Tp> & self_r,
00147                      const TraceCAD<_Tp> & rhs_r )
00148       {
00149         switch( what_r )
00150           {
00151           case TraceCADBase::CTOR:
00152           case TraceCADBase::PING:
00153           case TraceCADBase::DTOR:
00154             _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
00155             break;
00156 
00157           case TraceCADBase::COPYCTOR:
00158           case TraceCADBase::ASSIGN:
00159             _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
00160             break;
00161           }
00162       }
00164 
00165 
00167   } // namespace debug
00170 } // namespace zypp
00172 #endif // ZYPP_BASE_DEBUG_H