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 <malloc.h>
|
---|
34 | #include <str.h>
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include "logger.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | static FIBRIL_MUTEX_INITIALIZE(log_list_guard);
|
---|
41 | static LIST_INITIALIZE(log_list);
|
---|
42 |
|
---|
43 |
|
---|
44 | static logger_log_t *find_log_by_name_and_parent_no_list_lock(const char *name, logger_log_t *parent)
|
---|
45 | {
|
---|
46 | list_foreach(log_list, it) {
|
---|
47 | logger_log_t *log = list_get_instance(it, logger_log_t, link);
|
---|
48 | if ((parent == log->parent) && (str_cmp(log->name, name) == 0))
|
---|
49 | return log;
|
---|
50 | }
|
---|
51 |
|
---|
52 | return NULL;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static int create_dest(const char *name, logger_dest_t **dest)
|
---|
56 | {
|
---|
57 | logger_dest_t *result = malloc(sizeof(logger_dest_t));
|
---|
58 | if (result == NULL)
|
---|
59 | return ENOMEM;
|
---|
60 | int rc = asprintf(&result->filename, "/log/%s", name);
|
---|
61 | if (rc < 0) {
|
---|
62 | free(result);
|
---|
63 | return ENOMEM;
|
---|
64 | }
|
---|
65 | result->logfile = NULL;
|
---|
66 | fibril_mutex_initialize(&result->guard);
|
---|
67 | *dest = result;
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static logger_log_t *create_log_no_locking(const char *name, logger_log_t *parent)
|
---|
72 | {
|
---|
73 | logger_log_t *result = calloc(1, sizeof(logger_log_t));
|
---|
74 | if (result == NULL)
|
---|
75 | return NULL;
|
---|
76 |
|
---|
77 | result->name = str_dup(name);
|
---|
78 | if (result->name == NULL)
|
---|
79 | goto error;
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Notice that we create new dest as the last
|
---|
83 | * operation that can fail and thus there is no code
|
---|
84 | * to deallocate dest.
|
---|
85 | */
|
---|
86 | if (parent == NULL) {
|
---|
87 | result->full_name = str_dup(name);
|
---|
88 | if (result->full_name == NULL)
|
---|
89 | goto error;
|
---|
90 | int rc = create_dest(name, &result->dest);
|
---|
91 | if (rc != EOK)
|
---|
92 | goto error;
|
---|
93 | } else {
|
---|
94 | int rc = asprintf(&result->full_name, "%s/%s",
|
---|
95 | parent->full_name, name);
|
---|
96 | if (rc < 0)
|
---|
97 | goto error;
|
---|
98 | result->dest = parent->dest;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Following initializations cannot fail. */
|
---|
102 | result->logged_level = LOG_LEVEL_USE_DEFAULT;
|
---|
103 | fibril_mutex_initialize(&result->guard);
|
---|
104 | link_initialize(&result->link);
|
---|
105 | result->parent = parent;
|
---|
106 |
|
---|
107 | return result;
|
---|
108 |
|
---|
109 | error:
|
---|
110 | free(result->name);
|
---|
111 | free(result->full_name);
|
---|
112 | free(result);
|
---|
113 | return NULL;
|
---|
114 |
|
---|
115 | }
|
---|
116 |
|
---|
117 | logger_log_t *find_or_create_log_and_lock(const char *name, sysarg_t parent_id)
|
---|
118 | {
|
---|
119 | logger_log_t *result = NULL;
|
---|
120 | logger_log_t *parent = (logger_log_t *) parent_id;
|
---|
121 |
|
---|
122 | fibril_mutex_lock(&log_list_guard);
|
---|
123 |
|
---|
124 | result = find_log_by_name_and_parent_no_list_lock(name, parent);
|
---|
125 | if (result == NULL) {
|
---|
126 | result = create_log_no_locking(name, parent);
|
---|
127 | if (result == NULL)
|
---|
128 | goto leave;
|
---|
129 | list_append(&result->link, &log_list);
|
---|
130 | if (result->parent != NULL) {
|
---|
131 | fibril_mutex_lock(&result->parent->guard);
|
---|
132 | result->parent->ref_counter++;
|
---|
133 | fibril_mutex_unlock(&result->parent->guard);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | fibril_mutex_lock(&result->guard);
|
---|
138 |
|
---|
139 | leave:
|
---|
140 | fibril_mutex_unlock(&log_list_guard);
|
---|
141 |
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 |
|
---|
145 | logger_log_t *find_log_by_name_and_lock(const char *name)
|
---|
146 | {
|
---|
147 | logger_log_t *result = NULL;
|
---|
148 |
|
---|
149 | fibril_mutex_lock(&log_list_guard);
|
---|
150 | list_foreach(log_list, it) {
|
---|
151 | logger_log_t *log = list_get_instance(it, logger_log_t, link);
|
---|
152 | if (str_cmp(log->full_name, name) == 0) {
|
---|
153 | fibril_mutex_lock(&log->guard);
|
---|
154 | result = log;
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | fibril_mutex_unlock(&log_list_guard);
|
---|
159 |
|
---|
160 | return result;
|
---|
161 | }
|
---|
162 |
|
---|
163 | logger_log_t *find_log_by_id_and_lock(sysarg_t id)
|
---|
164 | {
|
---|
165 | logger_log_t *result = NULL;
|
---|
166 |
|
---|
167 | fibril_mutex_lock(&log_list_guard);
|
---|
168 | list_foreach(log_list, it) {
|
---|
169 | logger_log_t *log = list_get_instance(it, logger_log_t, link);
|
---|
170 | if ((sysarg_t) log == id) {
|
---|
171 | fibril_mutex_lock(&log->guard);
|
---|
172 | result = log;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | fibril_mutex_unlock(&log_list_guard);
|
---|
177 |
|
---|
178 | return result;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static log_level_t get_actual_log_level(logger_log_t *log)
|
---|
182 | {
|
---|
183 | /* Find recursively proper log level. */
|
---|
184 | if (log->logged_level == LOG_LEVEL_USE_DEFAULT) {
|
---|
185 | if (log->parent == NULL)
|
---|
186 | return get_default_logging_level();
|
---|
187 | else
|
---|
188 | return get_actual_log_level(log->parent);
|
---|
189 | }
|
---|
190 | return log->logged_level;
|
---|
191 | }
|
---|
192 |
|
---|
193 | bool shall_log_message(logger_log_t *log, log_level_t level)
|
---|
194 | {
|
---|
195 | fibril_mutex_lock(&log_list_guard);
|
---|
196 | bool result = level <= get_actual_log_level(log);
|
---|
197 | fibril_mutex_unlock(&log_list_guard);
|
---|
198 | return result;
|
---|
199 | }
|
---|
200 |
|
---|
201 | void log_unlock(logger_log_t *log)
|
---|
202 | {
|
---|
203 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
204 | fibril_mutex_unlock(&log->guard);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Decreases reference counter on the log and destory the log if
|
---|
208 | * necessary.
|
---|
209 | *
|
---|
210 | * Precondition: log is locked.
|
---|
211 | *
|
---|
212 | * @param log Log to release from using by the caller.
|
---|
213 | */
|
---|
214 | void log_release(logger_log_t *log)
|
---|
215 | {
|
---|
216 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
217 | assert(log->ref_counter > 0);
|
---|
218 |
|
---|
219 | /* We are definitely not the last ones. */
|
---|
220 | if (log->ref_counter > 1) {
|
---|
221 | log->ref_counter--;
|
---|
222 | fibril_mutex_unlock(&log->guard);
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * To prevent deadlock, we need to get the list lock first.
|
---|
228 | * Deadlock scenario:
|
---|
229 | * Us: LOCKED(log), want to LOCK(list)
|
---|
230 | * Someone else calls find_log_by_name_and_lock(log->fullname) ->
|
---|
231 | * LOCKED(list), wants to LOCK(log)
|
---|
232 | */
|
---|
233 | fibril_mutex_unlock(&log->guard);
|
---|
234 |
|
---|
235 | /* Ensuring correct locking order. */
|
---|
236 | fibril_mutex_lock(&log_list_guard);
|
---|
237 | /*
|
---|
238 | * The reference must be still valid because we have not decreased
|
---|
239 | * the reference counter.
|
---|
240 | */
|
---|
241 | fibril_mutex_lock(&log->guard);
|
---|
242 | assert(log->ref_counter > 0);
|
---|
243 | log->ref_counter--;
|
---|
244 |
|
---|
245 | if (log->ref_counter > 0) {
|
---|
246 | /*
|
---|
247 | * Meanwhile, someone else increased the ref counter.
|
---|
248 | * No big deal, we just return immediatelly.
|
---|
249 | */
|
---|
250 | fibril_mutex_unlock(&log->guard);
|
---|
251 | fibril_mutex_unlock(&log_list_guard);
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * Here we are on a destroy path. We need to
|
---|
257 | * - remove ourselves from the list
|
---|
258 | * - decrease reference of the parent (if not top-level log)
|
---|
259 | * - we must do that after we relaase list lock to prevent
|
---|
260 | * deadlock with ourselves
|
---|
261 | * - destroy dest (if top-level log)
|
---|
262 | */
|
---|
263 | assert(log->ref_counter == 0);
|
---|
264 |
|
---|
265 | list_remove(&log->link);
|
---|
266 | fibril_mutex_unlock(&log_list_guard);
|
---|
267 | fibril_mutex_unlock(&log->guard);
|
---|
268 |
|
---|
269 | if (log->parent == NULL) {
|
---|
270 | fclose(log->dest->logfile);
|
---|
271 | free(log->dest->filename);
|
---|
272 | free(log->dest);
|
---|
273 | } else {
|
---|
274 | fibril_mutex_lock(&log->parent->guard);
|
---|
275 | log_release(log->parent);
|
---|
276 | }
|
---|
277 |
|
---|
278 | printf("Destroying log %s.\n", log->full_name);
|
---|
279 |
|
---|
280 | free(log->name);
|
---|
281 | free(log->full_name);
|
---|
282 |
|
---|
283 | free(log);
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | void write_to_log(logger_log_t *log, log_level_t level, const char *message)
|
---|
288 | {
|
---|
289 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
290 | assert(log->dest != NULL);
|
---|
291 | fibril_mutex_lock(&log->dest->guard);
|
---|
292 | if (log->dest->logfile == NULL)
|
---|
293 | log->dest->logfile = fopen(log->dest->filename, "a");
|
---|
294 |
|
---|
295 | if (log->dest->logfile != NULL) {
|
---|
296 | fprintf(log->dest->logfile, "[%s] %s: %s\n",
|
---|
297 | log->full_name, log_level_str(level),
|
---|
298 | (const char *) message);
|
---|
299 | fflush(log->dest->logfile);
|
---|
300 | }
|
---|
301 |
|
---|
302 | fibril_mutex_unlock(&log->dest->guard);
|
---|
303 | }
|
---|
304 |
|
---|
305 | void registered_logs_init(logger_registered_logs_t *logs)
|
---|
306 | {
|
---|
307 | logs->logs_count = 0;
|
---|
308 | }
|
---|
309 |
|
---|
310 | bool register_log(logger_registered_logs_t *logs, logger_log_t *new_log)
|
---|
311 | {
|
---|
312 | if (logs->logs_count >= MAX_REFERENCED_LOGS_PER_CLIENT) {
|
---|
313 | return false;
|
---|
314 | }
|
---|
315 |
|
---|
316 | assert(fibril_mutex_is_locked(&new_log->guard));
|
---|
317 | new_log->ref_counter++;
|
---|
318 |
|
---|
319 | logs->logs[logs->logs_count] = new_log;
|
---|
320 | logs->logs_count++;
|
---|
321 |
|
---|
322 | return true;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void unregister_logs(logger_registered_logs_t *logs)
|
---|
326 | {
|
---|
327 | for (size_t i = 0; i < logs->logs_count; i++) {
|
---|
328 | logger_log_t *log = logs->logs[i];
|
---|
329 | fibril_mutex_lock(&log->guard);
|
---|
330 | log_release(log);
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * @}
|
---|
336 | */
|
---|