1 #ifndef DEBUG_H
2 #define DEBUG_H
3 
4 
5 #include <assert.h>
6 #include <stdio.h>
7 #include <string>
8 
9 
10 namespace audiere {
11 
12   class Log {
13   public:
14     static void Write(const char* str);
Write(const std::string & str)15     static void Write(const std::string& str) { Write(str.c_str()); }
IncrementIndent()16     static void IncrementIndent() { ++indent_count; }
DecrementIndent()17     static void DecrementIndent() { --indent_count; }
18 
19   private:
20     static void EnsureOpen();
21     static void Close();
22 
23   private:
24     static FILE* handle;
25     static int indent_count;
26   };
27 
28 
29   class Guard {
30   public:
Guard(const char * label)31     Guard(const char* label)
32     : m_label(label) {
33       Write("+");
34       Log::IncrementIndent();
35     }
36 
~Guard()37     ~Guard() {
38       Log::DecrementIndent();
39       Write("-");
40     }
41 
Write(const char * prefix)42     void Write(const char* prefix) {
43       Log::Write((prefix + m_label).c_str());
44     }
45 
46   private:
47     std::string m_label;
48   };
49 
50 }
51 
52 
53 //#define ADR_FORCE_DEBUG
54 
55 
56 #if defined(ADR_FORCE_DEBUG) || defined(_DEBUG) || defined(DEBUG)
57 
58   #define ADR_GUARD(label) Guard guard_obj__(label)
59   #define ADR_LOG(label)   (Log::Write(label))
60   #define ADR_IF_DEBUG     if (true)
61 
62   #ifdef _MSC_VER
63     #define ADR_ASSERT(condition, label) if (!(condition)) { __asm int 3 }
64   #else  // assume x86 gcc
65     #define ADR_ASSERT(condition, label) assert(condition && label);
66   #endif
67 
68 #else
69 
70   #define ADR_GUARD(label)
71   #define ADR_LOG(label)
72   #define ADR_IF_DEBUG     if (false)
73   #define ADR_ASSERT(condition, label)
74 
75 #endif
76 
77 
78 #endif
79