1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Debug.h
11 * Debuging tools which should not be used in released code.
14 #warning ZYPP_BASE_DEBUG_H included
15 #ifndef ZYPP_BASE_DEBUG_H
16 #define ZYPP_BASE_DEBUG_H
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/String.h"
23 #include "zypp/ExternalProgram.h"
24 #include "zypp/base/ProvideNumericId.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 /** \defgroup DEBUG Debug tools
36 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
39 inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
41 static std::string mypid( str::numstring( getpid() ) );
49 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
51 str << "MEMUSAGE " << msg << std::endl;
52 for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
59 ///////////////////////////////////////////////////////////////////
60 /** \defgroup DBG_TRACER Tracer
64 /** Base for a simple tracer. Provides an enum indicating which
65 * traced functions were called.
69 enum What { CTOR, COPYCTOR, MOVECTOR, ASSIGN, MOVEASSIGN, DTOR, PING };
73 /** \relates TraceCADBase Stream output of TraceCADBase::What. */
74 inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
78 case TraceCADBase::CTOR: return str << "CTOR";
79 case TraceCADBase::COPYCTOR: return str << "COPYCTOR";
80 case TraceCADBase::MOVECTOR: return str << "MOVECTOR";
81 case TraceCADBase::ASSIGN: return str << "ASSIGN";
82 case TraceCADBase::MOVEASSIGN: return str << "MOVEASSIGN";
83 case TraceCADBase::DTOR: return str << "DTOR";
84 case TraceCADBase::PING: return str << "PING";
89 /** A simple tracer for (copy) Construction, Assignment, and
90 * Destruction. To trace class Foo, derive public from
91 * TraceCAD<Foo>. This tracer simply calls traceCAD in each
92 * traced method, and traceCAD simply drops a line in the log.
94 * This tracer logs construction, copy construction, assignment,
95 * destruction and ping.
97 * assignment: In case the traced class defines an operator=
98 * it must be altered to call TraceCAD::operator=, otherwise it
101 * ping: Completely up to you. Call ping somewhere in the traced
102 * class to indicate something. In case you overload traceCAD, do
103 * whatever is appropriate on ping. It's just an offer to perform
104 * logging or actions here, and not in the traced code.
106 * But traceCAD may be overloaded to produce more stats.
108 * \see \c Example.COW_debug.cc.
111 struct TraceCAD : public base::ProvideNumericId<TraceCAD<Tp>, unsigned long>
112 , public TraceCADBase
114 static unsigned long & _totalTraceCAD()
115 { static unsigned long _val = 0;
119 { _ident = __PRETTY_FUNCTION__;
121 traceCAD( CTOR, *this, *this ); }
123 TraceCAD( const TraceCAD & rhs )
124 { ++_totalTraceCAD();
125 traceCAD( COPYCTOR, *this, rhs ); }
127 TraceCAD( TraceCAD && rhs )
128 { ++_totalTraceCAD();
129 traceCAD( MOVECTOR, *this, rhs ); }
131 TraceCAD & operator=( const TraceCAD & rhs )
132 { traceCAD( ASSIGN, *this, rhs ); return *this; }
134 TraceCAD & operator=( TraceCAD && rhs )
135 { traceCAD( MOVEASSIGN, *this, rhs ); return *this; }
138 { --_totalTraceCAD();
139 traceCAD( DTOR, *this, *this ); }
142 { traceCAD( PING, *this, *this ); }
145 /** \relates TraceCAD Stream output. */
147 inline std::ostream & operator<<( std::ostream & str, const TraceCAD<Tp> & obj )
148 { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
149 << ") [" << &obj << "] "; }
151 /** Drop a log line about the traced method. Overload to
155 void traceCAD( TraceCADBase::What what_r,
156 const TraceCAD<Tp> & self_r,
157 const TraceCAD<Tp> & rhs_r )
161 case TraceCADBase::CTOR:
162 case TraceCADBase::PING:
163 case TraceCADBase::DTOR:
164 L_DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
167 case TraceCADBase::COPYCTOR:
168 case TraceCADBase::MOVECTOR:
169 case TraceCADBase::ASSIGN:
170 case TraceCADBase::MOVEASSIGN:
171 L_DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
176 ///////////////////////////////////////////////////////////////////
178 /////////////////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////////////////
184 #endif // ZYPP_BASE_DEBUG_H
185 #endif // ZYPP_NDEBUG