hlswmaster/src/logging.c

54 lines
1.0 KiB
C
Raw Normal View History

2006-02-02 16:24:06 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
static FILE *log_fd = NULL;
void log_close() {
fclose(log_fd);
}
void log_open(char *logfile) {
if ((log_fd = fopen(logfile, "a" )) == NULL) {
fprintf(stderr, "log_open(\"%s\"): %s\n", logfile, strerror(errno));
exit(-1);
}
if (atexit(log_close) != 0) {
fprintf(stderr, "log_open(): atexit(): %s\n", strerror(errno));
exit(-1);
}
}
void log_print(const char *fmt, ...) {
va_list az;
time_t tzgr;
char tbuf[64];
char buffer[256];
va_start(az, fmt);
vsprintf(buffer, fmt, az);
va_end(az);
time(&tzgr);
strftime(tbuf, 64, "%b %d %H:%M:%S :", localtime(&tzgr));
if (log_fd) {
if (errno) {
fprintf(log_fd, "%s %s: %s\n", tbuf, buffer, strerror(errno));
} else {
fprintf(log_fd, "%s %s\n", tbuf, buffer);
}
} else {
if (errno) {
fprintf(stderr, "%s %s: %s\n", tbuf, buffer, strerror(errno));
} else {
fprintf(stderr, "%s %s\n", tbuf, buffer);
}
}
errno = 0;
fflush(log_fd);
}