I have write a program using err_doit function referred in APUE. This program will use this function print amount of message in a endless loop. But it always hangs up when running a couple of minutes later. I use strace command attached this process to debug it,below message are print.
......
gettimeofday({1397628460, 158883}, NULL) = 0
write(1, "\1\0\0\0", 4) = 4
futex(0xb76b0a6c, FUTEX_WAIT_PRIVATE, 2, NULL^C <unfinished ...>
root@slave:~/ci/cmake#
here are source code raise bug on,please help me,thanks very much.My program only have one thread.
#define MAXLINE 4096
/*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE] = {0};
struct timeval cur_time;
struct tm* loc_time = NULL;
int32_t len = 0;
int32_t i = 0;
gettimeofday(&cur_time,NULL);
/*it looks like hangs up at here from strace message*/
i = 1;
write(1,&i,4);
loc_time = localtime(&cur_time.tv_sec);
i = 2;
write(1,&i,4);
len += strftime(buf,MAXLINE,"[%F %T",loc_time);
len += snprintf(buf + len,MAXLINE - len,".%d]",(int)cur_time.tv_usec);
len += vsnprintf(buf + len, MAXLINE - len, fmt, ap);
if (errnoflag)
{
snprintf(buf + len, MAXLINE - len, ": %s", strerror(error));
}
i = 3;
write(1,&i,4);
strcat(buf, "\n");
write(1,buf,strlen(buf));
i = 4;
write(1,&i,4);
return;
}
void CILog_Msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
void CILog_Errno(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}