65.9K
CodeProject 正在变化。 阅读更多。
Home

CSAStatusLog - 状态记录器

1999年11月17日

CPOL
viewsIcon

71676

downloadIcon

1805

一个非常简单的文本记录器,允许你使用 printf 类型的格式化,并自动添加应用程序名称和日期戳。

CSAStatusLog 是一个非常简单的文本记录器。

这个小类的最酷之处在于,就像 printf 和 TRACE 一样,它允许你使用字符串格式文本:"%2.4f","%s" 等。它会自动将应用程序名称和时间戳(两者都是可选的)添加到每一行。这省去了每次需要记录内容时进行 CString::Formatsprintf 的麻烦。我们 Smaller Animals Software 的团队非常喜欢它。

示例输出

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
接口很简单
// 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);

像这样使用它

// 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);
...

就这样。玩得开心。

© . All rights reserved.