aboutsummaryrefslogtreecommitdiffstats
path: root/src/myfprintf.c
diff options
context:
space:
mode:
authorWarren Dukes <warren.dukes@gmail.com>2004-04-13 19:08:38 +0000
committerWarren Dukes <warren.dukes@gmail.com>2004-04-13 19:08:38 +0000
commit860f8bda714da8724777e47829e751585b4ca288 (patch)
tree293daaf8f52314a9fe2b63fb7bb75c54909e42bc /src/myfprintf.c
parent5a50fa7147be049212274534ccfbf7ed14708070 (diff)
downloadmpd-860f8bda714da8724777e47829e751585b4ca288.tar.gz
mpd-860f8bda714da8724777e47829e751585b4ca288.tar.xz
mpd-860f8bda714da8724777e47829e751585b4ca288.zip
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
Diffstat (limited to '')
-rw-r--r--src/myfprintf.c39
1 files changed, 28 insertions, 11 deletions
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);
}