Download demo project - 15 Kb
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 :
void Init(const char *pOutputFilename);
BOOL StatusOut(const char* fmt, ...);
void Enable(BOOL bEnable);
void PrintTime(BOOL b);
void PrintAppName(BOOL b);
void SetAppName(const char *pName);
Use it like this
CSAStatusLog log;
....
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh) | FirstPrevNext |
|
 |
|
 |
Nice work 
But i found a way to handle the buffer more dynamic for large buffersize support. Just Modify the StatusOut function:
BOOL CSAStatusLog::StatusOut(const char* fmt, ...) { ...
try { va_list argptr; m_tBuf = new char[buffersize+1]; int m = 1; va_start(argptr, fmt); while (_vsnprintf(m_tBuf, buffersize, fmt, argptr) < 0) { delete [] m_tBuf; buffersize = ++m * 255; m_tBuf = new char[buffersize+1]; va_end(argptr); va_start(argptr, fmt); } va_end(argptr); } catch (...) { m_tBuf[0] = 0; }
...
if (m_tBuf) { delete [] m_tBuf; m_tBuf = 0; } LeaveCriticalSection(&m_crit);
return bOK; }
And modify the header file from:
enum {TBUF_SIZE = 3000}; char m_tBuf[TBUF_SIZE];
to:
// enum {TBUF_SIZE = 3000}; char *m_tBuf;
and add to the constructor this:
CSAStatusLog::CSAStatusLog() { m_tBuf = 0; ... }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This class is great, but a suggestion is to add append support, since you might not always wish to overwrite the logs it generates.
Just modify the Init function header to this:
void Init(const char *pOutputFilename, bool bAppend = false);
... and change this (in the Init function):
remove(m_csFileName);
... to this:
if (!bAppend) { remove(m_csFileName); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Could it be possible to add filename and line number in log ?
Jonathan de Halleux, Belgium.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
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... 
Best Regards...
Carlos Antollini. Sonork ID 100.10529 cantollini
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin