关于写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实现类似功能?多谢了。=======================
...全文
490 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kugou123 2004-09-19
  • 打赏
  • 举报
回复
楼主可以找找CFile类的相关例子。
gaofg 2004-09-18
  • 打赏
  • 举报
回复
多谢了。实在抱歉,结得太晚。
pcyy 2004-04-01
  • 打赏
  • 举报
回复
学习
ymbymb 2004-04-01
  • 打赏
  • 举报
回复
定义一个全局变量
CFile logFile;

//打开文件
logFile.Open(...);


//写文件
CString sText;
sText.Format(...);
logFile.Write(sText, sText.GetLength());
...
//写文件
CString sText;
sText.Format(...);
logFile.Write(sText, sText.GetLength());
..
//关闭文件
logFile.Close();
gaofg 2004-04-01
  • 打赏
  • 举报
回复
多谢了。不过可不可以将打开文件、写文件和关闭文件分别成一个函数,这样不至于每次写文件时都重复打开和关闭。有没有可以作为全局的类似文件描述符的东西可以传递啊?
bohut 2004-04-01
  • 打赏
  • 举报
回复
void LOGERR(char *logfile, const char *fmt, ...)
{
#ifdef _MYDEBUG
va_list ap;
FILE* fpLog;
int errflag = 0;
char buf[1024];
char tmstamp[21];

if ((fpLog = fopen(logfile,"a+")) == NULL)
{
errflag = 1; /*open err*/
return;
}

memset(tmstamp,0,sizeof(tmstamp));
GetCurDateTime(tmstamp);
strcat(tmstamp," ");
strncpy(buf,tmstamp,strlen(tmstamp));

va_start(ap, fmt);
_vsnprintf(buf+strlen(tmstamp), sizeof(buf)-strlen(tmstamp), fmt, ap); /* this is safe */

if (!errflag) {/*no err*/
fputs(buf,fpLog);
fflush(fpLog);
fclose(fpLog);
}

va_end(ap);
#endif
return;
}
itmaster 2004-04-01
  • 打赏
  • 举报
回复
学习
薛定谔之死猫 2004-04-01
  • 打赏
  • 举报
回复
我是来看代码的,^_^
shilong 2004-04-01
  • 打赏
  • 举报
回复
bool WriteLog(CString filename,CString str)
{


CStdioFile file;
if (file.Open(filename,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeText)==0)
return false;
file.SeekToEnd();

CTime time=CTime::GetCurrentTime();
str="[%m/%d %H:%M:%S]"+str;
str=time.Format(str);

file.WriteString(str);
file.Close();
return true;
}

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧