Sierra Toolkit  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stk_util/stk_util/diag/Trace.hpp
1 #ifndef STK_UTIL_SIERRA_TRACE_HPP
2 #define STK_UTIL_SIERRA_TRACE_HPP
3 
4 #include <cstring>
5 #include <vector>
6 #include <map>
7 #include <string>
8 #include <functional>
9 
10 #include <stk_util/diag/Writer_fwd.hpp>
11 
12 #include <ostream>
13 
14 #define SLIB_TRACE_COVERAGE
15 
16 namespace stk_classic {
17 namespace diag {
18 
23 
32 class Tracespec
33 {
34 public:
41  explicit Tracespec(const char *function_spec)
42  : m_functionSpec(function_spec)
43  {}
44 
50  inline const char *getFunctionSpec() const {
51  return m_functionSpec;
52  }
53 
59  std::string getFunctionName() const;
60 
66  std::string getFunctionShortName() const;
67 
73  std::string getFunctionClass() const;
74 
80  std::string getFunctionNamespace() const;
81 
87  std::string getFunctionShortClass() const;
88 
89 protected:
90  const char * m_functionSpec;
91 };
92 
93 typedef std::vector<const char *> TracebackStack;
94 
102 class Traceback : public Tracespec
103 {
104 public:
114  enum TracebackState{RUNNING, THROWING};
115 
116  enum {STACK_SIZE = 2048};
117  typedef const char *Stack[STACK_SIZE] ;
118  typedef const char * const * const_stack_iterator;
119  typedef const char ** stack_iterator;
120 
129  typedef std::map<const char *, int> Coverage;
130 
136  class Preserve
137  {
138  public:
146  }
147 
154  }
155  };
156 
163  explicit Traceback(const char *function_spec)
164  : Tracespec(function_spec)
165  {
166  if (s_top >= &s_stack[STACK_SIZE - 1] || s_top == 0)
167  s_top = s_stack;
168  *s_top++ = function_spec;
169 
170  if (s_tracebackState == THROWING && !s_tracebackPreserve && !std::uncaught_exception())
171  s_tracebackState = RUNNING;
172 
173 #ifdef SLIB_TRACE_COVERAGE
174  if (s_coverageEnabled)
175  ++s_coverage[function_spec];
176 #endif
177  }
178 
179 
186  if (!s_tracebackPreserve && std::uncaught_exception() && s_tracebackState == RUNNING) {
187  s_tracebackState = THROWING;
188  s_storedTop = s_storedStack + (s_top - s_stack);
189  std::copy(s_stack, s_top, s_storedStack);
190  }
191  if (s_top > &s_stack[0])
192  --s_top;
193  }
194 
195  static TracebackStack snapshot();
196 
202  inline static void enableTracebackDisplay() {
203  --s_tracebackDisplay;
204  }
205 
211  inline static void disableTracebackDisplay() {
212  ++s_tracebackDisplay;
213  }
214 
222  inline static bool displayTraceback() {
223  return s_tracebackDisplay == 0;
224  }
225 
231  inline static void preserveStack() {
232  ++s_tracebackPreserve;
233  }
234 
240  inline static void releaseStack() {
241  --s_tracebackPreserve;
242  }
243 
253  inline static void enableCoverage(bool coverage_enabled = true) {
254  s_coverageEnabled = coverage_enabled;
255  }
256 
264  inline static bool coverageEnabled() {
265  return s_coverageEnabled;
266  }
267 
275  return s_tracebackState;
276  }
277 
283  {};
284 
293  return PrintCoverage();
294  }
295 
302  static std::ostream &printCoverage(std::ostream &os);
303 
313  static std::string printTraceback(const TracebackStack &traceback_stack);
314 
325  Writer &verbose_print(Writer &dout) const;
326 
327 private:
328  static TracebackState s_tracebackState;
329  static int s_tracebackPreserve;
330  static int s_tracebackDisplay;
331  static const char ** s_top;
332  static Stack s_stack;
333  static const char ** s_storedTop;
334  static Stack s_storedStack;
335  static bool s_coverageEnabled;
336  static Coverage s_coverage;
337 };
338 
339 
351 class Trace : public Traceback
352 {
353 public:
359  typedef Writer & (*ExtraFuncPtr)(Writer &);
360 
365  struct TraceList : public std::vector<const char *> {
366  public:
367  TraceList() {
368  s_traceListExists = true;
369  }
370 
371  ~TraceList() {
372  s_traceListExists = false;
373  }
374  };
375 
380  enum {
382  };
383 
401  Trace(Writer &dout, const char *function_name, int print_mask = LOG_TRACE, bool do_trace = true);
402 
408  ~Trace();
409 
419  inline static ExtraFuncPtr setExtra(ExtraFuncPtr extra) {
420  ExtraFuncPtr x = s_extra;
421  s_extra = extra;
422  return x;
423  }
424 
432  inline static void addTraceFunction(const std::string &function_prefix) {
433  char *s = std::strcpy(new char[function_prefix.length() + 1], function_prefix.c_str());
434 
435  s_traceList.push_back(s);
436  }
437 
443  inline static void clearTraceFunctions() {
444  for (std::vector<const char *>::iterator it = s_traceList.begin(); it != s_traceList.end(); ++it)
445  delete[] (*it);
446 
447  s_traceList.clear();
448  }
449 
458  Writer &verbose_print(Writer &dout) const;
459 
460 private:
461  Writer & m_diagWriter;
462  double m_startCpuTime;
463  size_t m_startMemAlloc;
464  PrintMask m_lineMask;
465  bool m_do_trace;
466  int m_flags;
467 
468  static ExtraFuncPtr s_extra;
469  static TraceList s_traceList;
470  static bool s_traceListExists;
471 };
472 
486 inline Writer &operator<<(Writer &dout, const Trace &diag_trace) {
487  return diag_trace.verbose_print(dout);
488 }
489 
503 inline std::ostream &operator<<(std::ostream &os, const Traceback::PrintCoverage &) {
504  return Traceback::printCoverage(os);
505 }
506 
507 
508 } // namespace diag
509 } // namespace stk_classic
510 
511 namespace sierra {
512 namespace Diag {
513 
514 typedef stk_classic::diag::Tracespec Tracespec;
515 typedef stk_classic::diag::Traceback Traceback;
516 typedef stk_classic::diag::Trace Trace;
517 
518 } // namespace sierra
519 } // namespace Diag
520 
521 
525 
526 #endif // STK_UTIL_SIERRA_TRACE_HPP
std::string getFunctionNamespace() const
Member function getFunctionName returns the function's name.
Writer &(* ExtraFuncPtr)(Writer &)
Typedef ExtraFuncPtr declares the extra function pointer signature.
Traceback(const char *function_spec)
Creates a new Trace instance, resulting in the printing of the member function name and pushing the d...
static void addTraceFunction(const std::string &function_prefix)
Member function addTraceFunction adds a function prefix to the list of function prefixes search to en...
static TracebackState getTracebackState()
Member function getTracebackState returns the value of the traceback state.
static void disableTracebackDisplay()
Member function disableTracebackDisplay disables the display of the traceback.
Trace(Writer &dout, const char *function_name, int print_mask=LOG_TRACE, bool do_trace=true)
Creates a new Trace instance, resulting in the printing of the member function name and pushing the d...
static std::string printTraceback(const TracebackStack &traceback_stack)
Member function printTraceback writes the traceback stack function specifications to the output strea...
Class Trace serves as a sentry for entering routines. Creating a trace object prints the specified me...
Class Tracespec dissects file specification strings. It contains a single char const pointer to a fun...
std::string getFunctionName() const
Member function getFunctionName returns the function's name.
static bool coverageEnabled()
Member function coverageEnabled returns true if coverage has been enabled.
static PrintCoverage printCoverage()
Member function printCoverage creates a PrintCoverage type holder class which enables operator<< to p...
static ExtraFuncPtr setExtra(ExtraFuncPtr extra)
Member function setExtra sets the extra function which is called during each trace construction and d...
const char * m_functionSpec
The member function specification.
std::map< const char *, int > Coverage
Typedef Coverage declares the function usage coverage data type.
Class Traceback is a stack of char constant pointers to function specifications which have been encou...
Writer & verbose_print(Writer &dout) const
Member function dump writes the trace to the specified Writer.
static void releaseStack()
Member function releaseStack decrements the traceback stack preservation counter. ...
~Traceback()
Destroys a Traceback instance, resulting in the pushing of the function specification if unwinding th...
Writer & verbose_print(Writer &dout) const
Member function verbose_print dumps the function specification stack to the diagnostic writer...
Class PrintCoverage is a type holder class for printing the stack.
Typedef TraceList declares the trace list data type.
std::string getFunctionClass() const
Member function getFunctionName returns the function's name.
std::string getFunctionShortClass() const
Member function getFunctionName returns the function's name.
const char * Stack[STACK_SIZE]
Stack type.
static void enableCoverage(bool coverage_enabled=true)
Member function enableCoverage enables the collection of function call coverage data. This is a very expensive operation, but allows function execution coverage data to be collected for testing.
Class Writer implements a runtime selectable diagnostic output writer to aid in the development and d...
Definition: Writer.hpp:49
Class Traceback::Preserve serves as a sentry for traceback stack preservation during additional exten...
const char *const * const_stack_iterator
const iterator thru stack
static void enableTracebackDisplay()
Member function enableTracebackDisplay enables the display of the traceback.
static bool displayTraceback()
Member function displayEnabled returns true if display of the tracback stack has been enabled...
static void clearTraceFunctions()
Member function clearTraceFunctions removes all function prefixes from the function signature prefix ...
const char * getFunctionSpec() const
Member function getFunctionSpec returns the function's name.
static void preserveStack()
Member function preserveStack increments the traceback stack preservation counter.
TracebackState
Enumeration TracebackState lists the traceback execution states.
~Preserve()
Destroys a Preserve sentry which allows traceback stack.
std::string getFunctionShortName() const
Member function getFunctionName returns the function's name.
Preserve()
Creates a new Traceback::Preserve sentry. When the sentry is in place, the traceback stack is preserv...
~Trace()
Destroys a Trace instance, resulting in the printing of the member function name and popping the diag...
const char ** stack_iterator
iterator thru stack