c++, debugview日志拼接,打印。

原因

OutputDebugString() 函数不能够拼接字符串,使用起来比较麻烦。

于是,我写了一个简单的可以拼接的使用debugview输出日志的方法。

debugHelper.h

#pragma once


#define XXCALL __cdecl


#if defined(DebugHelper_DLL)

#if defined(__DebugHelper__)
#define DebugHelperAPI __declspec(dllexport)
#else
#define DebugHelperAPI __declspec(dllimport)

//#pragma message("如果要在静态库中(LIB)使用 DebugHelper ,请取消 [DebugHelper_DLL] 宏定义.")

#endif

#else

#define DebugHelperAPI

#if defined(__DebugHelper__)

#else

//#pragma message("如果要在动态库(DLL)中使用 DebugHelper ,请设置 [DebugHelper_DLL] 宏定义.")

#endif


#endif

#if defined(_UNICODE)
#define DebugPrint(Format,...) DebugPrintW(Format,__VA_ARGS__)
#define DebugOutput(Format,...) DebugOutputW(Format,__VA_ARGS__)
#define DebugBox(Format,...) DebugBoxW(Format,__VA_ARGS__)
#define DebugFilePrint(FileName,Format,...) DebugFilePrintW(FileName,Format,__VA_ARGS__)
#define DebugLastFilePrint(Format,...) DebugLastFilePrintW(Format,__VA_ARGS__)
#else
#define DebugPrint(Format,...) DebugPrintA(Format,__VA_ARGS__)
#define DebugOutput(Format,...) DebugOutputA(Format,__VA_ARGS__)
#define DebugBox(Format,...) DebugBoxA(Format,__VA_ARGS__)
#define DebugFilePrint(FileName,Format,...) DebugFilePrintA(FileName,Format,__VA_ARGS__)
#define DebugLastFilePrint(Format,...) DebugLastFilePrintA(Format,__VA_ARGS__)
#endif

#if defined(_DEBUG)
#define DbgPrintA(FormatA,...) DebugPrintA(FormatA,__VA_ARGS__)
#define DbgOutputA(FormatA,...) DebugOutputA(FormatA,__VA_ARGS__)
#define DbgBoxA(FormatA,...) DebugBoxA(FormatA,__VA_ARGS__)
#define DbgFilePrintA(FileNameA,FormatA,...) DebugFilePrintA(FileNameA,FormatA,__VA_ARGS__)
#define DbgLastFilePrintA(FileNameA,FormatA,...) DebugLastFilePrintA(FileNameA,FormatA,__VA_ARGS__)

#define DbgPrintW(FormatW,...) DebugPrintW(FormatW,__VA_ARGS__)
#define DbgOutputW(FormatW,...) DebugOutputW(FormatW,__VA_ARGS__)
#define DbgBoxW(FormatW,...) DebugBoxW(FormatW,__VA_ARGS__)
#define DbgFilePrintW(FileNameW,FormatW,...) DebugFilePrintW(FileNameW,FormatW,__VA_ARGS__)
#define DbgLastFilePrintW(FormatW,...) DebugLastFilePrintW(FormatW,__VA_ARGS__)

#define DbgPrint(Format,...) DebugPrint(Format,__VA_ARGS__)
#define DbgOutput(Format,...) DebugOutput(Format,__VA_ARGS__)
#define DbgBox(Format,...) DebugBox(Format,__VA_ARGS__)
#define DbgFilePrint(FileName,Format,...) DebugFilePrint(FileName,Format,__VA_ARGS__)
#define DbgLastFilePrint(Format,...) DebugLastFilePrint(Format,__VA_ARGS__)
#else
#define DbgPrintA(FormatA,...)
#define DbgOutputA(FormatA,...)
#define DbgBoxA(FormatA,...)
#define DbgFilePrintA(FileNameA,FormatA,...)
#define DbgLastFilePrintA(FormatA,...)

#define DbgPrintW(FormatW,...)
#define DbgOutputW(FormatW,...)
#define DbgBoxW(FormatW,...)
#define DbgFilePrintW(FileNameW,FormatW,...)
#define DbgLastFilePrintW(FormatW,...)

#define DbgPrint(Format,...)
#define DbgOutput(Format,...)
#define DbgBox(Format,...)
#define DbgFilePrint(FileName,Format,...)
#define DbgLastFilePrint(Format,...)
#endif

#ifdef __cplusplus
#define __EXTERN_C       extern "C"
#define __EXTERN_C_START extern "C" {
#define __EXTERN_C_END   }
#else
#define __EXTERN_C       extern
#define __EXTERN_C_START
#define __EXTERN_C_END
#endif


__EXTERN_C_START


DebugHelperAPI void XXCALL DebugPrintA(char const* const Format, ...);

DebugHelperAPI void XXCALL DebugOutputA(char const* const Format, ...);

DebugHelperAPI void XXCALL DebugBoxA(char const* const Format, ...);

DebugHelperAPI void XXCALL DebugFilePrintA(const char* const FileName, char const* const Format, ...);

DebugHelperAPI void XXCALL DebugLastFilePrintA(char const* const Format, ...);

DebugHelperAPI void XXCALL DebugPrintW(wchar_t const* const Format, ...);

DebugHelperAPI void XXCALL DebugOutputW(wchar_t const* const Format, ...);

DebugHelperAPI void XXCALL DebugBoxW(wchar_t const* const Format, ...);

DebugHelperAPI void XXCALL DebugFilePrintW(wchar_t const* const FileName, wchar_t const* const Format, ...);

DebugHelperAPI void XXCALL DebugLastFilePrintW(wchar_t const* const Format, ...);

__EXTERN_C_END

debugHelper.cpp

#define __DebugHelper__
#if defined(_WINDLL)
//#define DebugHelper_DLL
#endif
#include "DebugHelper.h"

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdarg.h>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

__EXTERN_C_START

DebugHelperAPI void XXCALL DebugPrintA(char const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	vprintf(Format, args);
	va_end(args);
}

DebugHelperAPI void XXCALL DebugOutputA(char const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	int Count = _vscprintf(Format, args);
	char* Buffer = new char[size_t(Count) + 1];
	vsprintf(Buffer, Format, args);
	va_end(args);
	OutputDebugStringA(Buffer);
	delete[]Buffer;
}

DebugHelperAPI void XXCALL DebugBoxA(char const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	int Count = _vscprintf(Format, args);
	char* Buffer = new char[size_t(Count) + 1];
	vsprintf(Buffer, Format, args);
	va_end(args);
	MessageBoxA(NULL, Buffer, "DebugBox", MB_OK);
	delete[]Buffer;
}

DebugHelperAPI void XXCALL DebugFilePrintA(const char* FileName,char const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	FILE* file = fopen(FileName, "a");
	vfprintf(file, Format, args);
	fclose(file);
	va_end(args);
}


DebugHelperAPI void XXCALL DebugPrintW(wchar_t const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	vwprintf(Format, args);
	va_end(args);
}

DebugHelperAPI void XXCALL DebugOutputW(wchar_t const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	int Count = _vscwprintf(Format, args);
	wchar_t* Buffer = new wchar_t[size_t(Count) + 1];
	vswprintf(Buffer, Count,Format, args);
	va_end(args);
	OutputDebugStringW(Buffer);
	delete[]Buffer;
}

DebugHelperAPI void XXCALL DebugBoxW(wchar_t const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	int Count = _vscwprintf(Format, args);
	wchar_t* Buffer = new wchar_t[size_t(Count) + 1];
	vswprintf(Buffer, Count, Format, args);
	va_end(args);
	MessageBoxW(NULL, Buffer, L"DebugBox", MB_OK);
	delete[]Buffer;	
}

DebugHelperAPI void XXCALL DebugFilePrintW(wchar_t const* const FileName, wchar_t const* const Format, ...)
{
	va_list args;
	va_start(args, Format);
	FILE* file = _wfopen(FileName, L"a");
	vfwprintf(file, Format, args);
	fclose(file);
	va_end(args);
}

__EXTERN_C_END

把这两个文件添加到工程中,引用头文件即可使用。

DebugOutputA("CreateFile 成功!,%d", GetLastError());
### 回答1: C DebugView是一种动态调试工具,用于帮助开发人员在调试和分析程序时查看应用程序输出的日志信息。它可以捕获和显示来自应用程序和设备驱动程序的调试输出,帮助开发人员理解应用程序的行为和运行过程。 C DebugView工具简单易用,并且支持各种调试输出源,例如调试器,输出到控制台的调试测试和Windows应用程序。开发人员可以使用C DebugView监视应用程序的调试信息,并可根据需要进行筛选和分析。 C DebugView功能强大,可以显示和过滤应用程序的日志信息,还可以根据关键字和时间戳进行搜索。此外,开发人员还可以将日志信息保存到文件中,以便后续分析和调试。 使用C DebugView可以提高程序开发过程中的效率和准确性。开发人员可以实时查看应用程序的输出日志,及时发现潜在的错误和问题,并针对性地进行修复。同时,C DebugView还可以记录应用程序的调试信息,为开发人员提供详细的日志记录,方便后续的调试和分析工作。 总之,C DebugView是一款功能强大的调试工具,可以帮助开发人员监视和分析应用程序的调试信息,提高程序开发过程中的效率和准确性。 ### 回答2: c debugview是一款非常实用的调试工具。它可以用于查看C语言程序的调试信息和日志输出,帮助程序员快速定位bug并进行调试。 c debugview的使用非常简便,只需要在需要调试的程序中引入其头文件,并在代码中插入一些调试输出语句,如printf函数,debugview就可以将这些输出信息实时显示在它提供的窗口中。 通过c debugview,我们可以查看程序运行时的变量值、函数调用关系、错误信息等。这对于定位程序中的问题非常有帮助,尤其是对于一些难以复现的bug,它可以提供有关程序运行状态的实时信息,方便程序员进行追踪和分析。 此外,c debugview还支持实时日志输出和保存,我们可以将程序中重要的日志信息通过debugview展示出来,避免了在终端或其他地方进行查看和保存的麻烦。同时,它还提供了一些筛选和搜索功能,方便我们快速定位所需的日志信息。 总之,c debugview是一款优秀的调试工具,它可以大大提高我们的调试效率和工作效率。无论是对于小型还是大型C语言项目,都是非常值得推荐和使用的工具。 ### 回答3: C DebugView 是一款用于调试和监视 Windows 操作系统的实用工具。它可以帮助开发人员在程序运行时捕获和查看应用程序输出的日志信息。 使用 DebugView 可以方便地捕获和查看应用程序输出的调试信息、警告信息、错误信息等。它可以实时显示各种输出信息,包括程序启动信息、进程和线程的名称、时间戳等。同时,DebugView 还能够按照用户定义的过滤规则对输出信息进行过滤,用户可以根据自己的需求只查看特定类型的日志信息,以提高调试效率。 DebugView 提供了一个轻量级的界面,非常易于使用。用户只需要打开 DebugView,然后在应用程序中进行调试输出时,就能够实时地在 DebugView 中看到输出的信息。同时,DebugView 还支持将输出信息保存到文件中,方便开发人员进行后续分析和调试。 除了捕获应用程序的输出信息之外,DebugView 还可以用于监视操作系统内部的一些日志信息,比如系统启动时间、驱动加载信息等。这对于分析和解决一些与系统相关的问题非常有帮助。 总之,C DebugView 是一款功能强大、易于使用的调试工具,它能够帮助开发人员捕获和查看应用程序输出的日志信息,提高调试效率,解决问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值