如何在MFC中重定向TRACE语句以减少来自AfxDumpStack()的数据流
How is it possible to redirect TRACE statements in MFC to reduce the data stream from AfxDumpStack()?
我从AfxDumpStack看到可以重定向TRACE
输出。
- AFX_STACK_DUMP_TARGET_TRACE通过TRACE宏发送输出。TRACE宏仅在调试生成中生成输出;它在发布版本中不生成任何输出此外,TRACE可以重定向到除调试器之外的其他目标
斜体字是我的。
我从一个旧的应用程序用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 ===rn") == 0)
{
stack_out = true;
line = skip_stack_head;
}
else if (strcmp(message, "=== end AfxDumpStack() output ===rn") == 0)
{
OutputDebugString("rn");
stack_out = false;
}
else
{
if (strcmp(message, "rn") == 0) // AfxStackDump() sends CRLF out on separate calls
{
if (!stack_out || line < max_backtrace-1)
{
OutputDebugString("rnt");
}
++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
挂接输出)之外,似乎不可能直接将输出发送到文件。
在内部,MFC使用_CrtDbgReport
来输出其TRACE
消息。您可以调用_CrtSetReportMode
来指定其输出的位置。完成此操作后,您可以调用_CrtSetReportFile
来指定win32文件句柄(由CreateFile
返回)。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 如何在MFC中重定向TRACE语句以减少来自AfxDumpStack()的数据流