Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file logging.c |
| 2 | * Debugging/Logging support code. */ |
| 3 | /* |
| 4 | * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 5 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
Harald Welte | f01b238 | 2017-10-16 13:55:34 +0200 | [diff] [blame] | 26 | /*! \addtogroup logging |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 27 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 28 | * libosmocore Logging sub-system |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 29 | * |
| 30 | * \file logging.c */ |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 31 | |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 32 | #include "../config.h" |
| 33 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 34 | #include <stdarg.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 38 | |
| 39 | #ifdef HAVE_STRINGS_H |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 40 | #include <strings.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 41 | #endif |
Vadim Yanitskiy | 04f4271 | 2020-09-09 04:47:25 +0700 | [diff] [blame] | 42 | |
| 43 | #ifdef HAVE_SYSLOG_H |
| 44 | #include <syslog.h> |
| 45 | #endif |
| 46 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 47 | #include <time.h> |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 48 | #include <sys/time.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 49 | #include <errno.h> |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 50 | #include <pthread.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 51 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 52 | #include <osmocom/core/talloc.h> |
| 53 | #include <osmocom/core/utils.h> |
| 54 | #include <osmocom/core/logging.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 55 | #include <osmocom/core/timer.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 56 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 57 | #include <osmocom/vty/logging.h> /* for LOGGING_STR. */ |
| 58 | |
Harald Welte | a8b1b21 | 2020-09-27 17:21:07 +0200 | [diff] [blame] | 59 | /* maximum length of the log string of a single log event (typically line) */ |
| 60 | #define MAX_LOG_SIZE 4096 |
| 61 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 62 | osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 63 | enum_logging_ctx_items_fit_in_struct_log_context); |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 64 | osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 65 | enum_logging_filters_fit_in_log_target_filter_data); |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 66 | osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 67 | enum_logging_filters_fit_in_log_target_filter_map); |
| 68 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 69 | struct log_info *osmo_log_info; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 70 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 71 | static struct log_context log_context; |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 72 | void *tall_log_ctx = NULL; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 73 | LLIST_HEAD(osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 74 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 75 | #if (!EMBEDDED) |
| 76 | /*! This mutex must be held while using osmo_log_target_list or any of its |
| 77 | log_targets in a multithread program. Prevents race conditions between threads |
| 78 | like producing unordered timestamps or VTY deleting a target while another |
| 79 | thread is writing to it */ |
| 80 | static pthread_mutex_t osmo_log_tgt_mutex; |
| 81 | static bool osmo_log_tgt_mutex_on = false; |
| 82 | |
| 83 | /*! Enable multithread support (mutex) in libosmocore logging system. |
| 84 | * Must be called by processes willing to use logging subsystem from several |
| 85 | * threads. Once enabled, it's not possible to disable it again. |
| 86 | */ |
| 87 | void log_enable_multithread(void) { |
| 88 | if (osmo_log_tgt_mutex_on) |
| 89 | return; |
| 90 | pthread_mutex_init(&osmo_log_tgt_mutex, NULL); |
| 91 | osmo_log_tgt_mutex_on = true; |
| 92 | } |
| 93 | |
| 94 | /*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use |
| 95 | * macro log_tgt_mutex_lock() instead. |
| 96 | */ |
| 97 | void log_tgt_mutex_lock_impl(void) { |
| 98 | /* These lines are useful to debug scenarios where there's only 1 thread |
| 99 | and a double lock appears, for instance during startup and some |
| 100 | unlock() missing somewhere: |
| 101 | if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0) |
| 102 | osmo_panic("acquiring already locked mutex!\n"); |
| 103 | return; |
| 104 | */ |
| 105 | |
| 106 | if (osmo_log_tgt_mutex_on) |
| 107 | pthread_mutex_lock(&osmo_log_tgt_mutex); |
| 108 | } |
| 109 | |
| 110 | /*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use |
| 111 | * macro log_tgt_mutex_unlock() instead. |
| 112 | */ |
| 113 | void log_tgt_mutex_unlock_impl(void) { |
| 114 | if (osmo_log_tgt_mutex_on) |
| 115 | pthread_mutex_unlock(&osmo_log_tgt_mutex); |
| 116 | } |
| 117 | |
| 118 | #else /* if (!EMBEDDED) */ |
| 119 | #pragma message ("logging multithread support disabled in embedded build") |
| 120 | void log_enable_multithread(void) {} |
| 121 | void log_tgt_mutex_lock_impl(void) {} |
| 122 | void log_tgt_mutex_unlock_impl(void) {} |
| 123 | #endif /* if (!EMBEDDED) */ |
| 124 | |
Neels Hofmeyr | 098038a | 2018-09-11 23:49:13 +0200 | [diff] [blame] | 125 | const struct value_string loglevel_strs[] = { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 126 | { LOGL_DEBUG, "DEBUG" }, |
| 127 | { LOGL_INFO, "INFO" }, |
| 128 | { LOGL_NOTICE, "NOTICE" }, |
| 129 | { LOGL_ERROR, "ERROR" }, |
| 130 | { LOGL_FATAL, "FATAL" }, |
| 131 | { 0, NULL }, |
| 132 | }; |
| 133 | |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 134 | /* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 135 | #define INT2IDX(x) (-1*(x)-1) |
| 136 | static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = { |
| 137 | [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */ |
| 138 | .name = "DLGLOBAL", |
| 139 | .description = "Library-internal global log family", |
| 140 | .loglevel = LOGL_NOTICE, |
| 141 | .enabled = 1, |
| 142 | }, |
root | 8a996b4 | 2011-09-26 11:22:21 +0200 | [diff] [blame] | 143 | [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */ |
| 144 | .name = "DLLAPD", |
| 145 | .description = "LAPD in libosmogsm", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 146 | .loglevel = LOGL_NOTICE, |
| 147 | .enabled = 1, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 148 | .color = "\033[38;5;21m", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 149 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 150 | [INT2IDX(DLINP)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 151 | .name = "DLINP", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 152 | .description = "A-bis Intput Subsystem", |
| 153 | .loglevel = LOGL_NOTICE, |
| 154 | .enabled = 1, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 155 | .color = "\033[38;5;23m", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 156 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 157 | [INT2IDX(DLMUX)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 158 | .name = "DLMUX", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 159 | .description = "A-bis B-Subchannel TRAU Frame Multiplex", |
| 160 | .loglevel = LOGL_NOTICE, |
| 161 | .enabled = 1, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 162 | .color = "\033[38;5;25m", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 163 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 164 | [INT2IDX(DLMI)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 165 | .name = "DLMI", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 166 | .description = "A-bis Input Driver for Signalling", |
| 167 | .enabled = 0, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 168 | .color = "\033[38;5;27m", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 169 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 170 | [INT2IDX(DLMIB)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 171 | .name = "DLMIB", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 172 | .description = "A-bis Input Driver for B-Channels (voice)", |
| 173 | .enabled = 0, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 174 | .color = "\033[38;5;29m", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 175 | }, |
Andreas Eversberg | c626da9 | 2011-10-28 03:53:50 +0200 | [diff] [blame] | 176 | [INT2IDX(DLSMS)] = { |
| 177 | .name = "DLSMS", |
| 178 | .description = "Layer3 Short Message Service (SMS)", |
| 179 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 180 | .color = "\033[38;5;31m", |
Andreas Eversberg | c626da9 | 2011-10-28 03:53:50 +0200 | [diff] [blame] | 181 | }, |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 182 | [INT2IDX(DLCTRL)] = { |
| 183 | .name = "DLCTRL", |
| 184 | .description = "Control Interface", |
| 185 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 186 | .color = "\033[38;5;33m", |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 187 | }, |
Holger Hans Peter Freyther | a5dc19d | 2014-12-04 14:35:21 +0100 | [diff] [blame] | 188 | [INT2IDX(DLGTP)] = { |
| 189 | .name = "DLGTP", |
| 190 | .description = "GPRS GTP library", |
| 191 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 192 | .color = "\033[38;5;35m", |
Holger Hans Peter Freyther | a5dc19d | 2014-12-04 14:35:21 +0100 | [diff] [blame] | 193 | }, |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 194 | [INT2IDX(DLSTATS)] = { |
| 195 | .name = "DLSTATS", |
| 196 | .description = "Statistics messages and logging", |
| 197 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 198 | .color = "\033[38;5;37m", |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 199 | }, |
Neels Hofmeyr | 9795cf1 | 2016-12-10 17:01:06 +0100 | [diff] [blame] | 200 | [INT2IDX(DLGSUP)] = { |
| 201 | .name = "DLGSUP", |
| 202 | .description = "Generic Subscriber Update Protocol", |
| 203 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 204 | .color = "\033[38;5;39m", |
Neels Hofmeyr | 9795cf1 | 2016-12-10 17:01:06 +0100 | [diff] [blame] | 205 | }, |
Harald Welte | c0f0007 | 2016-04-27 18:32:35 +0200 | [diff] [blame] | 206 | [INT2IDX(DLOAP)] = { |
| 207 | .name = "DLOAP", |
| 208 | .description = "Osmocom Authentication Protocol", |
| 209 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 210 | .color = "\033[38;5;41m", |
Harald Welte | c0f0007 | 2016-04-27 18:32:35 +0200 | [diff] [blame] | 211 | }, |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 212 | [INT2IDX(DLSS7)] = { |
| 213 | .name = "DLSS7", |
| 214 | .description = "libosmo-sigtran Signalling System 7", |
| 215 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 216 | .color = "\033[38;5;43m", |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 217 | }, |
| 218 | [INT2IDX(DLSCCP)] = { |
| 219 | .name = "DLSCCP", |
| 220 | .description = "libosmo-sigtran SCCP Implementation", |
| 221 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 222 | .color = "\033[38;5;45m", |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 223 | }, |
| 224 | [INT2IDX(DLSUA)] = { |
| 225 | .name = "DLSUA", |
| 226 | .description = "libosmo-sigtran SCCP User Adaptation", |
| 227 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 228 | .color = "\033[38;5;47m", |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 229 | }, |
| 230 | [INT2IDX(DLM3UA)] = { |
| 231 | .name = "DLM3UA", |
| 232 | .description = "libosmo-sigtran MTP3 User Adaptation", |
| 233 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 234 | .color = "\033[38;5;49m", |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 235 | }, |
Neels Hofmeyr | a7ccf61 | 2017-07-11 18:43:09 +0200 | [diff] [blame] | 236 | [INT2IDX(DLMGCP)] = { |
| 237 | .name = "DLMGCP", |
| 238 | .description = "libosmo-mgcp Media Gateway Control Protocol", |
| 239 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 240 | .color = "\033[38;5;51m", |
Neels Hofmeyr | a7ccf61 | 2017-07-11 18:43:09 +0200 | [diff] [blame] | 241 | }, |
Pau Espin Pedrol | 8fd8557 | 2018-02-27 19:43:10 +0100 | [diff] [blame] | 242 | [INT2IDX(DLJIBUF)] = { |
| 243 | .name = "DLJIBUF", |
| 244 | .description = "libosmo-netif Jitter Buffer", |
| 245 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 246 | .color = "\033[38;5;53m", |
Pau Espin Pedrol | 8fd8557 | 2018-02-27 19:43:10 +0100 | [diff] [blame] | 247 | }, |
Max | 450f5ac | 2019-02-14 19:12:03 +0100 | [diff] [blame] | 248 | [INT2IDX(DLRSPRO)] = { |
| 249 | .name = "DLRSPRO", |
| 250 | .description = "Remote SIM protocol", |
| 251 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 252 | .color = "\033[38;5;55m", |
Max | 450f5ac | 2019-02-14 19:12:03 +0100 | [diff] [blame] | 253 | }, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 254 | [INT2IDX(DLNS)] = { |
| 255 | .name = "DLNS", |
| 256 | .description = "GPRS NS layer", |
| 257 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 258 | .color = "\033[38;5;57m", |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 259 | }, |
Harald Welte | fde19ed | 2020-12-07 21:43:51 +0100 | [diff] [blame] | 260 | [INT2IDX(DLBSSGP)] = { |
| 261 | .name = "DLBSSGP", |
| 262 | .description = "GPRS BSSGP layer", |
| 263 | .enabled = 1, .loglevel = LOGL_NOTICE, |
Harald Welte | 4c0e1a1 | 2020-12-07 22:03:09 +0100 | [diff] [blame] | 264 | .color = "\033[38;5;59m", |
Harald Welte | fde19ed | 2020-12-07 21:43:51 +0100 | [diff] [blame] | 265 | }, |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 266 | }; |
| 267 | |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 268 | void assert_loginfo(const char *src) |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 269 | { |
| 270 | if (!osmo_log_info) { |
| 271 | fprintf(stderr, "ERROR: osmo_log_info == NULL! " |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 272 | "You must call log_init() before using logging in %s()!\n", src); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 273 | OSMO_ASSERT(osmo_log_info); |
| 274 | } |
| 275 | } |
| 276 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 277 | /* special magic for negative (library-internal) log subsystem numbers */ |
| 278 | static int subsys_lib2index(int subsys) |
| 279 | { |
| 280 | return (subsys * -1) + (osmo_log_info->num_cat_user-1); |
| 281 | } |
| 282 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 283 | /*! Parse a human-readable log level into a numeric value |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 284 | * \param[in] lvl zero-terminated string containing log level name |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 285 | * \returns numeric log level |
| 286 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 287 | int log_parse_level(const char *lvl) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 288 | { |
| 289 | return get_string_value(loglevel_strs, lvl); |
| 290 | } |
| 291 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 292 | /*! convert a numeric log level into human-readable string |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 293 | * \param[in] lvl numeric log level |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 294 | * \returns zero-terminated string (log level name) |
| 295 | */ |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 296 | const char *log_level_str(unsigned int lvl) |
| 297 | { |
| 298 | return get_value_string(loglevel_strs, lvl); |
| 299 | } |
| 300 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 301 | /*! parse a human-readable log category into numeric form |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 302 | * \param[in] category human-readable log category name |
| 303 | * \returns numeric category value, or -EINVAL otherwise |
| 304 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 305 | int log_parse_category(const char *category) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 306 | { |
| 307 | int i; |
| 308 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 309 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 310 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 311 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 312 | if (osmo_log_info->cat[i].name == NULL) |
| 313 | continue; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 314 | if (!strcasecmp(osmo_log_info->cat[i].name+1, category)) |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 315 | return i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 321 | /*! parse the log category mask |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 322 | * \param[in] target log target to be configured |
| 323 | * \param[in] _mask log category mask string |
| 324 | * |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 325 | * The format can be this: category1:category2:category3 |
| 326 | * or category1,2:category2,3:... |
| 327 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 328 | void log_parse_category_mask(struct log_target* target, const char *_mask) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 329 | { |
| 330 | int i = 0; |
| 331 | char *mask = strdup(_mask); |
| 332 | char *category_token = NULL; |
| 333 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 334 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 335 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 336 | /* Disable everything to enable it afterwards */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 337 | for (i = 0; i < osmo_log_info->num_cat; ++i) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 338 | target->categories[i].enabled = 0; |
| 339 | |
| 340 | category_token = strtok(mask, ":"); |
Neels Hofmeyr | da1b20c | 2016-04-14 15:12:16 +0200 | [diff] [blame] | 341 | OSMO_ASSERT(category_token); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 342 | do { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 343 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Nico Golde | 0262d3f | 2012-09-21 17:44:58 +0200 | [diff] [blame] | 344 | size_t length, cat_length; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 345 | char* colon = strstr(category_token, ","); |
Nico Golde | 0262d3f | 2012-09-21 17:44:58 +0200 | [diff] [blame] | 346 | |
| 347 | if (!osmo_log_info->cat[i].name) |
| 348 | continue; |
| 349 | |
| 350 | length = strlen(category_token); |
| 351 | cat_length = strlen(osmo_log_info->cat[i].name); |
Pablo Neira Ayuso | 300e78d | 2011-08-11 13:24:18 +0200 | [diff] [blame] | 352 | |
| 353 | /* Use longest length not to match subocurrences. */ |
| 354 | if (cat_length > length) |
| 355 | length = cat_length; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 356 | |
| 357 | if (colon) |
| 358 | length = colon - category_token; |
| 359 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 360 | if (strncasecmp(osmo_log_info->cat[i].name, |
| 361 | category_token, length) == 0) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 362 | int level = 0; |
| 363 | |
| 364 | if (colon) |
| 365 | level = atoi(colon+1); |
| 366 | |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 367 | target->categories[i].enabled = 1; |
| 368 | target->categories[i].loglevel = level; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } while ((category_token = strtok(NULL, ":"))); |
| 372 | |
| 373 | free(mask); |
| 374 | } |
| 375 | |
| 376 | static const char* color(int subsys) |
| 377 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 378 | if (subsys < osmo_log_info->num_cat) |
| 379 | return osmo_log_info->cat[subsys].color; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 380 | |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 381 | return NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 382 | } |
| 383 | |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 384 | static const struct value_string level_colors[] = { |
Neels Hofmeyr | f2644ae | 2019-11-20 04:00:29 +0100 | [diff] [blame] | 385 | { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE }, |
| 386 | { LOGL_INFO, OSMO_LOGCOLOR_GREEN }, |
| 387 | { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW }, |
| 388 | { LOGL_ERROR, OSMO_LOGCOLOR_RED }, |
| 389 | { LOGL_FATAL, OSMO_LOGCOLOR_RED }, |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 390 | { 0, NULL } |
| 391 | }; |
| 392 | |
| 393 | static const char *level_color(int level) |
| 394 | { |
| 395 | const char *c = get_value_string_or_null(level_colors, level); |
| 396 | if (!c) |
| 397 | return get_value_string(level_colors, LOGL_FATAL); |
| 398 | return c; |
| 399 | } |
| 400 | |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 401 | const char* log_category_name(int subsys) |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 402 | { |
| 403 | if (subsys < osmo_log_info->num_cat) |
| 404 | return osmo_log_info->cat[subsys].name; |
| 405 | |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Neels Hofmeyr | 0e2a943 | 2018-01-16 02:49:48 +0100 | [diff] [blame] | 409 | static const char *const_basename(const char *path) |
| 410 | { |
| 411 | const char *bn = strrchr(path, '/'); |
| 412 | if (!bn || !bn[1]) |
| 413 | return path; |
| 414 | return bn + 1; |
| 415 | } |
| 416 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 417 | static void _output(struct log_target *target, unsigned int subsys, |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 418 | unsigned int level, const char *file, int line, int cont, |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 419 | const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 420 | { |
Harald Welte | a8b1b21 | 2020-09-27 17:21:07 +0200 | [diff] [blame] | 421 | char buf[MAX_LOG_SIZE]; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 422 | int ret, len = 0, offset = 0, rem = sizeof(buf); |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 423 | const char *c_subsys = NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 424 | |
| 425 | /* are we using color */ |
| 426 | if (target->use_color) { |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 427 | c_subsys = color(subsys); |
| 428 | if (c_subsys) { |
Neels Hofmeyr | 5e518b5 | 2018-01-17 13:20:02 +0100 | [diff] [blame] | 429 | ret = snprintf(buf + offset, rem, "%s", c_subsys); |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 430 | if (ret < 0) |
| 431 | goto err; |
| 432 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 433 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 434 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 435 | if (!cont) { |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 436 | if (target->print_ext_timestamp) { |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 437 | #ifdef HAVE_LOCALTIME_R |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 438 | struct tm tm; |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 439 | struct timeval tv; |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 440 | osmo_gettimeofday(&tv, NULL); |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 441 | localtime_r(&tv.tv_sec, &tm); |
| 442 | ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ", |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 443 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 444 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 445 | (int)(tv.tv_usec / 1000)); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 446 | if (ret < 0) |
| 447 | goto err; |
| 448 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 449 | #endif |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 450 | } else if (target->print_timestamp) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 451 | time_t tm; |
Pau Espin Pedrol | 3aef238 | 2019-06-12 18:50:29 +0200 | [diff] [blame] | 452 | if ((tm = time(NULL)) == (time_t) -1) |
| 453 | goto err; |
Pau Espin Pedrol | cc794e9 | 2019-06-12 16:22:53 +0200 | [diff] [blame] | 454 | /* Get human-readable representation of time. |
| 455 | man ctime: we need at least 26 bytes in buf */ |
| 456 | if (rem < 26 || !ctime_r(&tm, buf + offset)) |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 457 | goto err; |
Pau Espin Pedrol | cc794e9 | 2019-06-12 16:22:53 +0200 | [diff] [blame] | 458 | ret = strlen(buf + offset); |
| 459 | if (ret <= 0) |
| 460 | goto err; |
| 461 | /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */ |
| 462 | buf[offset + ret - 1] = ' '; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 463 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 464 | } |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 465 | if (target->print_category) { |
Neels Hofmeyr | e653472 | 2018-01-16 03:02:06 +0100 | [diff] [blame] | 466 | ret = snprintf(buf + offset, rem, "%s%s%s%s ", |
| 467 | target->use_color ? level_color(level) : "", |
| 468 | log_category_name(subsys), |
Neels Hofmeyr | f2644ae | 2019-11-20 04:00:29 +0100 | [diff] [blame] | 469 | target->use_color ? OSMO_LOGCOLOR_END : "", |
Neels Hofmeyr | e653472 | 2018-01-16 03:02:06 +0100 | [diff] [blame] | 470 | c_subsys ? c_subsys : ""); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 471 | if (ret < 0) |
| 472 | goto err; |
| 473 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 474 | } |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 475 | if (target->print_level) { |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 476 | ret = snprintf(buf + offset, rem, "%s%s%s%s ", |
| 477 | target->use_color ? level_color(level) : "", |
| 478 | log_level_str(level), |
Neels Hofmeyr | f2644ae | 2019-11-20 04:00:29 +0100 | [diff] [blame] | 479 | target->use_color ? OSMO_LOGCOLOR_END : "", |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 480 | c_subsys ? c_subsys : ""); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | goto err; |
| 483 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 484 | } |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 485 | if (target->print_category_hex) { |
| 486 | ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys); |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 487 | if (ret < 0) |
| 488 | goto err; |
| 489 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 490 | } |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 491 | |
| 492 | if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) { |
| 493 | switch (target->print_filename2) { |
| 494 | case LOG_FILENAME_NONE: |
| 495 | break; |
| 496 | case LOG_FILENAME_PATH: |
| 497 | ret = snprintf(buf + offset, rem, "%s:%d ", file, line); |
| 498 | if (ret < 0) |
| 499 | goto err; |
| 500 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 501 | break; |
| 502 | case LOG_FILENAME_BASENAME: |
| 503 | ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line); |
| 504 | if (ret < 0) |
| 505 | goto err; |
| 506 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 507 | break; |
| 508 | } |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 509 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 510 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 511 | ret = vsnprintf(buf + offset, rem, format, ap); |
| 512 | if (ret < 0) |
| 513 | goto err; |
| 514 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 515 | |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 516 | /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log |
| 517 | * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an |
| 518 | * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */ |
| 519 | if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END |
| 520 | && offset > 0 && buf[offset-1] == '\n') { |
| 521 | switch (target->print_filename2) { |
| 522 | case LOG_FILENAME_NONE: |
| 523 | break; |
| 524 | case LOG_FILENAME_PATH: |
| 525 | offset --; |
| 526 | ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line); |
| 527 | if (ret < 0) |
| 528 | goto err; |
| 529 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 530 | break; |
| 531 | case LOG_FILENAME_BASENAME: |
| 532 | offset --; |
| 533 | ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line); |
| 534 | if (ret < 0) |
| 535 | goto err; |
| 536 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
Pau Espin Pedrol | 6407c82 | 2020-07-20 16:41:44 +0200 | [diff] [blame] | 541 | if (target->use_color && c_subsys) { |
Neels Hofmeyr | f2644ae | 2019-11-20 04:00:29 +0100 | [diff] [blame] | 542 | ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END); |
Neels Hofmeyr | c475988 | 2018-01-16 02:10:48 +0100 | [diff] [blame] | 543 | if (ret < 0) |
| 544 | goto err; |
| 545 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 546 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 547 | err: |
| 548 | buf[sizeof(buf)-1] = '\0'; |
| 549 | target->output(target, level, buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 550 | } |
| 551 | |
Neels Hofmeyr | 42240de | 2016-12-12 15:13:56 +0100 | [diff] [blame] | 552 | /* Catch internal logging category indexes as well as out-of-bounds indexes. |
| 553 | * For internal categories, the ID is negative starting with -1; and internal |
| 554 | * logging categories are added behind the user categories. For out-of-bounds |
| 555 | * indexes, return the index of DLGLOBAL. The returned category index is |
| 556 | * guaranteed to exist in osmo_log_info, otherwise the program would abort, |
| 557 | * which should never happen unless even the DLGLOBAL category is missing. */ |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 558 | static inline int map_subsys(int subsys) |
| 559 | { |
Neels Hofmeyr | 7480226 | 2016-12-12 16:00:24 +0100 | [diff] [blame] | 560 | /* Note: comparing signed and unsigned integers */ |
| 561 | |
| 562 | if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user) |
| 563 | subsys = DLGLOBAL; |
| 564 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 565 | if (subsys < 0) |
| 566 | subsys = subsys_lib2index(subsys); |
| 567 | |
Neels Hofmeyr | ca13574 | 2016-12-12 14:18:54 +0100 | [diff] [blame] | 568 | if (subsys < 0 || subsys >= osmo_log_info->num_cat) |
Neels Hofmeyr | 42240de | 2016-12-12 15:13:56 +0100 | [diff] [blame] | 569 | subsys = subsys_lib2index(DLGLOBAL); |
| 570 | |
Neels Hofmeyr | ca13574 | 2016-12-12 14:18:54 +0100 | [diff] [blame] | 571 | OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat)); |
| 572 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 573 | return subsys; |
| 574 | } |
| 575 | |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 576 | static inline bool should_log_to_target(struct log_target *tar, int subsys, |
| 577 | int level) |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 578 | { |
| 579 | struct log_category *category; |
| 580 | |
| 581 | category = &tar->categories[subsys]; |
| 582 | |
| 583 | /* subsystem is not supposed to be logged */ |
| 584 | if (!category->enabled) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 585 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 586 | |
| 587 | /* Check the global log level */ |
| 588 | if (tar->loglevel != 0 && level < tar->loglevel) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 589 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 590 | |
| 591 | /* Check the category log level */ |
| 592 | if (tar->loglevel == 0 && category->loglevel != 0 && |
| 593 | level < category->loglevel) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 594 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 595 | |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 596 | /* Apply filters here... if that becomes messy we will |
| 597 | * need to put filters in a list and each filter will |
| 598 | * say stop, continue, output */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 599 | if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 600 | return true; |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 601 | |
| 602 | if (osmo_log_info->filter_fn) |
| 603 | return osmo_log_info->filter_fn(&log_context, tar); |
| 604 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 605 | /* TODO: Check the filter/selector too? */ |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 606 | return true; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 609 | /*! vararg version of logging function |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 610 | * \param[in] subsys Logging sub-system |
| 611 | * \param[in] level Log level |
| 612 | * \param[in] file name of source code file |
| 613 | * \param[in] cont continuation (1) or new line (0) |
| 614 | * \param[in] format format string |
| 615 | * \param[in] ap vararg-list containing format string arguments |
| 616 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 617 | void osmo_vlogp(int subsys, int level, const char *file, int line, |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 618 | int cont, const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 619 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 620 | struct log_target *tar; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 621 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 622 | subsys = map_subsys(subsys); |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 623 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 624 | log_tgt_mutex_lock(); |
| 625 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 626 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 627 | va_list bp; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 628 | |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 629 | if (!should_log_to_target(tar, subsys, level)) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 630 | continue; |
| 631 | |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 632 | /* According to the manpage, vsnprintf leaves the value of ap |
| 633 | * in undefined state. Since _output uses vsnprintf and it may |
| 634 | * be called several times, we have to pass a copy of ap. */ |
| 635 | va_copy(bp, ap); |
Harald Welte | d7c0a37 | 2016-12-02 13:52:59 +0100 | [diff] [blame] | 636 | if (tar->raw_output) |
| 637 | tar->raw_output(tar, subsys, level, file, line, cont, format, bp); |
| 638 | else |
| 639 | _output(tar, subsys, level, file, line, cont, format, bp); |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 640 | va_end(bp); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 641 | } |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 642 | |
| 643 | log_tgt_mutex_unlock(); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 644 | } |
| 645 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 646 | /*! logging function used by DEBUGP() macro |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 647 | * \param[in] subsys Logging sub-system |
| 648 | * \param[in] file name of source code file |
| 649 | * \param[in] cont continuation (1) or new line (0) |
| 650 | * \param[in] format format string |
| 651 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 652 | void logp(int subsys, const char *file, int line, int cont, |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 653 | const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 654 | { |
| 655 | va_list ap; |
| 656 | |
| 657 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 658 | osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 659 | va_end(ap); |
| 660 | } |
| 661 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 662 | /*! logging function used by LOGP() macro |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 663 | * \param[in] subsys Logging sub-system |
| 664 | * \param[in] level Log level |
| 665 | * \param[in] file name of source code file |
| 666 | * \param[in] cont continuation (1) or new line (0) |
| 667 | * \param[in] format format string |
| 668 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 669 | void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 670 | { |
| 671 | va_list ap; |
| 672 | |
| 673 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 674 | osmo_vlogp(subsys, level, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 675 | va_end(ap); |
| 676 | } |
| 677 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 678 | /*! Register a new log target with the logging core |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 679 | * \param[in] target Log target to be registered |
| 680 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 681 | void log_add_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 682 | { |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 683 | llist_add_tail(&target->entry, &osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 684 | } |
| 685 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 686 | /*! Unregister a log target from the logging core |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 687 | * \param[in] target Log target to be unregistered |
| 688 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 689 | void log_del_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 690 | { |
| 691 | llist_del(&target->entry); |
| 692 | } |
| 693 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 694 | /*! Reset (clear) the logging context */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 695 | void log_reset_context(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 696 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 697 | memset(&log_context, 0, sizeof(log_context)); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 698 | } |
| 699 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 700 | /*! Set the logging context |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 701 | * \param[in] ctx_nr logging context number |
| 702 | * \param[in] value value to which the context is to be set |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 703 | * \returns 0 in case of success; negative otherwise |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 704 | * |
| 705 | * A logging context is something like the subscriber identity to which |
| 706 | * the currently processed message relates, or the BTS through which it |
| 707 | * was received. As soon as this data is known, it can be set using |
| 708 | * this function. The main use of context information is for logging |
| 709 | * filters. |
| 710 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 711 | int log_set_context(uint8_t ctx_nr, void *value) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 712 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 713 | if (ctx_nr > LOG_MAX_CTX) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 714 | return -EINVAL; |
| 715 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 716 | log_context.ctx[ctx_nr] = value; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 721 | /*! Enable the \ref LOG_FLT_ALL log filter |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 722 | * \param[in] target Log target to be affected |
| 723 | * \param[in] all enable (1) or disable (0) the ALL filter |
| 724 | * |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 725 | * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 726 | * printed. It acts as a wildcard. Setting it to \a 1 means there is no |
| 727 | * filtering. |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 728 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 729 | void log_set_all_filter(struct log_target *target, int all) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 730 | { |
| 731 | if (all) |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 732 | target->filter_map |= (1 << LOG_FLT_ALL); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 733 | else |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 734 | target->filter_map &= ~(1 << LOG_FLT_ALL); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 735 | } |
| 736 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 737 | /*! Enable or disable the use of colored output |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 738 | * \param[in] target Log target to be affected |
| 739 | * \param[in] use_color Use color (1) or don't use color (0) |
| 740 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 741 | void log_set_use_color(struct log_target *target, int use_color) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 742 | { |
| 743 | target->use_color = use_color; |
| 744 | } |
| 745 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 746 | /*! Enable or disable printing of timestamps while logging |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 747 | * \param[in] target Log target to be affected |
| 748 | * \param[in] print_timestamp Enable (1) or disable (0) timestamps |
| 749 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 750 | void log_set_print_timestamp(struct log_target *target, int print_timestamp) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 751 | { |
| 752 | target->print_timestamp = print_timestamp; |
| 753 | } |
| 754 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 755 | /*! Enable or disable printing of extended timestamps while logging |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 756 | * \param[in] target Log target to be affected |
| 757 | * \param[in] print_timestamp Enable (1) or disable (0) timestamps |
| 758 | * |
| 759 | * When both timestamp and extended timestamp is enabled then only |
| 760 | * the extended timestamp will be used. The format of the timestamp |
| 761 | * is YYYYMMDDhhmmssnnn. |
| 762 | */ |
| 763 | void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp) |
| 764 | { |
| 765 | target->print_ext_timestamp = print_timestamp; |
| 766 | } |
| 767 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 768 | /*! Use log_set_print_filename2() instead. |
| 769 | * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call |
| 770 | * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy |
| 771 | * behavior, which combined the category in hex with the filename. For example, if the category-hex |
| 772 | * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as |
| 773 | * dependent projects) would fail since they expect the category to disappear along with the filename. |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 774 | * \param[in] target Log target to be affected |
| 775 | * \param[in] print_filename Enable (1) or disable (0) filenames |
| 776 | */ |
| 777 | void log_set_print_filename(struct log_target *target, int print_filename) |
| 778 | { |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 779 | log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE); |
| 780 | log_set_print_category_hex(target, print_filename); |
| 781 | } |
| 782 | |
| 783 | /*! Enable or disable printing of the filename while logging. |
| 784 | * \param[in] target Log target to be affected. |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 785 | * \param[in] lft An LOG_FILENAME_* enum value. |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 786 | * LOG_FILENAME_NONE omits the source file and line information from logs. |
| 787 | * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros. |
| 788 | */ |
| 789 | void log_set_print_filename2(struct log_target *target, enum log_filename_type lft) |
| 790 | { |
| 791 | target->print_filename2 = lft; |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 794 | /*! Set the position where on a log line the source file info should be logged. |
| 795 | * \param[in] target Log target to be affected. |
| 796 | * \param[in] pos A LOG_FILENAME_POS_* enum value. |
| 797 | * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message. |
| 798 | * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the |
| 799 | */ |
| 800 | void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos) |
| 801 | { |
| 802 | target->print_filename_pos = pos; |
| 803 | } |
| 804 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 805 | /*! Enable or disable printing of the category name |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 806 | * \param[in] target Log target to be affected |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 807 | * \param[in] print_category Enable (1) or disable (0) filenames |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 808 | * |
| 809 | * Print the category/subsys name in front of every log message. |
| 810 | */ |
| 811 | void log_set_print_category(struct log_target *target, int print_category) |
| 812 | { |
| 813 | target->print_category = print_category; |
| 814 | } |
| 815 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 816 | /*! Enable or disable printing of the category number in hex ('<000b>'). |
| 817 | * \param[in] target Log target to be affected. |
| 818 | * \param[in] print_category_hex Enable (1) or disable (0) hex category. |
| 819 | */ |
| 820 | void log_set_print_category_hex(struct log_target *target, int print_category_hex) |
| 821 | { |
| 822 | target->print_category_hex = print_category_hex; |
| 823 | } |
| 824 | |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 825 | /*! Enable or disable printing of the log level name. |
| 826 | * \param[in] target Log target to be affected |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 827 | * \param[in] print_level Enable (1) or disable (0) log level name |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 828 | * |
| 829 | * Print the log level name in front of every log message. |
| 830 | */ |
| 831 | void log_set_print_level(struct log_target *target, int print_level) |
| 832 | { |
| 833 | target->print_level = (bool)print_level; |
| 834 | } |
| 835 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 836 | /*! Set the global log level for a given log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 837 | * \param[in] target Log target to be affected |
| 838 | * \param[in] log_level New global log level |
| 839 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 840 | void log_set_log_level(struct log_target *target, int log_level) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 841 | { |
| 842 | target->loglevel = log_level; |
| 843 | } |
| 844 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 845 | /*! Set a category filter on a given log target |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 846 | * \param[in] target Log target to be affected |
| 847 | * \param[in] category Log category to be affected |
| 848 | * \param[in] enable whether to enable or disable the filter |
| 849 | * \param[in] level Log level of the filter |
| 850 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 851 | void log_set_category_filter(struct log_target *target, int category, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 852 | int enable, int level) |
| 853 | { |
Neels Hofmeyr | 886d6fd | 2016-12-12 13:49:03 +0100 | [diff] [blame] | 854 | if (!target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 855 | return; |
Neels Hofmeyr | 886d6fd | 2016-12-12 13:49:03 +0100 | [diff] [blame] | 856 | category = map_subsys(category); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 857 | target->categories[category].enabled = !!enable; |
| 858 | target->categories[category].loglevel = level; |
| 859 | } |
| 860 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 861 | #if (!EMBEDDED) |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 862 | static void _file_output(struct log_target *target, unsigned int level, |
| 863 | const char *log) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 864 | { |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 865 | fprintf(target->tgt_file.out, "%s", log); |
| 866 | fflush(target->tgt_file.out); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 867 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 868 | #endif |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 869 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 870 | /*! Create a new log target skeleton |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 871 | * \returns dynamically-allocated log target |
| 872 | * This funcition allocates a \ref log_target and initializes it |
| 873 | * with some default values. The newly created target is not |
| 874 | * registered yet. |
| 875 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 876 | struct log_target *log_target_create(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 877 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 878 | struct log_target *target; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 879 | unsigned int i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 880 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 881 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 882 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 883 | target = talloc_zero(tall_log_ctx, struct log_target); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 884 | if (!target) |
| 885 | return NULL; |
| 886 | |
Pau Espin Pedrol | 9d4a36e | 2018-07-26 11:55:33 +0200 | [diff] [blame] | 887 | target->categories = talloc_zero_array(target, |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 888 | struct log_category, |
| 889 | osmo_log_info->num_cat); |
| 890 | if (!target->categories) { |
| 891 | talloc_free(target); |
| 892 | return NULL; |
| 893 | } |
| 894 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 895 | INIT_LLIST_HEAD(&target->entry); |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 896 | |
| 897 | /* initialize the per-category enabled/loglevel from defaults */ |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 898 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 899 | struct log_category *cat = &target->categories[i]; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 900 | cat->enabled = osmo_log_info->cat[i].enabled; |
| 901 | cat->loglevel = osmo_log_info->cat[i].loglevel; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* global settings */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 905 | target->use_color = 1; |
| 906 | target->print_timestamp = 0; |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 907 | target->print_filename2 = LOG_FILENAME_PATH; |
| 908 | target->print_category_hex = true; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 909 | |
| 910 | /* global log level */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 911 | target->loglevel = 0; |
| 912 | return target; |
| 913 | } |
| 914 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 915 | /*! Create the STDERR log target |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 916 | * \returns dynamically-allocated \ref log_target for STDERR */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 917 | struct log_target *log_target_create_stderr(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 918 | { |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 919 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
Harald Welte | b93ce5a | 2017-05-15 10:58:15 +0200 | [diff] [blame] | 920 | #if !EMBEDDED && defined(stderr) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 921 | struct log_target *target; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 922 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 923 | target = log_target_create(); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 924 | if (!target) |
| 925 | return NULL; |
| 926 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 927 | target->type = LOG_TGT_TYPE_STDERR; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 928 | target->tgt_file.out = stderr; |
| 929 | target->output = _file_output; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 930 | return target; |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 931 | #else |
| 932 | return NULL; |
| 933 | #endif /* stderr */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 934 | } |
| 935 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 936 | #if (!EMBEDDED) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 937 | /*! Create a new file-based log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 938 | * \param[in] fname File name of the new log file |
| 939 | * \returns Log target in case of success, NULL otherwise |
| 940 | */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 941 | struct log_target *log_target_create_file(const char *fname) |
| 942 | { |
| 943 | struct log_target *target; |
| 944 | |
| 945 | target = log_target_create(); |
| 946 | if (!target) |
| 947 | return NULL; |
| 948 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 949 | target->type = LOG_TGT_TYPE_FILE; |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 950 | target->tgt_file.out = fopen(fname, "a"); |
Vadim Yanitskiy | b89114b | 2020-09-09 04:51:04 +0700 | [diff] [blame] | 951 | if (!target->tgt_file.out) { |
| 952 | log_target_destroy(target); |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 953 | return NULL; |
Vadim Yanitskiy | b89114b | 2020-09-09 04:51:04 +0700 | [diff] [blame] | 954 | } |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 955 | |
| 956 | target->output = _file_output; |
| 957 | |
| 958 | target->tgt_file.fname = talloc_strdup(target, fname); |
| 959 | |
| 960 | return target; |
| 961 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 962 | #endif |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 963 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 964 | /*! Find a registered log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 965 | * \param[in] type Log target type |
| 966 | * \param[in] fname File name |
| 967 | * \returns Log target (if found), NULL otherwise |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 968 | * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock. |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 969 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 970 | struct log_target *log_target_find(int type, const char *fname) |
| 971 | { |
| 972 | struct log_target *tgt; |
| 973 | |
| 974 | llist_for_each_entry(tgt, &osmo_log_target_list, entry) { |
| 975 | if (tgt->type != type) |
| 976 | continue; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 977 | switch (tgt->type) { |
| 978 | case LOG_TGT_TYPE_FILE: |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 979 | if (!strcmp(fname, tgt->tgt_file.fname)) |
| 980 | return tgt; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 981 | break; |
| 982 | case LOG_TGT_TYPE_GSMTAP: |
| 983 | if (!strcmp(fname, tgt->tgt_gsmtap.hostname)) |
| 984 | return tgt; |
| 985 | break; |
| 986 | default: |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 987 | return tgt; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 988 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 989 | } |
| 990 | return NULL; |
| 991 | } |
| 992 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 993 | /*! Unregister, close and delete a log target |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 994 | * \param[in] target log target to unregister, close and delete */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 995 | void log_target_destroy(struct log_target *target) |
| 996 | { |
| 997 | |
| 998 | /* just in case, to make sure we don't have any references */ |
| 999 | log_del_target(target); |
| 1000 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 1001 | #if (!EMBEDDED) |
Vadim Yanitskiy | 744236b | 2020-09-09 04:42:22 +0700 | [diff] [blame] | 1002 | switch (target->type) { |
| 1003 | case LOG_TGT_TYPE_FILE: |
Vadim Yanitskiy | b89114b | 2020-09-09 04:51:04 +0700 | [diff] [blame] | 1004 | if (target->tgt_file.out == NULL) |
| 1005 | break; |
Vadim Yanitskiy | 744236b | 2020-09-09 04:42:22 +0700 | [diff] [blame] | 1006 | fclose(target->tgt_file.out); |
| 1007 | target->tgt_file.out = NULL; |
| 1008 | break; |
Vadim Yanitskiy | 04f4271 | 2020-09-09 04:47:25 +0700 | [diff] [blame] | 1009 | #ifdef HAVE_SYSLOG_H |
| 1010 | case LOG_TGT_TYPE_SYSLOG: |
| 1011 | closelog(); |
| 1012 | break; |
| 1013 | #endif /* HAVE_SYSLOG_H */ |
Vadim Yanitskiy | 744236b | 2020-09-09 04:42:22 +0700 | [diff] [blame] | 1014 | default: |
| 1015 | /* make GCC happy */ |
| 1016 | break; |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 1017 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 1018 | #endif |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 1019 | |
| 1020 | talloc_free(target); |
| 1021 | } |
| 1022 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1023 | /*! close and re-open a log file (for log file rotation) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1024 | * \param[in] target log target to re-open |
| 1025 | * \returns 0 in case of success; negative otherwise */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 1026 | int log_target_file_reopen(struct log_target *target) |
| 1027 | { |
| 1028 | fclose(target->tgt_file.out); |
| 1029 | |
| 1030 | target->tgt_file.out = fopen(target->tgt_file.fname, "a"); |
| 1031 | if (!target->tgt_file.out) |
| 1032 | return -errno; |
| 1033 | |
| 1034 | /* we assume target->output already to be set */ |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1039 | /*! close and re-open all log files (for log file rotation) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 1040 | * \returns 0 in case of success; negative otherwise */ |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 1041 | int log_targets_reopen(void) |
| 1042 | { |
| 1043 | struct log_target *tar; |
| 1044 | int rc = 0; |
| 1045 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1046 | log_tgt_mutex_lock(); |
| 1047 | |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 1048 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
| 1049 | switch (tar->type) { |
| 1050 | case LOG_TGT_TYPE_FILE: |
| 1051 | if (log_target_file_reopen(tar) < 0) |
| 1052 | rc = -1; |
| 1053 | break; |
| 1054 | default: |
| 1055 | break; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1059 | log_tgt_mutex_unlock(); |
| 1060 | |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 1061 | return rc; |
| 1062 | } |
| 1063 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1064 | /*! Initialize the Osmocom logging core |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 1065 | * \param[in] inf Information regarding logging categories, could be NULL |
Vadim Yanitskiy | 73e66b3 | 2019-03-25 21:24:20 +0700 | [diff] [blame] | 1066 | * \param[in] ctx talloc context for logging allocations |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 1067 | * \returns 0 in case of success, negative in case of error |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 1068 | * |
| 1069 | * If inf is NULL then only library-internal categories are initialized. |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 1070 | */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1071 | int log_init(const struct log_info *inf, void *ctx) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 1072 | { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1073 | int i; |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1074 | struct log_info_cat *cat_ptr; |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1075 | |
Philipp Maier | dc02c06 | 2020-05-12 17:51:25 +0200 | [diff] [blame] | 1076 | /* Ensure that log_init is not called multiple times */ |
| 1077 | OSMO_ASSERT(tall_log_ctx == NULL) |
| 1078 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1079 | tall_log_ctx = talloc_named_const(ctx, 1, "logging"); |
| 1080 | if (!tall_log_ctx) |
| 1081 | return -ENOMEM; |
| 1082 | |
| 1083 | osmo_log_info = talloc_zero(tall_log_ctx, struct log_info); |
| 1084 | if (!osmo_log_info) |
| 1085 | return -ENOMEM; |
| 1086 | |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 1087 | osmo_log_info->num_cat = ARRAY_SIZE(internal_cat); |
| 1088 | |
| 1089 | if (inf) { |
| 1090 | osmo_log_info->filter_fn = inf->filter_fn; |
| 1091 | osmo_log_info->num_cat_user = inf->num_cat; |
| 1092 | osmo_log_info->num_cat += inf->num_cat; |
| 1093 | } |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1094 | |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1095 | cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat, |
| 1096 | osmo_log_info->num_cat); |
| 1097 | if (!cat_ptr) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1098 | talloc_free(osmo_log_info); |
| 1099 | osmo_log_info = NULL; |
| 1100 | return -ENOMEM; |
| 1101 | } |
| 1102 | |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1103 | /* copy over the user part and sanitize loglevel */ |
| 1104 | if (inf) { |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 1105 | for (i = 0; i < inf->num_cat; i++) { |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1106 | memcpy(&cat_ptr[i], &inf->cat[i], |
| 1107 | sizeof(struct log_info_cat)); |
| 1108 | |
| 1109 | /* Make sure that the loglevel is set to NOTICE in case |
| 1110 | * no loglevel has been preset. */ |
| 1111 | if (!cat_ptr[i].loglevel) { |
| 1112 | cat_ptr[i].loglevel = LOGL_NOTICE; |
| 1113 | } |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 1114 | } |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | /* copy over the library part */ |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 1118 | for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 1119 | unsigned int cn = osmo_log_info->num_cat_user + i; |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1120 | memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat)); |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Philipp Maier | dcad1c5 | 2020-03-25 11:25:59 +0100 | [diff] [blame] | 1123 | osmo_log_info->cat = cat_ptr; |
| 1124 | |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 1125 | return 0; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 1126 | } |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 1127 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1128 | /* De-initialize the Osmocom logging core |
Harald Welte | 69e6c3c | 2016-04-20 10:41:27 +0200 | [diff] [blame] | 1129 | * This function destroys all targets and releases associated memory */ |
| 1130 | void log_fini(void) |
| 1131 | { |
| 1132 | struct log_target *tar, *tar2; |
| 1133 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1134 | log_tgt_mutex_lock(); |
| 1135 | |
Harald Welte | 69e6c3c | 2016-04-20 10:41:27 +0200 | [diff] [blame] | 1136 | llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry) |
| 1137 | log_target_destroy(tar); |
| 1138 | |
| 1139 | talloc_free(osmo_log_info); |
| 1140 | osmo_log_info = NULL; |
| 1141 | talloc_free(tall_log_ctx); |
| 1142 | tall_log_ctx = NULL; |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1143 | |
| 1144 | log_tgt_mutex_unlock(); |
Harald Welte | 69e6c3c | 2016-04-20 10:41:27 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1147 | /*! Check whether a log entry will be generated. |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1148 | * \returns != 0 if a log entry might get generated by at least one target */ |
| 1149 | int log_check_level(int subsys, unsigned int level) |
| 1150 | { |
| 1151 | struct log_target *tar; |
| 1152 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 1153 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 1154 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 1155 | subsys = map_subsys(subsys); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1156 | |
| 1157 | /* TODO: The following could/should be cached (update on config) */ |
| 1158 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1159 | log_tgt_mutex_lock(); |
| 1160 | |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1161 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 1162 | if (!should_log_to_target(tar, subsys, level)) |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1163 | continue; |
| 1164 | |
| 1165 | /* This might get logged (ignoring filters) */ |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1166 | log_tgt_mutex_unlock(); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1167 | return 1; |
| 1168 | } |
| 1169 | |
| 1170 | /* We are sure, that this will not be logged. */ |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 1171 | log_tgt_mutex_unlock(); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1175 | /*! @} */ |