原因
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());