/* * Logging * * * Bug reports should be sent to * * harald@os2point.ping.de * harald@haport.sesam.com * Fido: 2:2448/434 * */ /* * This module contains routines to create and fill the logfile */ #include #include #include #include #include #include #include static FILE *fp = NULL; static int flushmode = 0; /************************************************************************/ /* lopen */ /* */ /* Open logfile. Returns true on success, 0 otherwise. */ /************************************************************************/ int lopen(char *filename) { if((fp = fopen(filename, "at")) != NULL) { fputc('\n', fp); return(1); } fprintf(stderr, "%s", _strerror(filename)); return(0); } /************************************************************************/ /* lclose */ /* */ /* Close logfile. */ /************************************************************************/ void lclose(void) { if(fp) fclose(fp); } /************************************************************************/ /* lflush */ /* */ /* Set or reset flush mode. */ /************************************************************************/ void lflush(int enable) { flushmode = enable; } /************************************************************************/ /* lprintf */ /* */ /* Output a line to the logfile. Works like printf. Inserts current */ /* date and time. */ /************************************************************************/ void _cdecl lprintf(char *format,...) { va_list marker; FILE *stream = stderr; char *cp = format; /* * Remove linefeeds */ while((cp = strchr(cp, '\n')) != NULL) *cp = ' '; /* * If our output goes to a file, then we insert date * and time in front of the line. */ if(fp) { time_t now = time(NULL); struct tm *snow = localtime(&now); fprintf(fp, "%02u/%02u-%02u:%02u ", snow->tm_mon + 1, snow->tm_mday, snow->tm_hour, snow->tm_min); stream = fp; } /* * Print the message */ va_start(marker, format); vfprintf(stream, format, marker); va_end(marker); fputc('\n', stream); if(flushmode) fflush(stream); } /************************************************************************/ /* lperror */ /* */ /* Print an error message to the logfile. Works like perror. */ /************************************************************************/ void lperror(const char *string) { char *emsg = strdup(_strerror(string)); char *cp = strchr(emsg, '\n'); if(cp) *cp = '\0'; lprintf("%s", emsg); free(emsg); }