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