00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00034
00035 #ifndef GROOVX_RUTZ_TRACE_CC_UTC20050626084020_DEFINED
00036 #define GROOVX_RUTZ_TRACE_CC_UTC20050626084020_DEFINED
00037
00038 #include "rutz/trace.h"
00039
00040 #include "rutz/backtrace.h"
00041
00042 #include <iostream>
00043
00045
00046
00047
00049
00050 namespace
00051 {
00052 unsigned int g_max_trace_level = 20;
00053 bool g_do_global_trace = false;
00054
00055 void print_in(const char* context_name) throw()
00056 {
00057 const unsigned int n = rutz::backtrace::current().size();
00058
00059 if (n < g_max_trace_level)
00060 {
00061 for (unsigned int i = 0; i < n; ++i)
00062 {
00063 std::cerr << "| ";
00064 }
00065
00066 if (n < 10)
00067 std::cerr << n << "-->> ";
00068 else if (n < 100)
00069 std::cerr << n << "->> ";
00070 else
00071 std::cerr << n << ">> ";
00072
00073 std::cerr << context_name << '\n';
00074 }
00075 }
00076
00077 void print_out(const char* context_name) throw()
00078 {
00079 const unsigned int n = rutz::backtrace::current().size();
00080
00081 if (n < g_max_trace_level)
00082 {
00083 for (unsigned int i = 0; i < n; ++i)
00084 {
00085 std::cerr << "| ";
00086 }
00087 std::cerr << "| <<--";
00088
00089 std::cerr << context_name << '\n';
00090 }
00091
00092 if (n == 0)
00093 std::cerr << '\n';
00094 }
00095 }
00096
00098
00099
00100
00102
00103 bool rutz::trace::get_global_trace() throw()
00104 {
00105 return g_do_global_trace;
00106 }
00107
00108 void rutz::trace::set_global_trace(bool on_off) throw()
00109 {
00110 g_do_global_trace = on_off;
00111 }
00112
00113 unsigned int rutz::trace::get_max_level() throw()
00114 {
00115 return g_max_trace_level;
00116 }
00117
00118 void rutz::trace::set_max_level(unsigned int lev) throw()
00119 {
00120 g_max_trace_level = lev;
00121 }
00122
00123 rutz::trace::trace(prof& p, bool use_msg) throw()
00124 :
00125 m_prof(p),
00126 m_start(),
00127 m_should_print_msg(g_do_global_trace || use_msg),
00128 m_should_pop(rutz::backtrace::current().push(&p)),
00129 m_timing_mode(rutz::prof::get_timing_mode())
00130 {
00131 if (this->m_should_print_msg)
00132 print_in(this->m_prof.context_name());
00133
00134
00135
00136
00137 this->m_start = rutz::prof::get_now_time(this->m_timing_mode);
00138 }
00139
00140 rutz::trace::~trace() throw()
00141 {
00142
00143
00144
00145 const rutz::time finish = rutz::prof::get_now_time(this->m_timing_mode);
00146 const rutz::time elapsed = finish - this->m_start;
00147
00148 this->m_prof.add_time(elapsed);
00149
00150
00151
00152
00153 if (this->m_should_pop)
00154 {
00155 rutz::backtrace& c = rutz::backtrace::current();
00156 c.pop();
00157 rutz::prof* parent = c.top();
00158 if (parent != 0)
00159 parent->add_child_time(elapsed);
00160 }
00161
00162 if (this->m_should_print_msg)
00163 print_out(this->m_prof.context_name());
00164 }
00165
00166 static const char __attribute__((used)) vcid_groovx_rutz_trace_cc_utc20050626084020[] = "$Id: trace.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00167 #endif // !GROOVX_RUTZ_TRACE_CC_UTC20050626084020_DEFINED