[英]Locking Output to Output Window in Visual Studio
My application sends debug data to the Output Window in Visual Studio using the TRACE
macro.我的应用程序使用TRACE
宏将调试数据发送到 Visual Studio 中的 Output Window。 Some of these output strings are built using several calls to the TRACE
macro, as is shown in the following snippet of code:其中一些 output 字符串是使用对TRACE
宏的多次调用构建的,如下面的代码片段所示:
BYTE i, len;
len = pMsg[APP_LEN_OFFSET] + sizeof(appPktHead_t) - APP_MSG_CODE_LEN;
TRACE(_T("%s: "), fnName);
TRACE(GetCmdIdStr( pMsg[APP_MSG_CODE_OFFSET] ));
TRACE(_T(" 0x"));
for ( i = 0; i < len; i++ )
{
TRACE(_T("%.2X "), pMsg[i]);
}
TRACE(_T("\r\n"));
How can I either lock the output to the Output Window for the duration of this function, or send the entire string in a single call to TRACE
? How can I either lock the output to the Output Window for the duration of this function, or send the entire string in a single call to TRACE
? Thanks.谢谢。
I don't believe there is a way to lock the output window in Visual Studio.我不相信有一种方法可以在 Visual Studio 中锁定 output window。 I think the easiest way to do this is to build up the entire message into a std::stringstream
or wchar_t[]
object and then output that single value to the TRACE
macro我认为最简单的方法是将整个消息构建到std::stringstream
或wchar_t[]
object 然后 output 到TRACE
宏的单个值
Been awhile since I used stringstream
so there are likely a few errors here but the following should get you on the right track自从我使用stringstream
以来已经有一段时间了,所以这里可能存在一些错误,但以下内容应该能让你走上正确的轨道
std::stringstream stream;
stream << fnName << ": ";
stream << GetCmdIdStr( pMsg[APP_MSG_CODE_OFFSET] );
stream << " 0x";
for ( i = 0; i < len; i++ )
{
stream << pMsg[i] << " ";
}
stream << "\r\n";
TRACE(stream.str().c_str());
Build a single string, before using the TRACE
macro.在使用TRACE
宏之前构建一个字符串。
byMsgLen = pMsg[DEV_LEN_OFFSET] + sizeof(devPktHead_t) + sizeof(devPktTail_t);
cmd = pMsg[DEV_CMD_MSB_OFFSET];
cmd <<= 8;
cmd |= pMsg[DEV_CMD_LSB_OFFSET];
pCmdIdStr = GetCmdIdStr( cmd );
iPreOffset = ::wcsnlen_s(fnName, 128) // function name
+ 2 // ": "
+ ::wcsnlen_s(pCmdIdStr, 128) // command ID string
+ 3; // " 0x"
iPostOffset = iPreOffset + byMsgLen * 3; // "%.2X " (formatted: 2 hex-nibble bytes and space)
iStrLen = iPostOffset + 3; // "\r\n\0"
pBuf = (wchar_t *)malloc( iStrLen * sizeof(wchar_t) );
::swprintf_s( pBuf, iStrLen, _T("%s: %s 0x"), fnName, pCmdIdStr);
for ( i = iPreOffset; i < iPostOffset; i += 3 )
{
::swprintf_s( &(pBuf[i]), 4, _T("%.2X "), pMsg[j++] );
}
::swprintf_s( &(pBuf[i]), 3, _T("\r\n") );
TRACE( pBuf );
::free( pBuf );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.