Mon, 06 May 2013 13:44:27 +0200
some fixes and new public APIs
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" |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
43 | #include "../ucx/map.h" |
28 | 44 | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
45 | static int is_initialized = 0; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
46 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
47 | static int log_file_fd; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
48 | static int log_level = 0; |
28 | 49 | |
50 | /* | |
51 | * if the log file is uninitialized, output is written to the ui_buffer | |
52 | */ | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
53 | static sbuf_t *ui_buffer = NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
54 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
55 | /* |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
56 | * access logfile map |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
57 | */ |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
58 | static UcxMap *access_log_files; // map of AccessLog* |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
59 | static AccessLog *default_access_log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
60 | |
28 | 61 | |
62 | char *log_date_month[] = { | |
63 | "Jan", | |
64 | "Feb", | |
65 | "Mar", | |
66 | "Apr", | |
67 | "May", | |
68 | "Jun", | |
69 | "Jul", | |
70 | "Aug", | |
71 | "Sep", | |
72 | "Oct", | |
73 | "Nov", | |
74 | "Dec" | |
75 | }; | |
76 | ||
77 | char *log_levels[] = { | |
78 | "error", | |
79 | "warning", | |
80 | "info" | |
81 | }; | |
82 | ||
83 | int init_log_file(LogConfig *cfg) { | |
84 | if(is_initialized) { | |
85 | return 0; | |
86 | } | |
87 | ||
88 | /* open the log file */ | |
89 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; | |
90 | log_file_fd = open(cfg->file, O_WRONLY | O_CREAT | O_APPEND, mode); | |
91 | if(log_file_fd == -1) { | |
92 | return -1; | |
93 | } | |
94 | ||
95 | if(!strcmp(cfg->level, "ERROR")) { | |
96 | log_level = LOG_LEVEL_ERROR; | |
97 | } else if(!strcmp(cfg->level, "WARNING")) { | |
98 | log_level = LOG_LEVEL_WARNING; | |
99 | } else if(!strcmp(cfg->level, "INFO")) { | |
100 | log_level = LOG_LEVEL_INFO; | |
101 | } | |
102 | ||
103 | if(cfg->log_stdout) { | |
104 | // TODO | |
105 | } | |
106 | if(cfg->log_stderr) { | |
107 | // TODO | |
108 | } | |
109 | ||
110 | ||
111 | is_initialized = 1; | |
112 | ||
113 | /* if ui_buffer is not NULL, write it to the log file */ | |
114 | if(ui_buffer) { | |
115 | size_t len = ui_buffer->length; | |
116 | size_t r; | |
117 | while(len > 0) { | |
118 | r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length); | |
119 | len -= r; | |
120 | } | |
121 | ||
122 | sbuf_free(ui_buffer); | |
123 | } | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | void log_uninitialized_writeln(char *str, size_t len) { | |
129 | if(ui_buffer == NULL) { | |
130 | ui_buffer = sbuf_new(1024); | |
131 | if(ui_buffer == NULL) { | |
132 | return; /* TODO: critical error, exit */ | |
133 | } | |
134 | } | |
135 | ||
136 | sstr_t s; | |
137 | s.ptr = str; | |
138 | s.length = len; | |
139 | ||
140 | sbuf_append(ui_buffer, s); | |
141 | sbuf_put(ui_buffer, '\n'); | |
142 | } | |
143 | ||
144 | void log_file_writeln(char *str, size_t len) { | |
145 | if(!is_initialized) { | |
146 | log_uninitialized_writeln(str, len); | |
147 | } | |
148 | ||
149 | struct iovec io[] = { | |
150 | { str, len }, | |
151 | { "\n", 1} | |
152 | }; | |
153 | ||
154 | writev(log_file_fd, io, 2); /* TODO: aio */ | |
155 | } | |
156 | ||
157 | sstr_t log_get_prefix(int level) { | |
158 | time_t t = time(NULL); | |
159 | ||
160 | sstr_t d; | |
161 | d.ptr = NULL; | |
162 | d.length = 0; | |
163 | ||
164 | struct tm date; | |
165 | localtime_r(&t, &date); | |
166 | ||
167 | char *buf = malloc(64); | |
168 | int len = snprintf( | |
169 | buf, | |
170 | 64, | |
171 | "[%02d/%s/%d:%02d:%02d:%02d] %s : ", | |
172 | date.tm_mday, | |
173 | log_date_month[date.tm_mon], | |
174 | 1900 + date.tm_year, | |
175 | date.tm_hour, | |
176 | date.tm_min, | |
177 | date.tm_sec, | |
178 | log_levels[level]); | |
179 | ||
180 | if(len > 0) { | |
181 | d.ptr = buf; | |
182 | d.length = len; | |
183 | } | |
184 | ||
185 | return d; | |
186 | } | |
187 | ||
188 | ||
189 | /* | |
190 | * log api functions | |
191 | */ | |
192 | ||
193 | int log_ereport(int degree, const char *format, ...) { | |
194 | if(degree > log_level) { | |
195 | return 0; | |
196 | } | |
197 | ||
198 | sstr_t lmsg; | |
199 | lmsg.ptr = NULL; | |
200 | va_list ap; | |
201 | va_start(ap, format); | |
202 | ||
203 | /* create log message prefix */ | |
204 | sstr_t lpre = log_get_prefix(degree); | |
205 | ||
206 | /* format message */ | |
207 | int len = vasprintf(&lmsg.ptr, format, ap); | |
208 | lmsg.length = len; | |
209 | ||
210 | /* create message string */ | |
211 | sstr_t message; | |
212 | message.length = lpre.length + len; | |
213 | message.ptr = malloc(message.length + 1); | |
214 | ||
215 | message = sstrncat(2, message, lpre, lmsg); | |
216 | ||
217 | /* write message to the log file */ | |
218 | log_file_writeln(message.ptr, message.length); | |
219 | ||
220 | /* cleanup */ | |
221 | free(lmsg.ptr); | |
222 | free(message.ptr); | |
223 | ||
224 | return 0; | |
225 | } | |
45
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
226 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
227 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
228 | /* |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
229 | * access log |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
230 | * 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
|
231 | * by AddLog safs. |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
232 | */ |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
233 | AccessLog* get_access_log(sstr_t file, sstr_t format) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
234 | if(!access_log_files) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
235 | access_log_files = ucx_map_new(4); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
236 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
237 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
238 | if(file.ptr == NULL || file.length == 0) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
239 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
240 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
241 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
242 | AccessLog *log = ucx_map_sstr_get(access_log_files, file); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
243 | if(log != NULL) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
244 | // TODO: ref |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
245 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
246 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
247 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
248 | // the log file is not opened |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
249 | // check first if we can open it |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
250 | sstr_t path = sstrdup(file); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
251 | FILE *out = fopen(path.ptr, "a"); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
252 | if(out == NULL) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
253 | free(path.ptr); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
254 | return NULL; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
255 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
256 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
257 | // create access log object |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
258 | log = calloc(1, sizeof(AccessLog)); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
259 | log->file = path; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
260 | if(format.ptr != NULL) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
261 | log->format = sstrdup(format); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
262 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
263 | log->log = out; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
264 | // TODO: log->ref = 1; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
265 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
266 | // add access log to the map |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
267 | ucx_map_sstr_put(access_log_files, file, log); |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
268 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
269 | if(!default_access_log) { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
270 | default_access_log = log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
271 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
272 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
273 | return log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
274 | } |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
275 | |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
276 | AccessLog* get_default_access_log() { |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
277 | // TODO: ref |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
278 | return default_access_log; |
a24aa388f02f
added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
279 | } |