00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Fennel_TraceSource_Included
00025 #define Fennel_TraceSource_Included
00026
00027 #include <sstream>
00028 #include "fennel/common/TraceTarget.h"
00029
00030 FENNEL_BEGIN_NAMESPACE
00031
00036 class FENNEL_COMMON_EXPORT TraceSource
00037 {
00038 SharedTraceTarget pTraceTarget;
00039
00040 std::string name;
00041
00042 TraceLevel minimumLevel;
00043
00044 protected:
00048 explicit TraceSource();
00049
00059 explicit TraceSource(
00060 SharedTraceTarget pTraceTarget,
00061 std::string name);
00062
00063 public:
00064 virtual ~TraceSource();
00065
00073 virtual void initTraceSource(
00074 SharedTraceTarget pTraceTarget, std::string name);
00075
00083 void trace(TraceLevel level,std::string message) const;
00084
00088 bool isTracing() const
00089 {
00090 return pTraceTarget.get() ? true : false;
00091 }
00092
00100 bool isTracingLevel(TraceLevel level) const
00101 {
00102 return level >= minimumLevel;
00103 }
00104
00108 TraceTarget &getTraceTarget() const
00109 {
00110 assert(isTracing());
00111 return *(pTraceTarget.get());
00112 }
00113
00117 SharedTraceTarget getSharedTraceTarget() const
00118 {
00119 return pTraceTarget;
00120 }
00121
00127 std::string getTraceSourceName() const
00128 {
00129 return name;
00130 }
00131
00136 void setTraceSourceName(std::string const& n)
00137 {
00138 name = n;
00139 }
00140
00141 TraceLevel getMinimumTraceLevel() const
00142 {
00143 return minimumLevel;
00144 }
00145
00146 void disableTracing();
00147 };
00148
00155 #define FENNEL_TRACE(level,msg) FENNEL_DELEGATE_TRACE(level,this,msg)
00156 #define FENNEL_DELEGATE_TRACE(level,tracer,msg) \
00157 do { \
00158 if ((tracer)->isTracingLevel(level)) { \
00159 std::ostringstream oss; \
00160 oss << msg; \
00161 (tracer)->trace(level,oss.str()); \
00162 } \
00163 } while (false)
00164
00165
00166
00167
00168
00169 inline char *get_tid(char *tidstr, int cb)
00170 {
00171 snprintf(tidstr, cb, "%d", getCurrentThreadId());
00172 return tidstr;
00173 }
00174
00179 #define FENNEL_TRACE_THREAD(level, expr) \
00180 { \
00181 char tidstr[32]; \
00182 FENNEL_TRACE(\
00183 level, \
00184 "[thread " << fennel::get_tid(tidstr,sizeof(tidstr)) << "] " << expr); \
00185 }
00186
00187 FENNEL_END_NAMESPACE
00188
00189 #endif
00190
00191