1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <assert.h>
|
---|
35 | #include <errno.h>
|
---|
36 | #include <fibril_synch.h>
|
---|
37 | #include <stdarg.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <ipc/logger.h>
|
---|
43 | #include <ns.h>
|
---|
44 |
|
---|
45 | /** Log messages are printed under this name. */
|
---|
46 | static const char *log_prog_name;
|
---|
47 |
|
---|
48 | static const char *log_level_names[] = {
|
---|
49 | "fatal",
|
---|
50 | "error",
|
---|
51 | "warn",
|
---|
52 | "note",
|
---|
53 | "debug",
|
---|
54 | "debug2",
|
---|
55 | NULL
|
---|
56 | };
|
---|
57 |
|
---|
58 | /** IPC session with the logger service. */
|
---|
59 | static async_sess_t *logger_session;
|
---|
60 |
|
---|
61 | /** Maximum length of a single log message (in bytes). */
|
---|
62 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
63 |
|
---|
64 | FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
|
---|
65 | log_level_t current_observed_level;
|
---|
66 |
|
---|
67 | static int logger_register(async_sess_t *session, const char *prog_name)
|
---|
68 | {
|
---|
69 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
70 | if (exchange == NULL) {
|
---|
71 | return ENOMEM;
|
---|
72 | }
|
---|
73 |
|
---|
74 | aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL);
|
---|
75 | int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
|
---|
76 | sysarg_t reg_msg_rc;
|
---|
77 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
78 |
|
---|
79 | async_exchange_end(exchange);
|
---|
80 |
|
---|
81 | if (rc != EOK) {
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 |
|
---|
85 | return reg_msg_rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | static int logger_message(async_sess_t *session, log_level_t level, const char *message)
|
---|
89 | {
|
---|
90 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
91 | if (exchange == NULL) {
|
---|
92 | return ENOMEM;
|
---|
93 | }
|
---|
94 |
|
---|
95 | aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL);
|
---|
96 | int rc = async_data_write_start(exchange, message, str_size(message));
|
---|
97 | sysarg_t reg_msg_rc;
|
---|
98 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
99 |
|
---|
100 | async_exchange_end(exchange);
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * Getting ENAK means no-one wants our message. That is not an
|
---|
104 | * error at all.
|
---|
105 | */
|
---|
106 | if (rc == ENAK)
|
---|
107 | rc = EOK;
|
---|
108 |
|
---|
109 | if (rc != EOK) {
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return reg_msg_rc;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static void cannot_use_level_changed_monitor(void)
|
---|
117 | {
|
---|
118 | fibril_rwlock_write_lock(¤t_observed_level_lock);
|
---|
119 | current_observed_level = LVL_LIMIT;
|
---|
120 | fibril_rwlock_write_unlock(¤t_observed_level_lock);
|
---|
121 | }
|
---|
122 |
|
---|
123 | static int observed_level_changed_monitor(void *arg)
|
---|
124 | {
|
---|
125 | async_sess_t *monitor_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
126 | if (monitor_session == NULL) {
|
---|
127 | cannot_use_level_changed_monitor();
|
---|
128 | return ENOMEM;
|
---|
129 | }
|
---|
130 |
|
---|
131 | int rc = logger_register(monitor_session, log_prog_name);
|
---|
132 | if (rc != EOK) {
|
---|
133 | cannot_use_level_changed_monitor();
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 |
|
---|
137 | async_exch_t *exchange = async_exchange_begin(monitor_session);
|
---|
138 | if (exchange == NULL) {
|
---|
139 | cannot_use_level_changed_monitor();
|
---|
140 | return ENOMEM;
|
---|
141 | }
|
---|
142 |
|
---|
143 | while (true) {
|
---|
144 | sysarg_t has_reader;
|
---|
145 | sysarg_t msg_rc = async_req_0_1(exchange,
|
---|
146 | LOGGER_BLOCK_UNTIL_READER_CHANGED, &has_reader);
|
---|
147 | if (msg_rc != EOK) {
|
---|
148 | cannot_use_level_changed_monitor();
|
---|
149 | break;
|
---|
150 | }
|
---|
151 |
|
---|
152 | fibril_rwlock_write_lock(¤t_observed_level_lock);
|
---|
153 | if ((bool) has_reader) {
|
---|
154 | current_observed_level = LVL_LIMIT;
|
---|
155 | } else {
|
---|
156 | current_observed_level = LVL_NOTE;
|
---|
157 | }
|
---|
158 | fibril_rwlock_write_unlock(¤t_observed_level_lock);
|
---|
159 | }
|
---|
160 |
|
---|
161 | async_exchange_end(exchange);
|
---|
162 |
|
---|
163 | return EOK;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static log_level_t get_current_observed_level(void)
|
---|
167 | {
|
---|
168 | fibril_rwlock_read_lock(¤t_observed_level_lock);
|
---|
169 | log_level_t level = current_observed_level;
|
---|
170 | fibril_rwlock_read_unlock(¤t_observed_level_lock);
|
---|
171 | return level;
|
---|
172 | }
|
---|
173 |
|
---|
174 | const char *log_level_str(log_level_t level)
|
---|
175 | {
|
---|
176 | if (level >= LVL_LIMIT)
|
---|
177 | return "unknown";
|
---|
178 | else
|
---|
179 | return log_level_names[level];
|
---|
180 | }
|
---|
181 |
|
---|
182 | int log_level_from_str(const char *name, log_level_t *level_out)
|
---|
183 | {
|
---|
184 | log_level_t level = LVL_FATAL;
|
---|
185 |
|
---|
186 | while (log_level_names[level] != NULL) {
|
---|
187 | if (str_cmp(name, log_level_names[level]) == 0) {
|
---|
188 | if (level_out != NULL)
|
---|
189 | *level_out = level;
|
---|
190 | return EOK;
|
---|
191 | }
|
---|
192 | level++;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /* Maybe user specified number directly. */
|
---|
196 | char *end_ptr;
|
---|
197 | int level_int = strtol(name, &end_ptr, 0);
|
---|
198 | if ((end_ptr == name) || (str_length(end_ptr) != 0))
|
---|
199 | return EINVAL;
|
---|
200 | if (level_int < 0)
|
---|
201 | return ERANGE;
|
---|
202 | if (level_int >= (int) LVL_LIMIT)
|
---|
203 | return ERANGE;
|
---|
204 |
|
---|
205 | if (level_out != NULL)
|
---|
206 | *level_out = (log_level_t) level_int;
|
---|
207 |
|
---|
208 | return EOK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Initialize the logging system.
|
---|
212 | *
|
---|
213 | * @param prog_name Program name, will be printed as part of message
|
---|
214 | * @param level Minimum message level to print
|
---|
215 | */
|
---|
216 | int log_init(const char *prog_name, log_level_t level)
|
---|
217 | {
|
---|
218 | assert(level < LVL_LIMIT);
|
---|
219 |
|
---|
220 | log_prog_name = str_dup(prog_name);
|
---|
221 | if (log_prog_name == NULL)
|
---|
222 | return ENOMEM;
|
---|
223 |
|
---|
224 | logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
225 | if (logger_session == NULL) {
|
---|
226 | return ENOMEM;
|
---|
227 | }
|
---|
228 |
|
---|
229 | int rc = logger_register(logger_session, log_prog_name);
|
---|
230 |
|
---|
231 | current_observed_level = LVL_NOTE;
|
---|
232 |
|
---|
233 | fid_t observed_level_changed_fibril = fibril_create(observed_level_changed_monitor, NULL);
|
---|
234 | if (observed_level_changed_fibril == 0) {
|
---|
235 | cannot_use_level_changed_monitor();
|
---|
236 | } else {
|
---|
237 | fibril_add_ready(observed_level_changed_fibril);
|
---|
238 | }
|
---|
239 |
|
---|
240 | return rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 | bool __log_shall_record(log_level_t level)
|
---|
244 | {
|
---|
245 | return get_current_observed_level() >= level;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /** Write an entry to the log.
|
---|
249 | *
|
---|
250 | * @param level Message verbosity level. Message is only printed
|
---|
251 | * if verbosity is less than or equal to current
|
---|
252 | * reporting level.
|
---|
253 | * @param fmt Format string (no traling newline).
|
---|
254 | */
|
---|
255 | void __log_msg(log_level_t level, const char *fmt, ...)
|
---|
256 | {
|
---|
257 | va_list args;
|
---|
258 |
|
---|
259 | va_start(args, fmt);
|
---|
260 | __log_msgv(level, fmt, args);
|
---|
261 | va_end(args);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** Write an entry to the log (va_list variant).
|
---|
265 | *
|
---|
266 | * @param level Message verbosity level. Message is only printed
|
---|
267 | * if verbosity is less than or equal to current
|
---|
268 | * reporting level.
|
---|
269 | * @param fmt Format string (no trailing newline)
|
---|
270 | */
|
---|
271 | void __log_msgv(log_level_t level, const char *fmt, va_list args)
|
---|
272 | {
|
---|
273 | assert(level < LVL_LIMIT);
|
---|
274 |
|
---|
275 | if (get_current_observed_level() < level) {
|
---|
276 | return;
|
---|
277 | }
|
---|
278 |
|
---|
279 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
280 | if (message_buffer == NULL) {
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
285 | logger_message(logger_session, level, message_buffer);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** @}
|
---|
289 | */
|
---|