Click here to Skip to main content
13,795,928 members
Click here to Skip to main content
Add your own
alternative version

Stats

221.6K views
9.1K downloads
73 bookmarked
Posted 25 Jun 2002
Licenced

A Simple LogFile

, , 19 Mar 2003
Rate this:
Please Sign up or sign in to vote.
A simple logfile to record runtime information

Introduction

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.

Features

  • 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;
SYSTEMTIME systime;

GetLocalTime(&systime);

name.Format("%i\\%02i\\log_%02i%02i%02i.txt",
        systime.wYear,
        systime.wMonth,
        systime.wYear,
        systime.wMonth,
        systime.wDay);
m_log.ChangeFile(name);

Features

  • 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

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

Share

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 | :)

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

You may also be interested in...

Comments and Discussions

 
Generalftell is '0' Pin
Jiminy16-Sep-02 22:52
memberJiminy16-Sep-02 22:52 
QuestionWhy, why, why, why, why ??? Pin
Christian Graus1-Jul-02 16:51
protectorChristian Graus1-Jul-02 16:51 
AnswerRe: Why, why, why, why, why ??? Pin
Rama Krishna Vavilala1-Jul-02 16:59
memberRama Krishna Vavilala1-Jul-02 16:59 
GeneralRe: Why, why, why, why, why ??? Pin
Christian Graus1-Jul-02 17:02
protectorChristian Graus1-Jul-02 17:02 
AnswerRe: Why, why, why, why, why ??? Pin
Neil Yao1-Jul-02 17:22
memberNeil Yao1-Jul-02 17:22 
GeneralRe: Why, why, why, why, why ??? Pin
Christian Graus1-Jul-02 17:27
protectorChristian Graus1-Jul-02 17:27 
GeneralRe: Why, why, why, why, why ??? Pin
Rama Krishna Vavilala1-Jul-02 17:39
memberRama Krishna Vavilala1-Jul-02 17:39 
GeneralRe: Why, why, why, why, why ??? Pin
Christian Graus1-Jul-02 17:49
protectorChristian Graus1-Jul-02 17:49 
GeneralRe: Why, why, why, why, why ??? Pin
Neil Yao1-Jul-02 17:43
memberNeil Yao1-Jul-02 17:43 
GeneralRe: Why, why, why, why, why ??? Pin
Christian Graus1-Jul-02 17:51
protectorChristian Graus1-Jul-02 17:51 
GeneralRe: Why, why, why, why, why ??? Pin
Dimiter Andonov16-Sep-02 2:08
memberDimiter Andonov16-Sep-02 2:08 
GeneralRe: Why, why, why, why, why ??? Pin
Christian Graus16-Sep-02 3:01
protectorChristian Graus16-Sep-02 3:01 
GeneralRe: Why, why, why, why, why ??? Pin
Brian Delahunty17-Sep-02 11:34
memberBrian Delahunty17-Sep-02 11:34 
AnswerRe: Why, why, why, why, why ??? Pin
Brian Delahunty17-Sep-02 11:31
memberBrian Delahunty17-Sep-02 11:31 
GeneralSImple But, Excellent.. Pin
mudummy1-Jul-02 16:34
membermudummy1-Jul-02 16:34 
Generalgood Pin
lucy27-Jun-02 10:31
memberlucy27-Jun-02 10:31 
GeneralV Good Pin
Brian Delahunty26-Jun-02 6:30
memberBrian Delahunty26-Jun-02 6:30 
GeneralExcellent Pin
26-Jun-02 5:04
suss26-Jun-02 5:04 
GeneralRe: Excellent Pin
Roshmon27-Jun-02 2:00
memberRoshmon27-Jun-02 2:00 
GeneralI like it Pin
Nish Nishant26-Jun-02 0:54
sitebuilderNish Nishant26-Jun-02 0:54 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.181207.3 | Last Updated 20 Mar 2003
Article Copyright 2002 by Neil Yao, Zoltan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid