[cba45af] | 1 | /*
|
---|
[a7a16a2f] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[cba45af] | 3 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 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 logger
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | #include <assert.h>
|
---|
| 34 | #include <errno.h>
|
---|
[38d150e] | 35 | #include <stdio.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <str.h>
|
---|
[cba45af] | 38 | #include "logger.h"
|
---|
| 39 |
|
---|
[f039dba] | 40 | static FIBRIL_MUTEX_INITIALIZE(log_list_guard);
|
---|
| 41 | static LIST_INITIALIZE(log_list);
|
---|
| 42 |
|
---|
[ae2c925] | 43 | static logger_log_t *find_log_by_name_and_parent_no_list_lock(const char *name, logger_log_t *parent)
|
---|
[cba45af] | 44 | {
|
---|
[feeac0d] | 45 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
[ae2c925] | 46 | if ((parent == log->parent) && (str_cmp(log->name, name) == 0))
|
---|
[cba45af] | 47 | return log;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return NULL;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[b7fd2a0] | 53 | static errno_t create_dest(const char *name, logger_dest_t **dest)
|
---|
[cba45af] | 54 | {
|
---|
[f039dba] | 55 | logger_dest_t *result = malloc(sizeof(logger_dest_t));
|
---|
[f5da671] | 56 | if (result == NULL)
|
---|
| 57 | return ENOMEM;
|
---|
[a7a16a2f] | 58 | if (asprintf(&result->filename, "/log/%s.txt", name) < 0) {
|
---|
[f5da671] | 59 | free(result);
|
---|
| 60 | return ENOMEM;
|
---|
| 61 | }
|
---|
[90dc458] | 62 | result->logfile = NULL;
|
---|
| 63 | fibril_mutex_initialize(&result->guard);
|
---|
[f5da671] | 64 | *dest = result;
|
---|
| 65 | return EOK;
|
---|
[f039dba] | 66 | }
|
---|
[cba45af] | 67 |
|
---|
[ae2c925] | 68 | static logger_log_t *create_log_no_locking(const char *name, logger_log_t *parent)
|
---|
[f039dba] | 69 | {
|
---|
[ae2c925] | 70 | logger_log_t *result = calloc(1, sizeof(logger_log_t));
|
---|
| 71 | if (result == NULL)
|
---|
| 72 | return NULL;
|
---|
[cba45af] | 73 |
|
---|
| 74 | result->name = str_dup(name);
|
---|
[ae2c925] | 75 | if (result->name == NULL)
|
---|
| 76 | goto error;
|
---|
| 77 |
|
---|
| 78 | /*
|
---|
| 79 | * Notice that we create new dest as the last
|
---|
| 80 | * operation that can fail and thus there is no code
|
---|
| 81 | * to deallocate dest.
|
---|
| 82 | */
|
---|
[f039dba] | 83 | if (parent == NULL) {
|
---|
| 84 | result->full_name = str_dup(name);
|
---|
[ae2c925] | 85 | if (result->full_name == NULL)
|
---|
| 86 | goto error;
|
---|
[b7fd2a0] | 87 | errno_t rc = create_dest(name, &result->dest);
|
---|
[f5da671] | 88 | if (rc != EOK)
|
---|
[ae2c925] | 89 | goto error;
|
---|
[f039dba] | 90 | } else {
|
---|
[d5c1051] | 91 | if (asprintf(&result->full_name, "%s/%s",
|
---|
| 92 | parent->full_name, name) < 0)
|
---|
[ae2c925] | 93 | goto error;
|
---|
[f039dba] | 94 | result->dest = parent->dest;
|
---|
| 95 | }
|
---|
[cba45af] | 96 |
|
---|
[ae2c925] | 97 | /* Following initializations cannot fail. */
|
---|
| 98 | result->logged_level = LOG_LEVEL_USE_DEFAULT;
|
---|
| 99 | fibril_mutex_initialize(&result->guard);
|
---|
[cba45af] | 100 | link_initialize(&result->link);
|
---|
[ae2c925] | 101 | result->parent = parent;
|
---|
[cba45af] | 102 |
|
---|
[ae2c925] | 103 | return result;
|
---|
[cba45af] | 104 |
|
---|
[ae2c925] | 105 | error:
|
---|
| 106 | free(result->name);
|
---|
| 107 | free(result->full_name);
|
---|
| 108 | free(result);
|
---|
| 109 | return NULL;
|
---|
[f5da671] | 110 |
|
---|
[ae2c925] | 111 | }
|
---|
[f5da671] | 112 |
|
---|
[1dec7cb] | 113 | logger_log_t *find_or_create_log_and_lock(const char *name, sysarg_t parent_id)
|
---|
[ae2c925] | 114 | {
|
---|
| 115 | logger_log_t *result = NULL;
|
---|
| 116 | logger_log_t *parent = (logger_log_t *) parent_id;
|
---|
[cba45af] | 117 |
|
---|
[ae2c925] | 118 | fibril_mutex_lock(&log_list_guard);
|
---|
| 119 |
|
---|
| 120 | result = find_log_by_name_and_parent_no_list_lock(name, parent);
|
---|
| 121 | if (result == NULL) {
|
---|
| 122 | result = create_log_no_locking(name, parent);
|
---|
| 123 | if (result == NULL)
|
---|
| 124 | goto leave;
|
---|
[92e963f] | 125 | list_append(&result->link, &log_list);
|
---|
[131d9a4] | 126 | if (result->parent != NULL) {
|
---|
| 127 | fibril_mutex_lock(&result->parent->guard);
|
---|
| 128 | result->parent->ref_counter++;
|
---|
| 129 | fibril_mutex_unlock(&result->parent->guard);
|
---|
| 130 | }
|
---|
[90dc458] | 131 | }
|
---|
| 132 |
|
---|
[ae2c925] | 133 | fibril_mutex_lock(&result->guard);
|
---|
[f5da671] | 134 |
|
---|
[ae2c925] | 135 | leave:
|
---|
[f08ab0f] | 136 | fibril_mutex_unlock(&log_list_guard);
|
---|
| 137 |
|
---|
[ae2c925] | 138 | return result;
|
---|
[cba45af] | 139 | }
|
---|
| 140 |
|
---|
[1dec7cb] | 141 | logger_log_t *find_log_by_name_and_lock(const char *name)
|
---|
[cba45af] | 142 | {
|
---|
[f039dba] | 143 | logger_log_t *result = NULL;
|
---|
| 144 |
|
---|
| 145 | fibril_mutex_lock(&log_list_guard);
|
---|
[feeac0d] | 146 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
[f039dba] | 147 | if (str_cmp(log->full_name, name) == 0) {
|
---|
[3cf862f] | 148 | fibril_mutex_lock(&log->guard);
|
---|
[f039dba] | 149 | result = log;
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
[cba45af] | 152 | }
|
---|
[f039dba] | 153 | fibril_mutex_unlock(&log_list_guard);
|
---|
[cba45af] | 154 |
|
---|
[f039dba] | 155 | return result;
|
---|
[cba45af] | 156 | }
|
---|
| 157 |
|
---|
[1dec7cb] | 158 | logger_log_t *find_log_by_id_and_lock(sysarg_t id)
|
---|
[cba45af] | 159 | {
|
---|
[f039dba] | 160 | logger_log_t *result = NULL;
|
---|
| 161 |
|
---|
| 162 | fibril_mutex_lock(&log_list_guard);
|
---|
[feeac0d] | 163 | list_foreach(log_list, link, logger_log_t, log) {
|
---|
[f039dba] | 164 | if ((sysarg_t) log == id) {
|
---|
[3cf862f] | 165 | fibril_mutex_lock(&log->guard);
|
---|
[f039dba] | 166 | result = log;
|
---|
| 167 | break;
|
---|
| 168 | }
|
---|
[cba45af] | 169 | }
|
---|
[f039dba] | 170 | fibril_mutex_unlock(&log_list_guard);
|
---|
[cba45af] | 171 |
|
---|
[f039dba] | 172 | return result;
|
---|
[cba45af] | 173 | }
|
---|
| 174 |
|
---|
[f039dba] | 175 | static log_level_t get_actual_log_level(logger_log_t *log)
|
---|
[cba45af] | 176 | {
|
---|
[f039dba] | 177 | /* Find recursively proper log level. */
|
---|
| 178 | if (log->logged_level == LOG_LEVEL_USE_DEFAULT) {
|
---|
| 179 | if (log->parent == NULL)
|
---|
| 180 | return get_default_logging_level();
|
---|
| 181 | else
|
---|
| 182 | return get_actual_log_level(log->parent);
|
---|
| 183 | }
|
---|
| 184 | return log->logged_level;
|
---|
[cba45af] | 185 | }
|
---|
| 186 |
|
---|
[f039dba] | 187 | bool shall_log_message(logger_log_t *log, log_level_t level)
|
---|
| 188 | {
|
---|
[3cf862f] | 189 | fibril_mutex_lock(&log_list_guard);
|
---|
| 190 | bool result = level <= get_actual_log_level(log);
|
---|
| 191 | fibril_mutex_unlock(&log_list_guard);
|
---|
| 192 | return result;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[1dec7cb] | 195 | void log_unlock(logger_log_t *log)
|
---|
[3cf862f] | 196 | {
|
---|
| 197 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
| 198 | fibril_mutex_unlock(&log->guard);
|
---|
[f039dba] | 199 | }
|
---|
[cba45af] | 200 |
|
---|
[c477c80] | 201 | /** Decreases reference counter on the log and destroy the log if
|
---|
[131d9a4] | 202 | * necessary.
|
---|
| 203 | *
|
---|
| 204 | * Precondition: log is locked.
|
---|
| 205 | *
|
---|
| 206 | * @param log Log to release from using by the caller.
|
---|
| 207 | */
|
---|
| 208 | void log_release(logger_log_t *log)
|
---|
| 209 | {
|
---|
| 210 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
| 211 | assert(log->ref_counter > 0);
|
---|
| 212 |
|
---|
| 213 | /* We are definitely not the last ones. */
|
---|
| 214 | if (log->ref_counter > 1) {
|
---|
| 215 | log->ref_counter--;
|
---|
| 216 | fibril_mutex_unlock(&log->guard);
|
---|
| 217 | return;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*
|
---|
| 221 | * To prevent deadlock, we need to get the list lock first.
|
---|
| 222 | * Deadlock scenario:
|
---|
| 223 | * Us: LOCKED(log), want to LOCK(list)
|
---|
| 224 | * Someone else calls find_log_by_name_and_lock(log->fullname) ->
|
---|
| 225 | * LOCKED(list), wants to LOCK(log)
|
---|
| 226 | */
|
---|
| 227 | fibril_mutex_unlock(&log->guard);
|
---|
| 228 |
|
---|
| 229 | /* Ensuring correct locking order. */
|
---|
| 230 | fibril_mutex_lock(&log_list_guard);
|
---|
| 231 | /*
|
---|
| 232 | * The reference must be still valid because we have not decreased
|
---|
| 233 | * the reference counter.
|
---|
| 234 | */
|
---|
| 235 | fibril_mutex_lock(&log->guard);
|
---|
| 236 | assert(log->ref_counter > 0);
|
---|
| 237 | log->ref_counter--;
|
---|
| 238 |
|
---|
| 239 | if (log->ref_counter > 0) {
|
---|
| 240 | /*
|
---|
| 241 | * Meanwhile, someone else increased the ref counter.
|
---|
| 242 | * No big deal, we just return immediatelly.
|
---|
| 243 | */
|
---|
| 244 | fibril_mutex_unlock(&log->guard);
|
---|
| 245 | fibril_mutex_unlock(&log_list_guard);
|
---|
| 246 | return;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /*
|
---|
| 250 | * Here we are on a destroy path. We need to
|
---|
| 251 | * - remove ourselves from the list
|
---|
| 252 | * - decrease reference of the parent (if not top-level log)
|
---|
| 253 | * - we must do that after we relaase list lock to prevent
|
---|
| 254 | * deadlock with ourselves
|
---|
| 255 | * - destroy dest (if top-level log)
|
---|
| 256 | */
|
---|
| 257 | assert(log->ref_counter == 0);
|
---|
| 258 |
|
---|
| 259 | list_remove(&log->link);
|
---|
| 260 | fibril_mutex_unlock(&log_list_guard);
|
---|
| 261 | fibril_mutex_unlock(&log->guard);
|
---|
| 262 |
|
---|
| 263 | if (log->parent == NULL) {
|
---|
[87e9392] | 264 | /*
|
---|
| 265 | * Due to lazy file opening in write_to_log(),
|
---|
| 266 | * it is possible that no file was actually opened.
|
---|
| 267 | */
|
---|
| 268 | if (log->dest->logfile != NULL) {
|
---|
| 269 | fclose(log->dest->logfile);
|
---|
| 270 | }
|
---|
[131d9a4] | 271 | free(log->dest->filename);
|
---|
| 272 | free(log->dest);
|
---|
| 273 | } else {
|
---|
| 274 | fibril_mutex_lock(&log->parent->guard);
|
---|
| 275 | log_release(log->parent);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[42bde6a] | 278 | logger_log("Destroyed log %s.\n", log->full_name);
|
---|
[131d9a4] | 279 |
|
---|
| 280 | free(log->name);
|
---|
| 281 | free(log->full_name);
|
---|
| 282 |
|
---|
| 283 | free(log);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[90dc458] | 286 | void write_to_log(logger_log_t *log, log_level_t level, const char *message)
|
---|
| 287 | {
|
---|
| 288 | assert(fibril_mutex_is_locked(&log->guard));
|
---|
| 289 | assert(log->dest != NULL);
|
---|
| 290 | fibril_mutex_lock(&log->dest->guard);
|
---|
| 291 | if (log->dest->logfile == NULL)
|
---|
| 292 | log->dest->logfile = fopen(log->dest->filename, "a");
|
---|
| 293 |
|
---|
| 294 | if (log->dest->logfile != NULL) {
|
---|
| 295 | fprintf(log->dest->logfile, "[%s] %s: %s\n",
|
---|
| 296 | log->full_name, log_level_str(level),
|
---|
| 297 | (const char *) message);
|
---|
| 298 | fflush(log->dest->logfile);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | fibril_mutex_unlock(&log->dest->guard);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[131d9a4] | 304 | void registered_logs_init(logger_registered_logs_t *logs)
|
---|
| 305 | {
|
---|
| 306 | logs->logs_count = 0;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | bool register_log(logger_registered_logs_t *logs, logger_log_t *new_log)
|
---|
| 310 | {
|
---|
| 311 | if (logs->logs_count >= MAX_REFERENCED_LOGS_PER_CLIENT) {
|
---|
| 312 | return false;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | assert(fibril_mutex_is_locked(&new_log->guard));
|
---|
| 316 | new_log->ref_counter++;
|
---|
| 317 |
|
---|
| 318 | logs->logs[logs->logs_count] = new_log;
|
---|
| 319 | logs->logs_count++;
|
---|
| 320 |
|
---|
| 321 | return true;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | void unregister_logs(logger_registered_logs_t *logs)
|
---|
| 325 | {
|
---|
| 326 | for (size_t i = 0; i < logs->logs_count; i++) {
|
---|
| 327 | logger_log_t *log = logs->logs[i];
|
---|
| 328 | fibril_mutex_lock(&log->guard);
|
---|
| 329 | log_release(log);
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[cba45af] | 333 | /**
|
---|
| 334 | * @}
|
---|
| 335 | */
|
---|