blob: 6941f9bf7cf15ab3e70e07214cff6425b5a614fa [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 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +080020 */
21
Harald Weltef01b2382017-10-16 13:55:34 +020022/*! \addtogroup logging
Harald Welte18fc4652011-08-17 14:14:17 +020023 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020024 * libosmocore Logging sub-system
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 *
26 * \file logging.c */
Harald Welte18fc4652011-08-17 14:14:17 +020027
Pau Espin Pedrol88955fb2023-01-18 18:54:00 +010028#include "config.h"
Harald Welte01fd5cb2010-03-26 23:51:31 +080029
Harald Welte4a2bb9e2010-03-26 09:33:40 +080030#include <stdarg.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <string.h>
Harald Welteb72867f2020-09-26 21:45:16 +020034#include <unistd.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080035
36#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080037#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080038#endif
Vadim Yanitskiy04f42712020-09-09 04:47:25 +070039
40#ifdef HAVE_SYSLOG_H
41#include <syslog.h>
42#endif
43
Harald Welte433005c2020-09-26 11:51:32 +020044#ifdef HAVE_SYSTEMTAP
45/* include the generated probes header and put markers in code */
46#include "probes.h"
47#define TRACE(probe) probe
48#define TRACE_ENABLED(probe) probe ## _ENABLED()
49#else
50/* Wrap the probe to allow it to be removed when no systemtap available */
51#define TRACE(probe)
52#define TRACE_ENABLED(probe) (0)
53#endif /* HAVE_SYSTEMTAP */
54
Harald Welte4a2bb9e2010-03-26 09:33:40 +080055#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010056#include <sys/time.h>
Harald Welteb72867f2020-09-26 21:45:16 +020057#include <sys/types.h>
58#include <sys/stat.h>
59#include <fcntl.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 Welteb72867f2020-09-26 21:45:16 +020068#include <osmocom/core/select.h>
69#include <osmocom/core/write_queue.h>
Vadim Yanitskiy2f4186a2021-12-29 21:58:19 +060070#include <osmocom/core/gsmtap_util.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080071
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010072#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
73
Harald Weltea8b1b212020-09-27 17:21:07 +020074/* maximum length of the log string of a single log event (typically line) */
75#define MAX_LOG_SIZE 4096
76
Harald Welteb72867f2020-09-26 21:45:16 +020077/* maximum number of log statements we queue in file/stderr target write queue */
Harald Weltea0b57d02020-09-27 17:15:10 +020078#define LOG_WQUEUE_LEN 156
Harald Welteb72867f2020-09-26 21:45:16 +020079
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010080osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010081 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010082osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010083 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010084osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010085 enum_logging_filters_fit_in_log_target_filter_map);
86
Harald Welteb43bc042011-06-27 10:29:17 +020087struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080088
Harald Welte3ae27582010-03-26 21:24:24 +080089static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020090void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010091LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080092
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010093static __thread long int logging_tid;
94
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020095#if (!EMBEDDED)
96/*! This mutex must be held while using osmo_log_target_list or any of its
97 log_targets in a multithread program. Prevents race conditions between threads
98 like producing unordered timestamps or VTY deleting a target while another
99 thread is writing to it */
100static pthread_mutex_t osmo_log_tgt_mutex;
101static bool osmo_log_tgt_mutex_on = false;
102
103/*! Enable multithread support (mutex) in libosmocore logging system.
104 * Must be called by processes willing to use logging subsystem from several
105 * threads. Once enabled, it's not possible to disable it again.
106 */
107void log_enable_multithread(void) {
108 if (osmo_log_tgt_mutex_on)
109 return;
110 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
111 osmo_log_tgt_mutex_on = true;
112}
113
114/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
115 * macro log_tgt_mutex_lock() instead.
116 */
117void log_tgt_mutex_lock_impl(void) {
118 /* These lines are useful to debug scenarios where there's only 1 thread
119 and a double lock appears, for instance during startup and some
120 unlock() missing somewhere:
121 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
122 osmo_panic("acquiring already locked mutex!\n");
123 return;
124 */
125
126 if (osmo_log_tgt_mutex_on)
127 pthread_mutex_lock(&osmo_log_tgt_mutex);
128}
129
130/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
131 * macro log_tgt_mutex_unlock() instead.
132 */
133void log_tgt_mutex_unlock_impl(void) {
134 if (osmo_log_tgt_mutex_on)
135 pthread_mutex_unlock(&osmo_log_tgt_mutex);
136}
137
138#else /* if (!EMBEDDED) */
139#pragma message ("logging multithread support disabled in embedded build")
140void log_enable_multithread(void) {}
141void log_tgt_mutex_lock_impl(void) {}
142void log_tgt_mutex_unlock_impl(void) {}
143#endif /* if (!EMBEDDED) */
144
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200145const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800146 { LOGL_DEBUG, "DEBUG" },
147 { LOGL_INFO, "INFO" },
148 { LOGL_NOTICE, "NOTICE" },
149 { LOGL_ERROR, "ERROR" },
150 { LOGL_FATAL, "FATAL" },
151 { 0, NULL },
152};
153
Harald Welte4c0e1a12020-12-07 22:03:09 +0100154/* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
Harald Welteb43bc042011-06-27 10:29:17 +0200155#define INT2IDX(x) (-1*(x)-1)
156static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
157 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
158 .name = "DLGLOBAL",
159 .description = "Library-internal global log family",
160 .loglevel = LOGL_NOTICE,
161 .enabled = 1,
162 },
root8a996b42011-09-26 11:22:21 +0200163 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
164 .name = "DLLAPD",
165 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200166 .loglevel = LOGL_NOTICE,
167 .enabled = 1,
Pau Espin Pedrol1573add2021-10-14 18:13:48 +0200168 .color = "\033[38;5;12m",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169 },
Harald Welte892e6212011-07-19 14:31:44 +0200170 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200171 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200172 .description = "A-bis Intput Subsystem",
173 .loglevel = LOGL_NOTICE,
174 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100175 .color = "\033[38;5;23m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200176 },
Harald Welte892e6212011-07-19 14:31:44 +0200177 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200178 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200179 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
180 .loglevel = LOGL_NOTICE,
181 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100182 .color = "\033[38;5;25m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200183 },
Harald Welte892e6212011-07-19 14:31:44 +0200184 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200185 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200186 .description = "A-bis Input Driver for Signalling",
187 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100188 .color = "\033[38;5;27m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200189 },
Harald Welte892e6212011-07-19 14:31:44 +0200190 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200191 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200192 .description = "A-bis Input Driver for B-Channels (voice)",
193 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100194 .color = "\033[38;5;29m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200195 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200196 [INT2IDX(DLSMS)] = {
197 .name = "DLSMS",
198 .description = "Layer3 Short Message Service (SMS)",
199 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100200 .color = "\033[38;5;31m",
Andreas Eversbergc626da92011-10-28 03:53:50 +0200201 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200202 [INT2IDX(DLCTRL)] = {
203 .name = "DLCTRL",
204 .description = "Control Interface",
205 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100206 .color = "\033[38;5;33m",
Harald Welte7fd0c832014-08-20 19:58:13 +0200207 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100208 [INT2IDX(DLGTP)] = {
209 .name = "DLGTP",
210 .description = "GPRS GTP library",
211 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100212 .color = "\033[38;5;35m",
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100213 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100214 [INT2IDX(DLSTATS)] = {
215 .name = "DLSTATS",
216 .description = "Statistics messages and logging",
217 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100218 .color = "\033[38;5;37m",
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100219 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100220 [INT2IDX(DLGSUP)] = {
221 .name = "DLGSUP",
222 .description = "Generic Subscriber Update Protocol",
223 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100224 .color = "\033[38;5;39m",
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100225 },
Harald Weltec0f00072016-04-27 18:32:35 +0200226 [INT2IDX(DLOAP)] = {
227 .name = "DLOAP",
228 .description = "Osmocom Authentication Protocol",
229 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100230 .color = "\033[38;5;41m",
Harald Weltec0f00072016-04-27 18:32:35 +0200231 },
Harald Welte059c4042017-04-03 22:20:49 +0200232 [INT2IDX(DLSS7)] = {
233 .name = "DLSS7",
234 .description = "libosmo-sigtran Signalling System 7",
235 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100236 .color = "\033[38;5;43m",
Harald Welte059c4042017-04-03 22:20:49 +0200237 },
238 [INT2IDX(DLSCCP)] = {
239 .name = "DLSCCP",
240 .description = "libosmo-sigtran SCCP Implementation",
241 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100242 .color = "\033[38;5;45m",
Harald Welte059c4042017-04-03 22:20:49 +0200243 },
244 [INT2IDX(DLSUA)] = {
245 .name = "DLSUA",
246 .description = "libosmo-sigtran SCCP User Adaptation",
247 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100248 .color = "\033[38;5;47m",
Harald Welte059c4042017-04-03 22:20:49 +0200249 },
250 [INT2IDX(DLM3UA)] = {
251 .name = "DLM3UA",
252 .description = "libosmo-sigtran MTP3 User Adaptation",
253 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100254 .color = "\033[38;5;49m",
Harald Welte059c4042017-04-03 22:20:49 +0200255 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200256 [INT2IDX(DLMGCP)] = {
257 .name = "DLMGCP",
258 .description = "libosmo-mgcp Media Gateway Control Protocol",
259 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100260 .color = "\033[38;5;51m",
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200261 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100262 [INT2IDX(DLJIBUF)] = {
263 .name = "DLJIBUF",
264 .description = "libosmo-netif Jitter Buffer",
265 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100266 .color = "\033[38;5;53m",
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100267 },
Max450f5ac2019-02-14 19:12:03 +0100268 [INT2IDX(DLRSPRO)] = {
269 .name = "DLRSPRO",
270 .description = "Remote SIM protocol",
271 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100272 .color = "\033[38;5;55m",
Max450f5ac2019-02-14 19:12:03 +0100273 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200274 [INT2IDX(DLNS)] = {
275 .name = "DLNS",
276 .description = "GPRS NS layer",
277 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100278 .color = "\033[38;5;57m",
Alexander Couzens6a161492020-07-12 13:45:50 +0200279 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100280 [INT2IDX(DLBSSGP)] = {
281 .name = "DLBSSGP",
282 .description = "GPRS BSSGP layer",
283 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100284 .color = "\033[38;5;59m",
Harald Weltefde19ed2020-12-07 21:43:51 +0100285 },
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100286 [INT2IDX(DLNSDATA)] = {
287 .name = "DLNSDATA",
288 .description = "GPRS NS layer data PDU",
289 .enabled = 1, .loglevel = LOGL_NOTICE,
290 .color = "\033[38;5;61m",
291 },
292 [INT2IDX(DLNSSIGNAL)] = {
293 .name = "DLNSSIGNAL",
294 .description = "GPRS NS layer signal PDU",
295 .enabled = 1, .loglevel = LOGL_NOTICE,
296 .color = "\033[38;5;63m",
297 },
Harald Welte9fe1f9f2018-11-29 13:47:39 +0100298 [INT2IDX(DLIUUP)] = {
299 .name = "DLIUUP",
300 .description = "Iu UP layer",
301 .enabled = 1, .loglevel = LOGL_NOTICE,
302 .color = "\033[38;5;65m",
303 },
Neels Hofmeyr13fa00e2021-11-26 11:02:47 +0100304 [INT2IDX(DLPFCP)] = {
305 .name = "DLPFCP",
306 .description = "libosmo-pfcp Packet Forwarding Control Protocol",
307 .enabled = 1, .loglevel = LOGL_NOTICE,
308 .color = "\033[38;5;51m",
309 },
Vadim Yanitskiy8137d9a2022-08-03 05:35:04 +0700310 [INT2IDX(DLCSN1)] = {
311 .name = "DLCSN1",
312 .description = "libosmo-csn1 Concrete Syntax Notation 1 codec",
313 .enabled = 1, .loglevel = LOGL_NOTICE,
314 .color = "\033[38;5;11m",
315 },
Harald Welte8857f3b2022-11-18 13:54:44 +0100316 [INT2IDX(DLIO)] = {
317 .name = "DLIO",
318 .description = "libosmocore IO Subsystem",
319 .enabled = 1, .loglevel = LOGL_NOTICE,
320 .color = "\033[38;5;67m",
321 },
Harald Welteb43bc042011-06-27 10:29:17 +0200322};
323
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200324void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100325{
326 if (!osmo_log_info) {
327 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100328 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100329 OSMO_ASSERT(osmo_log_info);
330 }
331}
332
Harald Welteb43bc042011-06-27 10:29:17 +0200333/* special magic for negative (library-internal) log subsystem numbers */
334static int subsys_lib2index(int subsys)
335{
336 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
337}
338
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200339/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700340 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200341 * \returns numeric log level
342 */
Harald Welte3ae27582010-03-26 21:24:24 +0800343int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800344{
345 return get_string_value(loglevel_strs, lvl);
346}
347
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200348/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700349 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200350 * \returns zero-terminated string (log level name)
351 */
Harald Welte9ac22252010-05-11 11:19:40 +0200352const char *log_level_str(unsigned int lvl)
353{
354 return get_value_string(loglevel_strs, lvl);
355}
356
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200357/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200358 * \param[in] category human-readable log category name
359 * \returns numeric category value, or -EINVAL otherwise
360 */
Harald Welte3ae27582010-03-26 21:24:24 +0800361int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800362{
363 int i;
364
Max68bf16a2018-01-10 17:00:43 +0100365 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100366
Harald Welte4ebdf742010-05-19 19:54:00 +0200367 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200368 if (osmo_log_info->cat[i].name == NULL)
369 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200370 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800371 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800372 }
373
374 return -EINVAL;
375}
376
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200377/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200378 * \param[in] target log target to be configured
379 * \param[in] _mask log category mask string
380 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800381 * The format can be this: category1:category2:category3
382 * or category1,2:category2,3:...
383 */
Harald Welte3ae27582010-03-26 21:24:24 +0800384void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800385{
386 int i = 0;
387 char *mask = strdup(_mask);
388 char *category_token = NULL;
389
Max68bf16a2018-01-10 17:00:43 +0100390 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100391
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800392 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200393 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800394 target->categories[i].enabled = 0;
395
396 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200397 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800398 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200399 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200400 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800401 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200402
403 if (!osmo_log_info->cat[i].name)
404 continue;
405
406 length = strlen(category_token);
407 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200408
409 /* Use longest length not to match subocurrences. */
410 if (cat_length > length)
411 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800412
413 if (colon)
414 length = colon - category_token;
415
Harald Welte4ebdf742010-05-19 19:54:00 +0200416 if (strncasecmp(osmo_log_info->cat[i].name,
417 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800418 int level = 0;
419
420 if (colon)
421 level = atoi(colon+1);
422
Harald Weltefaadfe22010-03-26 21:05:43 +0800423 target->categories[i].enabled = 1;
424 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800425 }
426 }
427 } while ((category_token = strtok(NULL, ":")));
428
429 free(mask);
430}
431
432static const char* color(int subsys)
433{
Harald Welte4ebdf742010-05-19 19:54:00 +0200434 if (subsys < osmo_log_info->num_cat)
435 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800436
Harald Welted788f662010-03-26 09:45:03 +0800437 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800438}
439
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100440static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100441 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
442 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
443 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
444 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
445 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100446 { 0, NULL }
447};
448
449static const char *level_color(int level)
450{
451 const char *c = get_value_string_or_null(level_colors, level);
452 if (!c)
453 return get_value_string(level_colors, LOGL_FATAL);
454 return c;
455}
456
Harald Welteaa00f992016-12-02 15:30:02 +0100457const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100458{
459 if (subsys < osmo_log_info->num_cat)
460 return osmo_log_info->cat[subsys].name;
461
462 return NULL;
463}
464
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100465static const char *const_basename(const char *path)
466{
467 const char *bn = strrchr(path, '/');
468 if (!bn || !bn[1])
469 return path;
470 return bn + 1;
471}
472
Harald Weltea0b57d02020-09-27 17:15:10 +0200473/*! main output formatting function for log lines.
474 * \param[out] buf caller-allocated output buffer for the generated string
475 * \param[in] buf_len number of bytes available in buf
476 * \param[in] target log target for which the string is to be formatted
477 * \param[in] subsys Log sub-system number
478 * \param[in] level Log level
479 * \param[in] file name of source code file generating the log
480 * \param[in] line line source code line number within 'file' generating the log
481 * \param[in] cont is this a continuation (true) or not (false)
482 * \param[in] format format string
483 * \param[in] ap variable argument list for format
484 * \returns number of bytes written to out */
485static int _output_buf(char *buf, int buf_len, struct log_target *target, unsigned int subsys,
486 unsigned int level, const char *file, int line, int cont,
487 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800488{
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100489 int ret;
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100490 const char *c_subsys = NULL;
Vadim Yanitskiy9c603e62023-12-10 17:09:23 +0700491 struct osmo_strbuf sb = { .buf = buf, .pos = buf, .len = buf_len };
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100492
493 /* safety net in case of encountering errors and returning nothing */
494 buf[0] = '\0';
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800495
496 /* are we using color */
497 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100498 c_subsys = color(subsys);
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100499 if (c_subsys)
500 OSMO_STRBUF_PRINTF(sb, "%s", c_subsys);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800501 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800502 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100503 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200504#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100505 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100506 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200507 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100508 localtime_r(&tv.tv_sec, &tm);
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100509 OSMO_STRBUF_PRINTF(sb, "%04d%02d%02d%02d%02d%02d%03d ",
510 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
511 tm.tm_hour, tm.tm_min, tm.tm_sec,
512 (int)(tv.tv_usec / 1000));
Harald Welte14c4c492018-06-28 08:28:52 +0200513#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100514 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800515 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200516 if ((tm = time(NULL)) == (time_t) -1)
517 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200518 /* Get human-readable representation of time.
519 man ctime: we need at least 26 bytes in buf */
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100520 if (OSMO_STRBUF_REMAIN(sb) < 26 || !ctime_r(&tm, sb.pos))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200521 goto err;
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100522 ret = strnlen(sb.pos, 26);
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200523 if (ret <= 0)
524 goto err;
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100525 OSMO_STRBUF_ADDED_TAIL(sb, ret);
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200526 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100527 OSMO_STRBUF_DROP_TAIL(sb, 1);
528 OSMO_STRBUF_PRINTF(sb, " ");
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800529 }
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100530 if (target->print_tid) {
531 if (logging_tid == 0)
532 logging_tid = (long int)osmo_gettid();
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100533 OSMO_STRBUF_PRINTF(sb, "%ld ", logging_tid);
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100534 }
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100535 if (target->print_category)
536 OSMO_STRBUF_PRINTF(sb, "%s%s%s%s ",
537 target->use_color ? level_color(level) : "",
538 log_category_name(subsys),
539 target->use_color ? OSMO_LOGCOLOR_END : "",
540 c_subsys ? c_subsys : "");
541 if (target->print_level)
542 OSMO_STRBUF_PRINTF(sb, "%s%s%s%s ",
543 target->use_color ? level_color(level) : "",
544 log_level_str(level),
545 target->use_color ? OSMO_LOGCOLOR_END : "",
546 c_subsys ? c_subsys : "");
547 if (target->print_category_hex)
548 OSMO_STRBUF_PRINTF(sb, "<%4.4x> ", subsys);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200549
550 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
551 switch (target->print_filename2) {
552 case LOG_FILENAME_NONE:
553 break;
554 case LOG_FILENAME_PATH:
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100555 OSMO_STRBUF_PRINTF(sb, "%s:%d ", file, line);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200556 break;
557 case LOG_FILENAME_BASENAME:
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100558 OSMO_STRBUF_PRINTF(sb, "%s:%d ", const_basename(file), line);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200559 break;
560 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100561 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800562 }
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100563 OSMO_STRBUF_APPEND(sb, vsnprintf, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800564
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200565 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
566 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
567 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
568 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100569 && sb.pos > sb.buf && *(sb.pos - 1) == '\n') {
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200570 switch (target->print_filename2) {
571 case LOG_FILENAME_NONE:
572 break;
573 case LOG_FILENAME_PATH:
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100574 OSMO_STRBUF_DROP_TAIL(sb, 1);
575 OSMO_STRBUF_PRINTF(sb, " (%s:%d)\n", file, line);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200576 break;
577 case LOG_FILENAME_BASENAME:
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100578 OSMO_STRBUF_DROP_TAIL(sb, 1);
579 OSMO_STRBUF_PRINTF(sb, " (%s:%d)\n", const_basename(file), line);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200580 break;
581 }
582 }
583
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200584 if (target->use_color && c_subsys) {
Manawyrm11a41682023-11-07 11:56:02 +0100585 /* Ensure the last color escape is sent before the newline
586 * (to not clobber journald, which works on single-lines only) */
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100587 if (sb.pos > sb.buf && *(sb.pos - 1) == '\n') {
588 OSMO_STRBUF_DROP_TAIL(sb, 1);
589 OSMO_STRBUF_PRINTF(sb, OSMO_LOGCOLOR_END "\n");
Manawyrm11a41682023-11-07 11:56:02 +0100590 } else {
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100591 OSMO_STRBUF_PRINTF(sb, OSMO_LOGCOLOR_END);
Manawyrm11a41682023-11-07 11:56:02 +0100592 }
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100593 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200594err:
Neels Hofmeyrd71331b2023-12-04 07:51:15 +0100595 return OSMO_STRBUF_CHAR_COUNT(sb);
Harald Weltea0b57d02020-09-27 17:15:10 +0200596}
597
598/* Format the log line for given target; use a stack buffer and call target->output */
599static void _output(struct log_target *target, unsigned int subsys,
600 unsigned int level, const char *file, int line, int cont,
601 const char *format, va_list ap)
602{
603 char buf[MAX_LOG_SIZE];
604 int rc;
605
606 rc = _output_buf(buf, sizeof(buf), target, subsys, level, file, line, cont, format, ap);
607 if (rc > 0)
608 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800609}
610
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100611/* Catch internal logging category indexes as well as out-of-bounds indexes.
612 * For internal categories, the ID is negative starting with -1; and internal
613 * logging categories are added behind the user categories. For out-of-bounds
614 * indexes, return the index of DLGLOBAL. The returned category index is
615 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
616 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100617static inline int map_subsys(int subsys)
618{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100619 /* Note: comparing signed and unsigned integers */
620
621 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
622 subsys = DLGLOBAL;
623
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100624 if (subsys < 0)
625 subsys = subsys_lib2index(subsys);
626
Neels Hofmeyrca135742016-12-12 14:18:54 +0100627 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100628 subsys = subsys_lib2index(DLGLOBAL);
629
Neels Hofmeyrca135742016-12-12 14:18:54 +0100630 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
631
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100632 return subsys;
633}
634
Maxc65c5b42017-03-15 13:20:23 +0100635static inline bool should_log_to_target(struct log_target *tar, int subsys,
636 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100637{
638 struct log_category *category;
639
640 category = &tar->categories[subsys];
641
642 /* subsystem is not supposed to be logged */
643 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100644 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100645
646 /* Check the global log level */
647 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100648 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100649
650 /* Check the category log level */
651 if (tar->loglevel == 0 && category->loglevel != 0 &&
652 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100653 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100654
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100655 /* Apply filters here... if that becomes messy we will
656 * need to put filters in a list and each filter will
657 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100658 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100659 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100660
661 if (osmo_log_info->filter_fn)
662 return osmo_log_info->filter_fn(&log_context, tar);
663
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100664 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100665 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100666}
667
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200668/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200669 * \param[in] subsys Logging sub-system
670 * \param[in] level Log level
671 * \param[in] file name of source code file
672 * \param[in] cont continuation (1) or new line (0)
673 * \param[in] format format string
674 * \param[in] ap vararg-list containing format string arguments
675 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200676void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200677 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800678{
Harald Welte3ae27582010-03-26 21:24:24 +0800679 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800680
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100681 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200682
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200683 log_tgt_mutex_lock();
684
Harald Welte28222962011-02-18 20:37:04 +0100685 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200686 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800687
Maxc65c5b42017-03-15 13:20:23 +0100688 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800689 continue;
690
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200691 /* According to the manpage, vsnprintf leaves the value of ap
692 * in undefined state. Since _output uses vsnprintf and it may
693 * be called several times, we have to pass a copy of ap. */
694 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100695 if (tar->raw_output)
696 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
697 else
698 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200699 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800700 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200701
702 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200706 * \param[in] subsys Logging sub-system
707 * \param[in] file name of source code file
708 * \param[in] cont continuation (1) or new line (0)
709 * \param[in] format format string
710 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200711void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800712 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800713{
714 va_list ap;
715
716 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200717 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800718 va_end(ap);
719}
720
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200721/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200722 * \param[in] subsys Logging sub-system
723 * \param[in] level Log level
724 * \param[in] file name of source code file
725 * \param[in] cont continuation (1) or new line (0)
726 * \param[in] format format string
727 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200728void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800729{
730 va_list ap;
731
Harald Welte433005c2020-09-26 11:51:32 +0200732 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800733 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200734 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800735 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200736 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800737}
738
Philipp Maier4c44d462022-01-19 11:43:05 +0100739/* This logging function is used as a fallback when the logging framework is
740 * not is not properly initialized. */
741void logp_stub(const char *file, int line, int cont, const char *format, ...)
742{
743 va_list ap;
744 if (!cont)
745 fprintf(stderr, "%s:%d ", file, line);
746 va_start(ap, format);
747 vfprintf(stderr, format, ap);
748 va_end(ap);
749}
750
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200751/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200752 * \param[in] target Log target to be registered
753 */
Harald Welte3ae27582010-03-26 21:24:24 +0800754void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755{
Harald Welte28222962011-02-18 20:37:04 +0100756 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800757}
758
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200759/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200760 * \param[in] target Log target to be unregistered
761 */
Harald Welte3ae27582010-03-26 21:24:24 +0800762void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800763{
764 llist_del(&target->entry);
765}
766
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200767/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800768void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800769{
Harald Welte3ae27582010-03-26 21:24:24 +0800770 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800771}
772
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200773/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200774 * \param[in] ctx_nr logging context number
775 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200776 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200777 *
778 * A logging context is something like the subscriber identity to which
779 * the currently processed message relates, or the BTS through which it
780 * was received. As soon as this data is known, it can be set using
781 * this function. The main use of context information is for logging
782 * filters.
783 */
Harald Welte3ae27582010-03-26 21:24:24 +0800784int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800785{
Harald Welte3ae27582010-03-26 21:24:24 +0800786 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800787 return -EINVAL;
788
Harald Welte3ae27582010-03-26 21:24:24 +0800789 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800790
791 return 0;
792}
793
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200794/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200795 * \param[in] target Log target to be affected
796 * \param[in] all enable (1) or disable (0) the ALL filter
797 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100798 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100799 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
800 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200801 */
Harald Welte3ae27582010-03-26 21:24:24 +0800802void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800803{
804 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100805 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800806 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100807 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800808}
809
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200810/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200811 * \param[in] target Log target to be affected
812 * \param[in] use_color Use color (1) or don't use color (0)
813 */
Harald Welte3ae27582010-03-26 21:24:24 +0800814void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800815{
816 target->use_color = use_color;
817}
818
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200819/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200820 * \param[in] target Log target to be affected
821 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
822 */
Harald Welte3ae27582010-03-26 21:24:24 +0800823void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800824{
825 target->print_timestamp = print_timestamp;
826}
827
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200828/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100829 * \param[in] target Log target to be affected
830 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
831 *
832 * When both timestamp and extended timestamp is enabled then only
833 * the extended timestamp will be used. The format of the timestamp
834 * is YYYYMMDDhhmmssnnn.
835 */
836void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
837{
838 target->print_ext_timestamp = print_timestamp;
839}
840
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100841/*! Enable or disable printing of timestamps while logging
842 * \param[in] target Log target to be affected
843 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
844 */
845void log_set_print_tid(struct log_target *target, int print_tid)
846{
847 target->print_tid = print_tid;
848}
849
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100850/*! Use log_set_print_filename2() instead.
851 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
852 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
853 * behavior, which combined the category in hex with the filename. For example, if the category-hex
854 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
855 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200856 * \param[in] target Log target to be affected
857 * \param[in] print_filename Enable (1) or disable (0) filenames
858 */
859void log_set_print_filename(struct log_target *target, int print_filename)
860{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100861 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
862 log_set_print_category_hex(target, print_filename);
863}
864
865/*! Enable or disable printing of the filename while logging.
866 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700867 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100868 * LOG_FILENAME_NONE omits the source file and line information from logs.
869 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
870 */
871void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
872{
873 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200874}
875
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200876/*! Set the position where on a log line the source file info should be logged.
877 * \param[in] target Log target to be affected.
878 * \param[in] pos A LOG_FILENAME_POS_* enum value.
879 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
880 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
881 */
882void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
883{
884 target->print_filename_pos = pos;
885}
886
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200887/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100888 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700889 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100890 *
891 * Print the category/subsys name in front of every log message.
892 */
893void log_set_print_category(struct log_target *target, int print_category)
894{
895 target->print_category = print_category;
896}
897
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100898/*! Enable or disable printing of the category number in hex ('<000b>').
899 * \param[in] target Log target to be affected.
900 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
901 */
902void log_set_print_category_hex(struct log_target *target, int print_category_hex)
903{
904 target->print_category_hex = print_category_hex;
905}
906
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100907/*! Enable or disable printing of the log level name.
908 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700909 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100910 *
911 * Print the log level name in front of every log message.
912 */
913void log_set_print_level(struct log_target *target, int print_level)
914{
915 target->print_level = (bool)print_level;
916}
917
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200918/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200919 * \param[in] target Log target to be affected
920 * \param[in] log_level New global log level
921 */
Harald Welte3ae27582010-03-26 21:24:24 +0800922void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800923{
924 target->loglevel = log_level;
925}
926
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200927/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100928 * \param[in] target Log target to be affected
929 * \param[in] category Log category to be affected
930 * \param[in] enable whether to enable or disable the filter
931 * \param[in] level Log level of the filter
932 */
Harald Welte3ae27582010-03-26 21:24:24 +0800933void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800934 int enable, int level)
935{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100936 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800937 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100938 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800939 target->categories[category].enabled = !!enable;
940 target->categories[category].loglevel = level;
941}
942
Harald Welte44c0f632017-01-15 17:58:29 +0100943#if (!EMBEDDED)
Harald Welte78d73672020-09-29 11:32:35 +0200944/* write-queue tells us we should write another msgb (log line) to the output fd */
945static int _file_wq_write_cb(struct osmo_fd *ofd, struct msgb *msg)
946{
947 int rc;
948
949 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
950 if (rc < 0)
951 return rc;
952 if (rc != msgb_length(msg)) {
953 /* pull the number of bytes we have already written */
954 msgb_pull(msg, rc);
955 /* ask write_queue to re-insert the msgb at the head of the queue */
956 return -EAGAIN;
957 }
958 return 0;
959}
960
961/* output via buffered, blocking stdio streams */
Harald Welteb72867f2020-09-26 21:45:16 +0200962static void _file_output_stream(struct log_target *target, unsigned int level,
963 const char *log)
964{
965 OSMO_ASSERT(target->tgt_file.out);
966 fputs(log, target->tgt_file.out);
967 fflush(target->tgt_file.out);
968}
969
970/* output via non-blocking write_queue, doing internal buffering */
Harald Weltea0b57d02020-09-27 17:15:10 +0200971static void _file_raw_output(struct log_target *target, int subsys, unsigned int level, const char *file,
972 int line, int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800973{
Harald Welteb72867f2020-09-26 21:45:16 +0200974 struct msgb *msg;
Harald Weltea0b57d02020-09-27 17:15:10 +0200975 int rc;
Harald Welteb72867f2020-09-26 21:45:16 +0200976
977 OSMO_ASSERT(target->tgt_file.wqueue);
Harald Weltea0b57d02020-09-27 17:15:10 +0200978 msg = msgb_alloc_c(target->tgt_file.wqueue, MAX_LOG_SIZE, "log_file_msg");
Harald Welteb72867f2020-09-26 21:45:16 +0200979 if (!msg)
980 return;
981
982 /* we simply enqueue the log message to a write queue here, to avoid any blocking
983 * writes on the output file. The write queue will tell us once the file is writable
984 * and call _file_wq_write_cb() */
Harald Weltea0b57d02020-09-27 17:15:10 +0200985 rc = _output_buf((char *)msgb_data(msg), msgb_tailroom(msg), target, subsys, level, file, line, cont, format, ap);
986 msgb_put(msg, rc);
Harald Welte78d73672020-09-29 11:32:35 +0200987
988 /* attempt a synchronous, non-blocking write, if the write queue is empty */
989 if (target->tgt_file.wqueue->current_length == 0) {
990 rc = _file_wq_write_cb(&target->tgt_file.wqueue->bfd, msg);
991 if (rc == 0) {
992 /* the write was complete, we can exit early */
993 msgb_free(msg);
994 return;
995 }
996 }
997 /* if we reach here, either we already had elements in the write_queue, or the synchronous write
998 * failed: enqueue the message to the write_queue (backlog) */
Harald Welte9a9627e2021-11-25 13:38:19 +0100999 if (osmo_wqueue_enqueue_quiet(target->tgt_file.wqueue, msg) < 0) {
1000 msgb_free(msg);
1001 /* TODO: increment some counter so we can see that messages were dropped */
1002 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001003}
Harald Welte44c0f632017-01-15 17:58:29 +01001004#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001005
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001006/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001007 * \returns dynamically-allocated log target
1008 * This funcition allocates a \ref log_target and initializes it
1009 * with some default values. The newly created target is not
1010 * registered yet.
1011 */
Harald Welte3ae27582010-03-26 21:24:24 +08001012struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001013{
Harald Welte3ae27582010-03-26 21:24:24 +08001014 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +08001015 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001016
Max68bf16a2018-01-10 17:00:43 +01001017 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001018
Harald Welte3ae27582010-03-26 21:24:24 +08001019 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001020 if (!target)
1021 return NULL;
1022
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +02001023 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +02001024 struct log_category,
1025 osmo_log_info->num_cat);
1026 if (!target->categories) {
1027 talloc_free(target);
1028 return NULL;
1029 }
1030
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001031 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +08001032
1033 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +02001034 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +08001035 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +02001036 cat->enabled = osmo_log_info->cat[i].enabled;
1037 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +08001038 }
1039
1040 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001041 target->use_color = 1;
1042 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +01001043 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001044 target->print_filename2 = LOG_FILENAME_PATH;
1045 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +08001046
1047 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001048 target->loglevel = 0;
1049 return target;
1050}
1051
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001052/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001053 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +08001054struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001055{
Harald Weltea3b844c2010-03-27 00:04:40 +08001056/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +02001057#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +08001058 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001059
Harald Welte3ae27582010-03-26 21:24:24 +08001060 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001061 if (!target)
1062 return NULL;
1063
Harald Welte28222962011-02-18 20:37:04 +01001064 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +02001065 target->tgt_file.out = stderr;
Harald Welteb72867f2020-09-26 21:45:16 +02001066 target->output = _file_output_stream;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001067 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +08001068#else
1069 return NULL;
1070#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001071}
1072
Harald Welte44c0f632017-01-15 17:58:29 +01001073#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001074/*! Create a new file-based log target using buffered, blocking stream output
Harald Welte18fc4652011-08-17 14:14:17 +02001075 * \param[in] fname File name of the new log file
1076 * \returns Log target in case of success, NULL otherwise
1077 */
Harald Welteb72867f2020-09-26 21:45:16 +02001078struct log_target *log_target_create_file_stream(const char *fname)
Harald Welte3086c392010-08-25 19:10:50 +02001079{
1080 struct log_target *target;
1081
1082 target = log_target_create();
1083 if (!target)
1084 return NULL;
1085
Harald Welte28222962011-02-18 20:37:04 +01001086 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +02001087 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001088 if (!target->tgt_file.out) {
1089 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +02001090 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001091 }
Harald Welteb72867f2020-09-26 21:45:16 +02001092 target->output = _file_output_stream;
1093 target->tgt_file.fname = talloc_strdup(target, fname);
Harald Welte3086c392010-08-25 19:10:50 +02001094
Harald Welteb72867f2020-09-26 21:45:16 +02001095 return target;
1096}
1097
1098/*! switch from non-blocking/write-queue to blocking + buffered stream output
1099 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001100 * \return 0 on success; 1 if already switched before; negative on error
1101 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1102 */
Harald Welteb72867f2020-09-26 21:45:16 +02001103int log_target_file_switch_to_stream(struct log_target *target)
1104{
1105 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001106
1107 if (!target)
1108 return -ENODEV;
1109
Harald Welteb72867f2020-09-26 21:45:16 +02001110 if (target->tgt_file.out) {
1111 /* target has already been switched over */
1112 return 1;
1113 }
1114
Harald Welteb72867f2020-09-26 21:45:16 +02001115 wq = target->tgt_file.wqueue;
1116 OSMO_ASSERT(wq);
1117
1118 /* re-open output as stream */
1119 if (target->type == LOG_TGT_TYPE_STDERR)
1120 target->tgt_file.out = stderr;
1121 else
1122 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1123 if (!target->tgt_file.out) {
Harald Welteb72867f2020-09-26 21:45:16 +02001124 return -EIO;
1125 }
1126
1127 /* synchronously write anything left in the queue */
1128 while (!llist_empty(&wq->msg_queue)) {
1129 struct msgb *msg = msgb_dequeue(&wq->msg_queue);
1130 fwrite(msgb_data(msg), msgb_length(msg), 1, target->tgt_file.out);
1131 msgb_free(msg);
1132 }
1133
1134 /* now that everything succeeded, we can finally close the old output fd */
1135 if (target->type == LOG_TGT_TYPE_FILE) {
1136 osmo_fd_unregister(&wq->bfd);
1137 close(wq->bfd.fd);
Pau Espin Pedrolfc376502023-03-14 14:32:46 +01001138 wq->bfd.fd = -1;
Harald Welteb72867f2020-09-26 21:45:16 +02001139 }
1140
1141 /* release the queue itself */
1142 talloc_free(wq);
1143 target->tgt_file.wqueue = NULL;
1144 target->output = _file_output_stream;
1145 target->raw_output = NULL;
1146
1147 return 0;
1148}
1149
1150/*! switch from blocking + buffered file output to non-blocking write-queue based output.
1151 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001152 * \return 0 on success; 1 if already switched before; negative on error
1153 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1154 */
Harald Welteb72867f2020-09-26 21:45:16 +02001155int log_target_file_switch_to_wqueue(struct log_target *target)
1156{
1157 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001158 int rc;
1159
1160 if (!target)
1161 return -ENODEV;
1162
Harald Welteb72867f2020-09-26 21:45:16 +02001163 if (!target->tgt_file.out) {
1164 /* target has already been switched over */
1165 return 1;
1166 }
1167
Harald Welteb72867f2020-09-26 21:45:16 +02001168 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1169 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1170 * reduce the OOM probability within logging, as the pool is already allocated */
1171 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001172 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001173 if (!wq)
1174 return -ENOMEM;
1175 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1176
1177 fflush(target->tgt_file.out);
1178 if (target->type == LOG_TGT_TYPE_FILE) {
1179 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1180 if (rc < 0) {
Harald Welteb72867f2020-09-26 21:45:16 +02001181 talloc_free(wq);
1182 return -errno;
1183 }
1184 } else {
1185 rc = STDERR_FILENO;
1186 }
1187 wq->bfd.fd = rc;
1188 wq->bfd.when = OSMO_FD_WRITE;
1189 wq->write_cb = _file_wq_write_cb;
1190
1191 rc = osmo_fd_register(&wq->bfd);
1192 if (rc < 0) {
1193 talloc_free(wq);
1194 return -EIO;
1195 }
1196 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001197 target->raw_output = _file_raw_output;
1198 target->output = NULL;
Harald Welte3086c392010-08-25 19:10:50 +02001199
Harald Welteb72867f2020-09-26 21:45:16 +02001200 /* now that everything succeeded, we can finally close the old output stream */
1201 if (target->type == LOG_TGT_TYPE_FILE)
1202 fclose(target->tgt_file.out);
1203 target->tgt_file.out = NULL;
1204
1205 return 0;
1206}
1207
1208/*! Create a new file-based log target using non-blocking write_queue
1209 * \param[in] fname File name of the new log file
1210 * \returns Log target in case of success, NULL otherwise
1211 */
1212struct log_target *log_target_create_file(const char *fname)
1213{
1214 struct log_target *target;
1215 struct osmo_wqueue *wq;
1216 int rc;
1217
1218 target = log_target_create();
1219 if (!target)
1220 return NULL;
1221
1222 target->type = LOG_TGT_TYPE_FILE;
1223 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1224 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1225 * reduce the OOM probability within logging, as the pool is already allocated */
1226 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001227 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001228 if (!wq) {
1229 log_target_destroy(target);
1230 return NULL;
1231 }
1232 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1233 wq->bfd.fd = open(fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1234 if (wq->bfd.fd < 0) {
1235 talloc_free(wq);
1236 log_target_destroy(target);
1237 return NULL;
1238 }
1239 wq->bfd.when = OSMO_FD_WRITE;
1240 wq->write_cb = _file_wq_write_cb;
1241
1242 rc = osmo_fd_register(&wq->bfd);
1243 if (rc < 0) {
1244 talloc_free(wq);
1245 log_target_destroy(target);
1246 return NULL;
1247 }
1248
1249 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001250 target->raw_output = _file_raw_output;
Harald Welte3086c392010-08-25 19:10:50 +02001251 target->tgt_file.fname = talloc_strdup(target, fname);
1252
1253 return target;
1254}
Harald Welte44c0f632017-01-15 17:58:29 +01001255#endif
Harald Welte3086c392010-08-25 19:10:50 +02001256
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001257/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +02001258 * \param[in] type Log target type
1259 * \param[in] fname File name
1260 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001261 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001262 */
Harald Weltee6fb8902022-01-09 12:00:03 +01001263struct log_target *log_target_find(enum log_target_type type, const char *fname)
Harald Welte28222962011-02-18 20:37:04 +01001264{
1265 struct log_target *tgt;
1266
1267 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1268 if (tgt->type != type)
1269 continue;
Maxc90f40a2018-01-11 10:52:28 +01001270 switch (tgt->type) {
1271 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001272 if (!strcmp(fname, tgt->tgt_file.fname))
1273 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001274 break;
1275 case LOG_TGT_TYPE_GSMTAP:
1276 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1277 return tgt;
1278 break;
1279 default:
Harald Welte28222962011-02-18 20:37:04 +01001280 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001281 }
Harald Welte28222962011-02-18 20:37:04 +01001282 }
1283 return NULL;
1284}
1285
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001286/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001287 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001288void log_target_destroy(struct log_target *target)
1289{
Harald Welte3086c392010-08-25 19:10:50 +02001290 /* just in case, to make sure we don't have any references */
1291 log_del_target(target);
1292
Harald Welte44c0f632017-01-15 17:58:29 +01001293#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001294 struct osmo_wqueue *wq;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001295 switch (target->type) {
1296 case LOG_TGT_TYPE_FILE:
Harald Welteb72867f2020-09-26 21:45:16 +02001297 case LOG_TGT_TYPE_STDERR:
1298 if (target->tgt_file.out) {
1299 if (target->type == LOG_TGT_TYPE_FILE)
1300 fclose(target->tgt_file.out);
1301 target->tgt_file.out = NULL;
1302 }
1303 wq = target->tgt_file.wqueue;
1304 if (wq) {
1305 if (wq->bfd.fd >= 0) {
Pau Espin Pedrolfc376502023-03-14 14:32:46 +01001306 osmo_fd_unregister(&wq->bfd);
Harald Welteb72867f2020-09-26 21:45:16 +02001307 if (target->type == LOG_TGT_TYPE_FILE)
1308 close(wq->bfd.fd);
1309 wq->bfd.fd = -1;
1310 }
Harald Welteb72867f2020-09-26 21:45:16 +02001311 osmo_wqueue_clear(wq);
1312 talloc_free(wq);
1313 target->tgt_file.wqueue = NULL;
1314 }
1315 talloc_free((void *)target->tgt_file.fname);
1316 target->tgt_file.fname = NULL;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001317 break;
Vadim Yanitskiy2f4186a2021-12-29 21:58:19 +06001318 case LOG_TGT_TYPE_GSMTAP:
1319 gsmtap_source_free(target->tgt_gsmtap.gsmtap_inst);
1320 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001321#ifdef HAVE_SYSLOG_H
1322 case LOG_TGT_TYPE_SYSLOG:
1323 closelog();
1324 break;
1325#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001326 default:
1327 /* make GCC happy */
1328 break;
Harald Welte3086c392010-08-25 19:10:50 +02001329 }
Harald Welte44c0f632017-01-15 17:58:29 +01001330#endif
Harald Welte3086c392010-08-25 19:10:50 +02001331
1332 talloc_free(target);
1333}
1334
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001335/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001336 * \param[in] target log target to re-open
1337 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001338int log_target_file_reopen(struct log_target *target)
1339{
Harald Welteb72867f2020-09-26 21:45:16 +02001340 struct osmo_wqueue *wq;
1341 int rc;
Harald Welte3086c392010-08-25 19:10:50 +02001342
Harald Welteb72867f2020-09-26 21:45:16 +02001343 OSMO_ASSERT(target->type == LOG_TGT_TYPE_FILE || target->type == LOG_TGT_TYPE_STDERR);
1344 OSMO_ASSERT(target->tgt_file.out || target->tgt_file.wqueue);
Harald Welte3086c392010-08-25 19:10:50 +02001345
Harald Welteb72867f2020-09-26 21:45:16 +02001346 if (target->tgt_file.out) {
1347 fclose(target->tgt_file.out);
1348 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1349 if (!target->tgt_file.out)
1350 return -errno;
1351 } else {
1352 wq = target->tgt_file.wqueue;
Harald Welteb72867f2020-09-26 21:45:16 +02001353 if (wq->bfd.fd >= 0) {
Pau Espin Pedrolfc376502023-03-14 14:32:46 +01001354 osmo_fd_unregister(&wq->bfd);
Harald Welteb72867f2020-09-26 21:45:16 +02001355 close(wq->bfd.fd);
1356 wq->bfd.fd = -1;
1357 }
1358
1359 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1360 if (rc < 0)
1361 return -errno;
1362 wq->bfd.fd = rc;
1363 rc = osmo_fd_register(&wq->bfd);
1364 if (rc < 0)
1365 return rc;
1366 }
Harald Welte3086c392010-08-25 19:10:50 +02001367
1368 return 0;
1369}
1370
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001371/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001372 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001373int log_targets_reopen(void)
1374{
1375 struct log_target *tar;
1376 int rc = 0;
1377
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001378 log_tgt_mutex_lock();
1379
Harald Welte4de854d2013-03-18 19:01:40 +01001380 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1381 switch (tar->type) {
1382 case LOG_TGT_TYPE_FILE:
1383 if (log_target_file_reopen(tar) < 0)
1384 rc = -1;
1385 break;
1386 default:
1387 break;
1388 }
1389 }
1390
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001391 log_tgt_mutex_unlock();
1392
Harald Welte4de854d2013-03-18 19:01:40 +01001393 return rc;
1394}
1395
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001396/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001397 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001398 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001399 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001400 *
1401 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001402 */
Harald Welteb43bc042011-06-27 10:29:17 +02001403int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001404{
Harald Welteb43bc042011-06-27 10:29:17 +02001405 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001406 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001407
Philipp Maierdc02c062020-05-12 17:51:25 +02001408 /* Ensure that log_init is not called multiple times */
1409 OSMO_ASSERT(tall_log_ctx == NULL)
1410
Harald Welteb43bc042011-06-27 10:29:17 +02001411 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1412 if (!tall_log_ctx)
1413 return -ENOMEM;
1414
1415 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1416 if (!osmo_log_info)
1417 return -ENOMEM;
1418
Max72dfd432018-12-04 11:24:18 +01001419 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1420
1421 if (inf) {
1422 osmo_log_info->filter_fn = inf->filter_fn;
1423 osmo_log_info->num_cat_user = inf->num_cat;
1424 osmo_log_info->num_cat += inf->num_cat;
1425 }
Harald Welteb43bc042011-06-27 10:29:17 +02001426
Philipp Maierdcad1c52020-03-25 11:25:59 +01001427 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1428 osmo_log_info->num_cat);
1429 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001430 talloc_free(osmo_log_info);
1431 osmo_log_info = NULL;
1432 return -ENOMEM;
1433 }
1434
Philipp Maierdcad1c52020-03-25 11:25:59 +01001435 /* copy over the user part and sanitize loglevel */
1436 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001437 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001438 memcpy(&cat_ptr[i], &inf->cat[i],
1439 sizeof(struct log_info_cat));
1440
1441 /* Make sure that the loglevel is set to NOTICE in case
1442 * no loglevel has been preset. */
1443 if (!cat_ptr[i].loglevel) {
1444 cat_ptr[i].loglevel = LOGL_NOTICE;
1445 }
Max72dfd432018-12-04 11:24:18 +01001446 }
Harald Welteb43bc042011-06-27 10:29:17 +02001447 }
1448
1449 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001450 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001451 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001452 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001453 }
1454
Philipp Maierdcad1c52020-03-25 11:25:59 +01001455 osmo_log_info->cat = cat_ptr;
1456
Harald Welte9fe16522011-06-27 14:00:03 +02001457 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001458}
Harald Welte18fc4652011-08-17 14:14:17 +02001459
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001460/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001461 * This function destroys all targets and releases associated memory */
1462void log_fini(void)
1463{
1464 struct log_target *tar, *tar2;
1465
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001466 log_tgt_mutex_lock();
1467
Harald Welte69e6c3c2016-04-20 10:41:27 +02001468 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1469 log_target_destroy(tar);
1470
1471 talloc_free(osmo_log_info);
1472 osmo_log_info = NULL;
1473 talloc_free(tall_log_ctx);
1474 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001475
1476 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001477}
1478
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001479/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001480 * \returns != 0 if a log entry might get generated by at least one target */
1481int log_check_level(int subsys, unsigned int level)
1482{
1483 struct log_target *tar;
1484
Max68bf16a2018-01-10 17:00:43 +01001485 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001486
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001487 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001488
1489 /* TODO: The following could/should be cached (update on config) */
1490
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001491 log_tgt_mutex_lock();
1492
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001493 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001494 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001495 continue;
1496
1497 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001498 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001499 return 1;
1500 }
1501
1502 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001503 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001504 return 0;
1505}
1506
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001507/*! @} */