source: mainline/uspace/lib/c/generic/io/log.c@ 14de4106

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

Speed-up logging when not recorded

  • Property mode set to 100644
File size: 6.4 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
48/** IPC session with the logger service. */
49static async_sess_t *logger_session;
50
51/** Maximum length of a single log message (in bytes). */
52#define MESSAGE_BUFFER_SIZE 4096
53
54FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
55log_level_t current_observed_level;
56
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);
91
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
99 if (rc != EOK) {
100 return rc;
101 }
102
103 return reg_msg_rc;
104}
105
106static void cannot_use_level_changed_monitor(void)
107{
108 fibril_rwlock_write_lock(&current_observed_level_lock);
109 current_observed_level = LVL_LIMIT;
110 fibril_rwlock_write_unlock(&current_observed_level_lock);
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
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
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
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
193 return rc;
194}
195
196bool __log_shall_record(log_level_t level)
197{
198 return get_current_observed_level() >= level;
199}
200
201/** Write an entry to the log.
202 *
203 * @param level Message verbosity level. Message is only printed
204 * if verbosity is less than or equal to current
205 * reporting level.
206 * @param fmt Format string (no traling newline).
207 */
208void __log_msg(log_level_t level, const char *fmt, ...)
209{
210 va_list args;
211
212 va_start(args, fmt);
213 __log_msgv(level, fmt, args);
214 va_end(args);
215}
216
217/** Write an entry to the log (va_list variant).
218 *
219 * @param level Message verbosity level. Message is only printed
220 * if verbosity is less than or equal to current
221 * reporting level.
222 * @param fmt Format string (no trailing newline)
223 */
224void __log_msgv(log_level_t level, const char *fmt, va_list args)
225{
226 assert(level < LVL_LIMIT);
227
228 if (get_current_observed_level() < level) {
229 return;
230 }
231
232 char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
233 if (message_buffer == NULL) {
234 return;
235 }
236
237 vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
238 logger_message(logger_session, level, message_buffer);
239}
240
241/** @}
242 */
Note: See TracBrowser for help on using the repository browser.