关于写log日志文件
gaofg 2004-04-01 02:19:11 我初学VC,如下一段程序是本人以前在c中用来写log信息以方便调试之用。现在需要在VC中用MFC的方式来实现写log的功能。真诚请教各位高手予以指点,本人感激不尽。
#include "common.h"
#include "prototypes.h"
static FILE *outfile=NULL; /* Logging to file disabled by default */
void log_open(void) {
int fd;
fd=open(LOG_FILE, O_CREAT|O_WRONLY|O_APPEND, 0640);
if(fd>=0) { /* file opened or created */
fcntl(fd, F_SETFD, FD_CLOEXEC);
outfile=fdopen(fd, "a");
if(outfile)
return; /* no need to setup syslog */
}
}
void log_close(void) {
if(outfile) {
fclose(outfile);
return;
}
}
void log(int level, const char *format, ...) {
va_list arglist;
char text[STRLEN], timestamped[STRLEN];
FILE *out;
time_t gmt;
struct tm *timeptr;
va_start(arglist, format);
#ifdef HAVE_VSNPRINTF
vsnprintf(text, STRLEN, format, arglist);
#else
vsprintf(text, format, arglist);
#endif
va_end(arglist);
out=outfile?outfile:stderr;
time(&gmt);
timeptr=localtime(&gmt);
#ifdef HAVE_SNPRINTF
snprintf(timestamped, STRLEN,
#else
sprintf(timestamped,
#endif
"%04d.%02d.%02d %02d:%02d:%02d LOG%d[%lu:%lu]: %s",
timeptr->tm_year+1900, timeptr->tm_mon+1, timeptr->tm_mday,
timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
level, (unsigned long)getpid(), (unsigned long)pthread_self(), text);
fprintf(out, "%s\n", timestamped);
fflush(out);
}
void log_raw(const char *format, ...) {
va_list arglist;
char text[STRLEN];
FILE *out;
va_start(arglist, format);
#ifdef HAVE_VSNPRINTF
vsnprintf(text, STRLEN, format, arglist);
#else
vsprintf(text, format, arglist);
#endif
va_end(arglist);
out=outfile?outfile:stderr;
fprintf(out, "%s\n", text);
fflush(out);
}
void log_data(const char *buf){
FILE* out;
out=outfile?outfile:stderr;
fprintf(out,"%s\n",buf);
fflush(out);
}
void log_error(int level, int error, char *txt) { /* generic error logger */
log(level, "%s: %s (%d)", txt, strerror(error), error);
}
======================如何用mfc实现类似功能?多谢了。=======================