一个封装OutputDebugString信息的输出调试信息的工具代码(支持自动输出文件,行号,线程ID)

由于开发环境经常不允许直接挂调试器,只能靠打输出调试,因此自己写了个便于输出调试的工具cpp分享出来

使用时直接导入MyDbgFunction.hpp,使用方法如下:
#include <windows.h>
#include <tchar.h>
#include "MyDbgFunction.hpp"

//支持项目的宽窄字符
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	const char* szMsg = "巨星大鸭子";
	int iNum = 12306;
	DbgPrint("%s hello world %d", szMsg, iNum);				//格式化输出OutputDebugString
	MyDbgMessageBox("%s hello world %d", szMsg, iNum);		//格式化输出MessageBox
	
	char* szMsg = new char[100];
	strcpy_s(szMsg, 100,  "hello world !!!");
	BinPrint(szMsg, 100);									//格式化输出二进制数据

	system("pause");
	return 0 ;
}
输出效果如下:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

MyDbgFunction.hpp
#ifndef __DBGFUNCTION__
#define __DBGFUNCTION__

#include <windows.h>
#include <stdio.h>

#ifdef _UNICODE
#define DbgPrint DbgPrintW
#define MyOutputDebugMsg MyOutputDebugMsgW
#define MyDbgMessageBox MyDbgMessageBoxW
#define __TFILE__ __FILEW__
#define __TFUNCTION__ __FUNCTIONW__
#else
#define MyOutputDebugMsg MyOutputDebugMsgA
#define DbgPrint DbgPrintA
#define MyDbgMessageBox MyDbgMessageBoxA
#define __TFILE__ __FILE__
#define __TFUNCTION__ __FUNCTION__
#endif

#define DbgPrintA(MSG, ...) {\
size_t __nFormat = _scprintf("[%s:%d T:%d] %s", __FILE__, __LINE__, GetCurrentThreadId(), MSG) + 1; \
char* __szFormat = new char[__nFormat]; \
sprintf_s(__szFormat, __nFormat, "[%s:%d T:%d] %s", __FILE__, __LINE__, GetCurrentThreadId(), MSG); \
MyOutputDebugMsgA(__szFormat, __VA_ARGS__); \
delete[] __szFormat;}

#define DbgPrintW(MSG, ...) {\
size_t __nFormat = _scwprintf(L"[%s:%d T:%d] %s", __FILEW__, __LINE__, GetCurrentThreadId(), MSG) + 1; \
wchar_t* __szFormat = new wchar_t[__nFormat]; \
swprintf_s(__szFormat, __nFormat, L"[%s:%d T:%d] %s", __FILEW__, __LINE__, GetCurrentThreadId(), MSG); \
MyOutputDebugMsgW(__szFormat, __VA_ARGS__); \
delete[] __szFormat;}

static void MyOutputDebugMsgW(const wchar_t* szOutputFormat, ...)
{
	va_list vlArgs = NULL;
	va_start(vlArgs, szOutputFormat);
	size_t nLen = _vscwprintf(szOutputFormat, vlArgs) + 1;
	wchar_t* szBuffer = new wchar_t[nLen];
	if (NULL != szBuffer)
	{
		_vsnwprintf_s(szBuffer, nLen, nLen - 1, szOutputFormat, vlArgs);
		OutputDebugStringW(szBuffer);
		delete[] szBuffer;
	}
	va_end(vlArgs);
}

static void MyOutputDebugMsgA(const char* szOutputFormat, ...)
{
	va_list vlArgs = NULL;
	va_start(vlArgs, szOutputFormat);
	size_t nLen = _vscprintf(szOutputFormat, vlArgs) + 1;
	char* szBuffer = new char[nLen];
	if (NULL != szBuffer)
	{
		_vsnprintf_s(szBuffer, nLen, nLen - 1, szOutputFormat, vlArgs);
		OutputDebugStringA(szBuffer);
		delete[] szBuffer;
	}
	va_end(vlArgs);
}

static void MyDbgMessageBoxW(const wchar_t* szOutputFormat, ...)
{
	va_list vlArgs = NULL;
	va_start(vlArgs, szOutputFormat);
	size_t nLen = _vscwprintf(szOutputFormat, vlArgs) + 1;
	wchar_t* szBuffer = new wchar_t[nLen];
	if (NULL != szBuffer)
	{
		_vsnwprintf_s(szBuffer, nLen, nLen - 1, szOutputFormat, vlArgs);
		MessageBoxW(NULL, szBuffer, L"Debug", MB_OK);
		delete[] szBuffer;
	}
	va_end(vlArgs);
}

static void MyDbgMessageBoxA(const char* szOutputFormat, ...)
{
	va_list vlArgs = NULL;
	va_start(vlArgs, szOutputFormat);
	size_t nLen = _vscprintf(szOutputFormat, vlArgs) + 1;
	char* szBuffer = new char[nLen];
	if (NULL != szBuffer)
	{
		_vsnprintf_s(szBuffer, nLen, nLen - 1, szOutputFormat, vlArgs);
		MessageBoxA(NULL, szBuffer, "Debug", MB_OK);
		delete[] szBuffer;
	}
	va_end(vlArgs);
}
static void BinPrint(const void* bBinaryData, unsigned int iSize)
{
	unsigned int LINEBYTEMAX = 16;
	const byte* bCurrentBinary = (const byte*)bBinaryData;
	
	MyOutputDebugMsg(TEXT("[==================================Binary aata on 0x%016x total %u Bytes==================================]\n"), bBinaryData, iSize);

	do
	{
		unsigned int iThisLineOutNum = LINEBYTEMAX > iSize ? iSize : LINEBYTEMAX;
		MyOutputDebugMsg(TEXT("0x%016x  "), bCurrentBinary);

		for (unsigned int index = 0; index < LINEBYTEMAX; ++index)
		{
			if (index < iThisLineOutNum)
			{
				MyOutputDebugMsg(TEXT("%02x  "), *(bCurrentBinary + index));
			}
			else
			{
				OutputDebugString(TEXT("    "));
			}
			
		}

		OutputDebugString(TEXT("   "));

		for (unsigned int index = 0; index < iThisLineOutNum; ++index)
		{
			bool bIsprint = *(bCurrentBinary + index) >= 0x20 && *(bCurrentBinary + index) <= 0x7f ? true : false;
			MyOutputDebugMsg(TEXT("%c "), bIsprint ? *(bCurrentBinary + index) : TEXT('.'));
		}

		OutputDebugString(TEXT("\n"));
		bCurrentBinary += iThisLineOutNum;
	} while (iSize > LINEBYTEMAX ? iSize -= LINEBYTEMAX : 0);
}

#endif // !__DBGFUNCTION__

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章