Click here to Skip to main content
Click here to Skip to main content

HTML logger - portable and thread-safe

, 3 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An article on using HTML formated log in your applications
<!-- Download Links --> <!-- Article image -->

Sample Image - HTMLLogger.gif

<!-- Add the rest of your HTML here -->


I needed to create an HTML logger that would be portable across Win32 and VXWorks OSs. This code was written in a weekend so don't judge it severely.

What the code does

This code will help you create colored, clear to read HTML logs from any C++ applications. The HTML log generated will be easy to present to a user: you just need to give a browser the generated file URL and refresh the browser once the log changes.

This code does not use MFC or any other library. In order to provide thread safety there are a couple of classes (namely: CLock, CGuard ) that helps make the code safe.

Furthermore, there are additional helper classes that hides nasty details:

  1. CGenFile - hides the file access details
  2. CLogFont - generally an implementation of HTML font tag
  3. CLogColor - an implementation of HTML color
  4. CSmartString - a string class.

Using the code

Instantiate CHtmlLogger in your code, create the log, create as many HTML tables (reports) as you wish.

	CHtmlLogger Log;
	Log.Create( "FirstHMLLog.htm" );
	//Create table with 3 columns
	Log.CreateTable( 3, "First HTML log" );

	//Fill the columns
	Log.SetColumn( 0, "Heading1" );
	Log.SetColumn( 1, "Heading2" );
	Log.SetColumn( 2, "Heading3" );
	//Add the line specifing the line type (header in this case)
	Log.AddLine( 0, CHtmlLogger::LT_Header );

Kindergarten stuff, right ? The return values validation was removed for clarity. You should look at the sample provided to see the full API. The sample will create a sample log you see in the picture.



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

About the Author

Stas Desyatnikov
Web Developer
Israel Israel
No Biography provided

Comments and Discussions

SuggestionThanks for your great work!Append my code. Pinmembercoral952718-Sep-12 4:29 
SuggestionVery good code with some changes PinmemberZitoune 229-Nov-11 23:58 
Many thanks for this very useful code Smile | :) .
I made some change to have a best fit to my need.
First in Close method, you don't close the file (you do this only when object is destroyed), so I change a bit the code:
	//In case you call this method twice...
	if(m_pLock == NULL)
	CGuard guard( m_pLock );
	m_uiLineNumber = 0;
	delete[] m_pColumnArr;
	m_pColumnArr = NULL;
	//Write the closing tags
	if ( (FILE*)m_File )
		char* szStr = "\n</TABLE>\n</BODY>\n</HTML>";
		m_File.WriteLine( szStr );
		m_File.Close();		//Now close the file !
	SAFE_DELETE( m_pLock );
I need also Hyper link features because I use this log in my code to have information about file generation. With hyper links, user can open generated files in one click on the log.
So I had these arguments to SetColumn method:
//Sets the selected column text and associated Hyper link 
bool CHtmlLogger::SetColumn( int iColumn, char* szData, char* szHyperlink/*=NULL*/ )
	CGuard guard( m_pLock );
	if ( !m_pColumnArr )
		return false;
	if ( iColumn >= m_iColumns || iColumn < 0 )
		return false;
	return m_pColumnArr[ iColumn ].SetText( szData, szHyperlink );
Then I changed code in SetText method of CColumn class so:
bool SetText( const char* szText, const char* szHypelink=NULL)
        char szBuff[1024];
        sprintf(szBuff,"<a href=\"%s\">%s</a>",szHypelink,szText);
        return m_szText.SetString( szBuff );
        return m_szText.SetString( szText );
Now if you specify an hyper link string in szHyperlink argument, you will have an hyperlink in your report... Cool | :cool:
SetColumn(0, "Microsoft web site", "");

GeneralThanks for this utility PinmemberDarren Sessions28-Mar-10 6:48 
GeneralAll fixes are wellcome PinmemberStas Desyatnikov9-Mar-08 5:39 
GeneralPerfect, really helpful. Just a small correction PinmemberZiv G9-Mar-08 4:23 
GeneralPerfect, that's what I need Pinmembervdoser128-Mar-05 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 4 Dec 2002
Article Copyright 2002 by Stas Desyatnikov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid