source: mainline/uspace/lib/c/generic/io/log.c@ 3ab9de0

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

Make logger runnable again

  • Property mode set to 100644
File size: 6.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
[1f2dd20]48/** IPC session with the logger service. */
49static async_sess_t *logger_session;
[9b415c9]50
[1f2dd20]51/** Maximum length of a single log message (in bytes). */
52#define MESSAGE_BUFFER_SIZE 4096
[9b415c9]53
[2e39656]54FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
55log_level_t current_observed_level;
56
[1f2dd20]57static int logger_register(async_sess_t *session, const char *prog_name)
58{
59 async_exch_t *exchange = async_exchange_begin(session);
60 if (exchange == NULL) {
61 return ENOMEM;
62 }
63
64 aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL);
65 int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
66 sysarg_t reg_msg_rc;
67 async_wait_for(reg_msg, &reg_msg_rc);
68
69 async_exchange_end(exchange);
70
71 if (rc != EOK) {
72 return rc;
73 }
74
75 return reg_msg_rc;
76}
77
78static int logger_message(async_sess_t *session, log_level_t level, const char *message)
79{
80 async_exch_t *exchange = async_exchange_begin(session);
81 if (exchange == NULL) {
82 return ENOMEM;
83 }
84
85 aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL);
86 int rc = async_data_write_start(exchange, message, str_size(message));
87 sysarg_t reg_msg_rc;
88 async_wait_for(reg_msg, &reg_msg_rc);
89
90 async_exchange_end(exchange);
[9b415c9]91
[f6bc83a]92 /*
93 * Getting ENAK means no-one wants our message. That is not an
94 * error at all.
95 */
96 if (rc == ENAK)
97 rc = EOK;
98
[1f2dd20]99 if (rc != EOK) {
100 return rc;
101 }
102
103 return reg_msg_rc;
104}
[9b415c9]105
[2e39656]106static void cannot_use_level_changed_monitor(void)
107{
[be73793]108 fibril_rwlock_write_lock(&current_observed_level_lock);
109 current_observed_level = LVL_LIMIT;
110 fibril_rwlock_write_unlock(&current_observed_level_lock);
[2e39656]111}
112
113static int observed_level_changed_monitor(void *arg)
114{
115 async_sess_t *monitor_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
116 if (monitor_session == NULL) {
117 cannot_use_level_changed_monitor();
118 return ENOMEM;
119 }
120
121 int rc = logger_register(monitor_session, log_prog_name);
122 if (rc != EOK) {
123 cannot_use_level_changed_monitor();
124 return rc;
125 }
126
127 async_exch_t *exchange = async_exchange_begin(monitor_session);
128 if (exchange == NULL) {
129 cannot_use_level_changed_monitor();
130 return ENOMEM;
131 }
132
133 while (true) {
134 sysarg_t has_reader;
135 sysarg_t msg_rc = async_req_0_1(exchange,
136 LOGGER_BLOCK_UNTIL_READER_CHANGED, &has_reader);
137 if (msg_rc != EOK) {
138 cannot_use_level_changed_monitor();
139 break;
140 }
141
142 fibril_rwlock_write_lock(&current_observed_level_lock);
143 if ((bool) has_reader) {
144 current_observed_level = LVL_LIMIT;
145 } else {
146 current_observed_level = LVL_NOTE;
147 }
148 fibril_rwlock_write_unlock(&current_observed_level_lock);
149 }
150
151 async_exchange_end(exchange);
152
153 return EOK;
154}
155
156static log_level_t get_current_observed_level(void)
157{
158 fibril_rwlock_read_lock(&current_observed_level_lock);
159 log_level_t level = current_observed_level;
160 fibril_rwlock_read_unlock(&current_observed_level_lock);
161 return level;
162}
163
[9b415c9]164/** Initialize the logging system.
165 *
166 * @param prog_name Program name, will be printed as part of message
167 * @param level Minimum message level to print
168 */
169int log_init(const char *prog_name, log_level_t level)
170{
171 assert(level < LVL_LIMIT);
172
173 log_prog_name = str_dup(prog_name);
174 if (log_prog_name == NULL)
175 return ENOMEM;
176
[1f2dd20]177 logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
178 if (logger_session == NULL) {
179 return ENOMEM;
180 }
181
182 int rc = logger_register(logger_session, log_prog_name);
183
[2e39656]184 current_observed_level = LVL_NOTE;
185
186 fid_t observed_level_changed_fibril = fibril_create(observed_level_changed_monitor, NULL);
187 if (observed_level_changed_fibril == 0) {
188 cannot_use_level_changed_monitor();
189 } else {
190 fibril_add_ready(observed_level_changed_fibril);
191 }
192
[1f2dd20]193 return rc;
[9b415c9]194}
195
196/** Write an entry to the log.
197 *
198 * @param level Message verbosity level. Message is only printed
199 * if verbosity is less than or equal to current
200 * reporting level.
[ebcb05a]201 * @param fmt Format string (no traling newline).
[9b415c9]202 */
203void log_msg(log_level_t level, const char *fmt, ...)
204{
205 va_list args;
206
[fc51296]207 va_start(args, fmt);
208 log_msgv(level, fmt, args);
209 va_end(args);
210}
211
212/** Write an entry to the log (va_list variant).
213 *
214 * @param level Message verbosity level. Message is only printed
215 * if verbosity is less than or equal to current
216 * reporting level.
[ebcb05a]217 * @param fmt Format string (no trailing newline)
[fc51296]218 */
219void log_msgv(log_level_t level, const char *fmt, va_list args)
220{
[9b415c9]221 assert(level < LVL_LIMIT);
222
[2e39656]223 if (get_current_observed_level() < level) {
224 return;
225 }
226
[1f2dd20]227 char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
228 if (message_buffer == NULL) {
229 return;
[9b415c9]230 }
[1f2dd20]231
232 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
233 logger_message(logger_session, level, message_buffer);
[9b415c9]234}
235
236/** @}
237 */
Note: See TracBrowser for help on using the repository browser.