一个封装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
#pragma once
#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\n", __FILE__, __LINE__, GetCurrentThreadId(), MSG) + 1; \
char* __szFormat = new char[__nFormat]; \
sprintf_s(__szFormat, __nFormat, "[%s:%d T:%d] %s\n", __FILE__, __LINE__, GetCurrentThreadId(), MSG); \
MyOutputDebugMsgA(__szFormat, __VA_ARGS__); \
delete[] __szFormat;}

#define DbgPrintW(MSG, ...) {\
size_t __nFormat = _scwprintf(L"[%s:%d T:%d] %s\n", __FILEW__, __LINE__, GetCurrentThreadId(), MSG) + 1; \
wchar_t* __szFormat = new wchar_t[__nFormat]; \
swprintf_s(__szFormat, __nFormat, L"[%s:%d T:%d] %s\n", __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 = (size_t)_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 = (size_t)_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 = (size_t)_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 = (size_t)_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)
{

#ifdef _WIN64
#define POINTLEN "16"
#else
#define POINTLEN "8"
#endif

	unsigned int LINEBYTEMAX = 16;
	const byte* bCurrentBinary = (const byte*)bBinaryData;

	MyOutputDebugMsg(TEXT("[==================================Binary data on 0x%0") TEXT(POINTLEN) TEXT("x total %u bytes==================================]\n"), bBinaryData, iSize);

	do
	{
		unsigned int iThisLineOutNum = LINEBYTEMAX > iSize ? iSize : LINEBYTEMAX;
		MyOutputDebugMsg(TEXT("0x%0") TEXT(POINTLEN) TEXT("x  "), 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);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值