[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 |
|
---|
[1f2dd20] | 48 | /** IPC session with the logger service. */
|
---|
| 49 | static async_sess_t *logger_session;
|
---|
[9b415c9] | 50 |
|
---|
[1f2dd20] | 51 | /** Maximum length of a single log message (in bytes). */
|
---|
| 52 | #define MESSAGE_BUFFER_SIZE 4096
|
---|
[9b415c9] | 53 |
|
---|
[2e39656] | 54 | FIBRIL_RWLOCK_INITIALIZE(current_observed_level_lock);
|
---|
| 55 | log_level_t current_observed_level;
|
---|
| 56 |
|
---|
[1f2dd20] | 57 | static int logger_register(async_sess_t *session, const char *prog_name)
|
---|
| 58 | {
|
---|
| 59 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
| 60 | if (exchange == NULL) {
|
---|
| 61 | return ENOMEM;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL);
|
---|
| 65 | int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
|
---|
| 66 | sysarg_t reg_msg_rc;
|
---|
| 67 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
| 68 |
|
---|
| 69 | async_exchange_end(exchange);
|
---|
| 70 |
|
---|
| 71 | if (rc != EOK) {
|
---|
| 72 | return rc;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return reg_msg_rc;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static int logger_message(async_sess_t *session, log_level_t level, const char *message)
|
---|
| 79 | {
|
---|
| 80 | async_exch_t *exchange = async_exchange_begin(session);
|
---|
| 81 | if (exchange == NULL) {
|
---|
| 82 | return ENOMEM;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL);
|
---|
| 86 | int rc = async_data_write_start(exchange, message, str_size(message));
|
---|
| 87 | sysarg_t reg_msg_rc;
|
---|
| 88 | async_wait_for(reg_msg, ®_msg_rc);
|
---|
| 89 |
|
---|
| 90 | async_exchange_end(exchange);
|
---|
[9b415c9] | 91 |
|
---|
[f6bc83a] | 92 | /*
|
---|
| 93 | * Getting ENAK means no-one wants our message. That is not an
|
---|
| 94 | * error at all.
|
---|
| 95 | */
|
---|
| 96 | if (rc == ENAK)
|
---|
| 97 | rc = EOK;
|
---|
| 98 |
|
---|
[1f2dd20] | 99 | if (rc != EOK) {
|
---|
| 100 | return rc;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return reg_msg_rc;
|
---|
| 104 | }
|
---|
[9b415c9] | 105 |
|
---|
[2e39656] | 106 | static void cannot_use_level_changed_monitor(void)
|
---|
| 107 | {
|
---|
[be73793] | 108 | fibril_rwlock_write_lock(¤t_observed_level_lock);
|
---|
| 109 | current_observed_level = LVL_LIMIT;
|
---|
| 110 | fibril_rwlock_write_unlock(¤t_observed_level_lock);
|
---|
[2e39656] | 111 | }
|
---|
| 112 |
|
---|
| 113 | static int observed_level_changed_monitor(void *arg)
|
---|
| 114 | {
|
---|
| 115 | async_sess_t *monitor_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
| 116 | if (monitor_session == NULL) {
|
---|
| 117 | cannot_use_level_changed_monitor();
|
---|
| 118 | return ENOMEM;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | int rc = logger_register(monitor_session, log_prog_name);
|
---|
| 122 | if (rc != EOK) {
|
---|
| 123 | cannot_use_level_changed_monitor();
|
---|
| 124 | return rc;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | async_exch_t *exchange = async_exchange_begin(monitor_session);
|
---|
| 128 | if (exchange == NULL) {
|
---|
| 129 | cannot_use_level_changed_monitor();
|
---|
| 130 | return ENOMEM;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | while (true) {
|
---|
| 134 | sysarg_t has_reader;
|
---|
| 135 | sysarg_t msg_rc = async_req_0_1(exchange,
|
---|
| 136 | LOGGER_BLOCK_UNTIL_READER_CHANGED, &has_reader);
|
---|
| 137 | if (msg_rc != EOK) {
|
---|
| 138 | cannot_use_level_changed_monitor();
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | fibril_rwlock_write_lock(¤t_observed_level_lock);
|
---|
| 143 | if ((bool) has_reader) {
|
---|
| 144 | current_observed_level = LVL_LIMIT;
|
---|
| 145 | } else {
|
---|
| 146 | current_observed_level = LVL_NOTE;
|
---|
| 147 | }
|
---|
| 148 | fibril_rwlock_write_unlock(¤t_observed_level_lock);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | async_exchange_end(exchange);
|
---|
| 152 |
|
---|
| 153 | return EOK;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | static log_level_t get_current_observed_level(void)
|
---|
| 157 | {
|
---|
| 158 | fibril_rwlock_read_lock(¤t_observed_level_lock);
|
---|
| 159 | log_level_t level = current_observed_level;
|
---|
| 160 | fibril_rwlock_read_unlock(¤t_observed_level_lock);
|
---|
| 161 | return level;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[9b415c9] | 164 | /** Initialize the logging system.
|
---|
| 165 | *
|
---|
| 166 | * @param prog_name Program name, will be printed as part of message
|
---|
| 167 | * @param level Minimum message level to print
|
---|
| 168 | */
|
---|
| 169 | int log_init(const char *prog_name, log_level_t level)
|
---|
| 170 | {
|
---|
| 171 | assert(level < LVL_LIMIT);
|
---|
| 172 |
|
---|
| 173 | log_prog_name = str_dup(prog_name);
|
---|
| 174 | if (log_prog_name == NULL)
|
---|
| 175 | return ENOMEM;
|
---|
| 176 |
|
---|
[1f2dd20] | 177 | logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
|
---|
| 178 | if (logger_session == NULL) {
|
---|
| 179 | return ENOMEM;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | int rc = logger_register(logger_session, log_prog_name);
|
---|
| 183 |
|
---|
[2e39656] | 184 | current_observed_level = LVL_NOTE;
|
---|
| 185 |
|
---|
| 186 | fid_t observed_level_changed_fibril = fibril_create(observed_level_changed_monitor, NULL);
|
---|
| 187 | if (observed_level_changed_fibril == 0) {
|
---|
| 188 | cannot_use_level_changed_monitor();
|
---|
| 189 | } else {
|
---|
| 190 | fibril_add_ready(observed_level_changed_fibril);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[1f2dd20] | 193 | return rc;
|
---|
[9b415c9] | 194 | }
|
---|
| 195 |
|
---|
[14de4106] | 196 | bool __log_shall_record(log_level_t level)
|
---|
| 197 | {
|
---|
| 198 | return get_current_observed_level() >= level;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[9b415c9] | 201 | /** Write an entry to the log.
|
---|
| 202 | *
|
---|
| 203 | * @param level Message verbosity level. Message is only printed
|
---|
| 204 | * if verbosity is less than or equal to current
|
---|
| 205 | * reporting level.
|
---|
[ebcb05a] | 206 | * @param fmt Format string (no traling newline).
|
---|
[9b415c9] | 207 | */
|
---|
[14de4106] | 208 | void __log_msg(log_level_t level, const char *fmt, ...)
|
---|
[9b415c9] | 209 | {
|
---|
| 210 | va_list args;
|
---|
| 211 |
|
---|
[fc51296] | 212 | va_start(args, fmt);
|
---|
[14de4106] | 213 | __log_msgv(level, fmt, args);
|
---|
[fc51296] | 214 | va_end(args);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /** Write an entry to the log (va_list variant).
|
---|
| 218 | *
|
---|
| 219 | * @param level Message verbosity level. Message is only printed
|
---|
| 220 | * if verbosity is less than or equal to current
|
---|
| 221 | * reporting level.
|
---|
[ebcb05a] | 222 | * @param fmt Format string (no trailing newline)
|
---|
[fc51296] | 223 | */
|
---|
[14de4106] | 224 | void __log_msgv(log_level_t level, const char *fmt, va_list args)
|
---|
[fc51296] | 225 | {
|
---|
[9b415c9] | 226 | assert(level < LVL_LIMIT);
|
---|
| 227 |
|
---|
[2e39656] | 228 | if (get_current_observed_level() < level) {
|
---|
| 229 | return;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[1f2dd20] | 232 | char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
|
---|
| 233 | if (message_buffer == NULL) {
|
---|
| 234 | return;
|
---|
[9b415c9] | 235 | }
|
---|
[1f2dd20] | 236 |
|
---|
| 237 | vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
|
---|
| 238 | logger_message(logger_session, level, message_buffer);
|
---|
[9b415c9] | 239 | }
|
---|
| 240 |
|
---|
| 241 | /** @}
|
---|
| 242 | */
|
---|