(更新)OutputDebugString函數簡單封裝,實現格式化打印輸出(VC++)

MyOutputDebugString是對OutputDebugString的簡單封裝。
微軟對OutputDebugString的說明文檔地址:https://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx
其中我們主要關心這些:
OutputDebugString function

Sends a string to the debugger for display.

Important  In the past, the operating system did not output Unicode strings via OutputDebugStringW and instead only output ASCII strings. To force OutputDebugStringW to correctly output Unicode strings, debuggers are required to callWaitForDebugEventEx to opt into the new behavior. On calling WaitForDebugEventEx, the operating system will know that the debugger supports Unicode and is specifically opting into receiving Unicode strings.

void WINAPI OutputDebugString(  _In_opt_ LPCTSTR lpOutputString);
ParameterslpOutputString [in, optional]

The null-terminated string to be displayed.

Return value

This function does not return a value.


英文差的朋友,可以多用詞典(我推薦靈格斯)。

簡單概括一下函數作用:輸出調試字符串,主要用於調試輸出。

但是該函數使用起來不是很方便,因爲他沒有類似sprintf格式化輸出的功能,也因此有了本文,下面的封裝可以讓它實現sprintf的功能。

//MyOutputDebugString.h 
#ifndef MY_OUTPUTDEBUGSTRING_H
#define MY_OUTPUTDEBUGSTRING_H
#define MYPRINT

#include <string>

//使用示例:MyOutputDebugStringA("%d,%s",123,"hello");
void MyOutputDebugStringA(const char * lpcszOutputString, ...);

//使用示例:MyOutputDebugStringW(L"%d,%s",456,L"world!");
void MyOutputDebugStringW(const wchar_t * szOutputString,...);

#endif
</string>
//MyOutputDebugString.cpp
#include <windows.h>
#include <stdlib.h>
#include <stdarg.h>
#include <vector>
using namespace std;

#define MYPRINT

void MyOutputDebugStringA(const char * lpcszOutputString, ...)
{
#ifdef MYPRINT
	string strResult;
	if (NULL != lpcszOutputString)
	{
		va_list marker = NULL;
		va_start(marker, lpcszOutputString); //初始化變量參數
		size_t nLength = _vscprintf(lpcszOutputString, marker) + 1; //獲取格式化字符串長度
		std::vector<char> vBuffer(nLength, '\0'); //創建用於存儲格式化字符串的字符數組
		int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszOutputString, marker);
		if (nWritten>0)
		{
			strResult = &vBuffer[0];
		}
		va_end(marker); //重置變量參數
	}
	if (!strResult.empty())
	{
		string strFormated = "[sunflover] ";
		strFormated.append(strResult);
		OutputDebugStringA(strFormated.c_str());
	}	
#endif
}

void MyOutputDebugStringW(const wchar_t * lpcwszOutputString, ...)
{
#ifdef MYPRINT
	wstring strResult;
	if (NULL != lpcwszOutputString)
	{
		va_list marker = NULL;
		va_start(marker, lpcwszOutputString); //初始化變量參數
		size_t nLength = _vscwprintf(lpcwszOutputString, marker) + 1; //獲取格式化字符串長度
		std::vector<wchar_t> vBuffer(nLength, '\0'); //創建用於存儲格式化字符串的字符數組
		int nWritten = _vsnwprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcwszOutputString, marker);
		if (nWritten>0)
		{
			strResult = &vBuffer[0];
		}
		va_end(marker); //重置變量參數
	}
	if (!strResult.empty())
	{
		wstring strFormated = L"[sunflover] ";
		strFormated.append(strResult);
		OutputDebugStringW(strFormated.c_str());
	}	
#endif
}
</wchar_t></char></vector></stdarg></stdlib></windows>

以上是.h和.cpp文件源代碼,我們只需要在使用時包含這兩個文件就可以使用MyOutputDebugStringA/W函數。

使用實例:

新建控制檯項目,項目名稱MyPrintf,然後將MyOutputDebugString.h和MyOutputDebugString.cpp添加到工程,就可以調用了,調用代碼如下

// MyPrintf.cpp : 定義控制檯應用程序的入口點。

#include "stdafx.h"
#include "MyOutputDebugString.h"//需要將.h,.cpp文件添加到項目中。
 
int _tmain(int argc, _TCHAR* argv[])
{
        MyOutputDebugStringW(L"%d %d  %s", 20, 100, L"www.jmpoep.com");//格式化輸出調試日誌;
        getchar();//作用:等待,防止直接退出;
        return 0;
}
編譯之後,我們打開DebugView,然後Ctrl+F5(直接運行程序,不調試),效果如下圖,如果調試運行,那麼調試輸出信息將被顯示在調試的輸出窗口,DebugView無法再次捕獲。


選中行爲我們打印出的調試信息,不過是不是感覺很亂,有很多信息,我們添加一個關鍵字過濾即可:(這兒關鍵字在MyOutputDebugString.cpp 中,大家可以根據需要來更改)

我們清一下屏,然後再次不調試運行程序,發現只有一條我們需要關注的信息了。

是不是感覺非常好用呢,那麼下次就添加到自己的項目中去吧!

發佈了38 篇原創文章 · 獲贊 29 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章