Jim Fell
Jim Fell

Reputation: 14254

Error When Using TRACE to Output File Name

I'm trying to use the TRACE macro to send the file name to the output window. The following compiles, but when it executes, instead of the desired output, I get an error in the output window:

TRACE(_T("Trace test.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ERROR_SUCCESS, __FILE__, __LINE__);

Produces error:

_CrtDbgReport: String too long or IO ErrorFirst-chance exception at 0x7c812afb in MyApp.exe: Microsoft C++ exception: long at memory location 0x0012fe18..

I'm sure it has to do with the __FILE__ macro, but I'm not sure what exactly is wrong. Does anyone know how get this working? Thanks.

Upvotes: 0

Views: 749

Answers (1)

ROAR
ROAR

Reputation: 1324

Isnt it that you need a wide string version of FILE to get that right ?

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
wchar_t *pwsz = __WFILE__;

wprintf(pwsz)

// Function to split path into file and directory parts..
void ExtractFileNameFromPath( const std::wstring &_sPath,std::wstring &_sFilename,std::wstring &_sDirectory)
{
    int iPos = _sPath.rfind('\\');
    if( iPos == std::_tstring::npos) iPos = _sPath.rfind(TCHAR("/"));
    if( iPos != std::_tstring::npos) 
    { _sFilename = _sPath.substr(iPos + 1); _sDirectory = _sPath.substr(0,iPos); }
    else _sFilename = _sPath;
    nsStringTools::Trim(_sFilename);
    nsStringTools::Trim(_sDirectory);
    if(_sDirectory[_sDirectory.length()-1] != _T('\\'))
        _sDirectory += _T("\\");
}

Upvotes: 1

Related Questions