source: mainline/uspace/lib/c/generic/io/log.c@ 717a0aa

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

Rename double-underscored functions

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[9b415c9]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>
[fc51296]37#include <stdarg.h>
[9b415c9]38#include <stdlib.h>
39#include <stdio.h>
[1f2dd20]40#include <async.h>
[9b415c9]41#include <io/log.h>
[1f2dd20]42#include <ipc/logger.h>
43#include <ns.h>
[9b415c9]44
[1f2dd20]45/** Log messages are printed under this name. */
46static const char *log_prog_name;
[9b415c9]47
[1c67b41]48static const char *log_level_names[] = {
49 "fatal",
50 "error",
51 "warn",
52 "note",
53 "debug",
[eab3d04]54 "debug2",
55 NULL
[1c67b41]56};
57
[1f2dd20]58/** IPC session with the logger service. */
59static async_sess_t *logger_session;
[9b415c9]60
[1f2dd20]61/** Maximum length of a single log message (in bytes). */
62#define MESSAGE_BUFFER_SIZE 4096
[9b415c9]63
[2e39656]64FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
65log_level_t current_observed_level;
66
[1f2dd20]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_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, &reg_msg_rc);
99
100 async_exchange_end(exchange);
[9b415c9]101
[f6bc83a]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
[1f2dd20]109 if (rc != EOK) {
110 return rc;
111 }
112
113 return reg_msg_rc;
114}
[9b415c9]115
[2e39656]116static void cannot_use_level_changed_monitor(void)
117{
[be73793]118 fibril_rwlock_write_lock(&current_observed_level_lock);
119 current_observed_level = LVL_LIMIT;
120 fibril_rwlock_write_unlock(&current_observed_level_lock);
[2e39656]121}
122
123static 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(&current_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(&current_observed_level_lock);
159 }
160
161 async_exchange_end(exchange);
162
163 return EOK;
164}
165
166static log_level_t get_current_observed_level(void)
167{
168 fibril_rwlock_read_lock(&current_observed_level_lock);
169 log_level_t level = current_observed_level;
170 fibril_rwlock_read_unlock(&current_observed_level_lock);
171 return level;
172}
173
[1c67b41]174const 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
[eab3d04]182int 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
[9b415c9]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 */
216int 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
[1f2dd20]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
[2e39656]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
[1f2dd20]240 return rc;
[9b415c9]241}
242
[717a0aa]243bool _log_shall_record(log_level_t level)
[14de4106]244{
245 return get_current_observed_level() >= level;
246}
247
[9b415c9]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.
[ebcb05a]253 * @param fmt Format string (no traling newline).
[9b415c9]254 */
[717a0aa]255void _log_msg(log_level_t level, const char *fmt, ...)
[9b415c9]256{
257 va_list args;
258
[fc51296]259 va_start(args, fmt);
[717a0aa]260 _log_msgv(level, fmt, args);
[fc51296]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.
[ebcb05a]269 * @param fmt Format string (no trailing newline)
[fc51296]270 */
[717a0aa]271void _log_msgv(log_level_t level, const char *fmt, va_list args)
[fc51296]272{
[9b415c9]273 assert(level < LVL_LIMIT);
274
[2e39656]275 if (get_current_observed_level() < level) {
276 return;
277 }
278
[1f2dd20]279 char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
280 if (message_buffer == NULL) {
281 return;
[9b415c9]282 }
[1f2dd20]283
284 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
285 logger_message(logger_session, level, message_buffer);
[9b415c9]286}
287
288/** @}
289 */
Note: See TracBrowser for help on using the repository browser.