Tue, 27 Dec 2016 18:42:36 +0100
ucx update
28 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
28 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
29 | #ifdef __gnu_linux__ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
30 | #define _GNU_SOURCE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
31 | #endif |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
32 | |
28 | 33 | #include <stdio.h> |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | #include <unistd.h> | |
37 | #include <aio.h> | |
38 | #include <time.h> | |
39 | ||
40 | #include "log.h" | |
41 | #include "../util/strbuf.h" | |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
28
diff
changeset
|
42 | #include "../util/io.h" |
77 | 43 | #include "../util/atomic.h" |
28 | 44 | |
91 | 45 | #include <ucx/map.h> |
46 | ||
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
47 | static int is_initialized = 0; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
48 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
49 | static int log_file_fd; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
50 | static int log_level = 0; |
28 | 51 | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
52 | WSBool main_is_daemon(void); |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
53 | |
28 | 54 | /* |
55 | * if the log file is uninitialized, output is written to the ui_buffer | |
56 | */ | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
57 | static sbuf_t *ui_buffer = NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
58 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
59 | /* |
77 | 60 | * access log file map |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
61 | */ |
77 | 62 | static UcxMap *access_log_files; // map of LogFile* |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
63 | |
28 | 64 | |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
65 | static char *log_date_month[] = { |
28 | 66 | "Jan", |
67 | "Feb", | |
68 | "Mar", | |
69 | "Apr", | |
70 | "May", | |
71 | "Jun", | |
72 | "Jul", | |
73 | "Aug", | |
74 | "Sep", | |
75 | "Oct", | |
76 | "Nov", | |
77 | "Dec" | |
78 | }; | |
79 | ||
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
80 | static char *log_levels[] = { |
28 | 81 | "warning", |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
82 | "config", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
83 | "security", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
84 | "failure", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
85 | "catastrophe", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
86 | "info", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
87 | "verbose" |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
88 | }; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
89 | |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
90 | static int can_log[] = { |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
91 | 1, // warning |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
92 | 1, // config |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
93 | 1, // security |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
94 | 1, // failure |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
95 | 1, // catastrophe |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
96 | 1, // info |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
97 | 0 // verbose |
28 | 98 | }; |
99 | ||
100 | int init_log_file(LogConfig *cfg) { | |
101 | if(is_initialized) { | |
102 | return 0; | |
103 | } | |
104 | ||
105 | /* open the log file */ | |
106 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; | |
107 | log_file_fd = open(cfg->file, O_WRONLY | O_CREAT | O_APPEND, mode); | |
108 | if(log_file_fd == -1) { | |
109 | return -1; | |
110 | } | |
111 | ||
112 | if(!strcmp(cfg->level, "ERROR")) { | |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
113 | can_log[LOG_WARN] = 0; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
114 | can_log[LOG_INFORM] = 0; |
28 | 115 | } else if(!strcmp(cfg->level, "WARNING")) { |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
116 | can_log[LOG_INFORM] = 0; |
28 | 117 | } else if(!strcmp(cfg->level, "INFO")) { |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
118 | |
28 | 119 | } |
120 | ||
121 | if(cfg->log_stdout) { | |
122 | // TODO | |
123 | } | |
124 | if(cfg->log_stderr) { | |
125 | // TODO | |
126 | } | |
127 | ||
128 | ||
129 | is_initialized = 1; | |
130 | ||
131 | /* if ui_buffer is not NULL, write it to the log file */ | |
132 | if(ui_buffer) { | |
133 | size_t len = ui_buffer->length; | |
134 | size_t r; | |
135 | while(len > 0) { | |
136 | r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length); | |
137 | len -= r; | |
138 | } | |
139 | ||
140 | sbuf_free(ui_buffer); | |
141 | } | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | void log_uninitialized_writeln(char *str, size_t len) { | |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
147 | printf("%.*s\n", (int)len, str); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
148 | |
28 | 149 | if(ui_buffer == NULL) { |
150 | ui_buffer = sbuf_new(1024); | |
151 | if(ui_buffer == NULL) { | |
152 | return; /* TODO: critical error, exit */ | |
153 | } | |
154 | } | |
155 | ||
156 | sstr_t s; | |
157 | s.ptr = str; | |
158 | s.length = len; | |
159 | ||
160 | sbuf_append(ui_buffer, s); | |
161 | sbuf_put(ui_buffer, '\n'); | |
162 | } | |
163 | ||
164 | void log_file_writeln(char *str, size_t len) { | |
165 | if(!is_initialized) { | |
166 | log_uninitialized_writeln(str, len); | |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
167 | return; |
28 | 168 | } |
169 | ||
170 | struct iovec io[] = { | |
171 | { str, len }, | |
172 | { "\n", 1} | |
173 | }; | |
174 | ||
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
175 | writev(log_file_fd, io, 2); /* TODO: aio? */ |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
176 | if(!main_is_daemon()) { |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
177 | writev(STDOUT_FILENO, io, 2); |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
178 | } |
28 | 179 | } |
180 | ||
181 | sstr_t log_get_prefix(int level) { | |
182 | time_t t = time(NULL); | |
183 | ||
184 | sstr_t d; | |
185 | d.ptr = NULL; | |
186 | d.length = 0; | |
187 | ||
188 | struct tm date; | |
189 | localtime_r(&t, &date); | |
190 | ||
191 | char *buf = malloc(64); | |
192 | int len = snprintf( | |
193 | buf, | |
194 | 64, | |
195 | "[%02d/%s/%d:%02d:%02d:%02d] %s : ", | |
196 | date.tm_mday, | |
197 | log_date_month[date.tm_mon], | |
198 | 1900 + date.tm_year, | |
199 | date.tm_hour, | |
200 | date.tm_min, | |
201 | date.tm_sec, | |
202 | log_levels[level]); | |
203 | ||
204 | if(len > 0) { | |
205 | d.ptr = buf; | |
206 | d.length = len; | |
207 | } | |
208 | ||
209 | return d; | |
210 | } | |
211 | ||
212 | ||
213 | /* | |
214 | * log api functions | |
215 | */ | |
216 | ||
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
217 | int log_ereport(int degree, const char *format, ...) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
218 | va_list args; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
219 | va_start(args, format); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
220 | int ret = log_ereport_v(degree, format, args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
221 | va_end(args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
222 | return ret; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
223 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
224 | |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
225 | int log_ereport_v(int degree, const char *format, va_list args) { |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
226 | if(degree > 6) { |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
227 | return 0; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
228 | } |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
229 | if(degree > 0 && !can_log[degree]) { |
28 | 230 | return 0; |
231 | } | |
232 | ||
233 | sstr_t lmsg; | |
234 | lmsg.ptr = NULL; | |
235 | ||
236 | /* create log message prefix */ | |
237 | sstr_t lpre = log_get_prefix(degree); | |
238 | ||
239 | /* format message */ | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
240 | int len = vasprintf(&lmsg.ptr, format, args); |
28 | 241 | lmsg.length = len; |
242 | ||
243 | /* create message string */ | |
99 | 244 | sstr_t message = sstrcat(2, lpre, lmsg); |
28 | 245 | |
246 | /* write message to the log file */ | |
247 | log_file_writeln(message.ptr, message.length); | |
248 | ||
249 | /* cleanup */ | |
250 | free(lmsg.ptr); | |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
77
diff
changeset
|
251 | free(lpre.ptr); |
28 | 252 | free(message.ptr); |
253 | ||
254 | return 0; | |
255 | } | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
256 | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
257 | int log_error(int degree, const char *func, Session *sn, Request *rq, |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
258 | const char *format, ...) |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
259 | { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
260 | va_list args; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
261 | va_start(args, format); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
262 | int ret = log_error_v(degree, func, sn, rq, format, args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
263 | va_end(args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
264 | return ret; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
265 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
266 | |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
267 | int log_error_v(int degree, const char *func, Session *sn, Request *rq, |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
268 | const char *format, va_list args) |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
269 | { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
270 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
271 | return log_ereport(degree, format, args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
272 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
273 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
274 | |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
275 | void ws_log_assert(const char *file, const char *func, int line) { |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
276 | log_ereport( |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
277 | LOG_CATASTROPHE, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
278 | "assertion failed: %s: %s:%d", |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
279 | file, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
280 | func, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
281 | line); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
282 | exit(-1); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
283 | } |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
284 | |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
285 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
286 | /* |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
287 | * access log |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
288 | * This source file only manages access log files. IO is performed directly |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
289 | * by AddLog safs. |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
290 | */ |
77 | 291 | LogFile* get_access_log_file(sstr_t file) { |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
292 | if(!access_log_files) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
293 | access_log_files = ucx_map_new(4); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
294 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
295 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
296 | if(file.ptr == NULL || file.length == 0) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
297 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
298 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
299 | |
77 | 300 | LogFile *log = ucx_map_sstr_get(access_log_files, file); |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
301 | if(log != NULL) { |
77 | 302 | ws_atomic_inc32(&log->ref); |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
303 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
304 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
305 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
306 | // the log file is not opened |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
307 | // check first if we can open it |
77 | 308 | FILE *out = fopen(file.ptr, "a"); |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
309 | if(out == NULL) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
310 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
311 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
312 | |
77 | 313 | // create LogFile object |
314 | log = calloc(1, sizeof(LogFile)); | |
315 | log->file = out; | |
316 | log->ref = 1; | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
317 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
318 | // add access log to the map |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
319 | ucx_map_sstr_put(access_log_files, file, log); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
320 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
321 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
322 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
323 |