00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <errno.h>
00069 #include <time.h>
00070 #include <stdarg.h>
00071 #include <string.h>
00072
00073 #include "gwlib.h"
00074
00075 static FILE *file = NULL;
00076 static char filename[FILENAME_MAX + 1];
00077 static int use_localtime;
00078 static int markers = 1;
00079
00080
00081
00082
00083 static List *writers = NULL;
00084
00085 void alog_reopen(void)
00086 {
00087 if (file == NULL)
00088 return;
00089
00090 if (markers)
00091 alog("Log ends");
00092
00093 gwlist_lock(writers);
00094
00095 gwlist_consume(writers);
00096
00097 fclose(file);
00098 file = fopen(filename, "a");
00099
00100 gwlist_unlock(writers);
00101
00102 if (file == NULL) {
00103 error(errno, "Couldn't re-open access logfile `%s'.", filename);
00104 }
00105 else if (markers) {
00106 alog("Log begins");
00107 }
00108 }
00109
00110
00111 void alog_close(void)
00112 {
00113
00114 if (file != NULL) {
00115 if (markers)
00116 alog("Log ends");
00117 gwlist_lock(writers);
00118
00119 gwlist_consume(writers);
00120 fclose(file);
00121 file = NULL;
00122 gwlist_unlock(writers);
00123 gwlist_destroy(writers, NULL);
00124 writers = NULL;
00125 }
00126 }
00127
00128
00129 void alog_open(char *fname, int use_localtm, int use_markers)
00130 {
00131 FILE *f;
00132
00133 use_localtime = use_localtm;
00134 markers = use_markers;
00135
00136 if (file != NULL) {
00137 warning(0, "Opening an already opened access log");
00138 alog_close();
00139 }
00140 if (strlen(fname) > FILENAME_MAX) {
00141 error(0, "Access Log filename too long: `%s', cannot open.", fname);
00142 return;
00143 }
00144
00145 if (writers == NULL)
00146 writers = gwlist_create();
00147
00148 f = fopen(fname, "a");
00149 if (f == NULL) {
00150 error(errno, "Couldn't open logfile `%s'.", fname);
00151 return;
00152 }
00153 file = f;
00154 strcpy(filename, fname);
00155 info(0, "Started access logfile `%s'.", filename);
00156 if (markers)
00157 alog("Log begins");
00158 }
00159
00160
00161 void alog_use_localtime(void)
00162 {
00163 use_localtime = 1;
00164 }
00165
00166
00167 void alog_use_gmtime(void)
00168 {
00169 use_localtime = 0;
00170 }
00171
00172
00173 #define FORMAT_SIZE (10*1024)
00174 static void format(char *buf, const char *fmt)
00175 {
00176 time_t t;
00177 struct tm tm;
00178 char *p, prefix[1024];
00179
00180 p = prefix;
00181
00182 if (markers) {
00183 time(&t);
00184 if (use_localtime)
00185 tm = gw_localtime(t);
00186 else
00187 tm = gw_gmtime(t);
00188
00189 sprintf(p, "%04d-%02d-%02d %02d:%02d:%02d ",
00190 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
00191 tm.tm_hour, tm.tm_min, tm.tm_sec);
00192 } else {
00193 *p = '\0';
00194 }
00195
00196 if (strlen(prefix) + strlen(fmt) > FORMAT_SIZE / 2) {
00197 sprintf(buf, "%s <OUTPUT message too long>\n", prefix);
00198 return;
00199 }
00200 sprintf(buf, "%s%s\n", prefix, fmt);
00201 }
00202
00203
00204
00205
00206 void alog(const char *fmt, ...)
00207 {
00208 char buf[FORMAT_SIZE + 1];
00209 va_list args;
00210
00211 if (file == NULL)
00212 return;
00213
00214 format(buf, fmt);
00215 va_start(args, fmt);
00216
00217 gwlist_lock(writers);
00218 gwlist_add_producer(writers);
00219 gwlist_unlock(writers);
00220
00221 vfprintf(file, buf, args);
00222 fflush(file);
00223
00224 gwlist_remove_producer(writers);
00225
00226 va_end(args);
00227 }
00228
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.