//in Debug.h
HRESULT OpenDebugFile(TCHAR *szFileName);
  void CloseDebugFile();
  #ifdef _DEBUG
        HRESULT FailureTest(HRESULT,TCHAR*,INT);
          #ifdef FAILED
        #undef FAILED
        #endif
        #define FAILED(result) FailureTest(result, _T(__FILE__), _T(__LINE__));
#endif     
//*********************************************************
//in Debug.cpp
ofstream g_DbgOut;
  HRESULT OpenDebugFile( TCHAR* szFileName )
{
    g_DbgOut.open( szFileName );
    if( g_DbgOut.good() )
        return S_OK;
      return E_FAIL;
}
  void CloseDebugFile()
{
    if( g_DbgOut.good() )
        g_DbgOut.close();
}
  HRESULT FailureTest( HRESULT hr, TCHAR* szFile, INT iLine )
{
    BOOL bFailed = ( hr < 0 );
      assert(g_DbgOut.good());
      if( bFailed )
        g_DbgOut << "Function Failure: " << "File: " << szFile << ", " 
                 << "Line:" << iLine << endl;
      return bFailed;
}
//*****************************************************
//in use
void main()
{
        OpenDebugFile( _T("Debug.txt") );  //should check for failure
        HRESULT hr = Func();
        if( FAILED( hr ) )
                goto Cleanup;
          /****Other code****/
  Cleanup:
        CloseDebugFile();  
  }
  //Output
Function Failure: File: /*Path with file name*/, Line: 4   |