HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Debug.h
Go to the documentation of this file.
1 #ifndef __UT_Debug__
2 #define __UT_Debug__
3 
4 #include "UT_API.h"
5 /*
6 // This is a convenience class for adding debugging code to your
7 // source. It allows the use of run-time switches via environment
8 // variables and can be compiled out by not defining the pre-processor
9 // symbol UT_DEBUG.
10 //
11 // Typical use of this class involves declaring a static variable of
12 // type UT_Debug with the name of an environment variable switch that
13 // will enable this object. Then use it as follows:
14 //
15 // static UT_Debug debug("UT_DEBUG_THIS");
16 // ...
17 // if( debug.on() )
18 // debug.output("This is %x\n", this);
19 */
20 
21 // It's UGLY but...
22 
23 #ifdef UT_DEBUG
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #define UT_IF(s) s
29 #define UT_IFNOT(s)
30 #define UT_IFELSE(t, f) t
31 
32 class UT_API UT_Debug
33 {
34 public:
35  explicit UT_Debug(const char *name)
36  { myState = (getenv(name) != 0); }
37  bool on() const { return myState; }
38  void setDebugFile(const char *) const;
39  void output(const char *fmt, ...) const;
40 private:
41  bool myState;
42 };
43 
44 #else
45 
46 #define UT_IF(s)
47 #define UT_IFNOT(s) s
48 #define UT_IFELSE(t, f) f
49 
51 {
52 public:
53  explicit UT_Debug(const char *) {}
54  int on() const { return 0; }
55  void setDebugFile(const char *) const;
56  void output(const char *, ...) const;
57 };
58 
59 #endif
60 
61 // Used by UT_DBGOUT(())
62 UT_API void UTdbgout(const char *file, int lineno, const char *text);
63 
64 UT_API const char *UTconsoleColorString(const char *colorname);
65 
66 // this macro must be used with a double parentheses!
67 // eg. UT_DBGOUT(( "value = %d", myValue ));
68 // The UTdebugFormat does not require double parentheses.
69 // Uses the same formatting codes as UTformat.
70 #if UT_ASSERT_LEVEL > 0
71  #include "UT_WorkBuffer.h"
72  #include <sstream>
73  #include <stdio.h>
74 
75  #define UTdebugFormat(...) do { \
76  UT_WorkBuffer dbostr; \
77  dbostr.format(__VA_ARGS__); \
78  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
79  } while (false)
80 
81  #define UTdebugFormatCd(color, ...) do { \
82  UT_WorkBuffer dbostr, cdstr; \
83  dbostr.format(__VA_ARGS__); \
84  if (strcmp(#color, "none") == 0) \
85  cdstr = dbostr; \
86  else \
87  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
88  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
89  } while (false)
90 
91  #define UTdebugPrint(...) do { \
92  UT_WorkBuffer dbostr; \
93  dbostr.print(__VA_ARGS__); \
94  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
95  } while (false)
96 
97  #define UTdebugPrintCd(color, ...) do { \
98  UT_WorkBuffer dbostr, cdstr; \
99  dbostr.print(__VA_ARGS__); \
100  if (strcmp(#color, "none") == 0) \
101  cdstr = dbostr; \
102  else \
103  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
104  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
105  } while (false)
106 
107  #define UT_DBGOUT(ZZ) do { \
108  UT_WorkBuffer dbostr; \
109  dbostr.sprintf ZZ ; \
110  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
111  } while (false)
112 
113  #define UT_DBGOS(ZZ) do { \
114  std::stringstream os; \
115  os << ZZ ; \
116  UTdbgout(__FILE__,__LINE__,os.str().c_str()); \
117  } while (false)
118 
119  #define UT_DBGPRINTF(ZZ) printf ZZ
120 
121  #define UT_DBG_CHECKMEMORY UTdebugCheckMemory()
122  #define UT_DBG_CHECKPOINTER(PTR) UTdebugCheckPointer(PTR)
123 #else
124  #define UTdebugFormat(...) ((void)0)
125  #define UTdebugFormatCd(...) ((void)0)
126  #define UTdebugPrint(...) ((void)0)
127  #define UTdebugPrintCd(...) ((void)0)
128  #define UT_DBGOUT(ZZ) ((void)0)
129  #define UT_DBGOS(ZZ) ((void)0)
130  #define UT_DBGPRINTF(ZZ) ((void)0)
131  #define UT_DBG_CHECKMEMORY ((void)0)
132  #define UT_DBG_CHECKPOINTER(PTR) ((void)0)
133 #endif
134 
135 // UTgetSESIlogFlag will use UT_ASSERT_LEVEL >= 1 to check if it should be
136 // enabled. If we don't have sufficient assertion level, then the appropriate
137 // environment variable passed in will be used instead.
138 // Note that it uses a UT_Symbol table to keep track of the different vars.
139 // So it shouldn't be terribly inefficient to call multiple times.
140 
141 UT_API int UTgetSESIlogFlag(int env_control);
142 
143 
144 // UTisSESImachine will efficiently check the IP address of this computer to
145 // see if it is one of SESI's IP addresses and returns 1 if so.
146 // WARNING: This can register false positive on customer machines depending on
147 // the network configuration.
148 UT_API int UTisSESImachine();
149 
151 
152 UT_API bool UTdebugCheckPointer(const void *ptr);
153 
154 #endif /* __UT_Debug__ */
OIIO_API string_view getenv(string_view name)
UT_API bool UTdebugCheckPointer(const void *ptr)
GLuint const GLchar * name
Definition: glew.h:1814
UT_Debug(const char *)
Definition: UT_Debug.h:53
UT_API const char * UTconsoleColorString(const char *colorname)
UT_API void UTdbgout(const char *file, int lineno, const char *text)
#define UT_API
Definition: UT_API.h:13
UT_API int UTdebugCheckMemory()
const void * ptr(const T *p)
Definition: format.h:3292
UT_API int UTgetSESIlogFlag(int env_control)
int on() const
Definition: UT_Debug.h:54
UT_API int UTisSESImachine()