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 /*
7 // This is a convenience class for adding debugging code to your
8 // source. It allows the use of run-time switches via environment
9 // variables and can be compiled out in release builds.
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 #if UT_ASSERT_LEVEL > 0
23 
24 // When CRT_DEBUG is enabled on Windows debug builds, this attempts to remap
25 // allocation calls for memory leak tracking.
26 #if defined(_WIN32) && defined(_DEBUG) && defined(CRT_DEBUG)
27  #define _CRTDBG_MAP_ALLOC
28  #include <crtdbg.h>
29 #endif
30 
31 #include <stdlib.h>
32 #include <string.h>
33 
34 class UT_API UT_Debug
35 {
36 public:
37  explicit UT_Debug(const char *name)
38  { myState = (getenv(name) != 0); }
39  bool on() const { return myState; }
40  bool off() const { return !myState; }
41  void setDebugFile(const char *) const;
42  void output(const char *fmt, ...) const;
43 private:
44  bool myState;
45 };
46 
47 #else
48 
50 {
51 public:
52  explicit UT_Debug(const char *) {}
53  bool on() const { return false; }
54  bool off() const { return true; }
55  void setDebugFile(const char *) const;
56  void output(const char *, ...) const;
57 };
58 
59 #endif
60 
61 #ifdef UT_DEBUG
62 
63 #define UT_IF(s) s
64 #define UT_IFNOT(s)
65 #define UT_IFELSE(t, f) t
66 
67 #else
68 
69 #define UT_IF(s)
70 #define UT_IFNOT(s) s
71 #define UT_IFELSE(t, f) f
72 
73 #endif
74 
75 
76 // Used by UT_DBGOUT(())
77 UT_API void UTdbgout(const char *file, int lineno, const char *text);
78 
79 UT_API const char *UTconsoleColorString(const char *colorname);
80 
81 // this macro must be used with a double parentheses!
82 // eg. UT_DBGOUT(( "value = %d", myValue ));
83 // The UTdebugFormat does not require double parentheses.
84 // Uses the same formatting codes as UTformat.
85 
86 // We only need these includes in one of these paths, but we want
87 // include rules to be the same in release and debug to ensure builds
88 // don't break by accident.
89 #include "UT_WorkBuffer.h"
90 #include <sstream>
91 #include <stdio.h>
92 
93 #if UT_ASSERT_LEVEL > 0
94 
95  #define UTdebugFormat(...) do { \
96  UT_WorkBuffer dbostr; \
97  dbostr.format(__VA_ARGS__); \
98  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
99  } while (false)
100 
101  #define UTdebugFormatCd(color, ...) do { \
102  UT_WorkBuffer dbostr, cdstr; \
103  dbostr.format(__VA_ARGS__); \
104  if (strcmp(#color, "none") == 0) \
105  cdstr = dbostr; \
106  else \
107  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
108  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
109  } while (false)
110 
111  #define UTdebugPrint(...) do { \
112  UT_WorkBuffer dbostr; \
113  dbostr.format(nullptr, __VA_ARGS__); \
114  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
115  } while (false)
116 
117  #define UTdebugPrintCd(color, ...) do { \
118  UT_WorkBuffer dbostr, cdstr; \
119  dbostr.format(nullptr, __VA_ARGS__); \
120  if (strcmp(#color, "none") == 0) \
121  cdstr = dbostr; \
122  else \
123  cdstr.sprintf("%s%s%s", UTconsoleColorString(#color), dbostr.buffer(), UTconsoleColorString("none")); \
124  UTdbgout(__FILE__, __LINE__, cdstr.buffer()); \
125  } while (false)
126 
127  #define UT_DBGOUT(ZZ) do { \
128  UT_WorkBuffer dbostr; \
129  dbostr.sprintf ZZ ; \
130  UTdbgout(__FILE__, __LINE__, dbostr.buffer()); \
131  } while (false)
132 
133  #define UT_DBGOS(ZZ) do { \
134  std::stringstream os; \
135  os << ZZ ; \
136  UTdbgout(__FILE__,__LINE__,os.str().c_str()); \
137  } while (false)
138 
139  #define UT_DBGPRINTF(ZZ) printf ZZ
140 
141  #define UT_DBG_CHECKMEMORY UTdebugCheckMemory()
142  #define UT_DBG_CHECKPOINTER(PTR) UTdebugCheckPointer(PTR)
143 #else
144  #define UTdebugFormat(...) ((void)0)
145  #define UTdebugFormatCd(...) ((void)0)
146  #define UTdebugPrint(...) ((void)0)
147  #define UTdebugPrintCd(...) ((void)0)
148  #define UT_DBGOUT(ZZ) ((void)0)
149  #define UT_DBGOS(ZZ) ((void)0)
150  #define UT_DBGPRINTF(ZZ) ((void)0)
151  #define UT_DBG_CHECKMEMORY ((void)0)
152  #define UT_DBG_CHECKPOINTER(PTR) ((void)0)
153 #endif
154 
155 // UTgetSESIlogFlag will use UT_ASSERT_LEVEL >= 1 to check if it should be
156 // enabled. If we don't have sufficient assertion level, then the appropriate
157 // environment variable passed in will be used instead.
158 // Note that it uses a UT_Symbol table to keep track of the different vars.
159 // So it shouldn't be terribly inefficient to call multiple times.
160 
161 UT_API int UTgetSESIlogFlag(int env_control);
162 
163 
164 // UTisSESImachine will efficiently check the IP address of this computer to
165 // see if it is one of SESI's IP addresses and returns 1 if so.
166 // WARNING: This can register false positive on customer machines depending on
167 // the network configuration.
168 UT_API int UTisSESImachine();
169 
171 
172 UT_API bool UTdebugCheckPointer(const void *ptr);
173 
174 #endif /* __UT_Debug__ */
UT_API bool UTdebugCheckPointer(const void *ptr)
UT_Debug(const char *)
Definition: UT_Debug.h:52
UT_API const char * UTconsoleColorString(const char *colorname)
UT_API void UTdbgout(const char *file, int lineno, const char *text)
bool off() const
Definition: UT_Debug.h:54
#define UT_API
Definition: UT_API.h:14
bool on() const
Definition: UT_Debug.h:53
GLuint const GLchar * name
Definition: glcorearb.h:786
UT_API int UTdebugCheckMemory()
OIIO_API string_view getenv(string_view name, string_view defaultval)
auto ptr(T p) -> const void *
Definition: format.h:2448
UT_API int UTgetSESIlogFlag(int env_control)
UT_API int UTisSESImachine()