source: mainline/uspace/lib/c/generic/io/log.c@ 80d8885

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 80d8885 was 793cce15, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Add logging contexts

  • Property mode set to 100644
File size: 8.0 KB
Line 
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. */
46static const char *log_prog_name;
47
48static 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. */
59static async_sess_t *logger_session;
60
61/** Maximum length of a single log message (in bytes). */
62#define MESSAGE_BUFFER_SIZE 4096
63
64FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
65log_level_t current_observed_level;
66
67static 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, &reg_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
88static int logger_message(async_sess_t *session, log_context_t ctx, 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_2(exchange, LOGGER_MESSAGE,
96 ctx, level, NULL);
97 int rc = async_data_write_start(exchange, message, str_size(message));
98 sysarg_t reg_msg_rc;
99 async_wait_for(reg_msg, &reg_msg_rc);
100
101 async_exchange_end(exchange);
102
103 /*
104 * Getting ENAK means no-one wants our message. That is not an
105 * error at all.
106 */
107 if (rc == ENAK)
108 rc = EOK;
109
110 if (rc != EOK) {
111 return rc;
112 }
113
114 return reg_msg_rc;
115}
116
117static void cannot_use_level_changed_monitor(void)
118{
119 fibril_rwlock_write_lock(&current_observed_level_lock);
120 current_observed_level = LVL_LIMIT;
121 fibril_rwlock_write_unlock(&current_observed_level_lock);
122}
123
124static int observed_level_changed_monitor(void *arg)
125{
126 async_sess_t *monitor_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
127 if (monitor_session == NULL) {
128 cannot_use_level_changed_monitor();
129 return ENOMEM;
130 }
131
132 int rc = logger_register(monitor_session, log_prog_name);
133 if (rc != EOK) {
134 cannot_use_level_changed_monitor();
135 return rc;
136 }
137
138 async_exch_t *exchange = async_exchange_begin(monitor_session);
139 if (exchange == NULL) {
140 cannot_use_level_changed_monitor();
141 return ENOMEM;
142 }
143
144 while (true) {
145 sysarg_t has_reader;
146 sysarg_t msg_rc = async_req_0_1(exchange,
147 LOGGER_BLOCK_UNTIL_READER_CHANGED, &has_reader);
148 if (msg_rc != EOK) {
149 cannot_use_level_changed_monitor();
150 break;
151 }
152
153 fibril_rwlock_write_lock(&current_observed_level_lock);
154 if ((bool) has_reader) {
155 current_observed_level = LVL_LIMIT;
156 } else {
157 current_observed_level = LVL_NOTE;
158 }
159 fibril_rwlock_write_unlock(&current_observed_level_lock);
160 }
161
162 async_exchange_end(exchange);
163
164 return EOK;
165}
166
167static log_level_t get_current_observed_level(void)
168{
169 fibril_rwlock_read_lock(&current_observed_level_lock);
170 log_level_t level = current_observed_level;
171 fibril_rwlock_read_unlock(&current_observed_level_lock);
172 return level;
173}
174
175const char *log_level_str(log_level_t level)
176{
177 if (level >= LVL_LIMIT)
178 return "unknown";
179 else
180 return log_level_names[level];
181}
182
183int log_level_from_str(const char *name, log_level_t *level_out)
184{
185 log_level_t level = LVL_FATAL;
186
187 while (log_level_names[level] != NULL) {
188 if (str_cmp(name, log_level_names[level]) == 0) {
189 if (level_out != NULL)
190 *level_out = level;
191 return EOK;
192 }
193 level++;
194 }
195
196 /* Maybe user specified number directly. */
197 char *end_ptr;
198 int level_int = strtol(name, &end_ptr, 0);
199 if ((end_ptr == name) || (str_length(end_ptr) != 0))
200 return EINVAL;
201 if (level_int < 0)
202 return ERANGE;
203 if (level_int >= (int) LVL_LIMIT)
204 return ERANGE;
205
206 if (level_out != NULL)
207 *level_out = (log_level_t) level_int;
208
209 return EOK;
210}
211
212/** Initialize the logging system.
213 *
214 * @param prog_name Program name, will be printed as part of message
215 * @param level Minimum message level to print
216 */
217int log_init(const char *prog_name, log_level_t level)
218{
219 assert(level < LVL_LIMIT);
220
221 log_prog_name = str_dup(prog_name);
222 if (log_prog_name == NULL)
223 return ENOMEM;
224
225 logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
226 if (logger_session == NULL) {
227 return ENOMEM;
228 }
229
230 int rc = logger_register(logger_session, log_prog_name);
231
232 current_observed_level = LVL_NOTE;
233
234 fid_t observed_level_changed_fibril = fibril_create(observed_level_changed_monitor, NULL);
235 if (observed_level_changed_fibril == 0) {
236 cannot_use_level_changed_monitor();
237 } else {
238 fibril_add_ready(observed_level_changed_fibril);
239 }
240
241 return rc;
242}
243
244/** Create logging context.
245 *
246 * This function always returns a valid context.
247 */
248log_context_t log_context_create(const char *name)
249{
250 async_exch_t *exchange = async_exchange_begin(logger_session);
251 if (exchange == NULL)
252 return LOG_CONTEXT_DEFAULT;
253
254 ipc_call_t answer;
255 aid_t reg_msg = async_send_0(exchange, LOGGER_CREATE_CONTEXT, &answer);
256 int rc = async_data_write_start(exchange, name, str_size(name));
257 sysarg_t reg_msg_rc;
258 async_wait_for(reg_msg, &reg_msg_rc);
259
260 async_exchange_end(exchange);
261
262 if ((rc != EOK) || (reg_msg_rc != EOK))
263 return LOG_CONTEXT_DEFAULT;
264
265 return IPC_GET_ARG1(answer);
266}
267
268bool _log_shall_record(log_context_t context, log_level_t level)
269{
270 return get_current_observed_level() >= level;
271}
272
273/** Write an entry to the log.
274 *
275 * @param level Message verbosity level. Message is only printed
276 * if verbosity is less than or equal to current
277 * reporting level.
278 * @param fmt Format string (no traling newline).
279 */
280void _log_ctx_msg(log_context_t ctx, log_level_t level, const char *fmt, ...)
281{
282 va_list args;
283
284 va_start(args, fmt);
285 _log_ctx_msgv(ctx, level, fmt, args);
286 va_end(args);
287}
288
289/** Write an entry to the log (va_list variant).
290 *
291 * @param level Message verbosity level. Message is only printed
292 * if verbosity is less than or equal to current
293 * reporting level.
294 * @param fmt Format string (no trailing newline)
295 */
296void _log_ctx_msgv(log_context_t ctx, log_level_t level, const char *fmt, va_list args)
297{
298 assert(level < LVL_LIMIT);
299
300 if (get_current_observed_level() < level) {
301 return;
302 }
303
304 char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
305 if (message_buffer == NULL) {
306 return;
307 }
308
309 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
310 logger_message(logger_session, ctx, level, message_buffer);
311}
312
313/** @}
314 */
Note: See TracBrowser for help on using the repository browser.