[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 |
|
---|
[1f2dd20] | 45 | /** Log messages are printed under this name. */
|
---|
| 46 | static const char *log_prog_name;
|
---|
[9b415c9] | 47 |
|
---|
[1c67b41] | 48 | static const char *log_level_names[] = {
|
---|
| 49 | "fatal",
|
---|
| 50 | "error",
|
---|
| 51 | "warn",
|
---|
| 52 | "note",
|
---|
| 53 | "debug",
|
---|
[eab3d04] | 54 | "debug2",
|
---|
| 55 | NULL
|
---|
[1c67b41] | 56 | };
|
---|
| 57 |
|
---|
[1f2dd20] | 58 | /** IPC session with the logger service. */
|
---|
| 59 | static async_sess_t *logger_session;
|
---|
[9b415c9] | 60 |
|
---|
[1f2dd20] | 61 | /** Maximum length of a single log message (in bytes). */
|
---|
| 62 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
[9b415c9] | 63 |
|
---|
[1f2dd20] | 64 | static 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, ®_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 |
|
---|
[793cce15] | 85 | static int logger_message(async_sess_t *session, log_context_t ctx, log_level_t level, const char *message)
|
---|
[1f2dd20] | 86 | {
|
---|
| 87 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
| 88 | if (exchange == NULL) {
|
---|
| 89 | return ENOMEM;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[793cce15] | 92 | aid_t reg_msg = async_send_2(exchange, LOGGER_MESSAGE,
|
---|
| 93 | ctx, level, NULL);
|
---|
[1f2dd20] | 94 | int rc = async_data_write_start(exchange, message, str_size(message));
|
---|
| 95 | sysarg_t reg_msg_rc;
|
---|
| 96 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
| 97 |
|
---|
| 98 | async_exchange_end(exchange);
|
---|
[9b415c9] | 99 |
|
---|
[f6bc83a] | 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 |
|
---|
[1f2dd20] | 107 | if (rc != EOK) {
|
---|
| 108 | return rc;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | return reg_msg_rc;
|
---|
| 112 | }
|
---|
[9b415c9] | 113 |
|
---|
[1c67b41] | 114 | const 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 |
|
---|
[eab3d04] | 122 | int 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 |
|
---|
[9b415c9] | 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 | */
|
---|
| 156 | int 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 |
|
---|
[1f2dd20] | 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;
|
---|
[9b415c9] | 172 | }
|
---|
| 173 |
|
---|
[793cce15] | 174 | /** Create logging context.
|
---|
| 175 | *
|
---|
| 176 | * This function always returns a valid context.
|
---|
| 177 | */
|
---|
| 178 | log_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, ®_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 |
|
---|
[9b415c9] | 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.
|
---|
[ebcb05a] | 203 | * @param fmt Format string (no traling newline).
|
---|
[9b415c9] | 204 | */
|
---|
[ebbc8a74] | 205 | void log_ctx_msg(log_context_t ctx, log_level_t level, const char *fmt, ...)
|
---|
[9b415c9] | 206 | {
|
---|
| 207 | va_list args;
|
---|
| 208 |
|
---|
[fc51296] | 209 | va_start(args, fmt);
|
---|
[ebbc8a74] | 210 | log_ctx_msgv(ctx, level, fmt, args);
|
---|
[fc51296] | 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.
|
---|
[ebcb05a] | 219 | * @param fmt Format string (no trailing newline)
|
---|
[fc51296] | 220 | */
|
---|
[ebbc8a74] | 221 | void log_ctx_msgv(log_context_t ctx, log_level_t level, const char *fmt, va_list args)
|
---|
[fc51296] | 222 | {
|
---|
[9b415c9] | 223 | assert(level < LVL_LIMIT);
|
---|
| 224 |
|
---|
[1f2dd20] | 225 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
| 226 | if (message_buffer == NULL) {
|
---|
| 227 | return;
|
---|
[9b415c9] | 228 | }
|
---|
[1f2dd20] | 229 |
|
---|
| 230 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
[793cce15] | 231 | logger_message(logger_session, ctx, level, message_buffer);
|
---|
[9b415c9] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /** @}
|
---|
| 235 | */
|
---|