Sun, 06 Jan 2019 14:38:57 +0100
fix file extension detection
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> | |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
39 | #include <pthread.h> |
28 | 40 | |
41 | #include "log.h" | |
42 | #include "../util/strbuf.h" | |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
28
diff
changeset
|
43 | #include "../util/io.h" |
77 | 44 | #include "../util/atomic.h" |
28 | 45 | |
91 | 46 | #include <ucx/map.h> |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
47 | #include <ucx/list.h> |
91 | 48 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
49 | static int is_initialized = 0; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
50 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
51 | static int log_file_fd; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
52 | static int log_level = 0; |
28 | 53 | |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
54 | static uint32_t log_dup_count = 0; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
55 | static UcxList *log_dup_list = NULL; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
56 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
57 | |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
58 | WSBool main_is_daemon(void); |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
59 | |
28 | 60 | /* |
61 | * if the log file is uninitialized, output is written to the ui_buffer | |
62 | */ | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
63 | static sbuf_t *ui_buffer = NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
64 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
65 | /* |
77 | 66 | * access log file map |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
67 | */ |
77 | 68 | static UcxMap *access_log_files; // map of LogFile* |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
69 | |
28 | 70 | |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
71 | static char *log_date_month[] = { |
28 | 72 | "Jan", |
73 | "Feb", | |
74 | "Mar", | |
75 | "Apr", | |
76 | "May", | |
77 | "Jun", | |
78 | "Jul", | |
79 | "Aug", | |
80 | "Sep", | |
81 | "Oct", | |
82 | "Nov", | |
83 | "Dec" | |
84 | }; | |
85 | ||
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
86 | static char *log_levels[] = { |
28 | 87 | "warning", |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
88 | "config", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
89 | "security", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
90 | "failure", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
91 | "catastrophe", |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
92 | "info", |
166
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
93 | "verbose", |
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
94 | "debug" |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
95 | }; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
96 | |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
97 | static int can_log[] = { |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
98 | 1, // warning |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
99 | 1, // config |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
100 | 1, // security |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
101 | 1, // failure |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
102 | 1, // catastrophe |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
103 | 1, // info |
166
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
104 | 0, // verbose |
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
105 | 0 // debug |
28 | 106 | }; |
107 | ||
108 | int init_log_file(LogConfig *cfg) { | |
109 | if(is_initialized) { | |
110 | return 0; | |
111 | } | |
112 | ||
113 | /* open the log file */ | |
114 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; | |
115 | log_file_fd = open(cfg->file, O_WRONLY | O_CREAT | O_APPEND, mode); | |
116 | if(log_file_fd == -1) { | |
117 | return -1; | |
118 | } | |
119 | ||
120 | if(!strcmp(cfg->level, "ERROR")) { | |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
121 | can_log[LOG_WARN] = 0; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
122 | can_log[LOG_INFORM] = 0; |
28 | 123 | } else if(!strcmp(cfg->level, "WARNING")) { |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
124 | can_log[LOG_INFORM] = 0; |
141 | 125 | } else if(!strcmp(cfg->level, "VERBOSE")) { |
126 | can_log[LOG_VERBOSE] = 1; | |
166
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
127 | } else if(!strcmp(cfg->level, "DEBUG")) { |
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
128 | can_log[LOG_VERBOSE] = 1; |
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
129 | can_log[LOG_DEBUG] = 1; |
28 | 130 | } |
131 | ||
132 | if(cfg->log_stdout) { | |
133 | // TODO | |
134 | } | |
135 | if(cfg->log_stderr) { | |
136 | // TODO | |
137 | } | |
138 | ||
139 | ||
140 | is_initialized = 1; | |
141 | ||
142 | /* if ui_buffer is not NULL, write it to the log file */ | |
143 | if(ui_buffer) { | |
144 | size_t len = ui_buffer->length; | |
145 | size_t r; | |
146 | while(len > 0) { | |
147 | r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length); | |
148 | len -= r; | |
149 | } | |
150 | ||
151 | sbuf_free(ui_buffer); | |
152 | } | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
157 | 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
|
158 | printf("%.*s\n", (int)len, str); |
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
159 | |
28 | 160 | if(ui_buffer == NULL) { |
161 | ui_buffer = sbuf_new(1024); | |
162 | if(ui_buffer == NULL) { | |
163 | return; /* TODO: critical error, exit */ | |
164 | } | |
165 | } | |
166 | ||
167 | sstr_t s; | |
168 | s.ptr = str; | |
169 | s.length = len; | |
170 | ||
171 | sbuf_append(ui_buffer, s); | |
172 | sbuf_put(ui_buffer, '\n'); | |
173 | } | |
174 | ||
175 | void log_file_writeln(char *str, size_t len) { | |
176 | if(!is_initialized) { | |
177 | log_uninitialized_writeln(str, len); | |
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
178 | return; |
28 | 179 | } |
180 | ||
181 | struct iovec io[] = { | |
182 | { str, len }, | |
183 | { "\n", 1} | |
184 | }; | |
185 | ||
130
198ad9d8cec1
adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
115
diff
changeset
|
186 | writev(log_file_fd, io, 2); /* TODO: aio? */ |
115
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
187 | if(!main_is_daemon()) { |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
188 | writev(STDOUT_FILENO, io, 2); |
51d9a15eac98
improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
99
diff
changeset
|
189 | } |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
190 | |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
191 | if(log_dup_count > 0) { |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
192 | char *msg = malloc(len + 1); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
193 | memcpy(msg, str, len); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
194 | msg[len] = '\n'; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
195 | |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
196 | pthread_mutex_lock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
197 | UCX_FOREACH(elm, log_dup_list) { |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
198 | LogDup *dup = elm->data; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
199 | dup->write(dup->cookie, msg, len + 1); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
200 | } |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
201 | pthread_mutex_unlock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
202 | |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
203 | free(msg); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
204 | } |
28 | 205 | } |
206 | ||
207 | sstr_t log_get_prefix(int level) { | |
208 | time_t t = time(NULL); | |
209 | ||
210 | sstr_t d; | |
211 | d.ptr = NULL; | |
212 | d.length = 0; | |
213 | ||
214 | struct tm date; | |
215 | localtime_r(&t, &date); | |
216 | ||
217 | char *buf = malloc(64); | |
218 | int len = snprintf( | |
219 | buf, | |
220 | 64, | |
221 | "[%02d/%s/%d:%02d:%02d:%02d] %s : ", | |
222 | date.tm_mday, | |
223 | log_date_month[date.tm_mon], | |
224 | 1900 + date.tm_year, | |
225 | date.tm_hour, | |
226 | date.tm_min, | |
227 | date.tm_sec, | |
228 | log_levels[level]); | |
229 | ||
230 | if(len > 0) { | |
231 | d.ptr = buf; | |
232 | d.length = len; | |
233 | } | |
234 | ||
235 | return d; | |
236 | } | |
237 | ||
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
238 | void log_add_logdup(LogDup *dup) { |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
239 | pthread_mutex_lock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
240 | log_dup_list = ucx_list_append(log_dup_list, dup); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
241 | ws_atomic_inc32(&log_dup_count); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
242 | pthread_mutex_unlock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
243 | } |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
244 | |
176
f2268fcbe487
fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
167
diff
changeset
|
245 | void log_remove_logdup(LogDup *ldup) { |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
246 | pthread_mutex_lock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
247 | UcxList *elm = log_dup_list; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
248 | while(elm) { |
176
f2268fcbe487
fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
167
diff
changeset
|
249 | if(elm->data == ldup) { |
167
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
250 | log_dup_list = ucx_list_remove(log_dup_list, elm); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
251 | ws_atomic_dec32(&log_dup_count); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
252 | break; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
253 | } |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
254 | elm = elm->next; |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
255 | } |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
256 | pthread_mutex_unlock(&mutex); |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
257 | } |
4be7dd2b75b9
srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
166
diff
changeset
|
258 | |
28 | 259 | |
260 | /* | |
261 | * log api functions | |
262 | */ | |
263 | ||
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
264 | int log_ereport(int degree, const char *format, ...) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
265 | va_list args; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
266 | va_start(args, format); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
267 | int ret = log_ereport_v(degree, format, args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
268 | va_end(args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
269 | return ret; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
270 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
271 | |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
272 | int log_ereport_v(int degree, const char *format, va_list args) { |
166
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
273 | if(degree < 0 || degree > 7) { |
60
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
274 | return 0; |
feb2f1e115c6
improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
275 | } |
166
c07122f66676
adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
141
diff
changeset
|
276 | if(!can_log[degree]) { |
28 | 277 | return 0; |
278 | } | |
279 | ||
280 | sstr_t lmsg; | |
281 | lmsg.ptr = NULL; | |
282 | ||
283 | /* create log message prefix */ | |
284 | sstr_t lpre = log_get_prefix(degree); | |
285 | ||
286 | /* format message */ | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
287 | int len = vasprintf(&lmsg.ptr, format, args); |
28 | 288 | lmsg.length = len; |
289 | ||
290 | /* create message string */ | |
99 | 291 | sstr_t message = sstrcat(2, lpre, lmsg); |
28 | 292 | |
293 | /* write message to the log file */ | |
294 | log_file_writeln(message.ptr, message.length); | |
295 | ||
296 | /* cleanup */ | |
297 | free(lmsg.ptr); | |
79
f48cea237ec3
fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
77
diff
changeset
|
298 | free(lpre.ptr); |
28 | 299 | free(message.ptr); |
300 | ||
301 | return 0; | |
302 | } | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
303 | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
304 | 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
|
305 | const char *format, ...) |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
306 | { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
307 | va_list args; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
308 | va_start(args, format); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
309 | 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
|
310 | va_end(args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
311 | return ret; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
312 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
313 | |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
314 | 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
|
315 | const char *format, va_list args) |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
316 | { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
317 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
318 | return log_ereport(degree, format, args); |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
319 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
320 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
321 | |
87
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
322 | 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
|
323 | log_ereport( |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
324 | LOG_CATASTROPHE, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
325 | "assertion failed: %s: %s:%d", |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
326 | file, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
327 | func, |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
328 | line); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
329 | exit(-1); |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
330 | } |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
331 | |
bdec069d2239
fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
332 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
333 | /* |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
334 | * access log |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
335 | * 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
|
336 | * by AddLog safs. |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
337 | */ |
77 | 338 | LogFile* get_access_log_file(sstr_t file) { |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
339 | if(!access_log_files) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
340 | access_log_files = ucx_map_new(4); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
341 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
342 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
343 | if(file.ptr == NULL || file.length == 0) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
344 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
345 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
346 | |
77 | 347 | 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
|
348 | if(log != NULL) { |
77 | 349 | ws_atomic_inc32(&log->ref); |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
350 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
351 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
352 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
353 | // the log file is not opened |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
354 | // check first if we can open it |
77 | 355 | FILE *out = fopen(file.ptr, "a"); |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
356 | if(out == NULL) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
357 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
358 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
359 | |
77 | 360 | // create LogFile object |
361 | log = calloc(1, sizeof(LogFile)); | |
362 | log->file = out; | |
363 | log->ref = 1; | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
364 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
365 | // add access log to the map |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
366 | ucx_map_sstr_put(access_log_files, file, log); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
367 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
368 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
369 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
370 |