[cba45af] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup logger
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | #include <assert.h>
|
---|
| 33 | #include <malloc.h>
|
---|
| 34 | #include <str.h>
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include "logger.h"
|
---|
| 38 |
|
---|
| 39 | static FIBRIL_MUTEX_INITIALIZE(toplog_list_guard);
|
---|
| 40 | static LIST_INITIALIZE(toplog_list);
|
---|
| 41 |
|
---|
| 42 | static logger_toplevel_log_t *find_log_by_name_no_lock(const char *name)
|
---|
| 43 | {
|
---|
| 44 | list_foreach(toplog_list, it) {
|
---|
| 45 | logger_toplevel_log_t *log = list_get_instance(it, logger_toplevel_log_t, link);
|
---|
| 46 | if (str_cmp(log->name, name) == 0) {
|
---|
| 47 | return log;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | return NULL;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | logger_toplevel_log_t *find_or_create_toplevel_log(const char *name)
|
---|
| 55 | {
|
---|
| 56 | logger_toplevel_log_t *result = NULL;
|
---|
| 57 |
|
---|
| 58 | fibril_mutex_lock(&toplog_list_guard);
|
---|
| 59 |
|
---|
| 60 | result = find_log_by_name_no_lock(name);
|
---|
| 61 | if (result != NULL)
|
---|
| 62 | goto leave;
|
---|
| 63 |
|
---|
| 64 | result = malloc(sizeof(logger_toplevel_log_t));
|
---|
| 65 | if (result == NULL)
|
---|
| 66 | goto leave;
|
---|
| 67 |
|
---|
| 68 | char *logfilename;
|
---|
| 69 | asprintf(&logfilename, "/log/%s", name);
|
---|
| 70 | result->logfile = fopen(logfilename, "a");
|
---|
| 71 | result->logged_level = LOG_LEVEL_USE_DEFAULT;
|
---|
| 72 | result->name = str_dup(name);
|
---|
| 73 | result->sublog_count = 1;
|
---|
| 74 | result->sublogs[0].name = "";
|
---|
| 75 | result->sublogs[0].logged_level = LOG_LEVEL_USE_DEFAULT;
|
---|
| 76 |
|
---|
| 77 | link_initialize(&result->link);
|
---|
| 78 |
|
---|
| 79 | list_append(&result->link, &toplog_list);
|
---|
| 80 |
|
---|
| 81 | leave:
|
---|
| 82 | fibril_mutex_unlock(&toplog_list_guard);
|
---|
| 83 |
|
---|
| 84 | return result;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | logger_toplevel_log_t *find_toplevel_log(sysarg_t id)
|
---|
| 88 | {
|
---|
| 89 | list_foreach(toplog_list, it) {
|
---|
| 90 | logger_toplevel_log_t *log = list_get_instance(it, logger_toplevel_log_t, link);
|
---|
| 91 | if ((sysarg_t) log == id)
|
---|
| 92 | return log;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return NULL;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | bool shall_log_message(logger_toplevel_log_t *toplog, sysarg_t log, log_level_t level)
|
---|
| 99 | {
|
---|
| 100 | if (log >= toplog->sublog_count)
|
---|
| 101 | return false;
|
---|
| 102 |
|
---|
| 103 | log_level_t logged_level = toplog->sublogs[log].logged_level;
|
---|
| 104 | if (logged_level == LOG_LEVEL_USE_DEFAULT) {
|
---|
| 105 | logged_level = toplog->logged_level;
|
---|
| 106 | if (logged_level == LOG_LEVEL_USE_DEFAULT)
|
---|
| 107 | logged_level = get_default_logging_level();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | return level <= logged_level;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | int add_sub_log(logger_toplevel_log_t *toplog, const char *name, sysarg_t *id)
|
---|
| 115 | {
|
---|
| 116 | if (toplog->sublog_count >= MAX_SUBLOGS)
|
---|
| 117 | return ELIMIT;
|
---|
| 118 |
|
---|
| 119 | logger_sublog_t *sublog = &toplog->sublogs[toplog->sublog_count];
|
---|
| 120 | sublog->name = str_dup(name);
|
---|
| 121 | sublog->logged_level = toplog->logged_level;
|
---|
| 122 |
|
---|
| 123 | *id = toplog->sublog_count;
|
---|
| 124 |
|
---|
| 125 | toplog->sublog_count++;
|
---|
| 126 |
|
---|
| 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * @}
|
---|
| 133 | */
|
---|