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