15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


WinNT services & object diagnostics

Martin Krumpolec -- krumpo@asset.sk
Monday, August 12, 1996

Environment: VC++ 4.0, NT 4.0

Hello,

  I have following problem ... I'm writing WinNT service and I would 
  like to use diagnostics offered by MFC's CObject ... But I don't 
  know how to "redirect" a afxDump stream into my own file ... Anyone 
  has a clue ?

Thank in an advance

						Martin

--
krumpo@asset.sk



PJ Durai -- pjdurai@pixtran.com
Tuesday, August 13, 1996

[Mini-digest: 4 responses]


     Hi Martin
     
     Instead of using afxDump you can create your own dumpcontext. 
     Dumpcontexts are usually associated with disk files so you can get 
     your dumpmessages in any file you want.
     
     Something like this should do the trick.
     
     MyObject obj;  // a CObject derived class
     CFile dumpfile("d:\\dumpfile.txt", CFile::modeCreate |                 
                                        File::modeWrite);
     CDumpContext dc( &dumpfile );
     obj.Dump (dc);
     dc.Flush ();
     dumpfile.Close ();

     Hope that helps.
     pj.

______________________________ Reply Separator _________________________________
Subject: WinNT services & object diagnostics
Author:  mfc-l@netcom.netcom.com at Internet
Date:    8/13/96 12:17 PM


Environment: VC++ 4.0, NT 4.0
     
Hello,
     
  I have following problem ... I'm writing WinNT service and I would 
  like to use diagnostics offered by MFC's CObject ... But I don't 
  know how to "redirect" a afxDump stream into my own file ... Anyone 
  has a clue ?
     
Thank in an advance
     
      Martin
     
--
krumpo@asset.sk
-----From: nasarre@col.bsf.alcatel.fr

Here is a solution to redirect TRACE() output to one of your log file :
Warning : TRACE() does not work in global object constructor


BOOL CTheApp::InitInstance()
{
// this sentence will be sent as usual to the debug output
   TRACE("InitInstance() in standard debug output\n");

// never forget to bracket any use of afxDump with #ifdef _DEBUG since 
// afxDump is only declared in _DEBUG build
#ifdef _DEBUG 
// the file associated to afxDump is : 
//    *> created if it does not already exist and
//    *> not truncated to 0 length if it already exists
//    *> a text file (with \n translation we need)
//    *> writable 
// Rem: this constructor opens the file
   afxDump.m_pFile = 
      new CStdioFile(
             "C:\\TRACE.TXT", 
             CFile::modeCreate 
               | CFile::modeNoTruncate 
               | CFile::typeText 
               | CFile::modeWrite
             );
   if (afxDump.m_pFile != NULL)
   // if you want to keep the content of previous output, you need to start 
   // writting at the end of the existing file
      afxDump.m_pFile->SeekToEnd();
#endif

// this sentence is now sent to C:\TRACE.TXT
   TRACE("InitInstance in new file output\n");


// your default InitInstance() code

// keep on working !
   return(TRUE);
}



int CTheApp::ExitInstance()
{
// never forget to bracket any use of afxDump with #ifdef _DEBUG since 
// afxDump is only declared in _DEBUG build
#ifdef _DEBUG
   if (afxDump.m_pFile != NULL)
   {
   // flush buffered data to file
      afxDump.Flush();

   // never forget to delete a dynamically allocated CStdioFile
   // Rem: since the object is deleted, its constructor is called and the
   //      file is automatically closed
      delete(afxDump.m_pFile);

   // reset m_pFile member with its initial NULL value
      afxDump.m_pFile = NULL;
   }
#endif
}


Good luck

   Chris


-----From: Martin Krumpolec 

Klaus Guetter wrote:

> You asked how to "redirect" a afxDump stream into your own file. 
> In a recent project (using VC 4.1, I don't kown if it works with 4.0) I
> succeeded redirecting all debug output to a file using _CrtSetReportHook. 

  It works, but I have sought for "MFC level" solution, but it seems to
be 
  imposible, because afxDump is defined too firmly in DLLs.

Thank

						Matko
-----From: Klaus Guetter <100031.1400@CompuServe.COM>

Martin,

You asked how to "redirect" a afxDump stream into your own file.

In a recent project (using VC 4.1, I don't kown if it works with 4.0) I
succeeded redirecting all debug output to a file using _CrtSetReportHook. Here's
some code:

// ---------------------- MyApp.h -------------------------------
class CMyApp : public CWinApp
{
	// ...
#ifdef _DEBUG
	CStdioFile m_fileLog;
	void WriteLogOutput(LPCSTR lpszText);
	_CRT_REPORT_HOOK m_lpOldReportHook;
	static int DebugHookProc(int nRepType, char* pszMsg, int* pnReturnValue);
#endif
	// ...
};

// ---------------------- MyApp.cpp -------------------------------
CMyApp g_theApp;

#ifdef _DEBUG
_CRT_REPORT_HOOK CMyApp::m_lpOldReportHook = NULL;

int CMyApp::DebugHookProc(int nRepType, char* pszMsg, int* pnReturnValue)
{
	// lock out recursive calls
	static BOOL bInHookProc = FALSE;
	if (!bInHookProc)
	{
		bInHookProc = TRUE;
		g_theApp.WriteLogOutput(pszMsg);
		bInHookProc = FALSE;
	}
	return FALSE;	// continue as usual
}

void CMyApp::WriteLogOutput(LPCSTR lpszText)
{
	// May be called from a DLL though DebugHookProc, so set up module state
	AFX_MANAGE_STATE(m_pModuleState);
	TRY
	{
		// write to the log file
		if (m_fileLog.m_pStream)
		{
			m_fileLog.WriteString(lpszText);
			m_fileLog.Flush();
		}
	}
	END_TRY
}

#endif


BOOL CMyApp::InitInstance()
{
#ifdef _DEBUG 
	CFileException fe;
	m_fileLog.Open(LOGFILE_NAME, 
		CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite |
CFile::typeBinary, &fe);
	m_lpOldReportHook = _CrtSetReportHook(DebugHookProc);
#endif

	// ...
}


int CMyApp::ExitInstance() 
{
#ifdef _DEBUG 
	_CrtSetReportHook(m_lpOldReportHook);
	TRY
		m_fileLog.Close();
	END_TRY
#endif

	return CWinApp::ExitInstance();
}

// ---------------------- End code -------------------------------

Hope this helps

- Klaus Guetter





| Вернуться в корень Архива |