remade the logging function to better deal with removed logfiles during

the execution of the tests
This commit is contained in:
Daniel Stenberg 2004-04-16 07:01:47 +00:00
parent caf37bc92e
commit bc11929395
1 changed files with 11 additions and 19 deletions

View File

@ -164,7 +164,6 @@ static const char *doc404 = "HTTP/1.1 404 Not Found\n"
#ifdef SIGPIPE #ifdef SIGPIPE
static volatile int sigpipe; static volatile int sigpipe;
#endif #endif
static FILE *logfp;
static void logmsg(const char *msg, ...) static void logmsg(const char *msg, ...)
{ {
@ -172,16 +171,21 @@ static void logmsg(const char *msg, ...)
va_list ap; va_list ap;
struct tm *curr_time = localtime(&t); struct tm *curr_time = localtime(&t);
char buffer[256]; /* possible overflow if you pass in a huge string */ char buffer[256]; /* possible overflow if you pass in a huge string */
FILE *logfp;
va_start(ap, msg); va_start(ap, msg);
vsprintf(buffer, msg, ap); vsprintf(buffer, msg, ap);
va_end(ap); va_end(ap);
fprintf(logfp, "%02d:%02d:%02d (%d) %s\n", logfp = fopen(DEFAULT_LOGFILE, "a");
fprintf(logfp?logfp:stderr, /* write to stderr if the logfile doesn't open */
"%02d:%02d:%02d (%d) %s\n",
curr_time->tm_hour, curr_time->tm_hour,
curr_time->tm_min, curr_time->tm_min,
curr_time->tm_sec, (int)getpid(), buffer); curr_time->tm_sec, (int)getpid(), buffer);
fflush(logfp); if(logfp)
fclose(logfp);
} }
@ -643,8 +647,7 @@ static void win32_init(void)
if (err != 0) { if (err != 0) {
perror("Winsock init failed"); perror("Winsock init failed");
fprintf(logfp, "Error initializing winsock -- aborting\n"); logmsg("Error initializing winsock -- aborting\n");
fclose(logfp);
exit(1); exit(1);
} }
@ -653,8 +656,7 @@ static void win32_init(void)
WSACleanup(); WSACleanup();
perror("Winsock init failed"); perror("Winsock init failed");
fprintf(logfp, "No suitable winsock.dll found -- aborting\n"); logmsg("No suitable winsock.dll found -- aborting\n");
fclose(logfp);
exit(1); exit(1);
} }
} }
@ -669,7 +671,6 @@ int main(int argc, char *argv[])
struct sockaddr_in me; struct sockaddr_in me;
int sock, msgsock, flag; int sock, msgsock, flag;
unsigned short port = DEFAULT_PORT; unsigned short port = DEFAULT_PORT;
const char *logfile = DEFAULT_LOGFILE;
FILE *pidfile; FILE *pidfile;
struct httprequest req; struct httprequest req;
@ -681,12 +682,6 @@ int main(int argc, char *argv[])
} }
} }
logfp = fopen(logfile, "a");
if (!logfp) {
perror(logfile);
exit(1);
}
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
win32_init(); win32_init();
#endif #endif
@ -703,8 +698,7 @@ int main(int argc, char *argv[])
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { if (sock < 0) {
perror("opening stream socket"); perror("opening stream socket");
fprintf(logfp, "Error opening socket -- aborting\n"); logmsg("Error opening socket -- aborting\n");
fclose(logfp);
exit(1); exit(1);
} }
@ -720,8 +714,7 @@ int main(int argc, char *argv[])
me.sin_port = htons(port); me.sin_port = htons(port);
if (bind(sock, (struct sockaddr *) &me, sizeof me) < 0) { if (bind(sock, (struct sockaddr *) &me, sizeof me) < 0) {
perror("binding stream socket"); perror("binding stream socket");
fprintf(logfp, "Error binding socket -- aborting\n"); logmsg("Error binding socket -- aborting\n");
fclose(logfp);
exit(1); exit(1);
} }
@ -782,7 +775,6 @@ int main(int argc, char *argv[])
} }
sclose(sock); sclose(sock);
fclose(logfp);
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__) #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
win32_cleanup(); win32_cleanup();