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 
71 // We only need these includes in one of these paths, but we want
72 // include rules to be the same in release and debug to ensure builds
73 // don't break by accident.
74 #include "UT_WorkBuffer.h"
75 #include <sstream>
76 #include <stdio.h>
77 
78 #if UT_ASSERT_LEVEL > 0
79 
80  #define UTdebugFormat(...) do { \
81  UT_WorkBuffer dbostr; \
82  dbostr.format(__VA_ARGS__); \
83  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
84  } while (false)
85 
86  #define UTdebugFormatCd(color, ...) do { \
87  UT_WorkBuffer dbostr, cdstr; \
88  dbostr.format(__VA_ARGS__); \
89  if (strcmp(#color, "none") == 0) \
90  cdstr = dbostr; \
91  else \
92  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
93  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
94  } while (false)
95 
96  #define UTdebugPrint(...) do { \
97  UT_WorkBuffer dbostr; \
98  dbostr.print(__VA_ARGS__); \
99  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
100  } while (false)
101 
102  #define UTdebugPrintCd(color, ...) do { \
103  UT_WorkBuffer dbostr, cdstr; \
104  dbostr.print(__VA_ARGS__); \
105  if (strcmp(#color, "none") == 0) \
106  cdstr = dbostr; \
107  else \
108  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
109  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
110  } while (false)
111 
112  #define UT_DBGOUT(ZZ) do { \
113  UT_WorkBuffer dbostr; \
114  dbostr.sprintf ZZ ; \
115  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
116  } while (false)
117 
118  #define UT_DBGOS(ZZ) do { \
119  std::stringstream os; \
120  os << ZZ ; \
121  UTdbgout(__FILE__,__LINE__,os.str().c_str()); \
122  } while (false)
123 
124  #define UT_DBGPRINTF(ZZ) printf ZZ
125 
126  #define UT_DBG_CHECKMEMORY UTdebugCheckMemory()
127  #define UT_DBG_CHECKPOINTER(PTR) UTdebugCheckPointer(PTR)
128 #else
129  #define UTdebugFormat(...) ((void)0)
130  #define UTdebugFormatCd(...) ((void)0)
131  #define UTdebugPrint(...) ((void)0)
132  #define UTdebugPrintCd(...) ((void)0)
133  #define UT_DBGOUT(ZZ) ((void)0)
134  #define UT_DBGOS(ZZ) ((void)0)
135  #define UT_DBGPRINTF(ZZ) ((void)0)
136  #define UT_DBG_CHECKMEMORY ((void)0)
137  #define UT_DBG_CHECKPOINTER(PTR) ((void)0)
138 #endif
139 
140 // UTgetSESIlogFlag will use UT_ASSERT_LEVEL >= 1 to check if it should be
141 // enabled. If we don't have sufficient assertion level, then the appropriate
142 // environment variable passed in will be used instead.
143 // Note that it uses a UT_Symbol table to keep track of the different vars.
144 // So it shouldn't be terribly inefficient to call multiple times.
145 
146 UT_API int UTgetSESIlogFlag(int env_control);
147 
148 
149 // UTisSESImachine will efficiently check the IP address of this computer to
150 // see if it is one of SESI's IP addresses and returns 1 if so.
151 // WARNING: This can register false positive on customer machines depending on
152 // the network configuration.
153 UT_API int UTisSESImachine();
154 
156 
157 UT_API bool UTdebugCheckPointer(const void *ptr);
158 
159 #endif /* __UT_Debug__ */
OIIO_API string_view getenv(string_view name)
UT_API bool UTdebugCheckPointer(const void *ptr)
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:14
GLuint const GLchar * name
Definition: glcorearb.h:785
UT_API int UTdebugCheckMemory()
const void * ptr(const T *p)
Definition: format.h:3603
UT_API int UTgetSESIlogFlag(int env_control)
int on() const
Definition: UT_Debug.h:54
UT_API int UTisSESImachine()