_CrtSetReportFile

After you use _CrtSetReportMode to specify _CRTDBG_MODE_FILE, you can specify the file handle to receive the message text. _CrtSetReportFile is also used by _CrtDbgReport, _CrtDbgReportW to specify the destination of text (debug version only).

Syntax

_HFILE _CrtSetReportFile(
   int reportType,
   _HFILE reportFile
);

Parameters

reportType
Report type: _CRT_WARN, _CRT_ERROR, and _CRT_ASSERT.

reportFile
New report file for reportType.

Return value

On successful completion, _CrtSetReportFile returns the previous report file defined for the report type specified in reportType. If an invalid value is passed in for reportType, this function invokes the invalid parameter handler, as described in Parameter validation. If execution is allowed to continue, errno is set to EINVAL, and the function returns _CRTDBG_HFILE_ERROR. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

_CrtSetReportFile is used with the _CrtSetReportMode function to define the destination or destinations for a specific report type generated by _CrtDbgReport. When you call _CrtSetReportMode to assign the _CRTDBG_MODE_FILE reporting mode for a specific report type, also call _CrtSetReportFile to specify the destination file or stream. When _DEBUG isn't defined, calls to _CrtSetReportFile are removed during preprocessing.

The following list shows the available choices for reportFile and the resulting behavior of _CrtDbgReport. These options are defined as bit flags in Crtdbg.h.

  • file handle

    A handle to the file that will be the destination for messages. No attempt is made to verify the validity of the handle. You must open and close the handle to the file. For example:

    HANDLE hLogFile;
    hLogFile = CreateFile("c:\\log.txt", GENERIC_WRITE,
        FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_WARN, hLogFile);
    
    _RPT0(_CRT_WARN,"file message\n");
    CloseHandle(hLogFile);
    
  • _CRTDBG_FILE_STDERR

    Writes message to stderr, which can be redirected as follows:

    freopen( "c:\\log2.txt", "w", stderr);
    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
    
    _RPT0(_CRT_ERROR,"1st message\n");
    
  • _CRTDBG_FILE_STDOUT

    Writes message to stdout, which you can redirect.

  • _CRTDBG_REPORT_FILE

    Returns the current report mode.

You can control the report file used by each report type separately. For example, it's possible to specify that a reportType of _CRT_ERROR reports through stderr, while a reportType of _CRT_ASSERT reports through a user-defined file handle or stream.

Requirements

Routine Required header Optional header
_CrtSetReportFile <crtdbg.h> <errno.h>

The console isn't supported in Universal Windows Platform (UWP) apps. The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. For more compatibility information, see Compatibility.

Libraries: Debug versions of CRT library features only.

See also

Debug routines