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