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

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

Remove premature optimizations

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