Click here to Skip to main content
15,392,442 members
Articles / Programming Languages / C++
Posted 25 Jun 2002


73 bookmarked

A Simple LogFile

Rate me:
Please Sign up or sign in to vote.
4.54/5 (21 votes)
19 Mar 20033 min read
A simple logfile to record runtime information


Sometime we need to record something after we give the software to our customers, so when something bad happened, we can find it out without the debug environment. Sometime we want to get some trace information in the release mode of the program. In these circumstances, a logfile is useful.

There are lots of logfile code examples here at codeproject and elsewhere. This one is very simple, compared to some of the others.

The class CLogFile has only three member functions: a constructor, a destructor and a member called Write. Let's see how to use it.

The constructor needs 3 parameters:

CLogFile(LPCTSTR strFile, bool bAppend = FALSE,
                                    long lTruncate = 4096)

The first parameter is the file name. It could include the absolute path, or just a file name. In the latter case, the logfile would be created in the same directory as the executable. Nothing concerned with the "Current Directory", which always brings me trouble.

The second parameter indicate whether the log information should append to the end of the file, if the file already exists.

The third parameter indicates the truncate size. If the logfile exceeds this number, it rewinds to the beginning, and continues. The information following writes to the file from the beginning.

The second member is the destructor. It does some cleaning jobs. Nothing more to say.

The most important member is the third:

void Write(LPCTSTR pszFormat, ...)

This member writes a line to the logfile. Its usage is just the same as printf. It seems that there could be much to say about this member, but I'd prefer to keep things simple. And it is simple.


  • File name could use absolute path or just the name.
  • Every log line has a time stamp attached.
  • Uses printf like format to write log lines.
  • Multithread safe.

Anything else is in the code. The thing is 108 lines of code in a .h file, including comments and blank lines.

Update by Zoltan:

My project was a 24/7 application, so one log file could be to big. So I made new log files every day which meant some months lots of files were made in each directory. Finally I made folders for every year, every month, and a log files are stored in the month folders.

Sample screenshot

I made the following function to change the current file name:

void ChangeFile(LPCTSTR strFile, bool bAppend = TRUE, long lTruncate = 4096);

These are the same params like old constructor, and when the file name is different from the stored m_filename, first close the old logger file, then open the new in the selected folder.

Using this feature

Most inportantly don't forget:

#define _DEBUG_LOG    TRUE
#include "logfile.h"

The following code shows an example how can be use this. %02i is a useful choose for month, so the 09 month will not overtake the 10 month.
For testing choose systime.wMinute to faster create new folders.

CString name;




  • File name could use absolute path or just the name. (original by Yao)
  • File name could be in a new folder, and a logger will create it.

Revision History

  • 26 Jun 2002 - Initial Revision
  • 20 Mar 2003 - updated by Zoltan


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 Authors

Neil Yao
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Web Developer Visionsoft, Best Film
Hungary Hungary
MCP ASP.NET Web Client Developer

Comments and Discussions

GeneralMy vote of 2 Pin
huanghaoxz17-Dec-12 18:15
Memberhuanghaoxz17-Dec-12 18:15 
QuestionAsynchronous logging speedups? Pin
AHTOXA21-Nov-12 5:03
MemberAHTOXA21-Nov-12 5:03 
Questionif the log length is greater than 256, what happening? Pin
Taiguo11-Jan-10 14:24
MemberTaiguo11-Jan-10 14:24 
GeneralMultithread and static Pin
christoferw26-Nov-09 4:42
Memberchristoferw26-Nov-09 4:42 
Generalplease try to upload whole application rather then header or c++ Pin
Chetan Sheladiya3-Mar-09 17:41
professionalChetan Sheladiya3-Mar-09 17:41 
GeneralChanges to avoid MFC Pin
Myzhar9-Feb-09 23:22
MemberMyzhar9-Feb-09 23:22 
To avoid CString using, you can change CreateDirectories code like this:
CLogFile::CreateDirectories(LPCTSTR filename)
	char drivename[4];
	char path[MAX_PATH+1];
	char name[MAX_PATH+1];
	char ext[MAX_PATH+1];
	int per=0;
	char seps[]   = "/\\";
	char *token;

	_splitpath( filename, drivename, path, name, ext );

	sprintf( drivename, "%s\\", drivename );
	_chdir( drivename );

	token = strtok( path, seps );
	while( token != NULL )
		if( _chdir( token ) == -1 )	
			_mkdir( token );
			_chdir( token );
		token = strtok( NULL, seps );

In header file make these changes:
char    m_filename[MAX_PATH]; // CString m_filename
void    CreateDirectories(LPCTSTR filename); // CreateDirectories(CString filename);

GeneralGetting Error C2352 CLogFile::Write : Illegal call of non-static member function Pin
Timb32024-Mar-08 12:05
MemberTimb32024-Mar-08 12:05 
GeneralRe: Getting Error C2352 CLogFile::Write : Illegal call of non-static member function Pin
Neil Yao24-Mar-08 15:20
MemberNeil Yao24-Mar-08 15:20 
GeneralRe: Getting Error C2352 CLogFile::Write : Illegal call of non-static member function Pin
Timb32025-Mar-08 9:04
MemberTimb32025-Mar-08 9:04 
GeneralWorking in the Heap Pin
triplebit12-Mar-07 10:05
Membertriplebit12-Mar-07 10:05 
Questionwithout MFC? Pin
caesten22-Sep-05 1:07
Membercaesten22-Sep-05 1:07 
AnswerRe: without MFC? Pin
Neil Yao22-Sep-05 14:36
MemberNeil Yao22-Sep-05 14:36 
Generaljust good :) Pin
dhxx29-Jul-05 8:23
Memberdhxx29-Jul-05 8:23 
QuestionWhy Write() does not work? Pin
yongdong22-Apr-04 6:44
Memberyongdong22-Apr-04 6:44 
AnswerRe: Why Write() does not work? Pin
Neil Yao22-Apr-04 15:02
MemberNeil Yao22-Apr-04 15:02 
GeneralRe: Why Write() does not work? Pin
yongdong23-Apr-04 3:31
Memberyongdong23-Apr-04 3:31 
Questionable to log events such as deleting files?? Pin
Josiemaran31-Mar-04 18:54
MemberJosiemaran31-Mar-04 18:54 
AnswerRe: able to log events such as deleting files?? Pin
Neil Yao1-Apr-04 14:01
MemberNeil Yao1-Apr-04 14:01 
GeneralExcellent, just a little bug in CreateDirectories method... Pin
guit4422-Apr-03 22:12
Memberguit4422-Apr-03 22:12 
GeneralRe: Excellent, just a little bug in CreateDirectories method... Pin
Zoltan22-Apr-03 22:57
MemberZoltan22-Apr-03 22:57 
GeneralRe: Excellent, just a little bug in CreateDirectories method... Pin
rsaqib1-Feb-12 22:31
Memberrsaqib1-Feb-12 22:31 
General_DEBUG_LOG directive Pin
Mav Rossi20-Mar-03 21:50
MemberMav Rossi20-Mar-03 21:50 
GeneralRe: _DEBUG_LOG directive Pin
Anonymous20-Mar-03 22:17
MemberAnonymous20-Mar-03 22:17 
GeneralRe: _DEBUG_LOG directive, YES! :) Pin
Zoltan20-Mar-03 22:44
MemberZoltan20-Mar-03 22:44 
GeneralAn example Pin
Piccinano27-Feb-03 3:48
MemberPiccinano27-Feb-03 3:48 

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

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