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