该类的作用是写入程序运行时的错误.
/******************头文件***************************/
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <stdarg.h>
class LOG
{
protected:
FILE * logfile;
bool Shutdown(void);
public:
bool Init(char * filename);
//output to log
void OutputNewline();
void OutputError(char * text, ...);
void OutputSuccess(char * text, ...);
LOG() {}
~LOG() { Shutdown(); }
};
#endif
/************实现文件*******************/
#include "LOG.h"
//initiates logfile
bool LOG::Init(char * filename)
{
if( (logfile = fopen(filename, "wb")) == NULL)
return false;
fclose(logfile);
return true;
}
//shuts down log
bool LOG::Shutdown()
{
if(logfile)
fclose(logfile);
return true;
}
//
void LOG::OutputNewline()
{
if( (logfile = fopen("Error Log.txt", "a+")) == NULL )
return;
putc('\n', logfile);
fclose(logfile);
}
void LOG::OutputError(char * text, ...)
{
va_list arg_list;
va_start(arg_list, text);
if( (logfile = fopen("Error Log.txt", "a+")) == NULL )
return;
//写入文件
fprintf(logfile, "<!>");
vfprintf(logfile, text, arg_list);
putc('\n', logfile);
//写到console
printf("<!>");
vprintf(text, arg_list);
printf("\n");
fclose(logfile);
va_end(arg_list);
}
posted on 2009-01-05 20:27
noBugnoGain 阅读(403)
评论(0) 编辑 收藏 引用 所属分类:
C++学习