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