Click here to Skip to main content
15,891,248 members
Articles / Desktop Programming / MFC

Event Logging in Windows 2000

Rate me:
Please Sign up or sign in to vote.
4.72/5 (14 votes)
30 May 20028 min read 181.1K   3.3K   51  
Sample code for performing event logging that is accessible from Event Viewer
// MCCreator.cpp: implementation of the MCCreator class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MCGenerator.h"
#include "MCCreator.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MCCreator::MCCreator()
{

}

MCCreator::~MCCreator()
{

}

MCCreator::CreateMC(CString szInFileName, UINT nCategory, UINT nEvent)
{
	CString szRawFileName =  szInFileName;
	szInFileName = szInFileName + ".mc";
	m_OutStrm.open(szInFileName);	//create/opens output file and attaches OSTREAM

	if(m_OutStrm.fail())		//checks for successful file opening
	{
		cout << "The file failed to be opened or created" << endl;
		exit(1);
	}

	CreateCategoryHeading(szInFileName);
	for(UINT nLoop = 1; nLoop <= nCategory; nLoop++)
	{
		CreateCategory();
	}
	CreateEOCategory();
	CreateEventHeading();

	for(nLoop = 1; nLoop <= nEvent; nLoop++)
	{
		CreateEvent();
	}

	CreateEOF();

	m_OutStrm.close();	//closing the output file


}

void MCCreator::CreateCategoryHeading(CString szInFileName)
{
	this->m_szOut = ";//Created using Message File Generator written by Nathan Iverson\n\n\n";


	m_szOut = m_szOut + ";//*********************************************************************\n";
	m_szOut = m_szOut + ";//The following example entries are user defined and must be changed.\n";
	m_szOut = m_szOut + ";//The capital letter portion is the code that must be modified in the\n;//code sections that follow.\n\n";
	m_szOut = m_szOut + ";//#ifndef EVENT_LOG_MESSAGE_DLL\n";
	m_szOut = m_szOut + ";//#define EVENT_LOG_MESSAGE_DLL\n";
	m_szOut = m_szOut + ";//SymbolicName=SOME SYMBOLIC CATEGORY NAME WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + ";//SymbolicName=SOME SYMBOLIC EVENT NAME WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + ";//SOME MESSAGE WHICH IS USER DEFINED\n";

	m_szOut = m_szOut + ";//*********************************************************************\n";

	m_szOut = m_szOut + "\n\n\n";

	m_szOut = m_szOut + ";// MessageIdTypeDef & SeverityNames should NOT be altered\n";
	m_szOut = m_szOut + ";// Category definitions are of type WORD\n";
	m_szOut = m_szOut + ";// Category definitions must start at MessageId=1 and increment from that value\n";
	m_szOut = m_szOut + "MessageIdTypedef=WORD\n\n";

	m_szOut = m_szOut + "SeverityNames=(Success=0x0\n";
    m_szOut = m_szOut + "\t\t\t\tInformational=0x1\n";
    m_szOut = m_szOut + "\t\t\t\tWarning=0x2\n";
    m_szOut = m_szOut + "\t\t\t\tError=0x3)\n\n";


	m_szOut = m_szOut + "LanguageNames=(English=0x409:";
	m_szOut = m_szOut +	"MSG00001";
	m_szOut = m_szOut + ")\n\n";

	m_szOut = m_szOut + ";// - Modify these defines to suit your own event log module name\n";

	m_szOut = m_szOut + ";#ifndef EVENT_LOG_MESSAGE_DLL\n";
	m_szOut = m_szOut + ";#define EVENT_LOG_MESSAGE_DLL\n\n\n";


    m_szOut = m_szOut + ";//**********************Category Definitions***********************\n";
	this->m_OutStrm << m_szOut;
}


void MCCreator::CreateCategory()
{
	static UINT nCatIDIncrementor = 0;
	nCatIDIncrementor++;

	this->m_szOut = "MessageId=";
	this->m_OutStrm << m_szOut;
	this->m_OutStrm << nCatIDIncrementor;
	this->m_szOut = "\n";
	m_szOut = m_szOut + "Facility=Application\n";
	m_szOut = m_szOut + "Severity=Success\n";
	m_szOut = m_szOut + "SymbolicName=SOME SYMBOLIC CATEGORY NAME WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + "Language=English\n";
	m_szOut = m_szOut + "SOME MESSAGE WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + ".\n\n";
	this->m_OutStrm << m_szOut;

}

void MCCreator::CreateEOCategory()
{
	this->m_szOut = ";//********************End of Category Definitions*******************\n\n\n\n";
	this->m_OutStrm << m_szOut;
}

void MCCreator::CreateEventHeading()
{
	
	this->m_szOut = ";// MessageIdTypeDef should NOT be altered\n";
	m_szOut = m_szOut + ";// Event definitions are of type DWORD\n";
	m_szOut = m_szOut + "MessageIdTypedef=DWORD\n\n";
	m_szOut = m_szOut + ";//***********************Event Definitions******************************\n";
	this->m_OutStrm << m_szOut;

}
void MCCreator::CreateEvent()
{
	static UINT nEvtIDIncrementor = 1000;
	nEvtIDIncrementor++;

	this->m_szOut = "MessageId=";
	this->m_OutStrm << m_szOut;
	this->m_OutStrm << nEvtIDIncrementor;
	this->m_szOut = "\n";
	m_szOut = m_szOut + "Facility=Application\n";
	m_szOut = m_szOut + "Severity=Success\n";
	m_szOut = m_szOut + "SymbolicName=SOME SYMBOLIC EVENT NAME WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + "Language=English\n";
	m_szOut = m_szOut + "SOME MESSAGE WHICH IS USER DEFINED\n";
	m_szOut = m_szOut + ".\n\n";
	this->m_OutStrm << m_szOut;

}

void MCCreator::CreateEOF()
{

	this->m_szOut = ";//***********************End of Event Definitions***********************\n\n\n";
	m_szOut = m_szOut + ";// DO NOT REMOVE THIS\n\n";
	m_szOut = m_szOut + ";#endif\n";
	this->m_OutStrm << m_szOut;

}




By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions