Skip to content

Instantly share code, notes, and snippets.

@wizche
Last active August 29, 2015 13:57
Show Gist options
  • Save wizche/9783671 to your computer and use it in GitHub Desktop.
Save wizche/9783671 to your computer and use it in GitHub Desktop.
Simple debug Macros with threshold level for Win C++
#include "Debug.h"
#ifdef DEBUG_ENABLED
const char *DEBUG_LEVEL_STRINGS[] = { "ERR", "WARN", "INFO", "TRACE" };
void DebugInternal(DEBUG_LEVEL_NS::DEBUG_LEVEL level, LPCSTR className, LPCSTR text)
{
if (level > DEFAULT_DEBUG_LEVEL)
return;
LPSTR szBuffer = (LPSTR)calloc(sizeof(char), 66666666);
sprintf_s(szBuffer, 66666666, "[%s] %s: %s", DEBUG_LEVEL_STRINGS[level], className, text);
std::string result = std::string(szBuffer) + "\r\n";
OutputDebugStringA(result.c_str());
delete(szBuffer);
}
void DebugInternal(DEBUG_LEVEL_NS::DEBUG_LEVEL level, LPCSTR className, LPCSTR lpszFormat, va_list args)
{
if (level > DEFAULT_DEBUG_LEVEL)
return;
std::string prefix = "[" + std::string(DEBUG_LEVEL_STRINGS[level]) + "] " + std::string(className) + ": ";
LPSTR szBuffer = (LPSTR)calloc(sizeof(char), 66666666);
vsprintf_s(szBuffer, 66666666, lpszFormat, args);
std::string suffix = std::string(szBuffer) + "\r\n";
std::string result = prefix + suffix;
OutputDebugStringA(result.c_str());
delete(szBuffer);
}
void DebugTrace(LPCSTR className, LPCSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
DebugInternal(DEBUG_LEVEL_NS::TRACE, className, lpszFormat, args);
va_end(args);
}
void DebugInfo(LPCSTR className, LPCSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
DebugInternal(DEBUG_LEVEL_NS::INFO, className, lpszFormat, args);
va_end(args);
}
void DebugWarn(LPCSTR className, LPCSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
DebugInternal(DEBUG_LEVEL_NS::WARN, className, lpszFormat, args);
va_end(args);
}
void DebugErr(LPCSTR className, LPCSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
DebugInternal(DEBUG_LEVEL_NS::ERR, className, lpszFormat, args);
va_end(args);
}
void DebugErrApi(LPCSTR className, LPCSTR funcName)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
lpDisplayBuf =
(LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)funcName)
+ 40) // account for format string
* sizeof(TCHAR));
if (FAILED(StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error code %d as follows:\n%s"),
funcName,
dw,
lpMsgBuf)))
{
printf("FATAL ERROR: Unable to output error code.\n");
}
DebugErr(className, "%s", lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
#endif
/**
* Macros for Win C++ debugging
*/
#pragma once
#ifdef DEBUG_ENABLED
#ifndef DEFAULT_DEBUG_LEVEL
#define DEFAULT_DEBUG_LEVEL DEBUG_LEVEL_NS::TRACE
#endif
namespace DEBUG_LEVEL_NS {
enum DEBUG_LEVEL { ERR, WARN, INFO, TRACE };
}
// Define threshold level for debug messages
//#define DEFAULT_DEBUG_LEVEL DEBUG_LEVEL_NS::TRACE
#define DEBUG_TRACE DebugTrace
#define DEBUG_INFO DebugInfo
#define DEBUG_WARN DebugWarn
#define DEBUG_ERR DebugErr
#define DEBUG_ERR_API DebugErrApi
#include <Windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <vector>
#include <tchar.h>
#include <strsafe.h>
void DebugInternal(DEBUG_LEVEL_NS::DEBUG_LEVEL level, LPCSTR className, LPCSTR text);
void DebugInternal(DEBUG_LEVEL_NS::DEBUG_LEVEL level, LPCSTR className, LPCSTR lpszFormat, va_list args);
void DebugTrace(LPCSTR className, LPCSTR lpszFormat, ...);
void DebugInfo(LPCSTR className, LPCSTR lpszFormat, ...);
void DebugWarn(LPCSTR className, LPCSTR lpszFormat, ...);
void DebugErr(LPCSTR className, LPCSTR lpszFormat, ...);
void DebugErrApi(LPCSTR className, LPCSTR funcName);
#else
#define DEBUG_TRACE
#define DEBUG_INFO
#define DEBUG_WARN
#define DEBUG_ERR
#define DEBUG_ERR_API
#endif
#include "Debug.h"
// Enabled debug
#define DEBUG_ENABLED
// Define debug level to TRACE
#define DEFAULT_DEBUG_LEVEL=DEBUG_LEVEL_NS::TRACE
int main(void){
int myInt = 1;
char *myString = "Hello!";
DEBUG_TRACE("Main", "Cool Debug Message %d, %s", myInt, myString);
// Resulting in following message:
// [TRACE] Main: Cool Debug Message 1, Hello!
DEBUG_ERR("FileStreamer", "Unable to write to file");
// Generate [ERR] message
DEBUG_ERR_API("FileStreamer", "WriteFile");
// Generate [ERR] with error code formatted message (FormatMessage)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment