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