blob: c2a0453fe9f456bbfee191b1122c14617d086272 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging.c
2 * Debugging/Logging support code. */
3/*
4 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte4a2bb9e2010-03-26 09:33:40 +08005 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +080010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Harald Weltef01b2382017-10-16 13:55:34 +020026/*! \addtogroup logging
Harald Welte18fc4652011-08-17 14:14:17 +020027 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 * libosmocore Logging sub-system
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 *
30 * \file logging.c */
Harald Welte18fc4652011-08-17 14:14:17 +020031
Harald Welte01fd5cb2010-03-26 23:51:31 +080032#include "../config.h"
33
Harald Welte4a2bb9e2010-03-26 09:33:40 +080034#include <stdarg.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080038
39#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080040#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080041#endif
Vadim Yanitskiy04f42712020-09-09 04:47:25 +070042
43#ifdef HAVE_SYSLOG_H
44#include <syslog.h>
45#endif
46
Harald Welte433005c2020-09-26 11:51:32 +020047#ifdef HAVE_SYSTEMTAP
48/* include the generated probes header and put markers in code */
49#include "probes.h"
50#define TRACE(probe) probe
51#define TRACE_ENABLED(probe) probe ## _ENABLED()
52#else
53/* Wrap the probe to allow it to be removed when no systemtap available */
54#define TRACE(probe)
55#define TRACE_ENABLED(probe) (0)
56#endif /* HAVE_SYSTEMTAP */
57
Harald Welte4a2bb9e2010-03-26 09:33:40 +080058#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010059#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080060#include <errno.h>
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020061#include <pthread.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080062
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010063#include <osmocom/core/talloc.h>
64#include <osmocom/core/utils.h>
65#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020066#include <osmocom/core/timer.h>
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010067#include <osmocom/core/thread.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080068
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010069#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
70
Harald Weltea8b1b212020-09-27 17:21:07 +020071/* maximum length of the log string of a single log event (typically line) */
72#define MAX_LOG_SIZE 4096
73
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010074osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010075 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010076osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010077 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010078osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010079 enum_logging_filters_fit_in_log_target_filter_map);
80
Harald Welteb43bc042011-06-27 10:29:17 +020081struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080082
Harald Welte3ae27582010-03-26 21:24:24 +080083static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020084void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010085LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080086
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010087static __thread long int logging_tid;
88
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020089#if (!EMBEDDED)
90/*! This mutex must be held while using osmo_log_target_list or any of its
91 log_targets in a multithread program. Prevents race conditions between threads
92 like producing unordered timestamps or VTY deleting a target while another
93 thread is writing to it */
94static pthread_mutex_t osmo_log_tgt_mutex;
95static bool osmo_log_tgt_mutex_on = false;
96
97/*! Enable multithread support (mutex) in libosmocore logging system.
98 * Must be called by processes willing to use logging subsystem from several
99 * threads. Once enabled, it's not possible to disable it again.
100 */
101void log_enable_multithread(void) {
102 if (osmo_log_tgt_mutex_on)
103 return;
104 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
105 osmo_log_tgt_mutex_on = true;
106}
107
108/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
109 * macro log_tgt_mutex_lock() instead.
110 */
111void log_tgt_mutex_lock_impl(void) {
112 /* These lines are useful to debug scenarios where there's only 1 thread
113 and a double lock appears, for instance during startup and some
114 unlock() missing somewhere:
115 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
116 osmo_panic("acquiring already locked mutex!\n");
117 return;
118 */
119
120 if (osmo_log_tgt_mutex_on)
121 pthread_mutex_lock(&osmo_log_tgt_mutex);
122}
123
124/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
125 * macro log_tgt_mutex_unlock() instead.
126 */
127void log_tgt_mutex_unlock_impl(void) {
128 if (osmo_log_tgt_mutex_on)
129 pthread_mutex_unlock(&osmo_log_tgt_mutex);
130}
131
132#else /* if (!EMBEDDED) */
133#pragma message ("logging multithread support disabled in embedded build")
134void log_enable_multithread(void) {}
135void log_tgt_mutex_lock_impl(void) {}
136void log_tgt_mutex_unlock_impl(void) {}
137#endif /* if (!EMBEDDED) */
138
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200139const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800140 { LOGL_DEBUG, "DEBUG" },
141 { LOGL_INFO, "INFO" },
142 { LOGL_NOTICE, "NOTICE" },
143 { LOGL_ERROR, "ERROR" },
144 { LOGL_FATAL, "FATAL" },
145 { 0, NULL },
146};
147
Harald Welte4c0e1a12020-12-07 22:03:09 +0100148/* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
Harald Welteb43bc042011-06-27 10:29:17 +0200149#define INT2IDX(x) (-1*(x)-1)
150static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
151 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
152 .name = "DLGLOBAL",
153 .description = "Library-internal global log family",
154 .loglevel = LOGL_NOTICE,
155 .enabled = 1,
156 },
root8a996b42011-09-26 11:22:21 +0200157 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
158 .name = "DLLAPD",
159 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200160 .loglevel = LOGL_NOTICE,
161 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100162 .color = "\033[38;5;21m",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200163 },
Harald Welte892e6212011-07-19 14:31:44 +0200164 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200165 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200166 .description = "A-bis Intput Subsystem",
167 .loglevel = LOGL_NOTICE,
168 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100169 .color = "\033[38;5;23m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200170 },
Harald Welte892e6212011-07-19 14:31:44 +0200171 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200172 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200173 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
174 .loglevel = LOGL_NOTICE,
175 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100176 .color = "\033[38;5;25m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200177 },
Harald Welte892e6212011-07-19 14:31:44 +0200178 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200179 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200180 .description = "A-bis Input Driver for Signalling",
181 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100182 .color = "\033[38;5;27m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200183 },
Harald Welte892e6212011-07-19 14:31:44 +0200184 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200185 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200186 .description = "A-bis Input Driver for B-Channels (voice)",
187 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100188 .color = "\033[38;5;29m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200189 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200190 [INT2IDX(DLSMS)] = {
191 .name = "DLSMS",
192 .description = "Layer3 Short Message Service (SMS)",
193 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100194 .color = "\033[38;5;31m",
Andreas Eversbergc626da92011-10-28 03:53:50 +0200195 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200196 [INT2IDX(DLCTRL)] = {
197 .name = "DLCTRL",
198 .description = "Control Interface",
199 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100200 .color = "\033[38;5;33m",
Harald Welte7fd0c832014-08-20 19:58:13 +0200201 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100202 [INT2IDX(DLGTP)] = {
203 .name = "DLGTP",
204 .description = "GPRS GTP library",
205 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100206 .color = "\033[38;5;35m",
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100207 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100208 [INT2IDX(DLSTATS)] = {
209 .name = "DLSTATS",
210 .description = "Statistics messages and logging",
211 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100212 .color = "\033[38;5;37m",
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100213 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100214 [INT2IDX(DLGSUP)] = {
215 .name = "DLGSUP",
216 .description = "Generic Subscriber Update Protocol",
217 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100218 .color = "\033[38;5;39m",
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100219 },
Harald Weltec0f00072016-04-27 18:32:35 +0200220 [INT2IDX(DLOAP)] = {
221 .name = "DLOAP",
222 .description = "Osmocom Authentication Protocol",
223 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100224 .color = "\033[38;5;41m",
Harald Weltec0f00072016-04-27 18:32:35 +0200225 },
Harald Welte059c4042017-04-03 22:20:49 +0200226 [INT2IDX(DLSS7)] = {
227 .name = "DLSS7",
228 .description = "libosmo-sigtran Signalling System 7",
229 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100230 .color = "\033[38;5;43m",
Harald Welte059c4042017-04-03 22:20:49 +0200231 },
232 [INT2IDX(DLSCCP)] = {
233 .name = "DLSCCP",
234 .description = "libosmo-sigtran SCCP Implementation",
235 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100236 .color = "\033[38;5;45m",
Harald Welte059c4042017-04-03 22:20:49 +0200237 },
238 [INT2IDX(DLSUA)] = {
239 .name = "DLSUA",
240 .description = "libosmo-sigtran SCCP User Adaptation",
241 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100242 .color = "\033[38;5;47m",
Harald Welte059c4042017-04-03 22:20:49 +0200243 },
244 [INT2IDX(DLM3UA)] = {
245 .name = "DLM3UA",
246 .description = "libosmo-sigtran MTP3 User Adaptation",
247 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100248 .color = "\033[38;5;49m",
Harald Welte059c4042017-04-03 22:20:49 +0200249 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200250 [INT2IDX(DLMGCP)] = {
251 .name = "DLMGCP",
252 .description = "libosmo-mgcp Media Gateway Control Protocol",
253 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100254 .color = "\033[38;5;51m",
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200255 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100256 [INT2IDX(DLJIBUF)] = {
257 .name = "DLJIBUF",
258 .description = "libosmo-netif Jitter Buffer",
259 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100260 .color = "\033[38;5;53m",
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100261 },
Max450f5ac2019-02-14 19:12:03 +0100262 [INT2IDX(DLRSPRO)] = {
263 .name = "DLRSPRO",
264 .description = "Remote SIM protocol",
265 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100266 .color = "\033[38;5;55m",
Max450f5ac2019-02-14 19:12:03 +0100267 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 [INT2IDX(DLNS)] = {
269 .name = "DLNS",
270 .description = "GPRS NS layer",
271 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100272 .color = "\033[38;5;57m",
Alexander Couzens6a161492020-07-12 13:45:50 +0200273 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100274 [INT2IDX(DLBSSGP)] = {
275 .name = "DLBSSGP",
276 .description = "GPRS BSSGP layer",
277 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100278 .color = "\033[38;5;59m",
Harald Weltefde19ed2020-12-07 21:43:51 +0100279 },
Harald Welteb43bc042011-06-27 10:29:17 +0200280};
281
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200282void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100283{
284 if (!osmo_log_info) {
285 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100286 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100287 OSMO_ASSERT(osmo_log_info);
288 }
289}
290
Harald Welteb43bc042011-06-27 10:29:17 +0200291/* special magic for negative (library-internal) log subsystem numbers */
292static int subsys_lib2index(int subsys)
293{
294 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
295}
296
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700298 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200299 * \returns numeric log level
300 */
Harald Welte3ae27582010-03-26 21:24:24 +0800301int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800302{
303 return get_string_value(loglevel_strs, lvl);
304}
305
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200306/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700307 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200308 * \returns zero-terminated string (log level name)
309 */
Harald Welte9ac22252010-05-11 11:19:40 +0200310const char *log_level_str(unsigned int lvl)
311{
312 return get_value_string(loglevel_strs, lvl);
313}
314
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200315/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200316 * \param[in] category human-readable log category name
317 * \returns numeric category value, or -EINVAL otherwise
318 */
Harald Welte3ae27582010-03-26 21:24:24 +0800319int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800320{
321 int i;
322
Max68bf16a2018-01-10 17:00:43 +0100323 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100324
Harald Welte4ebdf742010-05-19 19:54:00 +0200325 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200326 if (osmo_log_info->cat[i].name == NULL)
327 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200328 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800329 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800330 }
331
332 return -EINVAL;
333}
334
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200335/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200336 * \param[in] target log target to be configured
337 * \param[in] _mask log category mask string
338 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800339 * The format can be this: category1:category2:category3
340 * or category1,2:category2,3:...
341 */
Harald Welte3ae27582010-03-26 21:24:24 +0800342void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800343{
344 int i = 0;
345 char *mask = strdup(_mask);
346 char *category_token = NULL;
347
Max68bf16a2018-01-10 17:00:43 +0100348 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100349
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800350 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200351 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800352 target->categories[i].enabled = 0;
353
354 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200355 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800356 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200357 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200358 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800359 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200360
361 if (!osmo_log_info->cat[i].name)
362 continue;
363
364 length = strlen(category_token);
365 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200366
367 /* Use longest length not to match subocurrences. */
368 if (cat_length > length)
369 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800370
371 if (colon)
372 length = colon - category_token;
373
Harald Welte4ebdf742010-05-19 19:54:00 +0200374 if (strncasecmp(osmo_log_info->cat[i].name,
375 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800376 int level = 0;
377
378 if (colon)
379 level = atoi(colon+1);
380
Harald Weltefaadfe22010-03-26 21:05:43 +0800381 target->categories[i].enabled = 1;
382 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800383 }
384 }
385 } while ((category_token = strtok(NULL, ":")));
386
387 free(mask);
388}
389
390static const char* color(int subsys)
391{
Harald Welte4ebdf742010-05-19 19:54:00 +0200392 if (subsys < osmo_log_info->num_cat)
393 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800394
Harald Welted788f662010-03-26 09:45:03 +0800395 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800396}
397
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100398static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100399 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
400 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
401 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
402 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
403 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100404 { 0, NULL }
405};
406
407static const char *level_color(int level)
408{
409 const char *c = get_value_string_or_null(level_colors, level);
410 if (!c)
411 return get_value_string(level_colors, LOGL_FATAL);
412 return c;
413}
414
Harald Welteaa00f992016-12-02 15:30:02 +0100415const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100416{
417 if (subsys < osmo_log_info->num_cat)
418 return osmo_log_info->cat[subsys].name;
419
420 return NULL;
421}
422
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100423static const char *const_basename(const char *path)
424{
425 const char *bn = strrchr(path, '/');
426 if (!bn || !bn[1])
427 return path;
428 return bn + 1;
429}
430
Harald Welte3ae27582010-03-26 21:24:24 +0800431static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200432 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100433 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800434{
Harald Weltea8b1b212020-09-27 17:21:07 +0200435 char buf[MAX_LOG_SIZE];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200436 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100437 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800438
439 /* are we using color */
440 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100441 c_subsys = color(subsys);
442 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100443 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200444 if (ret < 0)
445 goto err;
446 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800447 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800448 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800449 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100450 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200451#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100452 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100453 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200454 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100455 localtime_r(&tv.tv_sec, &tm);
456 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100457 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100458 tm.tm_hour, tm.tm_min, tm.tm_sec,
459 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100460 if (ret < 0)
461 goto err;
462 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200463#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100464 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800465 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200466 if ((tm = time(NULL)) == (time_t) -1)
467 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200468 /* Get human-readable representation of time.
469 man ctime: we need at least 26 bytes in buf */
470 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200471 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200472 ret = strlen(buf + offset);
473 if (ret <= 0)
474 goto err;
475 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
476 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200477 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800478 }
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100479 if (target->print_tid) {
480 if (logging_tid == 0)
481 logging_tid = (long int)osmo_gettid();
482 ret = snprintf(buf + offset, rem, "%ld ", logging_tid);
483 if (ret < 0)
484 goto err;
485 OSMO_SNPRINTF_RET(ret, rem, offset, len);
486 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100487 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100488 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
489 target->use_color ? level_color(level) : "",
490 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100491 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100492 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100493 if (ret < 0)
494 goto err;
495 OSMO_SNPRINTF_RET(ret, rem, offset, len);
496 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100497 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100498 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
499 target->use_color ? level_color(level) : "",
500 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100501 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100502 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100503 if (ret < 0)
504 goto err;
505 OSMO_SNPRINTF_RET(ret, rem, offset, len);
506 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100507 if (target->print_category_hex) {
508 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200509 if (ret < 0)
510 goto err;
511 OSMO_SNPRINTF_RET(ret, rem, offset, len);
512 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200513
514 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
515 switch (target->print_filename2) {
516 case LOG_FILENAME_NONE:
517 break;
518 case LOG_FILENAME_PATH:
519 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
520 if (ret < 0)
521 goto err;
522 OSMO_SNPRINTF_RET(ret, rem, offset, len);
523 break;
524 case LOG_FILENAME_BASENAME:
525 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
526 if (ret < 0)
527 goto err;
528 OSMO_SNPRINTF_RET(ret, rem, offset, len);
529 break;
530 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100531 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800532 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200533 ret = vsnprintf(buf + offset, rem, format, ap);
534 if (ret < 0)
535 goto err;
536 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800537
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200538 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
539 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
540 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
541 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
542 && offset > 0 && buf[offset-1] == '\n') {
543 switch (target->print_filename2) {
544 case LOG_FILENAME_NONE:
545 break;
546 case LOG_FILENAME_PATH:
547 offset --;
548 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
549 if (ret < 0)
550 goto err;
551 OSMO_SNPRINTF_RET(ret, rem, offset, len);
552 break;
553 case LOG_FILENAME_BASENAME:
554 offset --;
555 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
556 if (ret < 0)
557 goto err;
558 OSMO_SNPRINTF_RET(ret, rem, offset, len);
559 break;
560 }
561 }
562
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200563 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100564 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100565 if (ret < 0)
566 goto err;
567 OSMO_SNPRINTF_RET(ret, rem, offset, len);
568 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200569err:
570 buf[sizeof(buf)-1] = '\0';
571 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800572}
573
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100574/* Catch internal logging category indexes as well as out-of-bounds indexes.
575 * For internal categories, the ID is negative starting with -1; and internal
576 * logging categories are added behind the user categories. For out-of-bounds
577 * indexes, return the index of DLGLOBAL. The returned category index is
578 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
579 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100580static inline int map_subsys(int subsys)
581{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100582 /* Note: comparing signed and unsigned integers */
583
584 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
585 subsys = DLGLOBAL;
586
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100587 if (subsys < 0)
588 subsys = subsys_lib2index(subsys);
589
Neels Hofmeyrca135742016-12-12 14:18:54 +0100590 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100591 subsys = subsys_lib2index(DLGLOBAL);
592
Neels Hofmeyrca135742016-12-12 14:18:54 +0100593 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
594
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100595 return subsys;
596}
597
Maxc65c5b42017-03-15 13:20:23 +0100598static inline bool should_log_to_target(struct log_target *tar, int subsys,
599 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100600{
601 struct log_category *category;
602
603 category = &tar->categories[subsys];
604
605 /* subsystem is not supposed to be logged */
606 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100607 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100608
609 /* Check the global log level */
610 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100611 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100612
613 /* Check the category log level */
614 if (tar->loglevel == 0 && category->loglevel != 0 &&
615 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100616 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100617
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100618 /* Apply filters here... if that becomes messy we will
619 * need to put filters in a list and each filter will
620 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100621 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100622 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100623
624 if (osmo_log_info->filter_fn)
625 return osmo_log_info->filter_fn(&log_context, tar);
626
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100627 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100628 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100629}
630
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200631/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200632 * \param[in] subsys Logging sub-system
633 * \param[in] level Log level
634 * \param[in] file name of source code file
635 * \param[in] cont continuation (1) or new line (0)
636 * \param[in] format format string
637 * \param[in] ap vararg-list containing format string arguments
638 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200639void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200640 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800641{
Harald Welte3ae27582010-03-26 21:24:24 +0800642 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800643
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100644 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200645
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200646 log_tgt_mutex_lock();
647
Harald Welte28222962011-02-18 20:37:04 +0100648 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200649 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800650
Maxc65c5b42017-03-15 13:20:23 +0100651 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800652 continue;
653
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200654 /* According to the manpage, vsnprintf leaves the value of ap
655 * in undefined state. Since _output uses vsnprintf and it may
656 * be called several times, we have to pass a copy of ap. */
657 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100658 if (tar->raw_output)
659 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
660 else
661 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200662 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800663 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200664
665 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800666}
667
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200668/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200669 * \param[in] subsys Logging sub-system
670 * \param[in] file name of source code file
671 * \param[in] cont continuation (1) or new line (0)
672 * \param[in] format format string
673 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200674void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800675 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800676{
677 va_list ap;
678
679 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200680 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800681 va_end(ap);
682}
683
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200684/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200685 * \param[in] subsys Logging sub-system
686 * \param[in] level Log level
687 * \param[in] file name of source code file
688 * \param[in] cont continuation (1) or new line (0)
689 * \param[in] format format string
690 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200691void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800692{
693 va_list ap;
694
Harald Welte433005c2020-09-26 11:51:32 +0200695 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800696 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200697 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800698 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200699 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800700}
701
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200702/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200703 * \param[in] target Log target to be registered
704 */
Harald Welte3ae27582010-03-26 21:24:24 +0800705void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800706{
Harald Welte28222962011-02-18 20:37:04 +0100707 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800708}
709
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200710/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200711 * \param[in] target Log target to be unregistered
712 */
Harald Welte3ae27582010-03-26 21:24:24 +0800713void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800714{
715 llist_del(&target->entry);
716}
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800719void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800720{
Harald Welte3ae27582010-03-26 21:24:24 +0800721 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800722}
723
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200724/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200725 * \param[in] ctx_nr logging context number
726 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200727 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200728 *
729 * A logging context is something like the subscriber identity to which
730 * the currently processed message relates, or the BTS through which it
731 * was received. As soon as this data is known, it can be set using
732 * this function. The main use of context information is for logging
733 * filters.
734 */
Harald Welte3ae27582010-03-26 21:24:24 +0800735int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800736{
Harald Welte3ae27582010-03-26 21:24:24 +0800737 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800738 return -EINVAL;
739
Harald Welte3ae27582010-03-26 21:24:24 +0800740 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800741
742 return 0;
743}
744
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200745/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200746 * \param[in] target Log target to be affected
747 * \param[in] all enable (1) or disable (0) the ALL filter
748 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100749 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100750 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
751 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200752 */
Harald Welte3ae27582010-03-26 21:24:24 +0800753void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800754{
755 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100756 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800757 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100758 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800759}
760
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200761/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200762 * \param[in] target Log target to be affected
763 * \param[in] use_color Use color (1) or don't use color (0)
764 */
Harald Welte3ae27582010-03-26 21:24:24 +0800765void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800766{
767 target->use_color = use_color;
768}
769
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200770/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200771 * \param[in] target Log target to be affected
772 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
773 */
Harald Welte3ae27582010-03-26 21:24:24 +0800774void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800775{
776 target->print_timestamp = print_timestamp;
777}
778
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200779/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100780 * \param[in] target Log target to be affected
781 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
782 *
783 * When both timestamp and extended timestamp is enabled then only
784 * the extended timestamp will be used. The format of the timestamp
785 * is YYYYMMDDhhmmssnnn.
786 */
787void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
788{
789 target->print_ext_timestamp = print_timestamp;
790}
791
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100792/*! Enable or disable printing of timestamps while logging
793 * \param[in] target Log target to be affected
794 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
795 */
796void log_set_print_tid(struct log_target *target, int print_tid)
797{
798 target->print_tid = print_tid;
799}
800
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100801/*! Use log_set_print_filename2() instead.
802 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
803 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
804 * behavior, which combined the category in hex with the filename. For example, if the category-hex
805 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
806 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200807 * \param[in] target Log target to be affected
808 * \param[in] print_filename Enable (1) or disable (0) filenames
809 */
810void log_set_print_filename(struct log_target *target, int print_filename)
811{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100812 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
813 log_set_print_category_hex(target, print_filename);
814}
815
816/*! Enable or disable printing of the filename while logging.
817 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700818 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100819 * LOG_FILENAME_NONE omits the source file and line information from logs.
820 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
821 */
822void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
823{
824 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200825}
826
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200827/*! Set the position where on a log line the source file info should be logged.
828 * \param[in] target Log target to be affected.
829 * \param[in] pos A LOG_FILENAME_POS_* enum value.
830 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
831 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
832 */
833void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
834{
835 target->print_filename_pos = pos;
836}
837
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200838/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100839 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700840 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100841 *
842 * Print the category/subsys name in front of every log message.
843 */
844void log_set_print_category(struct log_target *target, int print_category)
845{
846 target->print_category = print_category;
847}
848
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100849/*! Enable or disable printing of the category number in hex ('<000b>').
850 * \param[in] target Log target to be affected.
851 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
852 */
853void log_set_print_category_hex(struct log_target *target, int print_category_hex)
854{
855 target->print_category_hex = print_category_hex;
856}
857
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100858/*! Enable or disable printing of the log level name.
859 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700860 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100861 *
862 * Print the log level name in front of every log message.
863 */
864void log_set_print_level(struct log_target *target, int print_level)
865{
866 target->print_level = (bool)print_level;
867}
868
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200869/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200870 * \param[in] target Log target to be affected
871 * \param[in] log_level New global log level
872 */
Harald Welte3ae27582010-03-26 21:24:24 +0800873void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800874{
875 target->loglevel = log_level;
876}
877
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200878/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100879 * \param[in] target Log target to be affected
880 * \param[in] category Log category to be affected
881 * \param[in] enable whether to enable or disable the filter
882 * \param[in] level Log level of the filter
883 */
Harald Welte3ae27582010-03-26 21:24:24 +0800884void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800885 int enable, int level)
886{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100887 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800888 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100889 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800890 target->categories[category].enabled = !!enable;
891 target->categories[category].loglevel = level;
892}
893
Harald Welte44c0f632017-01-15 17:58:29 +0100894#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100895static void _file_output(struct log_target *target, unsigned int level,
896 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800897{
Harald Welte0083cd32010-08-25 14:55:44 +0200898 fprintf(target->tgt_file.out, "%s", log);
899 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800900}
Harald Welte44c0f632017-01-15 17:58:29 +0100901#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800902
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200903/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200904 * \returns dynamically-allocated log target
905 * This funcition allocates a \ref log_target and initializes it
906 * with some default values. The newly created target is not
907 * registered yet.
908 */
Harald Welte3ae27582010-03-26 21:24:24 +0800909struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800910{
Harald Welte3ae27582010-03-26 21:24:24 +0800911 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800912 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800913
Max68bf16a2018-01-10 17:00:43 +0100914 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100915
Harald Welte3ae27582010-03-26 21:24:24 +0800916 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800917 if (!target)
918 return NULL;
919
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200920 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200921 struct log_category,
922 osmo_log_info->num_cat);
923 if (!target->categories) {
924 talloc_free(target);
925 return NULL;
926 }
927
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800928 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800929
930 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200931 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800932 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200933 cat->enabled = osmo_log_info->cat[i].enabled;
934 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800935 }
936
937 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800938 target->use_color = 1;
939 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100940 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100941 target->print_filename2 = LOG_FILENAME_PATH;
942 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800943
944 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800945 target->loglevel = 0;
946 return target;
947}
948
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200949/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200950 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800951struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800952{
Harald Weltea3b844c2010-03-27 00:04:40 +0800953/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200954#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800955 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800956
Harald Welte3ae27582010-03-26 21:24:24 +0800957 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800958 if (!target)
959 return NULL;
960
Harald Welte28222962011-02-18 20:37:04 +0100961 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200962 target->tgt_file.out = stderr;
963 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800964 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800965#else
966 return NULL;
967#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800968}
969
Harald Welte44c0f632017-01-15 17:58:29 +0100970#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200971/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200972 * \param[in] fname File name of the new log file
973 * \returns Log target in case of success, NULL otherwise
974 */
Harald Welte3086c392010-08-25 19:10:50 +0200975struct log_target *log_target_create_file(const char *fname)
976{
977 struct log_target *target;
978
979 target = log_target_create();
980 if (!target)
981 return NULL;
982
Harald Welte28222962011-02-18 20:37:04 +0100983 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200984 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700985 if (!target->tgt_file.out) {
986 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +0200987 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700988 }
Harald Welte3086c392010-08-25 19:10:50 +0200989
990 target->output = _file_output;
991
992 target->tgt_file.fname = talloc_strdup(target, fname);
993
994 return target;
995}
Harald Welte44c0f632017-01-15 17:58:29 +0100996#endif
Harald Welte3086c392010-08-25 19:10:50 +0200997
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200998/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200999 * \param[in] type Log target type
1000 * \param[in] fname File name
1001 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001002 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001003 */
Harald Welte28222962011-02-18 20:37:04 +01001004struct log_target *log_target_find(int type, const char *fname)
1005{
1006 struct log_target *tgt;
1007
1008 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1009 if (tgt->type != type)
1010 continue;
Maxc90f40a2018-01-11 10:52:28 +01001011 switch (tgt->type) {
1012 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001013 if (!strcmp(fname, tgt->tgt_file.fname))
1014 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001015 break;
1016 case LOG_TGT_TYPE_GSMTAP:
1017 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1018 return tgt;
1019 break;
1020 default:
Harald Welte28222962011-02-18 20:37:04 +01001021 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001022 }
Harald Welte28222962011-02-18 20:37:04 +01001023 }
1024 return NULL;
1025}
1026
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001027/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001028 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001029void log_target_destroy(struct log_target *target)
1030{
1031
1032 /* just in case, to make sure we don't have any references */
1033 log_del_target(target);
1034
Harald Welte44c0f632017-01-15 17:58:29 +01001035#if (!EMBEDDED)
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001036 switch (target->type) {
1037 case LOG_TGT_TYPE_FILE:
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001038 if (target->tgt_file.out == NULL)
1039 break;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001040 fclose(target->tgt_file.out);
1041 target->tgt_file.out = NULL;
1042 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001043#ifdef HAVE_SYSLOG_H
1044 case LOG_TGT_TYPE_SYSLOG:
1045 closelog();
1046 break;
1047#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001048 default:
1049 /* make GCC happy */
1050 break;
Harald Welte3086c392010-08-25 19:10:50 +02001051 }
Harald Welte44c0f632017-01-15 17:58:29 +01001052#endif
Harald Welte3086c392010-08-25 19:10:50 +02001053
1054 talloc_free(target);
1055}
1056
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001057/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001058 * \param[in] target log target to re-open
1059 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001060int log_target_file_reopen(struct log_target *target)
1061{
1062 fclose(target->tgt_file.out);
1063
1064 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1065 if (!target->tgt_file.out)
1066 return -errno;
1067
1068 /* we assume target->output already to be set */
1069
1070 return 0;
1071}
1072
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001073/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001074 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001075int log_targets_reopen(void)
1076{
1077 struct log_target *tar;
1078 int rc = 0;
1079
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001080 log_tgt_mutex_lock();
1081
Harald Welte4de854d2013-03-18 19:01:40 +01001082 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1083 switch (tar->type) {
1084 case LOG_TGT_TYPE_FILE:
1085 if (log_target_file_reopen(tar) < 0)
1086 rc = -1;
1087 break;
1088 default:
1089 break;
1090 }
1091 }
1092
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001093 log_tgt_mutex_unlock();
1094
Harald Welte4de854d2013-03-18 19:01:40 +01001095 return rc;
1096}
1097
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001098/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001099 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001100 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001101 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001102 *
1103 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001104 */
Harald Welteb43bc042011-06-27 10:29:17 +02001105int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001106{
Harald Welteb43bc042011-06-27 10:29:17 +02001107 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001108 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001109
Philipp Maierdc02c062020-05-12 17:51:25 +02001110 /* Ensure that log_init is not called multiple times */
1111 OSMO_ASSERT(tall_log_ctx == NULL)
1112
Harald Welteb43bc042011-06-27 10:29:17 +02001113 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1114 if (!tall_log_ctx)
1115 return -ENOMEM;
1116
1117 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1118 if (!osmo_log_info)
1119 return -ENOMEM;
1120
Max72dfd432018-12-04 11:24:18 +01001121 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1122
1123 if (inf) {
1124 osmo_log_info->filter_fn = inf->filter_fn;
1125 osmo_log_info->num_cat_user = inf->num_cat;
1126 osmo_log_info->num_cat += inf->num_cat;
1127 }
Harald Welteb43bc042011-06-27 10:29:17 +02001128
Philipp Maierdcad1c52020-03-25 11:25:59 +01001129 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1130 osmo_log_info->num_cat);
1131 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001132 talloc_free(osmo_log_info);
1133 osmo_log_info = NULL;
1134 return -ENOMEM;
1135 }
1136
Philipp Maierdcad1c52020-03-25 11:25:59 +01001137 /* copy over the user part and sanitize loglevel */
1138 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001139 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001140 memcpy(&cat_ptr[i], &inf->cat[i],
1141 sizeof(struct log_info_cat));
1142
1143 /* Make sure that the loglevel is set to NOTICE in case
1144 * no loglevel has been preset. */
1145 if (!cat_ptr[i].loglevel) {
1146 cat_ptr[i].loglevel = LOGL_NOTICE;
1147 }
Max72dfd432018-12-04 11:24:18 +01001148 }
Harald Welteb43bc042011-06-27 10:29:17 +02001149 }
1150
1151 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001152 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001153 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001154 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001155 }
1156
Philipp Maierdcad1c52020-03-25 11:25:59 +01001157 osmo_log_info->cat = cat_ptr;
1158
Harald Welte9fe16522011-06-27 14:00:03 +02001159 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001160}
Harald Welte18fc4652011-08-17 14:14:17 +02001161
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001162/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001163 * This function destroys all targets and releases associated memory */
1164void log_fini(void)
1165{
1166 struct log_target *tar, *tar2;
1167
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001168 log_tgt_mutex_lock();
1169
Harald Welte69e6c3c2016-04-20 10:41:27 +02001170 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1171 log_target_destroy(tar);
1172
1173 talloc_free(osmo_log_info);
1174 osmo_log_info = NULL;
1175 talloc_free(tall_log_ctx);
1176 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001177
1178 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001179}
1180
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001181/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001182 * \returns != 0 if a log entry might get generated by at least one target */
1183int log_check_level(int subsys, unsigned int level)
1184{
1185 struct log_target *tar;
1186
Max68bf16a2018-01-10 17:00:43 +01001187 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001188
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001189 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001190
1191 /* TODO: The following could/should be cached (update on config) */
1192
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001193 log_tgt_mutex_lock();
1194
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001195 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001196 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001197 continue;
1198
1199 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001200 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001201 return 1;
1202 }
1203
1204 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001205 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001206 return 0;
1207}
1208
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001209/*! @} */