From 860f8bda714da8724777e47829e751585b4ca288 Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Tue, 13 Apr 2004 19:08:38 +0000 Subject: ok, rework myfprintf so it uses write() and never use any file stream print functions. this way we can always know wtf is going on! also, remove some places where we were using fprintf and printf instead of myfprintf git-svn-id: https://svn.musicpd.org/mpd/trunk@734 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/myfprintf.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/myfprintf.c') diff --git a/src/myfprintf.c b/src/myfprintf.c index 588e5df2f..77c11a297 100644 --- a/src/myfprintf.c +++ b/src/myfprintf.c @@ -33,6 +33,21 @@ int myfprintf_stdLogMode = 0; FILE * myfprintf_out; FILE * myfprintf_err; +void blockingWrite(int fd, char * string) { + int len = strlen(string); + int ret; + + while(len) { + ret = write(fd,string,len); + if(ret<0) { + if(errno==EAGAIN || errno==EINTR) continue; + return; + } + len-= ret; + string+= ret; + } +} + void myfprintfStdLogMode(FILE * out, FILE * err) { myfprintf_stdLogMode = 1; myfprintf_out = out; @@ -40,31 +55,33 @@ void myfprintfStdLogMode(FILE * out, FILE * err) { } void myfprintf(FILE * fp, char * format, ... ) { + char buffer[BUFFER_LENGTH+1]; va_list arglist; int fd = fileno(fp); int fcntlret; + memset(buffer,0,BUFFER_LENGTH+1); + va_start(arglist,format); while((fcntlret=fcntl(fd,F_GETFL))==-1 && errno==EINTR); if(myfprintf_stdLogMode && (fd==1 || fd==2)) { - char str[15]; time_t t = time(NULL); if(fd==1) fp = myfprintf_out; else fp = myfprintf_err; - strftime(str,14,"%b %e %R",localtime(&t)); - fprintf(fp,"%s : ",str); - vfprintf(fp,format,arglist); + strftime(buffer,14,"%b %e %R",localtime(&t)); + blockingWrite(fd,buffer); + blockingWrite(fd," : "); + vsnprintf(buffer,BUFFER_LENGTH,format,arglist); + blockingWrite(fd,buffer); } - else if(fcntlret & O_NONBLOCK) { - char buffer[BUFFER_LENGTH+1]; + else { vsnprintf(buffer,BUFFER_LENGTH,format,arglist); - if(interfacePrintWithFD(fd,buffer)<0) { - /* not a fd from a interface */ - vfprintf(fp,format,arglist); + if((fcntlret & O_NONBLOCK) && + interfacePrintWithFD(fd,buffer)==0) + { } + else blockingWrite(fd,buffer); } - else vfprintf(fp,format,arglist); - fflush(fp); va_end(arglist); } -- cgit v1.2.3