一個封裝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__

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