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