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 | typedef struct {
|
---|
46 | char *name;
|
---|
47 | sysarg_t top_log_id;
|
---|
48 | sysarg_t log_id;
|
---|
49 | } log_info_t;
|
---|
50 |
|
---|
51 | static log_info_t default_log = {
|
---|
52 | .name = NULL,
|
---|
53 | .top_log_id = 0,
|
---|
54 | .log_id = 0
|
---|
55 | };
|
---|
56 |
|
---|
57 | static sysarg_t default_top_log_id;
|
---|
58 |
|
---|
59 | /** Log messages are printed under this name. */
|
---|
60 | static const char *log_prog_name;
|
---|
61 |
|
---|
62 | static const char *log_level_names[] = {
|
---|
63 | "fatal",
|
---|
64 | "error",
|
---|
65 | "warn",
|
---|
66 | "note",
|
---|
67 | "debug",
|
---|
68 | "debug2",
|
---|
69 | NULL
|
---|
70 | };
|
---|
71 |
|
---|
72 | /** IPC session with the logger service. */
|
---|
73 | static async_sess_t *logger_session;
|
---|
74 |
|
---|
75 | /** Maximum length of a single log message (in bytes). */
|
---|
76 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
77 |
|
---|
78 |
|
---|
79 | static int logger_register(async_sess_t *session, const char *prog_name)
|
---|
80 | {
|
---|
81 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
82 | if (exchange == NULL) {
|
---|
83 | return ENOMEM;
|
---|
84 | }
|
---|
85 |
|
---|
86 | ipc_call_t answer;
|
---|
87 | aid_t reg_msg = async_send_0(exchange, LOGGER_WRITER_CREATE_TOPLEVEL_LOG, &answer);
|
---|
88 | int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
|
---|
89 | sysarg_t reg_msg_rc;
|
---|
90 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
91 |
|
---|
92 | async_exchange_end(exchange);
|
---|
93 |
|
---|
94 | if (rc != EOK) {
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (reg_msg_rc != EOK)
|
---|
99 | return reg_msg_rc;
|
---|
100 |
|
---|
101 | default_top_log_id = IPC_GET_ARG1(answer);
|
---|
102 | default_log.top_log_id = default_top_log_id;
|
---|
103 |
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static int logger_message(async_sess_t *session, log_t ctx, log_level_t level, const char *message)
|
---|
108 | {
|
---|
109 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
110 | if (exchange == NULL) {
|
---|
111 | return ENOMEM;
|
---|
112 | }
|
---|
113 | log_info_t *log_info = ctx != 0 ? (log_info_t *) ctx : &default_log;
|
---|
114 |
|
---|
115 | aid_t reg_msg = async_send_3(exchange, LOGGER_WRITER_MESSAGE,
|
---|
116 | log_info->top_log_id, log_info->log_id, level, NULL);
|
---|
117 | int rc = async_data_write_start(exchange, message, str_size(message));
|
---|
118 | sysarg_t reg_msg_rc;
|
---|
119 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
120 |
|
---|
121 | async_exchange_end(exchange);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Getting ENAK means no-one wants our message. That is not an
|
---|
125 | * error at all.
|
---|
126 | */
|
---|
127 | if (rc == ENAK)
|
---|
128 | rc = EOK;
|
---|
129 |
|
---|
130 | if (rc != EOK) {
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return reg_msg_rc;
|
---|
135 | }
|
---|
136 |
|
---|
137 | const char *log_level_str(log_level_t level)
|
---|
138 | {
|
---|
139 | if (level >= LVL_LIMIT)
|
---|
140 | return "unknown";
|
---|
141 | else
|
---|
142 | return log_level_names[level];
|
---|
143 | }
|
---|
144 |
|
---|
145 | int log_level_from_str(const char *name, log_level_t *level_out)
|
---|
146 | {
|
---|
147 | log_level_t level = LVL_FATAL;
|
---|
148 |
|
---|
149 | while (log_level_names[level] != NULL) {
|
---|
150 | if (str_cmp(name, log_level_names[level]) == 0) {
|
---|
151 | if (level_out != NULL)
|
---|
152 | *level_out = level;
|
---|
153 | return EOK;
|
---|
154 | }
|
---|
155 | level++;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* Maybe user specified number directly. */
|
---|
159 | char *end_ptr;
|
---|
160 | int level_int = strtol(name, &end_ptr, 0);
|
---|
161 | if ((end_ptr == name) || (str_length(end_ptr) != 0))
|
---|
162 | return EINVAL;
|
---|
163 | if (level_int < 0)
|
---|
164 | return ERANGE;
|
---|
165 | if (level_int >= (int) LVL_LIMIT)
|
---|
166 | return ERANGE;
|
---|
167 |
|
---|
168 | if (level_out != NULL)
|
---|
169 | *level_out = (log_level_t) level_int;
|
---|
170 |
|
---|
171 | return EOK;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Initialize the logging system.
|
---|
175 | *
|
---|
176 | * @param prog_name Program name, will be printed as part of message
|
---|
177 | * @param level Minimum message level to print
|
---|
178 | */
|
---|
179 | int log_init(const char *prog_name, log_level_t level)
|
---|
180 | {
|
---|
181 | assert(level < LVL_LIMIT);
|
---|
182 |
|
---|
183 | log_prog_name = str_dup(prog_name);
|
---|
184 | if (log_prog_name == NULL)
|
---|
185 | return ENOMEM;
|
---|
186 |
|
---|
187 | logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
188 | if (logger_session == NULL) {
|
---|
189 | return ENOMEM;
|
---|
190 | }
|
---|
191 |
|
---|
192 | int rc = logger_register(logger_session, log_prog_name);
|
---|
193 |
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /** Create a new (sub-) log.
|
---|
198 | *
|
---|
199 | * This function always returns a valid log_t. In case of errors,
|
---|
200 | * @c parent is returned and errors are silently ignored.
|
---|
201 | *
|
---|
202 | * @param name Log name under which message will be reported (appended to parents name).
|
---|
203 | * @param parent Parent log.
|
---|
204 | * @return Opaque identifier of the newly created log.
|
---|
205 | */
|
---|
206 | log_t log_create(const char *name, log_t parent)
|
---|
207 | {
|
---|
208 | log_info_t *info = malloc(sizeof(log_info_t));
|
---|
209 | if (info == NULL)
|
---|
210 | return LOG_DEFAULT;
|
---|
211 | info->name = NULL;
|
---|
212 |
|
---|
213 | if (parent == LOG_DEFAULT) {
|
---|
214 | info->name = str_dup(name);
|
---|
215 | if (info->name == NULL)
|
---|
216 | goto error;
|
---|
217 | info->top_log_id = default_top_log_id;
|
---|
218 | } else {
|
---|
219 | log_info_t *parent_info = (log_info_t *) parent;
|
---|
220 | int rc = asprintf(&info->name, "%s/%s",
|
---|
221 | parent_info->name, name);
|
---|
222 | if (rc < 0)
|
---|
223 | goto error;
|
---|
224 | info->top_log_id = parent_info->top_log_id;
|
---|
225 | }
|
---|
226 |
|
---|
227 | async_exch_t *exchange = async_exchange_begin(logger_session);
|
---|
228 | if (exchange == NULL)
|
---|
229 | goto error;
|
---|
230 |
|
---|
231 | ipc_call_t answer;
|
---|
232 | aid_t reg_msg = async_send_1(exchange, LOGGER_WRITER_CREATE_SUB_LOG,
|
---|
233 | info->top_log_id, &answer);
|
---|
234 | int rc = async_data_write_start(exchange, info->name, str_size(info->name));
|
---|
235 | sysarg_t reg_msg_rc;
|
---|
236 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
237 |
|
---|
238 | async_exchange_end(exchange);
|
---|
239 |
|
---|
240 | if ((rc != EOK) || (reg_msg_rc != EOK))
|
---|
241 | goto error;
|
---|
242 |
|
---|
243 | info->log_id = IPC_GET_ARG1(answer);
|
---|
244 | return (sysarg_t) info;
|
---|
245 |
|
---|
246 | error:
|
---|
247 | free(info->name);
|
---|
248 | free(info);
|
---|
249 | return parent;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Write an entry to the log.
|
---|
253 | *
|
---|
254 | * @param level Message verbosity level. Message is only printed
|
---|
255 | * if verbosity is less than or equal to current
|
---|
256 | * reporting level.
|
---|
257 | * @param fmt Format string (no traling newline).
|
---|
258 | */
|
---|
259 | void log_log_msg(log_t ctx, log_level_t level, const char *fmt, ...)
|
---|
260 | {
|
---|
261 | va_list args;
|
---|
262 |
|
---|
263 | va_start(args, fmt);
|
---|
264 | log_log_msgv(ctx, level, fmt, args);
|
---|
265 | va_end(args);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /** Write an entry to the log (va_list variant).
|
---|
269 | *
|
---|
270 | * @param level Message verbosity level. Message is only printed
|
---|
271 | * if verbosity is less than or equal to current
|
---|
272 | * reporting level.
|
---|
273 | * @param fmt Format string (no trailing newline)
|
---|
274 | */
|
---|
275 | void log_log_msgv(log_t ctx, log_level_t level, const char *fmt, va_list args)
|
---|
276 | {
|
---|
277 | assert(level < LVL_LIMIT);
|
---|
278 |
|
---|
279 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
280 | if (message_buffer == NULL) {
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
285 | logger_message(logger_session, ctx, level, message_buffer);
|
---|
286 | }
|
---|
287 |
|
---|
288 | /** @}
|
---|
289 | */
|
---|