在輸出調試信息的時候,經常會用到這幾個宏。首先看一段示例代碼,再來介紹這幾個宏:
- #include <stdlib.h>
- #include <stdio.h>
- //替換函數名
- #ifndef _DEBUG
- #define LOGFUNC(...) ((void)0)
- #else
- #define LOGFUNC(...) (printf(__VA_ARGS__))
- #endif
- //宏前面加上##的作用在於:當可變參數的個數為0時,這里的## 起到把前面多余的","去掉的作用
- #define DEBUG_INFO(format, ...) printf("File:%s, Line:%d, Function:%s, %s", \
- __FILE__, __LINE__ , __FUNCTION__, ##__VA_ARGS__);
- void show_debug_info()
- {
- DEBUG_INFO("%s", "hello world");
- }
- int main()
- {
- LOGFUNC("%s\n", "this is test");
- show_debug_info();
- system("pause");
- return 0;
- }
1) __VA_ARGS__ 是一個可變參數的宏,總體來說就是將左邊宏中 ... 的內容原樣抄寫在右邊 __VA_ARGS__ 所在的位置。
2) __FILE__ 宏在預編譯時會替換成當前的源文件名
3) __LINE__ 宏在預編譯時會替換成當前的行號
4) __FUNCTION__ 宏在預編譯時會替換成當前的函數名稱
5)類似的宏還有__DATE__, __TIME__,__STDC__, __TIMESTAMP__等,可以當作一個變量來使用。
關於宏##的有關解析,在另一篇文章有介紹:http://www.cnblogs.com/fnlingnzb-learner/p/6823575.html
上述代碼中定義的DEBUG_INFO宏,就是輸出控制台的調試信息。比如說,我們通過 OutputDebugString 輸出調試信息這樣寫:
- #ifdef _DEBUG
- #define OUTPUT_DEBUGW(fmt, ...) PrintDebugStringW(_T(__FILE__),__LINE__, fmt, __VA_ARGS__)
- #else
- #define OUTPUT_DEBUGW ((void)0)
- #endif
- void PrintDebugStringW(const wchar_t *file, int lineno, const wchar_t *pszFmt, ...)
- {
- va_list vlArgs = NULL;
- va_start(vlArgs, pszFmt);
- size_t nLen = _vscwprintf(pszFmt, vlArgs) + 1;
- wchar_t *strBuffer = new wchar_t[nLen];
- _vsnwprintf_s(strBuffer, nLen, nLen, pszFmt, vlArgs);
- va_end(vlArgs);
- //OutputDebugStringW(strBuffer);
- wchar_t buf[4096];
- swprintf_s(buf, 4096, L"%s<%d>: tid[%d] :\t%s\n", file, lineno, GetCurrentThreadId(), strBuffer);
- OutputDebugStringW(buf);
- delete [] strBuffer;
- }