[9b415c9] | 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>
|
---|
[fc51296] | 37 | #include <stdarg.h>
|
---|
[9b415c9] | 38 | #include <stdlib.h>
|
---|
| 39 | #include <stdio.h>
|
---|
[1f2dd20] | 40 | #include <async.h>
|
---|
[9b415c9] | 41 | #include <io/log.h>
|
---|
[1f2dd20] | 42 | #include <ipc/logger.h>
|
---|
[1d6dd2a] | 43 | #include <str.h>
|
---|
[1f2dd20] | 44 | #include <ns.h>
|
---|
[9b415c9] | 45 |
|
---|
[e0c836e8] | 46 | /** Id of the first log we create at logger. */
|
---|
[f039dba] | 47 | static sysarg_t default_log_id;
|
---|
[2bf781a] | 48 |
|
---|
[1f2dd20] | 49 | /** Log messages are printed under this name. */
|
---|
| 50 | static const char *log_prog_name;
|
---|
[9b415c9] | 51 |
|
---|
[e0c836e8] | 52 | /** Names of individual log levels. */
|
---|
[1c67b41] | 53 | static const char *log_level_names[] = {
|
---|
| 54 | "fatal",
|
---|
| 55 | "error",
|
---|
| 56 | "warn",
|
---|
| 57 | "note",
|
---|
| 58 | "debug",
|
---|
[eab3d04] | 59 | "debug2",
|
---|
| 60 | NULL
|
---|
[1c67b41] | 61 | };
|
---|
| 62 |
|
---|
[1f2dd20] | 63 | /** IPC session with the logger service. */
|
---|
| 64 | static async_sess_t *logger_session;
|
---|
[9b415c9] | 65 |
|
---|
[1f2dd20] | 66 | /** Maximum length of a single log message (in bytes). */
|
---|
| 67 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
[9b415c9] | 68 |
|
---|
[e0c836e8] | 69 | /** Send formatted message to the logger service.
|
---|
| 70 | *
|
---|
| 71 | * @param session Initialized IPC session with the logger.
|
---|
| 72 | * @param log Log to use.
|
---|
| 73 | * @param level Verbosity level of the message.
|
---|
| 74 | * @param message The actual message.
|
---|
| 75 | * @return Error code of the conversion or EOK on success.
|
---|
| 76 | */
|
---|
[b7fd2a0] | 77 | static errno_t logger_message(async_sess_t *session, log_t log, log_level_t level, char *message)
|
---|
[1f2dd20] | 78 | {
|
---|
| 79 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
| 80 | if (exchange == NULL) {
|
---|
| 81 | return ENOMEM;
|
---|
| 82 | }
|
---|
[f039dba] | 83 | if (log == LOG_DEFAULT)
|
---|
| 84 | log = default_log_id;
|
---|
[1f2dd20] | 85 |
|
---|
[bc0ccab] | 86 | // FIXME: remove when all USB drivers use libc logging explicitly
|
---|
| 87 | str_rtrim(message, '\n');
|
---|
| 88 |
|
---|
[f039dba] | 89 | aid_t reg_msg = async_send_2(exchange, LOGGER_WRITER_MESSAGE,
|
---|
| 90 | log, level, NULL);
|
---|
[b7fd2a0] | 91 | errno_t rc = async_data_write_start(exchange, message, str_size(message));
|
---|
| 92 | errno_t reg_msg_rc;
|
---|
[1f2dd20] | 93 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
| 94 |
|
---|
| 95 | async_exchange_end(exchange);
|
---|
[9b415c9] | 96 |
|
---|
[f6bc83a] | 97 | /*
|
---|
| 98 | * Getting ENAK means no-one wants our message. That is not an
|
---|
| 99 | * error at all.
|
---|
| 100 | */
|
---|
| 101 | if (rc == ENAK)
|
---|
| 102 | rc = EOK;
|
---|
| 103 |
|
---|
[1f2dd20] | 104 | if (rc != EOK) {
|
---|
| 105 | return rc;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return reg_msg_rc;
|
---|
| 109 | }
|
---|
[9b415c9] | 110 |
|
---|
[e0c836e8] | 111 | /** Get name of the log level.
|
---|
| 112 | *
|
---|
| 113 | * @param level The log level.
|
---|
| 114 | * @return String name or "unknown".
|
---|
| 115 | */
|
---|
[1c67b41] | 116 | const char *log_level_str(log_level_t level)
|
---|
| 117 | {
|
---|
| 118 | if (level >= LVL_LIMIT)
|
---|
| 119 | return "unknown";
|
---|
| 120 | else
|
---|
| 121 | return log_level_names[level];
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[e0c836e8] | 124 | /** Convert log level name to the enum.
|
---|
| 125 | *
|
---|
| 126 | * @param[in] name Log level name or log level number.
|
---|
| 127 | * @param[out] level_out Where to store the result (set to NULL to ignore).
|
---|
| 128 | * @return Error code of the conversion or EOK on success.
|
---|
| 129 | */
|
---|
[b7fd2a0] | 130 | errno_t log_level_from_str(const char *name, log_level_t *level_out)
|
---|
[eab3d04] | 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 |
|
---|
[9b415c9] | 159 | /** Initialize the logging system.
|
---|
| 160 | *
|
---|
[e0c836e8] | 161 | * @param prog_name Program name, will be printed as part of message
|
---|
[9b415c9] | 162 | */
|
---|
[b7fd2a0] | 163 | errno_t log_init(const char *prog_name)
|
---|
[9b415c9] | 164 | {
|
---|
| 165 | log_prog_name = str_dup(prog_name);
|
---|
| 166 | if (log_prog_name == NULL)
|
---|
| 167 | return ENOMEM;
|
---|
| 168 |
|
---|
[f9b2cb4c] | 169 | logger_session = service_connect_blocking(SERVICE_LOGGER,
|
---|
| 170 | INTERFACE_LOGGER_WRITER, 0);
|
---|
[1f2dd20] | 171 | if (logger_session == NULL) {
|
---|
| 172 | return ENOMEM;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[b1912b0c] | 175 | default_log_id = log_create(prog_name, LOG_NO_PARENT);
|
---|
[1f2dd20] | 176 |
|
---|
[b1912b0c] | 177 | return EOK;
|
---|
[9b415c9] | 178 | }
|
---|
| 179 |
|
---|
[f72ae3b] | 180 | /** Create a new (sub-) log.
|
---|
[793cce15] | 181 | *
|
---|
[f72ae3b] | 182 | * This function always returns a valid log_t. In case of errors,
|
---|
| 183 | * @c parent is returned and errors are silently ignored.
|
---|
| 184 | *
|
---|
| 185 | * @param name Log name under which message will be reported (appended to parents name).
|
---|
| 186 | * @param parent Parent log.
|
---|
| 187 | * @return Opaque identifier of the newly created log.
|
---|
[793cce15] | 188 | */
|
---|
[2bf781a] | 189 | log_t log_create(const char *name, log_t parent)
|
---|
[793cce15] | 190 | {
|
---|
| 191 | async_exch_t *exchange = async_exchange_begin(logger_session);
|
---|
| 192 | if (exchange == NULL)
|
---|
[f039dba] | 193 | return parent;
|
---|
| 194 |
|
---|
| 195 | if (parent == LOG_DEFAULT)
|
---|
| 196 | parent = default_log_id;
|
---|
[793cce15] | 197 |
|
---|
| 198 | ipc_call_t answer;
|
---|
[f039dba] | 199 | aid_t reg_msg = async_send_1(exchange, LOGGER_WRITER_CREATE_LOG,
|
---|
| 200 | parent, &answer);
|
---|
[b7fd2a0] | 201 | errno_t rc = async_data_write_start(exchange, name, str_size(name));
|
---|
| 202 | errno_t reg_msg_rc;
|
---|
[793cce15] | 203 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
| 204 |
|
---|
| 205 | async_exchange_end(exchange);
|
---|
| 206 |
|
---|
| 207 | if ((rc != EOK) || (reg_msg_rc != EOK))
|
---|
[f039dba] | 208 | return parent;
|
---|
[793cce15] | 209 |
|
---|
[fafb8e5] | 210 | return ipc_get_arg1(&answer);
|
---|
[793cce15] | 211 | }
|
---|
| 212 |
|
---|
[9b415c9] | 213 | /** Write an entry to the log.
|
---|
| 214 | *
|
---|
[e0c836e8] | 215 | * The message is printed only if the verbosity level is less than or
|
---|
| 216 | * equal to currently set reporting level of the log.
|
---|
| 217 | *
|
---|
| 218 | * @param ctx Log to use (use LOG_DEFAULT if you have no idea what it means).
|
---|
| 219 | * @param level Severity level of the message.
|
---|
| 220 | * @param fmt Format string in printf-like format (without trailing newline).
|
---|
[9b415c9] | 221 | */
|
---|
[a1a101d] | 222 | void log_msg(log_t ctx, log_level_t level, const char *fmt, ...)
|
---|
[9b415c9] | 223 | {
|
---|
| 224 | va_list args;
|
---|
| 225 |
|
---|
[fc51296] | 226 | va_start(args, fmt);
|
---|
[a1a101d] | 227 | log_msgv(ctx, level, fmt, args);
|
---|
[fc51296] | 228 | va_end(args);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** Write an entry to the log (va_list variant).
|
---|
| 232 | *
|
---|
[e0c836e8] | 233 | * @param ctx Log to use (use LOG_DEFAULT if you have no idea what it means).
|
---|
| 234 | * @param level Severity level of the message.
|
---|
| 235 | * @param fmt Format string in printf-like format (without trailing newline).
|
---|
| 236 | * @param args Arguments.
|
---|
[fc51296] | 237 | */
|
---|
[a1a101d] | 238 | void log_msgv(log_t ctx, log_level_t level, const char *fmt, va_list args)
|
---|
[fc51296] | 239 | {
|
---|
[9b415c9] | 240 | assert(level < LVL_LIMIT);
|
---|
| 241 |
|
---|
[1f2dd20] | 242 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
[849bebe4] | 243 | if (message_buffer == NULL)
|
---|
[1f2dd20] | 244 | return;
|
---|
| 245 |
|
---|
| 246 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
[793cce15] | 247 | logger_message(logger_session, ctx, level, message_buffer);
|
---|
[849bebe4] | 248 | free(message_buffer);
|
---|
[9b415c9] | 249 | }
|
---|
| 250 |
|
---|
| 251 | /** @}
|
---|
| 252 | */
|
---|