示例代码:
/*
author : kj
time : 2013-12-06 21:36
instruction:
get the num of the current seconds
*/
int get_current_time_second(void)
{
struct tm *tmnow;
struct timeval tv;
gettimeofday(&tv,NULL);
tmnow = localtime(&tv.tv_sec);
return tv.tv_sec;
}
int get_current_time_to_jms(char *buf)
{
struct tm *tmnow;
struct timeval tv;
gettimeofday(&tv,NULL);
tmnow = localtime(&tv.tv_sec);
sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",\
tmnow->tm_year+1900, tmnow->tm_mon+1, tmnow->tm_mday,tmnow->tm_hour,\
tmnow->tm_min, tmnow->tm_sec);
return tv.tv_sec;
}
/*
可以整体设计一个开关:
1、可以使用Makefile定义宏
2、可以使用全局标记量
3、可以使用标记量写入文件,走配置文件进行log开关
4、方便对log信息的筛选,可以定义多个personal_print(例如:每一类或者模块定义一个printf)
*/
void personal_print(const char *format, ...)
{
char now_time[32];
char s[1024];
char content[1024];
char *ptr = content;
struct tm *tmnow;
struct timeval tv;
bzero(content,1024);
va_list arg;
va_start (arg, format);
vsprintf (s, format, arg);
va_end (arg);
gettimeofday(&tv,NULL);
tmnow = localtime(&tv.tv_sec);
sprintf(now_time,"%04d/%02d/%02d %02d:%02d:%02d:%06ld ",\
tmnow->tm_year+1900, tmnow->tm_mon+1, tmnow->tm_mday,tmnow->tm_hour,\
tmnow->tm_min, tmnow->tm_sec, tv.tv_usec);
sprintf(content,"%s %s",now_time,s);
printf("%s",content);
//save_log2_file(content);
}
call:
personal_print("%d %s %s\n",len,info->jms_ip,info->jms_port);