Click here to Skip to main content
Licence CPOL
First Posted 10 Feb 2009
Views 9,348
Downloads 199
Bookmarked 11 times

A Simple LogFile

By , , | 10 Feb 2009 | Article
A simple logfile to record runtime information

Introduction

Sometimes, we need to record something after we give the software to our customers, so when something bad happens, we can find it out without the debug environment. Sometimes, 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 on The Code Project and elsewhere. This one is very simple, compared to some of the others.

Using the Code

Code is very simple. All you need is to create you CLogFile variable, open the log file and then write in it:

CFileLog log;
log.OpenFile( "myLog.log" );
log.Write( "La variable a vale: %d", a );

Features

  • Does not depend on MFC or ATL
  • File name could use absolute path or just the name, in which case the file will be created at the same place with the binary module, no concern with current directory, which always brings me trouble.
  • Every log line has a time stamp attached, with million seconds.
  • Uses printf like format to write log lines
  • Multi thread safe 

Code Example

With this code, you can have a log folder for every different year and month.

CString name;
SYSTEMTIME systime;

GetLocalTime(&systime);

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

Remember: File name does not want ".\" or "\" at the beginning if you want to use relative path to application folder.

History

  • 26 Jun 2002 - Initial revision
  • 20 Mar 2003 - updated by Zoltan
  • 10 Feb 2009 - update by Myzhar
    • Modified code to avoid using CString MFC class in Zoltan's CreateDirectories function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Neil Yao



China China

Member

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

Member

MCP ASP.NET Web Client Developer

Myzhar

Software Developer (Senior)
Rekno srl
Italy Italy

Member

Follow on Twitter Follow on Twitter
Walter Lucetti, Myzhar over the Internet, is an Informatic Engineer specialized in Robotics, Computer vision and Machine Learning

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberJepy3:24 21 Feb '09  
For simplicity, I like things which take few minutes to understand and use.
This one is easy to modify to suit a specific debug need. It would be good to have more tools like this.
Thank you guys!
Questiondoes it have memory leaking? Pinmemberftai21:08 17 Feb '09  
AnswerRe: does it have memory leaking? PinmemberMyzhar0:42 18 Feb '09  
GeneralRe: does it have memory leaking? Pinmemberftai18:24 18 Feb '09  
GeneralMy vote of 1 PinmemberMember 223821820:06 17 Feb '09  
GeneralRe: My vote of 1 PinmemberMyzhar0:44 18 Feb '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 10 Feb 2009
Article Copyright 2009 by Neil Yao, Zoltan, Myzhar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid