Skip to content

Instantly share code, notes, and snippets.

@wyyqyl
Last active August 29, 2015 13:58
Show Gist options
  • Save wyyqyl/9949620 to your computer and use it in GitHub Desktop.
Save wyyqyl/9949620 to your computer and use it in GitHub Desktop.
debug utility encapsulating OutputDebugStringA
#include <windows.h>
#include <stdio.h>
#define UID "[yy]"
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define LINE_STRING STRINGIZE(__LINE__)
#define DEBUG_PREFIX "["__FUNCTION__":"LINE_STRING"] "
void PRINT(const char* fmt, ...);
#ifdef _DEBUG
#define DebugPrint(...) PRINT(UID DEBUG_PREFIX __VA_ARGS__)
#define TracePrint(...) PRINT(UID __VA_ARGS__)
#define DebugPrintAPI(API) \
DebugPrint(STRINGIZE(API)##" failed, err: 0x%08x\n", GetLastError())
#else
#define DebugPrint(...)
#define TracePrint(...)
#define DebugPrintAPI(API)
#endif
class FunctionTracer {
public:
FunctionTracer(char* function) : function_(function) {
TracePrint("[ENTER][%s]\n", function_);
}
~FunctionTracer() { TracePrint("[EXIT][%s]\n", function_); }
private:
char* function_;
};
#define TRACE_FUNCTION FunctionTracer ft(__FUNCTION__);
void PRINT(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int len = _vscprintf(fmt, args);
LPSTR buffer = (LPSTR) new TCHAR[len + 1];
_vsnprintf_s(buffer, len + 1, len, fmt, args);
va_end(args);
OutputDebugStringA(buffer);
delete[] buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment