c – 有没有更好的方法将格式化输出传递给OutputDebugString?

c – 有没有更好的方法将格式化输出传递给OutputDebugString?,第1张

概述通常,当我需要在 Windows中进行调试输出时,我使用以下C代码段: #ifdef _DEBUG#define DBGPRINT( kwszDebugFormatString, ... ) \{ \ wprintf_s( L"[%s:%d] ", __FUNCTIONW__, __LINE__ ); \ wprintf_s( kwszDebugFormatString, __ 通常,当我需要在 Windows中进行调试输出时,我使用以下C代码段:

#ifdef _DEBUG#define DBGPRINT( kwszDeBUGFormatString,... ) \{ \    wprintf_s( L"[%s:%d] ",__FUNCTIONW__,__liNE__ ); \    wprintf_s( kwszDeBUGFormatString,__VA_ARGS__ ); \}#else#define DBGPRINT( kwszDeBUGFormatString,...) ;;#endif

我想重新编码它以使用不接受格式字符串的OutputDeBUGString.我认为静态地在堆栈上分配一个小数组(例如,WCHAR wszBuf [100] = {0};)有点粗糙,因为它可能消耗比分配的内存更多或更少的内存并截断输出或浪费内存.我编写了以下代码来解决所有这些问题,但我担心因为宏有点大.

#ifdef _DEBUG#define DBGPRINT( kwszDeBUGFormatString,... ) \{ \    INT ilineNumber = __liNE__; \    file *fileNul = NulL; \    INT cbFormatString = 0; \    PWCHAR wszDeBUGString = NulL; \    size_t st_Offset = 0; \    \    /* Determine the number of characters in the format string by writing to Nul. */\    fopen_s( &fileNul,"nul","w" ); \    cbFormatString = fwprintf_s( fileNul,L"[%s:%d]",ilineNumber ) * sizeof( WCHAR ); \    cbFormatString += fwprintf_s( fileNul,kwszDeBUGFormatString,__VA_ARGS__ ) * sizeof( WCHAR ) + 2; \    \    /* Depending on the size of the format string,allocate space on the stack or the heap. */ \    wszDeBUGString = (PWCHAR)_malloca( cbFormatString ); \    \    /* Populate the buffer with the contents of the format string. */ \    StringCbPrintfW( wszDeBUGString,cbFormatString,ilineNumber ); \    StringCbLengthW( wszDeBUGString,&st_Offset ); \    StringCbPrintfW( &wszDeBUGString[st_Offset / sizeof(WCHAR)],cbFormatString - st_Offset,__VA_ARGS__ ); \    \    OutputDeBUGStringW( wszDeBUGString ); \    \    _freea( wszDeBUGString ); \    fclose( fileNul ); \}#else#define DBGPRINT( kwszDeBUGFormatString,... ) ;;#endif

几点说明:

>我使用_malloca()和_freea(),以便在输出足够小时可以使用堆栈分配.
>我不知道如何获得完全展开的格式字符串的正确大小.我能想到的最好的解决方案是将其转储到Nul并从那里计算出正确的尺寸.
>我使用了一个宏,因为我不相信在使用函数时有一种简单的方法可以实现相同的结果.

我的问题很简单,因为某种原因(特别是尺寸或效率低下),这个宏会被视为不良做法吗?如果是这样,我应该考虑哪些替代方案?

如果其他人想知道,我使用了评论和所选答案中的建议,并提出了以下代码.非常感谢所有评论或回答的人 – 如果你有一个聪明的方法想要分享,请随意添加更多!

#ifdef _DEBUG#define DBGPRINT(kwszDeBUGFormatString,...) _DBGPRINT(__FUNCTIONW__,__liNE__,__VA_ARGS__)VOID _DBGPRINT( LPCWSTR kwszFunction,INT ilineNumber,LPCWSTR kwszDeBUGFormatString,... ) \{    INT cbFormatString = 0;    va_List args;    PWCHAR wszDeBUGString = NulL;    size_t st_Offset = 0;    va_start( args,kwszDeBUGFormatString );    cbFormatString = _scwprintf( L"[%s:%d] ",kwszFunction,ilineNumber ) * sizeof( WCHAR );    cbFormatString += _vscwprintf( kwszDeBUGFormatString,args ) * sizeof( WCHAR ) + 2;    /* Depending on the size of the format string,allocate space on the stack or the heap. */    wszDeBUGString = (PWCHAR)_malloca( cbFormatString );    /* Populate the buffer with the contents of the format string. */    StringCbPrintfW( wszDeBUGString,L"[%s:%d] ",ilineNumber );    StringCbLengthW( wszDeBUGString,&st_Offset );    StringCbVPrintfW( &wszDeBUGString[st_Offset / sizeof(WCHAR)],args );    OutputDeBUGStringW( wszDeBUGString );    _freea( wszDeBUGString );    va_end( args );}#else#define DBGPRINT( kwszDeBUGFormatString,... ) ;;#endif
解决方法 如果你将所有代码放入普通的varargs函数然后在你的宏中调用它会更简单,更不容易出错,类似于:

voID dbgprint(const wchar_t *func,int line,const wchar_t *fmt,...) {   // Fomat the string,maybe with vsprintf,log it,etc.}#define DBGPRINT(fmt,...) dbgprint(__WFUNCTION__,fmt,__VA_ARGS__)
总结

以上是内存溢出为你收集整理的c – 有没有更好的方法将格式化输出传递给OutputDebugString?全部内容,希望文章能够帮你解决c – 有没有更好的方法将格式化输出传递给OutputDebugString?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1216610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存