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 | /** @file
|
---|
40 | * Logging namespaces.
|
---|
41 | */
|
---|
42 |
|
---|
43 |
|
---|
44 | struct logging_namespace {
|
---|
45 | fibril_mutex_t guard;
|
---|
46 | size_t writers_count;
|
---|
47 | fibril_condvar_t level_changed_cv;
|
---|
48 | FILE *logfile;
|
---|
49 | log_level_t level;
|
---|
50 | const char *name;
|
---|
51 |
|
---|
52 | link_t link;
|
---|
53 | };
|
---|
54 |
|
---|
55 | static FIBRIL_MUTEX_INITIALIZE(namespace_list_guard);
|
---|
56 | static LIST_INITIALIZE(namespace_list);
|
---|
57 |
|
---|
58 | static log_level_t namespace_get_actual_log_level(logging_namespace_t *namespace)
|
---|
59 | {
|
---|
60 | fibril_mutex_lock(&namespace->guard);
|
---|
61 | log_level_t level = namespace->level;
|
---|
62 | fibril_mutex_unlock(&namespace->guard);
|
---|
63 |
|
---|
64 | if (level == LOG_LEVEL_USE_DEFAULT)
|
---|
65 | level = get_default_logging_level();
|
---|
66 |
|
---|
67 | return level;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static logging_namespace_t *namespace_find_no_lock(const char *name)
|
---|
71 | {
|
---|
72 | list_foreach(namespace_list, it) {
|
---|
73 | logging_namespace_t *namespace = list_get_instance(it, logging_namespace_t, link);
|
---|
74 | if (str_cmp(namespace->name, name) == 0) {
|
---|
75 | return namespace;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | return NULL;
|
---|
80 | }
|
---|
81 |
|
---|
82 | static logging_namespace_t *namespace_create_no_lock(const char *name)
|
---|
83 | {
|
---|
84 | logging_namespace_t *existing = namespace_find_no_lock(name);
|
---|
85 | if (existing != NULL) {
|
---|
86 | return NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | logging_namespace_t *namespace = malloc(sizeof(logging_namespace_t));
|
---|
90 | if (namespace == NULL) {
|
---|
91 | return NULL;
|
---|
92 | }
|
---|
93 |
|
---|
94 | namespace->name = str_dup(name);
|
---|
95 | if (namespace->name == NULL) {
|
---|
96 | free(namespace);
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 |
|
---|
100 | char *logfilename;
|
---|
101 | int rc = asprintf(&logfilename, "/log/%s", name);
|
---|
102 | if (rc < 0) {
|
---|
103 | free(namespace->name);
|
---|
104 | free(namespace);
|
---|
105 | return NULL;
|
---|
106 | }
|
---|
107 | namespace->logfile = fopen(logfilename, "a");
|
---|
108 | free(logfilename);
|
---|
109 | if (namespace->logfile == NULL) {
|
---|
110 | free(namespace->name);
|
---|
111 | free(namespace);
|
---|
112 | return NULL;
|
---|
113 | }
|
---|
114 |
|
---|
115 | namespace->level = LOG_LEVEL_USE_DEFAULT;
|
---|
116 |
|
---|
117 | fibril_mutex_initialize(&namespace->guard);
|
---|
118 | fibril_condvar_initialize(&namespace->level_changed_cv);
|
---|
119 | namespace->writers_count = 0;
|
---|
120 | link_initialize(&namespace->link);
|
---|
121 |
|
---|
122 | list_append(&namespace->link, &namespace_list);
|
---|
123 |
|
---|
124 | return namespace;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | logging_namespace_t *namespace_create(const char *name)
|
---|
129 | {
|
---|
130 | fibril_mutex_lock(&namespace_list_guard);
|
---|
131 | logging_namespace_t *result = namespace_create_no_lock(name);
|
---|
132 | fibril_mutex_unlock(&namespace_list_guard);
|
---|
133 | return result;
|
---|
134 | }
|
---|
135 |
|
---|
136 | const char *namespace_get_name(logging_namespace_t *namespace)
|
---|
137 | {
|
---|
138 | assert(namespace);
|
---|
139 | return namespace->name;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void namespace_destroy_careful(logging_namespace_t *namespace)
|
---|
143 | {
|
---|
144 | assert(namespace);
|
---|
145 | fibril_mutex_lock(&namespace_list_guard);
|
---|
146 |
|
---|
147 | fibril_mutex_lock(&namespace->guard);
|
---|
148 | if (namespace->writers_count > 0) {
|
---|
149 | fibril_mutex_unlock(&namespace->guard);
|
---|
150 | fibril_mutex_unlock(&namespace_list_guard);
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | list_remove(&namespace->link);
|
---|
155 |
|
---|
156 | fibril_mutex_unlock(&namespace->guard);
|
---|
157 | fibril_mutex_unlock(&namespace_list_guard);
|
---|
158 |
|
---|
159 | // TODO - destroy pending messages
|
---|
160 | fclose(namespace->logfile);
|
---|
161 | free(namespace->name);
|
---|
162 | free(namespace);
|
---|
163 | }
|
---|
164 |
|
---|
165 | void namespace_destroy(logging_namespace_t *namespace)
|
---|
166 | {
|
---|
167 | namespace_destroy_careful(namespace);
|
---|
168 | }
|
---|
169 |
|
---|
170 | logging_namespace_t *namespace_writer_attach(const char *name)
|
---|
171 | {
|
---|
172 | logging_namespace_t *namespace = NULL;
|
---|
173 |
|
---|
174 | fibril_mutex_lock(&namespace_list_guard);
|
---|
175 |
|
---|
176 | namespace = namespace_find_no_lock(name);
|
---|
177 |
|
---|
178 | if (namespace == NULL) {
|
---|
179 | namespace = namespace_create_no_lock(name);
|
---|
180 | }
|
---|
181 |
|
---|
182 | fibril_mutex_lock(&namespace->guard);
|
---|
183 | namespace->writers_count++;
|
---|
184 | fibril_mutex_unlock(&namespace->guard);
|
---|
185 |
|
---|
186 | fibril_mutex_unlock(&namespace_list_guard);
|
---|
187 |
|
---|
188 | return namespace;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void namespace_writer_detach(logging_namespace_t *namespace)
|
---|
192 | {
|
---|
193 | fibril_mutex_lock(&namespace->guard);
|
---|
194 | assert(namespace->writers_count > 0);
|
---|
195 | namespace->writers_count--;
|
---|
196 | fibril_mutex_unlock(&namespace->guard);
|
---|
197 |
|
---|
198 | namespace_destroy_careful(namespace);
|
---|
199 | }
|
---|
200 |
|
---|
201 | int namespace_change_level(logging_namespace_t *namespace, log_level_t level)
|
---|
202 | {
|
---|
203 | if (level >= LVL_LIMIT)
|
---|
204 | return ERANGE;
|
---|
205 |
|
---|
206 | fibril_mutex_lock(&namespace->guard);
|
---|
207 | namespace->level = level;
|
---|
208 | fibril_condvar_broadcast(&namespace->level_changed_cv);
|
---|
209 | fibril_mutex_unlock(&namespace->guard);
|
---|
210 |
|
---|
211 | return EOK;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | bool namespace_has_reader(logging_namespace_t *namespace, log_level_t level)
|
---|
216 | {
|
---|
217 | return level <= namespace_get_actual_log_level(namespace);
|
---|
218 | }
|
---|
219 |
|
---|
220 | void namespace_wait_for_reader_change(logging_namespace_t *namespace, bool *has_reader_now)
|
---|
221 | {
|
---|
222 | fibril_mutex_lock(&namespace->guard);
|
---|
223 | log_level_t previous_level = namespace->level;
|
---|
224 | while (previous_level == namespace->level) {
|
---|
225 | fibril_condvar_wait(&namespace->level_changed_cv, &namespace->guard);
|
---|
226 | }
|
---|
227 | *has_reader_now = true;
|
---|
228 | fibril_mutex_unlock(&namespace->guard);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | void namespace_add_message(logging_namespace_t *namespace, const char *message, log_level_t level)
|
---|
233 | {
|
---|
234 | if (level <= namespace_get_actual_log_level(namespace)) {
|
---|
235 | const char *level_name = log_level_str(level);
|
---|
236 | printf("[%s %s]: %s\n", namespace->name, level_name, message);
|
---|
237 | fprintf(namespace->logfile, "%s: %s\n", level_name, message);
|
---|
238 | fflush(namespace->logfile);
|
---|
239 | fflush(stdout);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @}
|
---|
246 | */
|
---|