uLib  User mode C/C++ extended API library for Win32 programmers.
Debug.h
Go to the documentation of this file.
1 //==============================================================================
2 // Project: uLib - User mode utility library.
3 //==============================================================================
9 //==============================================================================
10 
11 #ifndef _Debug_h_incl_
12 #define _Debug_h_incl_
13 
14 #include <uLib/Common.h>
15 
16 #ifdef __GNUC__
17 // NOTE: If you're debugging in CodeBlocks, you will not see output from DPrint
18 // in the IDE, since it doesn't capture OutputDebugString strings, so run an
19 // instance of e.g Mark Russinovitch's DbgView on the side to see your trace.
20 #endif
21 
22 //==============================================================================
36 //==============================================================================
38 
40 
41 //==---------------------------------------------------------------------------
46 //==---------------------------------------------------------------------------
47 
48 #ifdef _MSC_VER // MSVC allows concatenation of the __FUNCTION__ value.
49  #define _F(s) _T("[") _T(__FUNCTION__) _T("] ") _T(s)
50 #else //__GNUC__ does not.
51  #if 911 // NOTE: This one only works in var-arg param list..
52  // Emit __func__ as the first parameter in the arg list, since GCC defines
53  // __func__ as an implicit variable, not a string literal as MSVC does.
54  #define _F(s) _T("[%s] ") _T(s), __func__
55  #else
56  #define _F(s) _T(s) /* If it doesn't work, use this degenerate def */
57  // TODO: Find a GCC definition of _F that works like the MSVC version.
58  #endif
59 #endif
60 
61 //==---------------------------------------------------------------------------
71 //==---------------------------------------------------------------------------
72 
73 void __cdecl DPrint( int Level, CSTR Fmt, ... );
74 
78 #define DP_DISABLE -1
79 #define DP_ALWAYS 0
80 #define DP_FATAL 1
81 #define DP_SEVERE 2
82 #define DP_ERROR 3
83 #define DP_WARNING 4
84 #define DP_INFO 5
85 #define DP_DEBUG 6
86 #define DP_VERBOSE 7
87 #define DP_EXTREME 8
88 
90 //==---------------------------------------------------------------------------
98 //==---------------------------------------------------------------------------
99 
100 CSTR SysErrorMsg( DWORD Err DEF_(0), TSTR Buf DEF_(NULL), UINT Length DEF_(0) );
101 
102 //==---------------------------------------------------------------------------
106 //==---------------------------------------------------------------------------
107 
108 CSTR NtErrorMsg( NTSTATUS Status DEF_(0), TSTR Buf DEF_(NULL), UINT Size DEF_(0) );
109 
110 //==---------------------------------------------------------------------------
114 //==---------------------------------------------------------------------------
115 
116 bool SetLastErrorFromNtStatus( NTSTATUS Status );
117 
118 //==---------------------------------------------------------------------------
121 //==---------------------------------------------------------------------------
122 
123 ACSTR GetWinMsgName( WORD msgId ); // MsgTbl.cpp
124 
125 //==---------------------------------------------------------------------------
128 //==---------------------------------------------------------------------------
129 
130 ACSTR GetExceptionName( DWORD xCode ); // MsgTbl.cpp
131 
132 //==---------------------------------------------------------------------------
135 //==---------------------------------------------------------------------------
136 
137 int SetDebugLevel( int Level );
138 
139 //==---------------------------------------------------------------------------
143 //==---------------------------------------------------------------------------
144 
145 HANDLE OpenLogFile( CSTR Name, bool Append );
146 
147 //==---------------------------------------------------------------------------
151 //==---------------------------------------------------------------------------
152 
153 CSTR DefaultLogFileName( HMODULE hModule );
154 
155 //==---------------------------------------------------------------------------
156 extern CSTR dbg_Name;
157 extern int dbg_Level;
158 extern HANDLE dbg_LogFile;
159 extern bool dbg_Flush;
160 
161 #ifdef _DEBUG
162 extern volatile ULONG nDPrintSkipped; // Nr of skipped (due to re-entry) DPrint calls.
163 #endif
165 //==---------------------------------------------------------------------------
166 
168 
169 #define DP_StdFailMsg( What ) \
170  DPrint( DP_ERROR, What _T(" failed: %s\n"), SysErrorMsg() )
171 
172 #ifdef _DEBUG //----------------------------------------------------------------
173 
177  // Sadly, MSVC does *not* allow inline assembly for _WIN64 :/
178  // That's a *huge step backward* for advanced/high perfomance programming!
179  // (This particular little trinket notwithstanding..)
180  //
181  // I thought we were supposed to go forward and evolve, not de-evolve.
182  // What's next ?
183  // - "We think you don't need a steering wheel in your car,
184  // since we believe you don't know where you're going" ??
185 
187  #ifdef _WIN64
188  #ifdef _MSC_VER // Later on, an intrinsic came along..
189  #define _BREAK __debugbreak() /* asm int 3 */
190  #else
191  #define _BREAK DebugBreak()
192  #endif
193  #else // Break *here*, not in DebugBreak()
194  #ifdef __GNUC__
195  // FIXME: Replace this hack with a proper GCC inline asm statement.
196  #define _BREAK DebugBreak()
197  #else
198  #define _BREAK __asm Int 3 /* The original, circa 1996? */
199  #endif
200  #endif
201 
202  #define BREAK_IF( cond ) if (cond) _BREAK
203 
207 
208  #define BREAK() \
209  if (IsDebuggerPresent()) _BREAK; \
210  else MessageBox( NULL, \
211  _T("Debugging Breakpoint:\nNo debugger present.. You may want to attach one now."), \
212  dbg_Name, MB_OK| MB_ICONSTOP )
213 
214  #define DBGBREAK( Msg ) OutputDebugString( Msg ); BREAK()
215 
216  #define TRACE(_lvl,...) DPrint(_lvl,__VA_ARGS__)
217  #define _TRACE(_va_) DPrint##_va_
218 
219  //( TRACEX produces a trace that's fairly compatible w std ReactOS traces. )
223 
224  #define TRACEX(_lvl,_fmt,...) DPrint(_lvl,_F(_fmt" (%s:%u)\n"),__VA_ARGS__,__FILE__,__LINE__)
225 
227  #define TRACE_IF(cond,...) if (cond) DPrint( __VA_ARGS__ )
228  #define TRACEX_IF(_cond,_lvl,_fmt,...) if (_cond) DPrint(_lvl,_fmt" (%s:%u)\n",__VA_ARGS__,__FILE__,__LINE__)
230 
232  //#define ERROR_TRACE(What) DPrint( DP_ERROR, _T("%s failed: %s\n"), What, SysErrorMsg() )
233  #define ERROR_TRACE(What) DP_StdFailMsg( What )
234 
236  #define IF_DEBUG( code ) code
237 
239 #else // NDEBUG //--------------------------------------------------------------
240 
241  #define _BREAK
242  #define BREAK_IF(cond)
243  #define BREAK()
244  #define DBGBREAK(msg)
245  #define _TRACE(_va_)
246  #define TRACE(...)
247  #define TRACEX(...)
248  #define TRACE_IF(cond,...)
249  #define TRACEX_IF(...)
250  #define ERROR_TRACE(what)
251  #ifdef NO_DPRINT
252  #define DPrint(...)
253  #endif
254  #define IF_DEBUG( code )
255 
256 #endif //-----------------------------------------------------------------------
257 
260 #endif //ndef _Debug_h_incl_
261 // EOF
unsigned long DWORD
Definition: Common.h:414
const char * ACSTR
Definition: Common.h:345
#define CSTR
Definition: Common.h:329
unsigned short WORD
Definition: Common.h:413
#define TSTR
Definition: Common.h:328
void __cdecl DPrint(int Level, CSTR Fmt,...)
Definition: Debug.cpp:134
HANDLE OpenLogFile(CSTR Name, bool Append)
Definition: Debug.cpp:220
int dbg_Level
Definition: Debug.cpp:105
CSTR dbg_Name
Definition: Debug.cpp:114
bool SetLastErrorFromNtStatus(NTSTATUS Status)
Definition: Debug.cpp:74
int SetDebugLevel(int Level)
Definition: Debug.cpp:107
CSTR SysErrorMsg(DWORD Err=0, TSTR Buf=NULL, UINT Length=0)
Definition: Debug.cpp:39
CSTR NtErrorMsg(NTSTATUS Status=0, TSTR Buf=NULL, UINT Size=0)
Definition: Debug.cpp:67
#define DEF_(x)
Definition: Common.h:240
Common include; Added types, small "ubiquitous" utilities, et c.
#define END_EXTERN_C
Definition: Common.h:221
CSTR DefaultLogFileName(HMODULE hModule)
Definition: Debug.cpp:239
#define BEGIN_EXTERN_C
Definition: Common.h:220
ACSTR GetWinMsgName(WORD msgId)
Definition: MsgTbl.cpp:37
bool dbg_Flush
Definition: Debug.cpp:116
HANDLE dbg_LogFile
Definition: Debug.cpp:115
ACSTR GetExceptionName(DWORD xCode)
Definition: MsgTbl.cpp:641