在 Visual Studio 中将输出锁定到输出窗口

发布于 2024-11-09 08:55:10 字数 482 浏览 0 评论 0原文

我的应用程序使用 TRACE 宏将调试数据发送到 Visual Studio 中的输出窗口。其中一些输出字符串是使用多次调用 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"));

如何在此函数的持续时间内将输出锁定到输出窗口,或者在一次调用 TRACE 中发送整个字符串?谢谢。

My application sends debug data to the Output Window in Visual Studio using the TRACE macro. Some of these output strings are built using several calls to the TRACE macro, as is shown in the following snippet of code:

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? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-11-16 08:55:10

我不相信有办法在 Visual Studio 中锁定输出窗口。我认为最简单的方法是将整个消息构建到 std::stringstream 或 wchar_t[] 对象中,然后将该单个值输出到>TRACE 宏

自从我使用 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());

I don't believe there is a way to lock the output window in Visual Studio. 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

Been awhile since I used stringstream so there are likely a few errors here but the following should get you on the right track

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());
荒芜了季节 2024-11-16 08:55:10

在使用 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 );

Build a single string, before using the TRACE macro.

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文