gpt4 book ai didi

c++ - 如何在 MFC 中重定向 TRACE 语句以减少来自 AfxDumpStack() 的数据流?

转载 作者:搜寻专家 更新时间:2023-10-31 01:04:53 25 4
gpt4 key购买 nike

我从AfxDumpStack看到可以重定向 TRACE 输出。

  • AFX_STACK_DUMP_TARGET_TRACE Sends output by means of the TRACE macro. The TRACE macro generates output in debug builds only; it generates no output in release builds. Also, TRACE can be redirected to other targets besides the debugger.

斜体字是我的。

我正在使用旧应用程序的 C++ 进行编程,并希望使用仅输出到 TRACE 或剪贴板的 AfxDumpStack() 转储部分堆栈。我只想输出几行,所以我需要在输出之前处理字符串。

我将如何以最简单的方式完成此任务?

编辑

所以这是我的解决方案的代码:

class hook
{
public:
hook()
{
VERIFY(_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, Hook) != -1);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, INVALID_HANDLE_VALUE);
}
~hook()
{
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
VERIFY(_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, Hook) != -1);
}
static const size_t max_backtrace = 4; // max calls to view
static const size_t skip_stack_head = -5; // skips over calls related to AfxDumpStack()
static size_t line; // current line being output from begin of stack dump
static bool stack_out; // currently outputting stack dump
static int Hook( int reportType, char *message, int *returnValue )
{
if (strcmp(message, "=== begin AfxDumpStack output ===\r\n") == 0)
{
stack_out = true;
line = skip_stack_head;
}
else if (strcmp(message, "=== end AfxDumpStack() output ===\r\n") == 0)
{
OutputDebugString("\r\n");
stack_out = false;
}
else
{
if (strcmp(message, "\r\n") == 0) // AfxStackDump() sends CRLF out on separate calls
{
if (!stack_out || line < max_backtrace-1)
{
OutputDebugString("\r\n\t");
}
++line;
}
else if (!stack_out || line < max_backtrace)
{
OutputDebugString(message);
}
}
return TRUE;
}
};
size_t hook::line = -1;
bool hook::stack_out = false;

static hook junk;

唯一的问题是调用 AfxDumpStack() 需要产生一个线程。如果调用太多,这会降低应用程序的性能。

最佳答案

默认情况下,TRACE 调用 OutputDebugString

你可以使用 DebugView ,它捕获 OutputDebugString 输出并可以选择将其记录到文件中。

除非更改应用程序,否则似乎无法直接将输出发送到文件,例如通过使用 _CrtSetReportHook2 Hook 输出.

在内部,MFC 使用 _CrtDbgReport输出其 TRACE 消息。您可以调用_CrtSetReportMode指定它的输出应该去哪里。完成此操作后,您可以调用 _CrtSetReportFile指定 win32 文件句柄(由 CreateFile 返回)。

关于c++ - 如何在 MFC 中重定向 TRACE 语句以减少来自 AfxDumpStack() 的数据流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com