1 | /*
|
---|
2 | * Copyright (c) 2012 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup logger
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | #include <assert.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stdlib.h>
|
---|
36 | #include <str.h>
|
---|
37 | #include "logger.h"
|
---|
38 |
|
---|
39 | static FIBRIL_MUTEX_INITIALIZE(log_list_guard);
|
---|
40 | static LIST_INITIALIZE(log_list);
|
---|
41 |
|
---|
42 | static logger_log_t *find_log_by_name_and_parent_no_list_lock(const char *name, logger_log_t *parent)
|
---|
43 | {
|
---|
44 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
45 | if ((parent == log->parent) && (str_cmp(log->name, name) == 0))
|
---|
46 | return log;
|
---|
47 | }
|
---|
48 |
|
---|
49 | return NULL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static errno_t create_dest(const char *name, logger_dest_t **dest)
|
---|
53 | {
|
---|
54 | logger_dest_t *result = malloc(sizeof(logger_dest_t));
|
---|
55 | if (result == NULL)
|
---|
56 | return ENOMEM;
|
---|
57 | if (asprintf(&result->filename, "/log/%s", name) < 0) {
|
---|
58 | free(result);
|
---|
59 | return ENOMEM;
|
---|
60 | }
|
---|
61 | result->logfile = NULL;
|
---|
62 | fibril_mutex_initialize(&result->guard);
|
---|
63 | *dest = result;
|
---|
64 | return EOK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static logger_log_t *create_log_no_locking(const char *name, logger_log_t *parent)
|
---|
68 | {
|
---|
69 | logger_log_t *result = calloc(1, sizeof(logger_log_t));
|
---|
70 | if (result == NULL)
|
---|
71 | return NULL;
|
---|
72 |
|
---|
73 | result->name = str_dup(name);
|
---|
74 | if (result->name == NULL)
|
---|
75 | goto error;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Notice that we create new dest as the last
|
---|
79 | * operation that can fail and thus there is no code
|
---|
80 | * to deallocate dest.
|
---|
81 | */
|
---|
82 | if (parent == NULL) {
|
---|
83 | result->full_name = str_dup(name);
|
---|
84 | if (result->full_name == NULL)
|
---|
85 | goto error;
|
---|
86 | errno_t rc = create_dest(name, &result->dest);
|
---|
87 | if (rc != EOK)
|
---|
88 | goto error;
|
---|
89 | } else {
|
---|
90 | if (asprintf(&result->full_name, "%s/%s",
|
---|
91 | parent->full_name, name) < 0)
|
---|
92 | goto error;
|
---|
93 | result->dest = parent->dest;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Following initializations cannot fail. */
|
---|
97 | result->logged_level = LOG_LEVEL_USE_DEFAULT;
|
---|
98 | fibril_mutex_initialize(&result->guard);
|
---|
99 | link_initialize(&result->link);
|
---|
100 | result->parent = parent;
|
---|
101 |
|
---|
102 | return result;
|
---|
103 |
|
---|
104 | error:
|
---|
105 | free(result->name);
|
---|
106 | free(result->full_name);
|
---|
107 | free(result);
|
---|
108 | return NULL;
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | logger_log_t *find_or_create_log_and_lock(const char *name, sysarg_t parent_id)
|
---|
113 | {
|
---|
114 | logger_log_t *result = NULL;
|
---|
115 | logger_log_t *parent = (logger_log_t *) parent_id;
|
---|
116 |
|
---|
117 | fibril_mutex_lock(&log_list_guard);
|
---|
118 |
|
---|
119 | result = find_log_by_name_and_parent_no_list_lock(name, parent);
|
---|
120 | if (result == NULL) {
|
---|
121 | result = create_log_no_locking(name, parent);
|
---|
122 | if (result == NULL)
|
---|
123 | goto leave;
|
---|
124 | list_append(&result->link, &log_list);
|
---|
125 | if (result->parent != NULL) {
|
---|
126 | fibril_mutex_lock(&result->parent->guard);
|
---|
127 | result->parent->ref_counter++;
|
---|
128 | fibril_mutex_unlock(&result->parent->guard);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | fibril_mutex_lock(&result->guard);
|
---|
133 |
|
---|
134 | leave:
|
---|
135 | fibril_mutex_unlock(&log_list_guard);
|
---|
136 |
|
---|
137 | return result;
|
---|
138 | }
|
---|
139 |
|
---|
140 | logger_log_t *find_log_by_name_and_lock(const char *name)
|
---|
141 | {
|
---|
142 | logger_log_t *result = NULL;
|
---|
143 |
|
---|
144 | fibril_mutex_lock(&log_list_guard);
|
---|
145 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
146 | if (str_cmp(log->full_name, name) == 0) {
|
---|
147 | fibril_mutex_lock(&log->guard);
|
---|
148 | result = log;
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | fibril_mutex_unlock(&log_list_guard);
|
---|
153 |
|
---|
154 | return result;
|
---|
155 | }
|
---|
156 |
|
---|
157 | logger_log_t *find_log_by_id_and_lock(sysarg_t id)
|
---|
158 | {
|
---|
159 | logger_log_t *result = NULL;
|
---|
160 |
|
---|
161 | fibril_mutex_lock(&log_list_guard);
|
---|
162 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
163 | if ((sysarg_t) log == id) {
|
---|
164 | fibril_mutex_lock(&log->guard);
|
---|
165 | result = log;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | fibril_mutex_unlock(&log_list_guard);
|
---|
170 |
|
---|
171 | return result;
|
---|
172 | }
|
---|
173 |
|
---|
174 | static log_level_t get_actual_log_level(logger_log_t *log)
|
---|
175 | {
|
---|
176 | /* Find recursively proper log level. */
|
---|
177 | if (log->logged_level == LOG_LEVEL_USE_DEFAULT) {
|
---|
178 | if (log->parent == NULL)
|
---|
179 | return get_default_logging_level();
|
---|
180 | else
|
---|
181 | return get_actual_log_level(log->parent);
|
---|
182 | }
|
---|
183 | return log->logged_level;
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool shall_log_message(logger_log_t *log, log_level_t level)
|
---|
187 | {
|
---|
188 | fibril_mutex_lock(&log_list_guard);
|
---|
189 | bool result = level <= get_actual_log_level(log);
|
---|
190 | fibril_mutex_unlock(&log_list_guard);
|
---|
191 | return result;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void log_unlock(logger_log_t *log)
|
---|
195 | {
|
---|
196 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
197 | fibril_mutex_unlock(&log->guard);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Decreases reference counter on the log and destroy the log if
|
---|
201 | * necessary.
|
---|
202 | *
|
---|
203 | * Precondition: log is locked.
|
---|
204 | *
|
---|
205 | * @param log Log to release from using by the caller.
|
---|
206 | */
|
---|
207 | void log_release(logger_log_t *log)
|
---|
208 | {
|
---|
209 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
210 | assert(log->ref_counter > 0);
|
---|
211 |
|
---|
212 | /* We are definitely not the last ones. */
|
---|
213 | if (log->ref_counter > 1) {
|
---|
214 | log->ref_counter--;
|
---|
215 | fibril_mutex_unlock(&log->guard);
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * To prevent deadlock, we need to get the list lock first.
|
---|
221 | * Deadlock scenario:
|
---|
222 | * Us: LOCKED(log), want to LOCK(list)
|
---|
223 | * Someone else calls find_log_by_name_and_lock(log->fullname) ->
|
---|
224 | * LOCKED(list), wants to LOCK(log)
|
---|
225 | */
|
---|
226 | fibril_mutex_unlock(&log->guard);
|
---|
227 |
|
---|
228 | /* Ensuring correct locking order. */
|
---|
229 | fibril_mutex_lock(&log_list_guard);
|
---|
230 | /*
|
---|
231 | * The reference must be still valid because we have not decreased
|
---|
232 | * the reference counter.
|
---|
233 | */
|
---|
234 | fibril_mutex_lock(&log->guard);
|
---|
235 | assert(log->ref_counter > 0);
|
---|
236 | log->ref_counter--;
|
---|
237 |
|
---|
238 | if (log->ref_counter > 0) {
|
---|
239 | /*
|
---|
240 | * Meanwhile, someone else increased the ref counter.
|
---|
241 | * No big deal, we just return immediatelly.
|
---|
242 | */
|
---|
243 | fibril_mutex_unlock(&log->guard);
|
---|
244 | fibril_mutex_unlock(&log_list_guard);
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Here we are on a destroy path. We need to
|
---|
250 | * - remove ourselves from the list
|
---|
251 | * - decrease reference of the parent (if not top-level log)
|
---|
252 | * - we must do that after we relaase list lock to prevent
|
---|
253 | * deadlock with ourselves
|
---|
254 | * - destroy dest (if top-level log)
|
---|
255 | */
|
---|
256 | assert(log->ref_counter == 0);
|
---|
257 |
|
---|
258 | list_remove(&log->link);
|
---|
259 | fibril_mutex_unlock(&log_list_guard);
|
---|
260 | fibril_mutex_unlock(&log->guard);
|
---|
261 |
|
---|
262 | if (log->parent == NULL) {
|
---|
263 | /*
|
---|
264 | * Due to lazy file opening in write_to_log(),
|
---|
265 | * it is possible that no file was actually opened.
|
---|
266 | */
|
---|
267 | if (log->dest->logfile != NULL) {
|
---|
268 | fclose(log->dest->logfile);
|
---|
269 | }
|
---|
270 | free(log->dest->filename);
|
---|
271 | free(log->dest);
|
---|
272 | } else {
|
---|
273 | fibril_mutex_lock(&log->parent->guard);
|
---|
274 | log_release(log->parent);
|
---|
275 | }
|
---|
276 |
|
---|
277 | logger_log("Destroyed log %s.\n", log->full_name);
|
---|
278 |
|
---|
279 | free(log->name);
|
---|
280 | free(log->full_name);
|
---|
281 |
|
---|
282 | free(log);
|
---|
283 | }
|
---|
284 |
|
---|
285 | void write_to_log(logger_log_t *log, log_level_t level, const char *message)
|
---|
286 | {
|
---|
287 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
288 | assert(log->dest != NULL);
|
---|
289 | fibril_mutex_lock(&log->dest->guard);
|
---|
290 | if (log->dest->logfile == NULL)
|
---|
291 | log->dest->logfile = fopen(log->dest->filename, "a");
|
---|
292 |
|
---|
293 | if (log->dest->logfile != NULL) {
|
---|
294 | fprintf(log->dest->logfile, "[%s] %s: %s\n",
|
---|
295 | log->full_name, log_level_str(level),
|
---|
296 | (const char *) message);
|
---|
297 | fflush(log->dest->logfile);
|
---|
298 | }
|
---|
299 |
|
---|
300 | fibril_mutex_unlock(&log->dest->guard);
|
---|
301 | }
|
---|
302 |
|
---|
303 | void registered_logs_init(logger_registered_logs_t *logs)
|
---|
304 | {
|
---|
305 | logs->logs_count = 0;
|
---|
306 | }
|
---|
307 |
|
---|
308 | bool register_log(logger_registered_logs_t *logs, logger_log_t *new_log)
|
---|
309 | {
|
---|
310 | if (logs->logs_count >= MAX_REFERENCED_LOGS_PER_CLIENT) {
|
---|
311 | return false;
|
---|
312 | }
|
---|
313 |
|
---|
314 | assert(fibril_mutex_is_locked(&new_log->guard));
|
---|
315 | new_log->ref_counter++;
|
---|
316 |
|
---|
317 | logs->logs[logs->logs_count] = new_log;
|
---|
318 | logs->logs_count++;
|
---|
319 |
|
---|
320 | return true;
|
---|
321 | }
|
---|
322 |
|
---|
323 | void unregister_logs(logger_registered_logs_t *logs)
|
---|
324 | {
|
---|
325 | for (size_t i = 0; i < logs->logs_count; i++) {
|
---|
326 | logger_log_t *log = logs->logs[i];
|
---|
327 | fibril_mutex_lock(&log->guard);
|
---|
328 | log_release(log);
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * @}
|
---|
334 | */
|
---|