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