写了个log类。但是越写越觉得不对劲,越写发现越需要扩充。。
最大的问题是发现封装成类后反而别扭- -主要应该还是自己功底不够吧。
最后用C语言,InitLog(),WriteLog(),CloseLog(). 三个函数搞定。暂且满足程序的需要,虽然CloseLog()这个函数也不是必须地,不过加上能让跟踪出的log信息更完整吧。
//
// wde_log.h
//
#ifndef WDE_LOG_H
#define WDE_LOG_H
#include < windows.h >
#include < cstdio >
using namespace std;
#define LOGFILENAME “log.txt”
void OpenLog();
void GameLog( const char * p_Message, bool p_bNeedTime = true );
void CloseLog();
#endif
//
// wde_log.cpp
//
#include “wde_Log.h”
void OpenLog()
{
FILE * logfile;
logfile = fopen(LOGFILENAME, “w”);
if ( ! logfile)
{
MessageBox(NULL, “无法创建日志文件,程序终止”, “ERROR”,MB_OK);
return ;
}
fputs(”打开应用程序日志文件/n”,logfile);
fputs(”——————————————————————— - /n”,logfile);
}
void GameLog( const char * p_Message, bool p_bNeedTime /* = true */ )
{
FILE * writelog;
writelog = fopen(LOGFILENAME, “a + ”);
if ( ! writelog)
{
MessageBox(NULL,”无法写入日志文件,程序终止”, “ERROR”, MB_OK);
return ;
}
static SYSTEMTIME CurrentTime;
GetLocalTime( & CurrentTime);
if (p_bNeedTime)
{
fprintf(writelog,” % 2d /% 2d /% 4d “, CurrentTime.wMonth, CurrentTime.wDay, CurrentTime.wYear);
fprintf(writelog,” % 2d: % 2d: % 2d “,CurrentTime.wHour, CurrentTime.wMinute, CurrentTime.wSecond);
}
fputs(p_Message, writelog);
}
void CloseLog()
{
FILE * closefile;
closefile = fopen(LOGFILENAME, “a + ”);
if ( ! closefile)
{
MessageBox(NULL,”无法写入日志文件,程序终止”, “ERROR”, MB_OK);
return ;
}
fputs(”——————————————————————— - /n”,closefile);
fputs(”关闭应用程序日志文件/n”,closefile);
}
//
// wde_log.h
//
#ifndef WDE_LOG_H
#define WDE_LOG_H
#include < windows.h >
#include < cstdio >
using namespace std;
#define LOGFILENAME “log.txt”
void OpenLog();
void GameLog( const char * p_Message, bool p_bNeedTime = true );
void CloseLog();
#endif
//
// wde_log.cpp
//
#include “wde_Log.h”
void OpenLog()
{
FILE * logfile;
logfile = fopen(LOGFILENAME, “w”);
if ( ! logfile)
{
MessageBox(NULL, “无法创建日志文件,程序终止”, “ERROR”,MB_OK);
return ;
}
fputs(”打开应用程序日志文件/n”,logfile);
fputs(”——————————————————————— - /n”,logfile);
}
void GameLog( const char * p_Message, bool p_bNeedTime /* = true */ )
{
FILE * writelog;
writelog = fopen(LOGFILENAME, “a + ”);
if ( ! writelog)
{
MessageBox(NULL,”无法写入日志文件,程序终止”, “ERROR”, MB_OK);
return ;
}
static SYSTEMTIME CurrentTime;
GetLocalTime( & CurrentTime);
if (p_bNeedTime)
{
fprintf(writelog,” % 2d /% 2d /% 4d “, CurrentTime.wMonth, CurrentTime.wDay, CurrentTime.wYear);
fprintf(writelog,” % 2d: % 2d: % 2d “,CurrentTime.wHour, CurrentTime.wMinute, CurrentTime.wSecond);
}
fputs(p_Message, writelog);
}
void CloseLog()
{
FILE * closefile;
closefile = fopen(LOGFILENAME, “a + ”);
if ( ! closefile)
{
MessageBox(NULL,”无法写入日志文件,程序终止”, “ERROR”, MB_OK);
return ;
}
fputs(”——————————————————————— - /n”,closefile);
fputs(”关闭应用程序日志文件/n”,closefile);
}
//