Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++

CSAStatusLog - status logger

Rate me:
Please Sign up or sign in to vote.
4.38/5 (8 votes)
17 Nov 1999CPOL 70K   1.8K   30   5
A very simple text logger that allows you to use printf-type formatting, with automatic application name and date stamping

CSAStatusLog is a very simple text logger.

The coolest part of this little class is that, much like printf and TRACE, it allows you to use string format text : "%2.4f", "%s", etc.. It will automatically add the application name and timestamp (both optional) to each line. That saves you the trouble of doing CString::Formats or sprintfs each time you need to log something. We love it here at Smaller Animals Software.

Sample output :

CStatusLogDemo : 11/18/1999 16:47:43 : Howdy, stranger!
CStatusLogDemo : 11/18/1999 16:47:43 : Did you know that 15 * 32.43 = 486.51 ?
CStatusLogDemo : 11/18/1999 16:47:43 : Ain't that great?
CStatusLogDemo : 11/18/1999 16:47:43 : I'm at : 0x4069b8
CStatusLogDemo : 11/18/1999 16:47:43 : Is anyone else here?
CStatusLogDemo : 11/18/1999 16:47:43 : Howdy! CCStatusLogDemoDlg here. 
CStatusLogDemo : 11/18/1999 16:47:43 : My address is 0x12fe78
CStatusLogDemo : 11/18/1999 16:47:45 : CCStatusLogDemoDlg, sayin "Good bye" 
CStatusLogDemo : 11/18/1999 16:47:45 : (cancel)
CStatusLogDemo : 11/18/1999 16:47:45 : Bye bye
The interface is simple :
C++
// set the base file name ("Status.txt", for example)
void	Init(const char *pOutputFilename);

// output text, just like 
// TRACE or printf ( log.StatusOut("%d %s", iData, lpsData); )
BOOL	StatusOut(const char* fmt, ...);

// turn it on or off
void	Enable(BOOL bEnable);

// timestamp each line?
void    PrintTime(BOOL b);

// print the application name?
void    PrintAppName(BOOL b);

// override the default app name, which is the name the EXE (minus the ".exe")
void    SetAppName(const char *pName);

Use it like this

C++
// instantiate one of these somewhere.
// we usually do a single logger for the entire app.
CSAStatusLog log;
....

// start it up
log.Init("Status.txt");
log.Enable(TRUE);
log.PrintTime(TRUE);
log.PrintAppName(TRUE);

....
log.StatusOut("%s, %4.2f complete", pProcedureName, fPercentComplete);
....
log.StatusOut("The thingy returned %d but I was expecting %d", 
              iReturn, iExpected);
....
log.StatusOut("All hope is lost. %s has asked for %d %% of %d.", 
               lpsDummy, iPercent, iTotal);
...

That's it. Have fun.

License

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


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
QuestionFilename and line number ? Pin
Jonathan de Halleux25-Mar-02 2:20
Jonathan de Halleux25-Mar-02 2:20 
AnswerRe: Filename and line number ? Pin
Carlos Antollini25-Mar-02 2:50
Carlos Antollini25-Mar-02 2:50 
You need to use the function StatusOut for make that. The function will don't that for you but you can use that function, informing the Filename and Line... Wink | ;)


Best Regards...

Carlos Antollini.
Sonork ID 100.10529 cantollini

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.