blob: a40008e938a9b1e3a1f2e6b17ebde49b626de948 [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>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080067
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010068#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
69
Harald Weltea8b1b212020-09-27 17:21:07 +020070/* maximum length of the log string of a single log event (typically line) */
71#define MAX_LOG_SIZE 4096
72
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010073osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010074 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010075osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010076 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010077osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010078 enum_logging_filters_fit_in_log_target_filter_map);
79
Harald Welteb43bc042011-06-27 10:29:17 +020080struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080081
Harald Welte3ae27582010-03-26 21:24:24 +080082static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020083void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010084LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080085
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020086#if (!EMBEDDED)
87/*! This mutex must be held while using osmo_log_target_list or any of its
88 log_targets in a multithread program. Prevents race conditions between threads
89 like producing unordered timestamps or VTY deleting a target while another
90 thread is writing to it */
91static pthread_mutex_t osmo_log_tgt_mutex;
92static bool osmo_log_tgt_mutex_on = false;
93
94/*! Enable multithread support (mutex) in libosmocore logging system.
95 * Must be called by processes willing to use logging subsystem from several
96 * threads. Once enabled, it's not possible to disable it again.
97 */
98void log_enable_multithread(void) {
99 if (osmo_log_tgt_mutex_on)
100 return;
101 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
102 osmo_log_tgt_mutex_on = true;
103}
104
105/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
106 * macro log_tgt_mutex_lock() instead.
107 */
108void log_tgt_mutex_lock_impl(void) {
109 /* These lines are useful to debug scenarios where there's only 1 thread
110 and a double lock appears, for instance during startup and some
111 unlock() missing somewhere:
112 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
113 osmo_panic("acquiring already locked mutex!\n");
114 return;
115 */
116
117 if (osmo_log_tgt_mutex_on)
118 pthread_mutex_lock(&osmo_log_tgt_mutex);
119}
120
121/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
122 * macro log_tgt_mutex_unlock() instead.
123 */
124void log_tgt_mutex_unlock_impl(void) {
125 if (osmo_log_tgt_mutex_on)
126 pthread_mutex_unlock(&osmo_log_tgt_mutex);
127}
128
129#else /* if (!EMBEDDED) */
130#pragma message ("logging multithread support disabled in embedded build")
131void log_enable_multithread(void) {}
132void log_tgt_mutex_lock_impl(void) {}
133void log_tgt_mutex_unlock_impl(void) {}
134#endif /* if (!EMBEDDED) */
135
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200136const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800137 { LOGL_DEBUG, "DEBUG" },
138 { LOGL_INFO, "INFO" },
139 { LOGL_NOTICE, "NOTICE" },
140 { LOGL_ERROR, "ERROR" },
141 { LOGL_FATAL, "FATAL" },
142 { 0, NULL },
143};
144
Harald Welte4c0e1a12020-12-07 22:03:09 +0100145/* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
Harald Welteb43bc042011-06-27 10:29:17 +0200146#define INT2IDX(x) (-1*(x)-1)
147static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
148 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
149 .name = "DLGLOBAL",
150 .description = "Library-internal global log family",
151 .loglevel = LOGL_NOTICE,
152 .enabled = 1,
153 },
root8a996b42011-09-26 11:22:21 +0200154 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
155 .name = "DLLAPD",
156 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200157 .loglevel = LOGL_NOTICE,
158 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100159 .color = "\033[38;5;21m",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200160 },
Harald Welte892e6212011-07-19 14:31:44 +0200161 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200162 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200163 .description = "A-bis Intput Subsystem",
164 .loglevel = LOGL_NOTICE,
165 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100166 .color = "\033[38;5;23m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200167 },
Harald Welte892e6212011-07-19 14:31:44 +0200168 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200169 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200170 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
171 .loglevel = LOGL_NOTICE,
172 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100173 .color = "\033[38;5;25m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200174 },
Harald Welte892e6212011-07-19 14:31:44 +0200175 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200176 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200177 .description = "A-bis Input Driver for Signalling",
178 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100179 .color = "\033[38;5;27m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200180 },
Harald Welte892e6212011-07-19 14:31:44 +0200181 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200182 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200183 .description = "A-bis Input Driver for B-Channels (voice)",
184 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100185 .color = "\033[38;5;29m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200186 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200187 [INT2IDX(DLSMS)] = {
188 .name = "DLSMS",
189 .description = "Layer3 Short Message Service (SMS)",
190 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100191 .color = "\033[38;5;31m",
Andreas Eversbergc626da92011-10-28 03:53:50 +0200192 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200193 [INT2IDX(DLCTRL)] = {
194 .name = "DLCTRL",
195 .description = "Control Interface",
196 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100197 .color = "\033[38;5;33m",
Harald Welte7fd0c832014-08-20 19:58:13 +0200198 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100199 [INT2IDX(DLGTP)] = {
200 .name = "DLGTP",
201 .description = "GPRS GTP library",
202 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100203 .color = "\033[38;5;35m",
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100204 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100205 [INT2IDX(DLSTATS)] = {
206 .name = "DLSTATS",
207 .description = "Statistics messages and logging",
208 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100209 .color = "\033[38;5;37m",
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100210 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100211 [INT2IDX(DLGSUP)] = {
212 .name = "DLGSUP",
213 .description = "Generic Subscriber Update Protocol",
214 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100215 .color = "\033[38;5;39m",
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100216 },
Harald Weltec0f00072016-04-27 18:32:35 +0200217 [INT2IDX(DLOAP)] = {
218 .name = "DLOAP",
219 .description = "Osmocom Authentication Protocol",
220 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100221 .color = "\033[38;5;41m",
Harald Weltec0f00072016-04-27 18:32:35 +0200222 },
Harald Welte059c4042017-04-03 22:20:49 +0200223 [INT2IDX(DLSS7)] = {
224 .name = "DLSS7",
225 .description = "libosmo-sigtran Signalling System 7",
226 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100227 .color = "\033[38;5;43m",
Harald Welte059c4042017-04-03 22:20:49 +0200228 },
229 [INT2IDX(DLSCCP)] = {
230 .name = "DLSCCP",
231 .description = "libosmo-sigtran SCCP Implementation",
232 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100233 .color = "\033[38;5;45m",
Harald Welte059c4042017-04-03 22:20:49 +0200234 },
235 [INT2IDX(DLSUA)] = {
236 .name = "DLSUA",
237 .description = "libosmo-sigtran SCCP User Adaptation",
238 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100239 .color = "\033[38;5;47m",
Harald Welte059c4042017-04-03 22:20:49 +0200240 },
241 [INT2IDX(DLM3UA)] = {
242 .name = "DLM3UA",
243 .description = "libosmo-sigtran MTP3 User Adaptation",
244 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100245 .color = "\033[38;5;49m",
Harald Welte059c4042017-04-03 22:20:49 +0200246 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200247 [INT2IDX(DLMGCP)] = {
248 .name = "DLMGCP",
249 .description = "libosmo-mgcp Media Gateway Control Protocol",
250 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100251 .color = "\033[38;5;51m",
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200252 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100253 [INT2IDX(DLJIBUF)] = {
254 .name = "DLJIBUF",
255 .description = "libosmo-netif Jitter Buffer",
256 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100257 .color = "\033[38;5;53m",
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100258 },
Max450f5ac2019-02-14 19:12:03 +0100259 [INT2IDX(DLRSPRO)] = {
260 .name = "DLRSPRO",
261 .description = "Remote SIM protocol",
262 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100263 .color = "\033[38;5;55m",
Max450f5ac2019-02-14 19:12:03 +0100264 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200265 [INT2IDX(DLNS)] = {
266 .name = "DLNS",
267 .description = "GPRS NS layer",
268 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100269 .color = "\033[38;5;57m",
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100271 [INT2IDX(DLBSSGP)] = {
272 .name = "DLBSSGP",
273 .description = "GPRS BSSGP layer",
274 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100275 .color = "\033[38;5;59m",
Harald Weltefde19ed2020-12-07 21:43:51 +0100276 },
Harald Welteb43bc042011-06-27 10:29:17 +0200277};
278
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200279void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100280{
281 if (!osmo_log_info) {
282 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100283 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100284 OSMO_ASSERT(osmo_log_info);
285 }
286}
287
Harald Welteb43bc042011-06-27 10:29:17 +0200288/* special magic for negative (library-internal) log subsystem numbers */
289static int subsys_lib2index(int subsys)
290{
291 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
292}
293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700295 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200296 * \returns numeric log level
297 */
Harald Welte3ae27582010-03-26 21:24:24 +0800298int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800299{
300 return get_string_value(loglevel_strs, lvl);
301}
302
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200303/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700304 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200305 * \returns zero-terminated string (log level name)
306 */
Harald Welte9ac22252010-05-11 11:19:40 +0200307const char *log_level_str(unsigned int lvl)
308{
309 return get_value_string(loglevel_strs, lvl);
310}
311
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200312/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200313 * \param[in] category human-readable log category name
314 * \returns numeric category value, or -EINVAL otherwise
315 */
Harald Welte3ae27582010-03-26 21:24:24 +0800316int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800317{
318 int i;
319
Max68bf16a2018-01-10 17:00:43 +0100320 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100321
Harald Welte4ebdf742010-05-19 19:54:00 +0200322 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200323 if (osmo_log_info->cat[i].name == NULL)
324 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200325 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800326 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800327 }
328
329 return -EINVAL;
330}
331
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200332/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200333 * \param[in] target log target to be configured
334 * \param[in] _mask log category mask string
335 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800336 * The format can be this: category1:category2:category3
337 * or category1,2:category2,3:...
338 */
Harald Welte3ae27582010-03-26 21:24:24 +0800339void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800340{
341 int i = 0;
342 char *mask = strdup(_mask);
343 char *category_token = NULL;
344
Max68bf16a2018-01-10 17:00:43 +0100345 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100346
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800347 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200348 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800349 target->categories[i].enabled = 0;
350
351 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200352 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800353 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200354 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200355 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800356 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200357
358 if (!osmo_log_info->cat[i].name)
359 continue;
360
361 length = strlen(category_token);
362 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200363
364 /* Use longest length not to match subocurrences. */
365 if (cat_length > length)
366 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800367
368 if (colon)
369 length = colon - category_token;
370
Harald Welte4ebdf742010-05-19 19:54:00 +0200371 if (strncasecmp(osmo_log_info->cat[i].name,
372 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800373 int level = 0;
374
375 if (colon)
376 level = atoi(colon+1);
377
Harald Weltefaadfe22010-03-26 21:05:43 +0800378 target->categories[i].enabled = 1;
379 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800380 }
381 }
382 } while ((category_token = strtok(NULL, ":")));
383
384 free(mask);
385}
386
387static const char* color(int subsys)
388{
Harald Welte4ebdf742010-05-19 19:54:00 +0200389 if (subsys < osmo_log_info->num_cat)
390 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800391
Harald Welted788f662010-03-26 09:45:03 +0800392 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800393}
394
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100395static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100396 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
397 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
398 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
399 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
400 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100401 { 0, NULL }
402};
403
404static const char *level_color(int level)
405{
406 const char *c = get_value_string_or_null(level_colors, level);
407 if (!c)
408 return get_value_string(level_colors, LOGL_FATAL);
409 return c;
410}
411
Harald Welteaa00f992016-12-02 15:30:02 +0100412const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100413{
414 if (subsys < osmo_log_info->num_cat)
415 return osmo_log_info->cat[subsys].name;
416
417 return NULL;
418}
419
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100420static const char *const_basename(const char *path)
421{
422 const char *bn = strrchr(path, '/');
423 if (!bn || !bn[1])
424 return path;
425 return bn + 1;
426}
427
Harald Welte3ae27582010-03-26 21:24:24 +0800428static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200429 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100430 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800431{
Harald Weltea8b1b212020-09-27 17:21:07 +0200432 char buf[MAX_LOG_SIZE];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200433 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100434 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800435
436 /* are we using color */
437 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100438 c_subsys = color(subsys);
439 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100440 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200441 if (ret < 0)
442 goto err;
443 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800444 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800445 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800446 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100447 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200448#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100449 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100450 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200451 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100452 localtime_r(&tv.tv_sec, &tm);
453 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100454 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100455 tm.tm_hour, tm.tm_min, tm.tm_sec,
456 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100457 if (ret < 0)
458 goto err;
459 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200460#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100461 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800462 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200463 if ((tm = time(NULL)) == (time_t) -1)
464 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200465 /* Get human-readable representation of time.
466 man ctime: we need at least 26 bytes in buf */
467 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200468 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200469 ret = strlen(buf + offset);
470 if (ret <= 0)
471 goto err;
472 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
473 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200474 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800475 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100476 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100477 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
478 target->use_color ? level_color(level) : "",
479 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100480 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100481 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100482 if (ret < 0)
483 goto err;
484 OSMO_SNPRINTF_RET(ret, rem, offset, len);
485 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100486 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100487 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
488 target->use_color ? level_color(level) : "",
489 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100490 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100491 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100492 if (ret < 0)
493 goto err;
494 OSMO_SNPRINTF_RET(ret, rem, offset, len);
495 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100496 if (target->print_category_hex) {
497 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200498 if (ret < 0)
499 goto err;
500 OSMO_SNPRINTF_RET(ret, rem, offset, len);
501 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200502
503 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
504 switch (target->print_filename2) {
505 case LOG_FILENAME_NONE:
506 break;
507 case LOG_FILENAME_PATH:
508 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
509 if (ret < 0)
510 goto err;
511 OSMO_SNPRINTF_RET(ret, rem, offset, len);
512 break;
513 case LOG_FILENAME_BASENAME:
514 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
515 if (ret < 0)
516 goto err;
517 OSMO_SNPRINTF_RET(ret, rem, offset, len);
518 break;
519 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100520 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800521 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200522 ret = vsnprintf(buf + offset, rem, format, ap);
523 if (ret < 0)
524 goto err;
525 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800526
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200527 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
528 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
529 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
530 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
531 && offset > 0 && buf[offset-1] == '\n') {
532 switch (target->print_filename2) {
533 case LOG_FILENAME_NONE:
534 break;
535 case LOG_FILENAME_PATH:
536 offset --;
537 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
538 if (ret < 0)
539 goto err;
540 OSMO_SNPRINTF_RET(ret, rem, offset, len);
541 break;
542 case LOG_FILENAME_BASENAME:
543 offset --;
544 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
545 if (ret < 0)
546 goto err;
547 OSMO_SNPRINTF_RET(ret, rem, offset, len);
548 break;
549 }
550 }
551
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200552 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100553 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100554 if (ret < 0)
555 goto err;
556 OSMO_SNPRINTF_RET(ret, rem, offset, len);
557 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200558err:
559 buf[sizeof(buf)-1] = '\0';
560 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800561}
562
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100563/* Catch internal logging category indexes as well as out-of-bounds indexes.
564 * For internal categories, the ID is negative starting with -1; and internal
565 * logging categories are added behind the user categories. For out-of-bounds
566 * indexes, return the index of DLGLOBAL. The returned category index is
567 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
568 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100569static inline int map_subsys(int subsys)
570{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100571 /* Note: comparing signed and unsigned integers */
572
573 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
574 subsys = DLGLOBAL;
575
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100576 if (subsys < 0)
577 subsys = subsys_lib2index(subsys);
578
Neels Hofmeyrca135742016-12-12 14:18:54 +0100579 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100580 subsys = subsys_lib2index(DLGLOBAL);
581
Neels Hofmeyrca135742016-12-12 14:18:54 +0100582 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
583
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100584 return subsys;
585}
586
Maxc65c5b42017-03-15 13:20:23 +0100587static inline bool should_log_to_target(struct log_target *tar, int subsys,
588 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100589{
590 struct log_category *category;
591
592 category = &tar->categories[subsys];
593
594 /* subsystem is not supposed to be logged */
595 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100596 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100597
598 /* Check the global log level */
599 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100600 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100601
602 /* Check the category log level */
603 if (tar->loglevel == 0 && category->loglevel != 0 &&
604 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100605 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100606
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100607 /* Apply filters here... if that becomes messy we will
608 * need to put filters in a list and each filter will
609 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100610 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100611 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100612
613 if (osmo_log_info->filter_fn)
614 return osmo_log_info->filter_fn(&log_context, tar);
615
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100616 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100617 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100618}
619
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200620/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200621 * \param[in] subsys Logging sub-system
622 * \param[in] level Log level
623 * \param[in] file name of source code file
624 * \param[in] cont continuation (1) or new line (0)
625 * \param[in] format format string
626 * \param[in] ap vararg-list containing format string arguments
627 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200628void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200629 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800630{
Harald Welte3ae27582010-03-26 21:24:24 +0800631 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800632
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100633 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200634
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200635 log_tgt_mutex_lock();
636
Harald Welte28222962011-02-18 20:37:04 +0100637 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200638 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800639
Maxc65c5b42017-03-15 13:20:23 +0100640 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800641 continue;
642
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200643 /* According to the manpage, vsnprintf leaves the value of ap
644 * in undefined state. Since _output uses vsnprintf and it may
645 * be called several times, we have to pass a copy of ap. */
646 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100647 if (tar->raw_output)
648 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
649 else
650 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200651 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800652 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200653
654 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800655}
656
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200657/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200658 * \param[in] subsys Logging sub-system
659 * \param[in] file name of source code file
660 * \param[in] cont continuation (1) or new line (0)
661 * \param[in] format format string
662 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200663void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800664 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800665{
666 va_list ap;
667
668 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200669 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800670 va_end(ap);
671}
672
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200673/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200674 * \param[in] subsys Logging sub-system
675 * \param[in] level Log level
676 * \param[in] file name of source code file
677 * \param[in] cont continuation (1) or new line (0)
678 * \param[in] format format string
679 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200680void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800681{
682 va_list ap;
683
Harald Welte433005c2020-09-26 11:51:32 +0200684 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800685 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200686 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800687 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200688 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800689}
690
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200691/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200692 * \param[in] target Log target to be registered
693 */
Harald Welte3ae27582010-03-26 21:24:24 +0800694void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800695{
Harald Welte28222962011-02-18 20:37:04 +0100696 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800697}
698
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200699/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200700 * \param[in] target Log target to be unregistered
701 */
Harald Welte3ae27582010-03-26 21:24:24 +0800702void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800703{
704 llist_del(&target->entry);
705}
706
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200707/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800708void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800709{
Harald Welte3ae27582010-03-26 21:24:24 +0800710 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800711}
712
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200713/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200714 * \param[in] ctx_nr logging context number
715 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200716 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200717 *
718 * A logging context is something like the subscriber identity to which
719 * the currently processed message relates, or the BTS through which it
720 * was received. As soon as this data is known, it can be set using
721 * this function. The main use of context information is for logging
722 * filters.
723 */
Harald Welte3ae27582010-03-26 21:24:24 +0800724int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800725{
Harald Welte3ae27582010-03-26 21:24:24 +0800726 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800727 return -EINVAL;
728
Harald Welte3ae27582010-03-26 21:24:24 +0800729 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800730
731 return 0;
732}
733
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200734/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200735 * \param[in] target Log target to be affected
736 * \param[in] all enable (1) or disable (0) the ALL filter
737 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100738 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100739 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
740 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200741 */
Harald Welte3ae27582010-03-26 21:24:24 +0800742void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800743{
744 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100745 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800746 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100747 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800748}
749
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200750/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200751 * \param[in] target Log target to be affected
752 * \param[in] use_color Use color (1) or don't use color (0)
753 */
Harald Welte3ae27582010-03-26 21:24:24 +0800754void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755{
756 target->use_color = use_color;
757}
758
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200759/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200760 * \param[in] target Log target to be affected
761 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
762 */
Harald Welte3ae27582010-03-26 21:24:24 +0800763void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800764{
765 target->print_timestamp = print_timestamp;
766}
767
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200768/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100769 * \param[in] target Log target to be affected
770 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
771 *
772 * When both timestamp and extended timestamp is enabled then only
773 * the extended timestamp will be used. The format of the timestamp
774 * is YYYYMMDDhhmmssnnn.
775 */
776void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
777{
778 target->print_ext_timestamp = print_timestamp;
779}
780
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100781/*! Use log_set_print_filename2() instead.
782 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
783 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
784 * behavior, which combined the category in hex with the filename. For example, if the category-hex
785 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
786 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200787 * \param[in] target Log target to be affected
788 * \param[in] print_filename Enable (1) or disable (0) filenames
789 */
790void log_set_print_filename(struct log_target *target, int print_filename)
791{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100792 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
793 log_set_print_category_hex(target, print_filename);
794}
795
796/*! Enable or disable printing of the filename while logging.
797 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700798 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100799 * LOG_FILENAME_NONE omits the source file and line information from logs.
800 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
801 */
802void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
803{
804 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200805}
806
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200807/*! Set the position where on a log line the source file info should be logged.
808 * \param[in] target Log target to be affected.
809 * \param[in] pos A LOG_FILENAME_POS_* enum value.
810 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
811 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
812 */
813void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
814{
815 target->print_filename_pos = pos;
816}
817
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200818/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100819 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700820 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100821 *
822 * Print the category/subsys name in front of every log message.
823 */
824void log_set_print_category(struct log_target *target, int print_category)
825{
826 target->print_category = print_category;
827}
828
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100829/*! Enable or disable printing of the category number in hex ('<000b>').
830 * \param[in] target Log target to be affected.
831 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
832 */
833void log_set_print_category_hex(struct log_target *target, int print_category_hex)
834{
835 target->print_category_hex = print_category_hex;
836}
837
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100838/*! Enable or disable printing of the log level name.
839 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700840 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100841 *
842 * Print the log level name in front of every log message.
843 */
844void log_set_print_level(struct log_target *target, int print_level)
845{
846 target->print_level = (bool)print_level;
847}
848
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200849/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200850 * \param[in] target Log target to be affected
851 * \param[in] log_level New global log level
852 */
Harald Welte3ae27582010-03-26 21:24:24 +0800853void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800854{
855 target->loglevel = log_level;
856}
857
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200858/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100859 * \param[in] target Log target to be affected
860 * \param[in] category Log category to be affected
861 * \param[in] enable whether to enable or disable the filter
862 * \param[in] level Log level of the filter
863 */
Harald Welte3ae27582010-03-26 21:24:24 +0800864void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800865 int enable, int level)
866{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100867 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800868 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100869 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800870 target->categories[category].enabled = !!enable;
871 target->categories[category].loglevel = level;
872}
873
Harald Welte44c0f632017-01-15 17:58:29 +0100874#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100875static void _file_output(struct log_target *target, unsigned int level,
876 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800877{
Harald Welte0083cd32010-08-25 14:55:44 +0200878 fprintf(target->tgt_file.out, "%s", log);
879 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800880}
Harald Welte44c0f632017-01-15 17:58:29 +0100881#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800882
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200883/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200884 * \returns dynamically-allocated log target
885 * This funcition allocates a \ref log_target and initializes it
886 * with some default values. The newly created target is not
887 * registered yet.
888 */
Harald Welte3ae27582010-03-26 21:24:24 +0800889struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800890{
Harald Welte3ae27582010-03-26 21:24:24 +0800891 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800892 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800893
Max68bf16a2018-01-10 17:00:43 +0100894 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100895
Harald Welte3ae27582010-03-26 21:24:24 +0800896 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800897 if (!target)
898 return NULL;
899
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200900 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200901 struct log_category,
902 osmo_log_info->num_cat);
903 if (!target->categories) {
904 talloc_free(target);
905 return NULL;
906 }
907
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800908 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800909
910 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200911 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800912 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200913 cat->enabled = osmo_log_info->cat[i].enabled;
914 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800915 }
916
917 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800918 target->use_color = 1;
919 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100920 target->print_filename2 = LOG_FILENAME_PATH;
921 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800922
923 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800924 target->loglevel = 0;
925 return target;
926}
927
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200928/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200929 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800930struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800931{
Harald Weltea3b844c2010-03-27 00:04:40 +0800932/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200933#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800934 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800935
Harald Welte3ae27582010-03-26 21:24:24 +0800936 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800937 if (!target)
938 return NULL;
939
Harald Welte28222962011-02-18 20:37:04 +0100940 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200941 target->tgt_file.out = stderr;
942 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800943 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800944#else
945 return NULL;
946#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800947}
948
Harald Welte44c0f632017-01-15 17:58:29 +0100949#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200950/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200951 * \param[in] fname File name of the new log file
952 * \returns Log target in case of success, NULL otherwise
953 */
Harald Welte3086c392010-08-25 19:10:50 +0200954struct log_target *log_target_create_file(const char *fname)
955{
956 struct log_target *target;
957
958 target = log_target_create();
959 if (!target)
960 return NULL;
961
Harald Welte28222962011-02-18 20:37:04 +0100962 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200963 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700964 if (!target->tgt_file.out) {
965 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +0200966 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700967 }
Harald Welte3086c392010-08-25 19:10:50 +0200968
969 target->output = _file_output;
970
971 target->tgt_file.fname = talloc_strdup(target, fname);
972
973 return target;
974}
Harald Welte44c0f632017-01-15 17:58:29 +0100975#endif
Harald Welte3086c392010-08-25 19:10:50 +0200976
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200977/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200978 * \param[in] type Log target type
979 * \param[in] fname File name
980 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200981 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +0200982 */
Harald Welte28222962011-02-18 20:37:04 +0100983struct log_target *log_target_find(int type, const char *fname)
984{
985 struct log_target *tgt;
986
987 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
988 if (tgt->type != type)
989 continue;
Maxc90f40a2018-01-11 10:52:28 +0100990 switch (tgt->type) {
991 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100992 if (!strcmp(fname, tgt->tgt_file.fname))
993 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100994 break;
995 case LOG_TGT_TYPE_GSMTAP:
996 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
997 return tgt;
998 break;
999 default:
Harald Welte28222962011-02-18 20:37:04 +01001000 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001001 }
Harald Welte28222962011-02-18 20:37:04 +01001002 }
1003 return NULL;
1004}
1005
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001006/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001007 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001008void log_target_destroy(struct log_target *target)
1009{
1010
1011 /* just in case, to make sure we don't have any references */
1012 log_del_target(target);
1013
Harald Welte44c0f632017-01-15 17:58:29 +01001014#if (!EMBEDDED)
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001015 switch (target->type) {
1016 case LOG_TGT_TYPE_FILE:
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001017 if (target->tgt_file.out == NULL)
1018 break;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001019 fclose(target->tgt_file.out);
1020 target->tgt_file.out = NULL;
1021 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001022#ifdef HAVE_SYSLOG_H
1023 case LOG_TGT_TYPE_SYSLOG:
1024 closelog();
1025 break;
1026#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001027 default:
1028 /* make GCC happy */
1029 break;
Harald Welte3086c392010-08-25 19:10:50 +02001030 }
Harald Welte44c0f632017-01-15 17:58:29 +01001031#endif
Harald Welte3086c392010-08-25 19:10:50 +02001032
1033 talloc_free(target);
1034}
1035
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001036/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001037 * \param[in] target log target to re-open
1038 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001039int log_target_file_reopen(struct log_target *target)
1040{
1041 fclose(target->tgt_file.out);
1042
1043 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1044 if (!target->tgt_file.out)
1045 return -errno;
1046
1047 /* we assume target->output already to be set */
1048
1049 return 0;
1050}
1051
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001052/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001053 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001054int log_targets_reopen(void)
1055{
1056 struct log_target *tar;
1057 int rc = 0;
1058
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001059 log_tgt_mutex_lock();
1060
Harald Welte4de854d2013-03-18 19:01:40 +01001061 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1062 switch (tar->type) {
1063 case LOG_TGT_TYPE_FILE:
1064 if (log_target_file_reopen(tar) < 0)
1065 rc = -1;
1066 break;
1067 default:
1068 break;
1069 }
1070 }
1071
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001072 log_tgt_mutex_unlock();
1073
Harald Welte4de854d2013-03-18 19:01:40 +01001074 return rc;
1075}
1076
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001077/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001078 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001079 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001080 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001081 *
1082 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001083 */
Harald Welteb43bc042011-06-27 10:29:17 +02001084int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001085{
Harald Welteb43bc042011-06-27 10:29:17 +02001086 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001087 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001088
Philipp Maierdc02c062020-05-12 17:51:25 +02001089 /* Ensure that log_init is not called multiple times */
1090 OSMO_ASSERT(tall_log_ctx == NULL)
1091
Harald Welteb43bc042011-06-27 10:29:17 +02001092 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1093 if (!tall_log_ctx)
1094 return -ENOMEM;
1095
1096 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1097 if (!osmo_log_info)
1098 return -ENOMEM;
1099
Max72dfd432018-12-04 11:24:18 +01001100 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1101
1102 if (inf) {
1103 osmo_log_info->filter_fn = inf->filter_fn;
1104 osmo_log_info->num_cat_user = inf->num_cat;
1105 osmo_log_info->num_cat += inf->num_cat;
1106 }
Harald Welteb43bc042011-06-27 10:29:17 +02001107
Philipp Maierdcad1c52020-03-25 11:25:59 +01001108 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1109 osmo_log_info->num_cat);
1110 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001111 talloc_free(osmo_log_info);
1112 osmo_log_info = NULL;
1113 return -ENOMEM;
1114 }
1115
Philipp Maierdcad1c52020-03-25 11:25:59 +01001116 /* copy over the user part and sanitize loglevel */
1117 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001118 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001119 memcpy(&cat_ptr[i], &inf->cat[i],
1120 sizeof(struct log_info_cat));
1121
1122 /* Make sure that the loglevel is set to NOTICE in case
1123 * no loglevel has been preset. */
1124 if (!cat_ptr[i].loglevel) {
1125 cat_ptr[i].loglevel = LOGL_NOTICE;
1126 }
Max72dfd432018-12-04 11:24:18 +01001127 }
Harald Welteb43bc042011-06-27 10:29:17 +02001128 }
1129
1130 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001131 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001132 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001133 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001134 }
1135
Philipp Maierdcad1c52020-03-25 11:25:59 +01001136 osmo_log_info->cat = cat_ptr;
1137
Harald Welte9fe16522011-06-27 14:00:03 +02001138 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001139}
Harald Welte18fc4652011-08-17 14:14:17 +02001140
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001141/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001142 * This function destroys all targets and releases associated memory */
1143void log_fini(void)
1144{
1145 struct log_target *tar, *tar2;
1146
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001147 log_tgt_mutex_lock();
1148
Harald Welte69e6c3c2016-04-20 10:41:27 +02001149 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1150 log_target_destroy(tar);
1151
1152 talloc_free(osmo_log_info);
1153 osmo_log_info = NULL;
1154 talloc_free(tall_log_ctx);
1155 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001156
1157 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001158}
1159
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001160/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001161 * \returns != 0 if a log entry might get generated by at least one target */
1162int log_check_level(int subsys, unsigned int level)
1163{
1164 struct log_target *tar;
1165
Max68bf16a2018-01-10 17:00:43 +01001166 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001167
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001168 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001169
1170 /* TODO: The following could/should be cached (update on config) */
1171
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001172 log_tgt_mutex_lock();
1173
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001174 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001175 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001176 continue;
1177
1178 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001179 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001180 return 1;
1181 }
1182
1183 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001184 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001185 return 0;
1186}
1187
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001188/*! @} */