merge exportvis branch into cairocanvas, to reduce the number of "floating" branches.
[ardour.git] / libs / audiographer / audiographer / debuggable.h
1 #ifndef AUDIOGRAPHER_DEBUGGABLE_H
2 #define AUDIOGRAPHER_DEBUGGABLE_H
3
4 #ifndef DEFAULT_DEBUG_LEVEL
5 #define DEFAULT_DEBUG_LEVEL DebugNone
6 #endif
7
8 #include <iostream>
9
10 #include "audiographer/visibility.h"
11
12 namespace AudioGrapher
13 {
14
15 /// Compile time defined debug level
16 enum LIBAUDIOGRAPHER_API DebugLevel
17 {
18         DebugNone,     ///< Disabled
19         DebugObject,   ///< Object level stuff, ctors, initalizers etc.
20         DebugFlags,    ///< Debug ProcessContext flags only on process cycle level
21         DebugProcess,  ///< Process cycle level stuff
22         DebugVerbose,  ///< Lots of output, not on sample level
23         DebugSample    ///< Sample level stuff
24 };
25
26 /** Class that allows optimizing out debugging code during compile time.
27   * Usage: to take all advantage of this class you should wrap all 
28   * debugging statemets like this:
29   * \code
30   * if (debug_level (SomeDebugLevel) && other_optional_conditionals) {
31   *     debug_stream() << "Debug output" << std::endl;
32   * }
33   * \endcode
34   *
35   * The order of the conditionals in the if-clause is important.
36   * The checks specified in \a other_optional_conditionals are only
37   * optimized out if \a debug_level() is placed before it with a
38   * logical and (short-circuiting).
39   */
40 template<DebugLevel L = DEFAULT_DEBUG_LEVEL>
41 class LIBAUDIOGRAPHER_API Debuggable
42 {
43   protected:
44         Debuggable(std::ostream & debug_stream = std::cerr)
45                 : stream (debug_stream) {}
46
47         bool debug_level (DebugLevel level) {
48 #ifndef NDEBUG
49                 (void) level; /* stop pedantic gcc complaints about unused parameter */
50                 return false;
51 #else
52                 return L >= level;
53 #endif
54         }
55         std::ostream & debug_stream() { return stream; }
56
57   private:
58           std::ostream & stream;
59 };
60
61
62 } // namespace
63
64 #endif // AUDIOGRAPHER_DEBUGGABLE_H