00001
00002
00003
00004
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include <stdarg.h>
00009 #include <time.h>
00010
00011 static FILE *log_file = NULL;
00012
00013 static void
00014 done_log(void)
00015 {
00016 char errbuf[4096];
00017 time_t curtime = time(NULL);
00018 struct tm *loctime = localtime(&curtime);
00019 int len;
00020
00021 len = strftime(errbuf, sizeof(errbuf), "%a, %d %b %Y %H:%M:%S %z",
00022 loctime);
00023 errbuf[len] = '\0';
00024
00025 fprintf(log_file, "[%-5s %-15s %4s]: Log stopped at %s\n\n\n",
00026 "", "", "", errbuf);
00027
00028 fclose(log_file);
00029 }
00030
00031 void
00032 ersp_log(char *msg, char *file, int line, char *fmt, ...)
00033 {
00034 static char *log_files = NULL;
00035 static char *log_msg = NULL;
00036 char errbuf[4096];
00037 va_list params;
00038
00039 if (!log_file) {
00040 char *log_name;
00041 time_t curtime = time(NULL);
00042 struct tm *loctime = localtime(&curtime);
00043 int len;
00044
00045 log_files = getenv("ERSP_FILES");
00046 log_msg = getenv("ERSP_MSG");
00047 log_name = getenv("ERSP_LOG");
00048 log_file = log_name ? fopen(log_name, "ab") : stderr;
00049
00050 if (!log_file) return;
00051
00052 len = strftime(errbuf, sizeof(errbuf), "%a, %d %b %Y %H:%M:%S %z",
00053 loctime);
00054 errbuf[len] = '\0';
00055
00056 fprintf(log_file, "\n\n[%-5s %-15s %4s]: Log started at %s\n",
00057 "type", "file", "line", errbuf);
00058
00059 atexit(done_log);
00060 }
00061
00062 if (log_files && !strstr(log_files, file))
00063 return;
00064
00065 if (log_msg && !strstr(log_msg, msg))
00066 return;
00067
00068 va_start(params, fmt);
00069
00070 snprintf(errbuf, sizeof(errbuf), "[%-5s %-15s %4d]: %s",
00071 msg, file, line, fmt);
00072
00073 vfprintf(log_file, errbuf, params);
00074 fputc('\n', log_file);
00075 fflush(log_file);
00076
00077 va_end(params);
00078 }