中国开发网: 论坛: 程序员情感CBD: 贴子 65904
haitao
我的写日志函数也差不多,请大家指正
使用了vsprintf会不会效率低?


int commlogStrs(char withdate,...)
{

if (logfilename[0]=='\0')
return -1;

if (inlog)
return -3;
inlog=1;

FILE* f=NULL;
if (commlogHead(&f,withdate)!=0)
{ inlog=0;
return -2;
}

char* pa;
va_list marker;

va_start( marker, withdate ); /* Initialize variable arguments. */
while( 1 )
{
pa = va_arg( marker, char*);
if (pa==NULL) //这里不是太爽,必须调用者多传入一个NULL
break;
fputs(pa,f);
}
va_end( marker ); /* Reset variable arguments. */

fputs("\n",f);
fclose(f);

inlog=0;
return 0;

}


int commlogHead(FILE** f,char withdate)
//因为还有commlogStr2、commlogInt等,所以把前面提取了出来
{
*f=fopen(logfilename,"a+t");
if (*f==NULL)
return -1;


struct tm t;
struct tm *pt;
long ti;

time(&ti);
pt=localtime(&ti);
t=*pt;


if (withdate<0)
withdate=curwithdate;

if (withdate!=4 && (lastt.tm_year!=t.tm_year || lastt.tm_yday!=t.tm_yday || lastt.tm_hour!=t.tm_hour))
withdate=4;
lastt=t;

if (withdate==0)
fprintf(*f,"%.2d:%.2d|",t.tm_min,t.tm_sec);
else if (withdate==1)
fprintf(*f,"%.2d:%.2d:%.2d|",t.tm_hour,t.tm_min,t.tm_sec);
else if (withdate==2)
fprintf(*f,"%.2d %.2d:%.2d:%.2d|",t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);
else if (withdate==3)
fprintf(*f,"%.2d-%.2d %.2d:%.2d:%.2d|",t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);
else
fprintf(*f,"%.4d-%.2d-%.2d %.2d:%.2d:%.2d|",t.tm_year+1900,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);

return 0;
}
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录