linux c写入日志文件系统时间的问题
charpcaDispMsg[512];time_tt;time(&t);structtm*tp=localtime(&t);FILE*fp=fopen("/home/y...
char pcaDispMsg[512];
time_t t;
time(&t);
struct tm *tp = localtime(&t);
FILE *fp = fopen("/home/yz/calculator/log/calculator.log", "a+");
va_list arg_ptr;
va_start(arg_ptr, format);
vsnprintf(pcaDispMsg,512,format,arg_ptr);
sprintf(pcaDispMsg, "%04d-%02d-%02d %02d:%02d:%02d", fp, tp->tm_year
+ 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
vfprintf(fp, format, arg_ptr);
va_end(arg_ptr);
fclose(fp);
return 0;
这是我的程序,没有报错,但是在LOG文件夹下的文件不显示系统时间啊,大家帮帮忙,谢谢了!~ 展开
time_t t;
time(&t);
struct tm *tp = localtime(&t);
FILE *fp = fopen("/home/yz/calculator/log/calculator.log", "a+");
va_list arg_ptr;
va_start(arg_ptr, format);
vsnprintf(pcaDispMsg,512,format,arg_ptr);
sprintf(pcaDispMsg, "%04d-%02d-%02d %02d:%02d:%02d", fp, tp->tm_year
+ 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
vfprintf(fp, format, arg_ptr);
va_end(arg_ptr);
fclose(fp);
return 0;
这是我的程序,没有报错,但是在LOG文件夹下的文件不显示系统时间啊,大家帮帮忙,谢谢了!~ 展开
1个回答
展开全部
用我写的吧 实测过了的 可以直接使用
void WriteSysLog(char *str)
{
char buf[512];
long MAXLEN = 10*1024*1024;//10MB
time_t timep;
FILE *fp = NULL;
struct tm *p;
time(&timep);
p = localtime(&timep);
memset(buf,0,sizeof(buf));
sprintf(buf,"%d-%d-%d %d:%d:%d : ",(1900+p->tm_year),(1+p->tm_mon),\
p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec); //星期p->tm_wday
strcat(buf,str);
strcat(buf,"\r\n");
fp = fopen("./syslog.log","r");
if(fp==NULL)
{
fp = fopen("./syslog.log","w+");
}
else
{
fseek(fp,0,2);
if(ftell(fp) >= MAXLEN)
{
fclose(fp);
fp = fopen("./syslog.log","w+");
//大于10MB则清空原日志文件
}
else
{
fclose(fp);
fp = fopen("./syslog.log","a");
}
}
fwrite(buf,1,strlen(buf),fp);
fflush(fp);
fsync(fileno(fp));
fclose(fp);
}
void WriteSysLog(char *str)
{
char buf[512];
long MAXLEN = 10*1024*1024;//10MB
time_t timep;
FILE *fp = NULL;
struct tm *p;
time(&timep);
p = localtime(&timep);
memset(buf,0,sizeof(buf));
sprintf(buf,"%d-%d-%d %d:%d:%d : ",(1900+p->tm_year),(1+p->tm_mon),\
p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec); //星期p->tm_wday
strcat(buf,str);
strcat(buf,"\r\n");
fp = fopen("./syslog.log","r");
if(fp==NULL)
{
fp = fopen("./syslog.log","w+");
}
else
{
fseek(fp,0,2);
if(ftell(fp) >= MAXLEN)
{
fclose(fp);
fp = fopen("./syslog.log","w+");
//大于10MB则清空原日志文件
}
else
{
fclose(fp);
fp = fopen("./syslog.log","a");
}
}
fwrite(buf,1,strlen(buf),fp);
fflush(fp);
fsync(fileno(fp));
fclose(fp);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询