00001
00002
00003
00004
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 <iostream>
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
00106 template<class _Tp>
00107 struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
00108 , public TraceCADBase
00109 {
00110 static unsigned long & _totalTraceCAD()
00111 { static unsigned long _val = 0;
00112 return _val; }
00113
00114 TraceCAD()
00115 { _ident = __PRETTY_FUNCTION__;
00116 ++_totalTraceCAD();
00117 traceCAD( CTOR, *this, *this ); }
00118
00119 TraceCAD( const TraceCAD & rhs )
00120 { ++_totalTraceCAD();
00121 traceCAD( COPYCTOR, *this, rhs ); }
00122
00123 TraceCAD & operator=( const TraceCAD & rhs )
00124 { traceCAD( ASSIGN, *this, rhs ); return *this; }
00125
00126 virtual ~TraceCAD()
00127 { --_totalTraceCAD();
00128 traceCAD( DTOR, *this, *this ); }
00129
00130 void _PING() const
00131 { traceCAD( PING, *this, *this ); }
00132 };
00133
00135 template<class _Tp>
00136 inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
00137 { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
00138 << ") [" << &obj << "] "; }
00139
00143 template<class _Tp>
00144 void traceCAD( TraceCADBase::What what_r,
00145 const TraceCAD<_Tp> & self_r,
00146 const TraceCAD<_Tp> & rhs_r )
00147 {
00148 switch( what_r )
00149 {
00150 case TraceCADBase::CTOR:
00151 case TraceCADBase::PING:
00152 case TraceCADBase::DTOR:
00153 _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
00154 break;
00155
00156 case TraceCADBase::COPYCTOR:
00157 case TraceCADBase::ASSIGN:
00158 _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
00159 break;
00160 }
00161 }
00163
00164
00166 }
00169 }
00171 #endif // ZYPP_BASE_DEBUG_H