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