现在的位置: 首页 > 综合 > 正文

WinSDK编程另类调试输出

2014年10月23日 ⁄ 综合 ⁄ 共 1119字 ⁄ 字号 评论关闭

ref : http://www.qpsoft.com/blog/winsdk-debug-info-command/

 

不想使用OutputDebugStr函数,因为输入这个API很麻烦,而且数字向字符串的转换也麻烦。那么有没有直观、简单、易用的调试信息输出呢?如何在Release版本中也输出相应信息:如果愿意的话?
于是,想到了控制台输出,而又不影响Release的发布。
方案:iostream+#define
在def.h文件中定义宏:

#ifdef qpDEBUG
#include <iostream>
#include <locale>
#define LOGW(o) /
    setlocale(LC_ALL, "chs"); /
    std::wcout << __FILE__ << " (" << __LINE__ << ") : " << std::endl << o << std::endl << std::endl
#define LOGA(o) /
    std::cout << __FILE__ << " (" << __LINE__ << ") : " << std::endl << o << std::endl << std::endl
#ifdef _UNICODE
#define LOG(o) LOGW(o)
#else
#define LOG(o) LOGA(o)
#endif // _UNICODE
#else
#define LOG(o)
#define LOGW(o)
#define LOGA(o)
#endif // qpDEBUG

将入口函数修改成:

#ifdef qpDEBUG
#pragma comment(linker, "/SUBSYSTEM:/"CONSOLE/" /entry:/"wmainCRTStartup/"")
int wmain(int argc, wchar_t* argv[])
#else
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE preInst, PSTR cmdLine, int cmdShow)
#endif // qpDEBUG

在任何想得到调试信息的地方使用:

    LOG(0);
    LOG(1 << "-" << 2 << "-" << 3 << "-" << 4);
    LOG("English info");
    LOGA("因为对应wcout,中文字符只能用LOGA");
    LOG(L"测试宽字符");

输出结果如下:

main/main.cpp (25) :
0

main/main.cpp (26) :
1-2-3-4

main/main.cpp (27) :
English info

main/main.cpp (28) :
因为对应wcout,中文字符只能用LOGA

main/main.cpp (29) :
测试宽字符

抱歉!评论已关闭.