How is it possible to redirect TRACE statements in MFC to reduce the data stream from AfxDumpStack()?

StackOverflow https://stackoverflow.com/questions/23229335

  •  07-07-2023
  •  | 
  •  

I saw from AfxDumpStack that one can redirect TRACE output.

  • 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.

The italics is mine.

I'm programming in C++ from an old application and would like to dump part of the stack using AfxDumpStack() which only outputs to TRACE or the clipboard. I want to output only a few of the lines, so I need to process the string prior to output.

How would I accomplish this in the simplest way possible?

EDIT

So this is the code for my solution:

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;

The only problem is that the call to AfxDumpStack() requires a spawning of a thread. If called too much, this will kill the performance of the application.

有帮助吗?

解决方案

By default, TRACE calls OutputDebugString.

You could use DebugView, which captures OutputDebugString output and has an option to log it to a file.

It doesn't seem to be possible to directly send the output to a file other than by changing the application, e.g. by hooking the output with _CrtSetReportHook2.

Internally, MFC uses _CrtDbgReport to output its TRACE messages. You can call _CrtSetReportMode to specify where its output should go. Having done this you can call _CrtSetReportFile to specify the win32 file handle (as returned by CreateFile).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top