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

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

Rewrite logger (work in progress)

Major issues

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