blob: 148bb272b2210891618827b55f1b2e5589bb25eb [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
Harald Welte01fd5cb2010-03-26 23:51:31 +080028#include "../config.h"
29
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 },
Harald Welteb43bc042011-06-27 10:29:17 +0200310};
311
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200312void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100313{
314 if (!osmo_log_info) {
315 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100316 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100317 OSMO_ASSERT(osmo_log_info);
318 }
319}
320
Harald Welteb43bc042011-06-27 10:29:17 +0200321/* special magic for negative (library-internal) log subsystem numbers */
322static int subsys_lib2index(int subsys)
323{
324 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
325}
326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700328 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200329 * \returns numeric log level
330 */
Harald Welte3ae27582010-03-26 21:24:24 +0800331int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800332{
333 return get_string_value(loglevel_strs, lvl);
334}
335
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200336/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700337 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200338 * \returns zero-terminated string (log level name)
339 */
Harald Welte9ac22252010-05-11 11:19:40 +0200340const char *log_level_str(unsigned int lvl)
341{
342 return get_value_string(loglevel_strs, lvl);
343}
344
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200345/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200346 * \param[in] category human-readable log category name
347 * \returns numeric category value, or -EINVAL otherwise
348 */
Harald Welte3ae27582010-03-26 21:24:24 +0800349int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800350{
351 int i;
352
Max68bf16a2018-01-10 17:00:43 +0100353 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100354
Harald Welte4ebdf742010-05-19 19:54:00 +0200355 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200356 if (osmo_log_info->cat[i].name == NULL)
357 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200358 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800359 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800360 }
361
362 return -EINVAL;
363}
364
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200365/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200366 * \param[in] target log target to be configured
367 * \param[in] _mask log category mask string
368 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800369 * The format can be this: category1:category2:category3
370 * or category1,2:category2,3:...
371 */
Harald Welte3ae27582010-03-26 21:24:24 +0800372void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800373{
374 int i = 0;
375 char *mask = strdup(_mask);
376 char *category_token = NULL;
377
Max68bf16a2018-01-10 17:00:43 +0100378 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100379
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800380 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200381 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800382 target->categories[i].enabled = 0;
383
384 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200385 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800386 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200387 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200388 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800389 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200390
391 if (!osmo_log_info->cat[i].name)
392 continue;
393
394 length = strlen(category_token);
395 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200396
397 /* Use longest length not to match subocurrences. */
398 if (cat_length > length)
399 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800400
401 if (colon)
402 length = colon - category_token;
403
Harald Welte4ebdf742010-05-19 19:54:00 +0200404 if (strncasecmp(osmo_log_info->cat[i].name,
405 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800406 int level = 0;
407
408 if (colon)
409 level = atoi(colon+1);
410
Harald Weltefaadfe22010-03-26 21:05:43 +0800411 target->categories[i].enabled = 1;
412 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800413 }
414 }
415 } while ((category_token = strtok(NULL, ":")));
416
417 free(mask);
418}
419
420static const char* color(int subsys)
421{
Harald Welte4ebdf742010-05-19 19:54:00 +0200422 if (subsys < osmo_log_info->num_cat)
423 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800424
Harald Welted788f662010-03-26 09:45:03 +0800425 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800426}
427
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100428static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100429 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
430 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
431 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
432 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
433 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100434 { 0, NULL }
435};
436
437static const char *level_color(int level)
438{
439 const char *c = get_value_string_or_null(level_colors, level);
440 if (!c)
441 return get_value_string(level_colors, LOGL_FATAL);
442 return c;
443}
444
Harald Welteaa00f992016-12-02 15:30:02 +0100445const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100446{
447 if (subsys < osmo_log_info->num_cat)
448 return osmo_log_info->cat[subsys].name;
449
450 return NULL;
451}
452
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100453static const char *const_basename(const char *path)
454{
455 const char *bn = strrchr(path, '/');
456 if (!bn || !bn[1])
457 return path;
458 return bn + 1;
459}
460
Harald Weltea0b57d02020-09-27 17:15:10 +0200461/*! main output formatting function for log lines.
462 * \param[out] buf caller-allocated output buffer for the generated string
463 * \param[in] buf_len number of bytes available in buf
464 * \param[in] target log target for which the string is to be formatted
465 * \param[in] subsys Log sub-system number
466 * \param[in] level Log level
467 * \param[in] file name of source code file generating the log
468 * \param[in] line line source code line number within 'file' generating the log
469 * \param[in] cont is this a continuation (true) or not (false)
470 * \param[in] format format string
471 * \param[in] ap variable argument list for format
472 * \returns number of bytes written to out */
473static int _output_buf(char *buf, int buf_len, struct log_target *target, unsigned int subsys,
474 unsigned int level, const char *file, int line, int cont,
475 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800476{
Harald Weltea0b57d02020-09-27 17:15:10 +0200477 int ret, len = 0, offset = 0, rem = buf_len;
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100478 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800479
480 /* are we using color */
481 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100482 c_subsys = color(subsys);
483 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100484 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200485 if (ret < 0)
486 goto err;
487 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800488 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800489 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800490 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100491 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200492#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100493 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100494 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200495 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100496 localtime_r(&tv.tv_sec, &tm);
497 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100498 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100499 tm.tm_hour, tm.tm_min, tm.tm_sec,
500 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100501 if (ret < 0)
502 goto err;
503 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200504#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100505 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800506 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200507 if ((tm = time(NULL)) == (time_t) -1)
508 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200509 /* Get human-readable representation of time.
510 man ctime: we need at least 26 bytes in buf */
511 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200512 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200513 ret = strlen(buf + offset);
514 if (ret <= 0)
515 goto err;
516 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
517 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200518 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800519 }
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100520 if (target->print_tid) {
521 if (logging_tid == 0)
522 logging_tid = (long int)osmo_gettid();
523 ret = snprintf(buf + offset, rem, "%ld ", logging_tid);
524 if (ret < 0)
525 goto err;
526 OSMO_SNPRINTF_RET(ret, rem, offset, len);
527 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100528 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100529 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
530 target->use_color ? level_color(level) : "",
531 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100532 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100533 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100534 if (ret < 0)
535 goto err;
536 OSMO_SNPRINTF_RET(ret, rem, offset, len);
537 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100538 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100539 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
540 target->use_color ? level_color(level) : "",
541 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100542 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100543 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100544 if (ret < 0)
545 goto err;
546 OSMO_SNPRINTF_RET(ret, rem, offset, len);
547 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100548 if (target->print_category_hex) {
549 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200550 if (ret < 0)
551 goto err;
552 OSMO_SNPRINTF_RET(ret, rem, offset, len);
553 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200554
555 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
556 switch (target->print_filename2) {
557 case LOG_FILENAME_NONE:
558 break;
559 case LOG_FILENAME_PATH:
560 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
561 if (ret < 0)
562 goto err;
563 OSMO_SNPRINTF_RET(ret, rem, offset, len);
564 break;
565 case LOG_FILENAME_BASENAME:
566 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
567 if (ret < 0)
568 goto err;
569 OSMO_SNPRINTF_RET(ret, rem, offset, len);
570 break;
571 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100572 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800573 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200574 ret = vsnprintf(buf + offset, rem, format, ap);
575 if (ret < 0)
576 goto err;
577 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800578
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200579 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
580 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
581 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
582 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
583 && offset > 0 && buf[offset-1] == '\n') {
584 switch (target->print_filename2) {
585 case LOG_FILENAME_NONE:
586 break;
587 case LOG_FILENAME_PATH:
588 offset --;
589 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
590 if (ret < 0)
591 goto err;
592 OSMO_SNPRINTF_RET(ret, rem, offset, len);
593 break;
594 case LOG_FILENAME_BASENAME:
595 offset --;
596 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
597 if (ret < 0)
598 goto err;
599 OSMO_SNPRINTF_RET(ret, rem, offset, len);
600 break;
601 }
602 }
603
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200604 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100605 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100606 if (ret < 0)
607 goto err;
608 OSMO_SNPRINTF_RET(ret, rem, offset, len);
609 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200610err:
Pau Espin Pedrolbeaf2a22022-01-04 13:36:18 +0100611 len = OSMO_MIN(buf_len - 1, len);
612 buf[len] = '\0';
Harald Weltea0b57d02020-09-27 17:15:10 +0200613 return len;
614}
615
616/* Format the log line for given target; use a stack buffer and call target->output */
617static void _output(struct log_target *target, unsigned int subsys,
618 unsigned int level, const char *file, int line, int cont,
619 const char *format, va_list ap)
620{
621 char buf[MAX_LOG_SIZE];
622 int rc;
623
624 rc = _output_buf(buf, sizeof(buf), target, subsys, level, file, line, cont, format, ap);
625 if (rc > 0)
626 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800627}
628
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100629/* Catch internal logging category indexes as well as out-of-bounds indexes.
630 * For internal categories, the ID is negative starting with -1; and internal
631 * logging categories are added behind the user categories. For out-of-bounds
632 * indexes, return the index of DLGLOBAL. The returned category index is
633 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
634 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100635static inline int map_subsys(int subsys)
636{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100637 /* Note: comparing signed and unsigned integers */
638
639 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
640 subsys = DLGLOBAL;
641
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100642 if (subsys < 0)
643 subsys = subsys_lib2index(subsys);
644
Neels Hofmeyrca135742016-12-12 14:18:54 +0100645 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100646 subsys = subsys_lib2index(DLGLOBAL);
647
Neels Hofmeyrca135742016-12-12 14:18:54 +0100648 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
649
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100650 return subsys;
651}
652
Maxc65c5b42017-03-15 13:20:23 +0100653static inline bool should_log_to_target(struct log_target *tar, int subsys,
654 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100655{
656 struct log_category *category;
657
658 category = &tar->categories[subsys];
659
660 /* subsystem is not supposed to be logged */
661 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100662 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100663
664 /* Check the global log level */
665 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100666 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100667
668 /* Check the category log level */
669 if (tar->loglevel == 0 && category->loglevel != 0 &&
670 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100671 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100672
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100673 /* Apply filters here... if that becomes messy we will
674 * need to put filters in a list and each filter will
675 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100676 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100677 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100678
679 if (osmo_log_info->filter_fn)
680 return osmo_log_info->filter_fn(&log_context, tar);
681
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100682 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100683 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100684}
685
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200686/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200687 * \param[in] subsys Logging sub-system
688 * \param[in] level Log level
689 * \param[in] file name of source code file
690 * \param[in] cont continuation (1) or new line (0)
691 * \param[in] format format string
692 * \param[in] ap vararg-list containing format string arguments
693 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200694void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200695 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800696{
Harald Welte3ae27582010-03-26 21:24:24 +0800697 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800698
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100699 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200700
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200701 log_tgt_mutex_lock();
702
Harald Welte28222962011-02-18 20:37:04 +0100703 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200704 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800705
Maxc65c5b42017-03-15 13:20:23 +0100706 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800707 continue;
708
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200709 /* According to the manpage, vsnprintf leaves the value of ap
710 * in undefined state. Since _output uses vsnprintf and it may
711 * be called several times, we have to pass a copy of ap. */
712 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100713 if (tar->raw_output)
714 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
715 else
716 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200717 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800718 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200719
720 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800721}
722
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200723/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200724 * \param[in] subsys Logging sub-system
725 * \param[in] file name of source code file
726 * \param[in] cont continuation (1) or new line (0)
727 * \param[in] format format string
728 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200729void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800730 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800731{
732 va_list ap;
733
734 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200735 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800736 va_end(ap);
737}
738
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200739/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200740 * \param[in] subsys Logging sub-system
741 * \param[in] level Log level
742 * \param[in] file name of source code file
743 * \param[in] cont continuation (1) or new line (0)
744 * \param[in] format format string
745 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200746void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800747{
748 va_list ap;
749
Harald Welte433005c2020-09-26 11:51:32 +0200750 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800751 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200752 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800753 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200754 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755}
756
Philipp Maier4c44d462022-01-19 11:43:05 +0100757/* This logging function is used as a fallback when the logging framework is
758 * not is not properly initialized. */
759void logp_stub(const char *file, int line, int cont, const char *format, ...)
760{
761 va_list ap;
762 if (!cont)
763 fprintf(stderr, "%s:%d ", file, line);
764 va_start(ap, format);
765 vfprintf(stderr, format, ap);
766 va_end(ap);
767}
768
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200769/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200770 * \param[in] target Log target to be registered
771 */
Harald Welte3ae27582010-03-26 21:24:24 +0800772void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800773{
Harald Welte28222962011-02-18 20:37:04 +0100774 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800775}
776
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200777/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200778 * \param[in] target Log target to be unregistered
779 */
Harald Welte3ae27582010-03-26 21:24:24 +0800780void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800781{
782 llist_del(&target->entry);
783}
784
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200785/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800786void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800787{
Harald Welte3ae27582010-03-26 21:24:24 +0800788 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800789}
790
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200791/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200792 * \param[in] ctx_nr logging context number
793 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200794 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200795 *
796 * A logging context is something like the subscriber identity to which
797 * the currently processed message relates, or the BTS through which it
798 * was received. As soon as this data is known, it can be set using
799 * this function. The main use of context information is for logging
800 * filters.
801 */
Harald Welte3ae27582010-03-26 21:24:24 +0800802int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800803{
Harald Welte3ae27582010-03-26 21:24:24 +0800804 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800805 return -EINVAL;
806
Harald Welte3ae27582010-03-26 21:24:24 +0800807 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800808
809 return 0;
810}
811
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200812/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200813 * \param[in] target Log target to be affected
814 * \param[in] all enable (1) or disable (0) the ALL filter
815 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100816 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100817 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
818 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200819 */
Harald Welte3ae27582010-03-26 21:24:24 +0800820void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800821{
822 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100823 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800824 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100825 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800826}
827
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200828/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200829 * \param[in] target Log target to be affected
830 * \param[in] use_color Use color (1) or don't use color (0)
831 */
Harald Welte3ae27582010-03-26 21:24:24 +0800832void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800833{
834 target->use_color = use_color;
835}
836
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200837/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200838 * \param[in] target Log target to be affected
839 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
840 */
Harald Welte3ae27582010-03-26 21:24:24 +0800841void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800842{
843 target->print_timestamp = print_timestamp;
844}
845
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200846/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100847 * \param[in] target Log target to be affected
848 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
849 *
850 * When both timestamp and extended timestamp is enabled then only
851 * the extended timestamp will be used. The format of the timestamp
852 * is YYYYMMDDhhmmssnnn.
853 */
854void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
855{
856 target->print_ext_timestamp = print_timestamp;
857}
858
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100859/*! Enable or disable printing of timestamps while logging
860 * \param[in] target Log target to be affected
861 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
862 */
863void log_set_print_tid(struct log_target *target, int print_tid)
864{
865 target->print_tid = print_tid;
866}
867
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100868/*! Use log_set_print_filename2() instead.
869 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
870 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
871 * behavior, which combined the category in hex with the filename. For example, if the category-hex
872 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
873 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200874 * \param[in] target Log target to be affected
875 * \param[in] print_filename Enable (1) or disable (0) filenames
876 */
877void log_set_print_filename(struct log_target *target, int print_filename)
878{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100879 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
880 log_set_print_category_hex(target, print_filename);
881}
882
883/*! Enable or disable printing of the filename while logging.
884 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700885 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100886 * LOG_FILENAME_NONE omits the source file and line information from logs.
887 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
888 */
889void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
890{
891 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200892}
893
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200894/*! Set the position where on a log line the source file info should be logged.
895 * \param[in] target Log target to be affected.
896 * \param[in] pos A LOG_FILENAME_POS_* enum value.
897 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
898 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
899 */
900void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
901{
902 target->print_filename_pos = pos;
903}
904
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200905/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100906 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700907 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100908 *
909 * Print the category/subsys name in front of every log message.
910 */
911void log_set_print_category(struct log_target *target, int print_category)
912{
913 target->print_category = print_category;
914}
915
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100916/*! Enable or disable printing of the category number in hex ('<000b>').
917 * \param[in] target Log target to be affected.
918 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
919 */
920void log_set_print_category_hex(struct log_target *target, int print_category_hex)
921{
922 target->print_category_hex = print_category_hex;
923}
924
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100925/*! Enable or disable printing of the log level name.
926 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700927 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100928 *
929 * Print the log level name in front of every log message.
930 */
931void log_set_print_level(struct log_target *target, int print_level)
932{
933 target->print_level = (bool)print_level;
934}
935
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200936/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200937 * \param[in] target Log target to be affected
938 * \param[in] log_level New global log level
939 */
Harald Welte3ae27582010-03-26 21:24:24 +0800940void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800941{
942 target->loglevel = log_level;
943}
944
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200945/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100946 * \param[in] target Log target to be affected
947 * \param[in] category Log category to be affected
948 * \param[in] enable whether to enable or disable the filter
949 * \param[in] level Log level of the filter
950 */
Harald Welte3ae27582010-03-26 21:24:24 +0800951void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800952 int enable, int level)
953{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100954 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800955 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100956 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800957 target->categories[category].enabled = !!enable;
958 target->categories[category].loglevel = level;
959}
960
Harald Welte44c0f632017-01-15 17:58:29 +0100961#if (!EMBEDDED)
Harald Welte78d73672020-09-29 11:32:35 +0200962/* write-queue tells us we should write another msgb (log line) to the output fd */
963static int _file_wq_write_cb(struct osmo_fd *ofd, struct msgb *msg)
964{
965 int rc;
966
967 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
968 if (rc < 0)
969 return rc;
970 if (rc != msgb_length(msg)) {
971 /* pull the number of bytes we have already written */
972 msgb_pull(msg, rc);
973 /* ask write_queue to re-insert the msgb at the head of the queue */
974 return -EAGAIN;
975 }
976 return 0;
977}
978
979/* output via buffered, blocking stdio streams */
Harald Welteb72867f2020-09-26 21:45:16 +0200980static void _file_output_stream(struct log_target *target, unsigned int level,
981 const char *log)
982{
983 OSMO_ASSERT(target->tgt_file.out);
984 fputs(log, target->tgt_file.out);
985 fflush(target->tgt_file.out);
986}
987
988/* output via non-blocking write_queue, doing internal buffering */
Harald Weltea0b57d02020-09-27 17:15:10 +0200989static void _file_raw_output(struct log_target *target, int subsys, unsigned int level, const char *file,
990 int line, int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800991{
Harald Welteb72867f2020-09-26 21:45:16 +0200992 struct msgb *msg;
Harald Weltea0b57d02020-09-27 17:15:10 +0200993 int rc;
Harald Welteb72867f2020-09-26 21:45:16 +0200994
995 OSMO_ASSERT(target->tgt_file.wqueue);
Harald Weltea0b57d02020-09-27 17:15:10 +0200996 msg = msgb_alloc_c(target->tgt_file.wqueue, MAX_LOG_SIZE, "log_file_msg");
Harald Welteb72867f2020-09-26 21:45:16 +0200997 if (!msg)
998 return;
999
1000 /* we simply enqueue the log message to a write queue here, to avoid any blocking
1001 * writes on the output file. The write queue will tell us once the file is writable
1002 * and call _file_wq_write_cb() */
Harald Weltea0b57d02020-09-27 17:15:10 +02001003 rc = _output_buf((char *)msgb_data(msg), msgb_tailroom(msg), target, subsys, level, file, line, cont, format, ap);
1004 msgb_put(msg, rc);
Harald Welte78d73672020-09-29 11:32:35 +02001005
1006 /* attempt a synchronous, non-blocking write, if the write queue is empty */
1007 if (target->tgt_file.wqueue->current_length == 0) {
1008 rc = _file_wq_write_cb(&target->tgt_file.wqueue->bfd, msg);
1009 if (rc == 0) {
1010 /* the write was complete, we can exit early */
1011 msgb_free(msg);
1012 return;
1013 }
1014 }
1015 /* if we reach here, either we already had elements in the write_queue, or the synchronous write
1016 * failed: enqueue the message to the write_queue (backlog) */
Harald Welte9a9627e2021-11-25 13:38:19 +01001017 if (osmo_wqueue_enqueue_quiet(target->tgt_file.wqueue, msg) < 0) {
1018 msgb_free(msg);
1019 /* TODO: increment some counter so we can see that messages were dropped */
1020 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001021}
Harald Welte44c0f632017-01-15 17:58:29 +01001022#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001023
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001024/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001025 * \returns dynamically-allocated log target
1026 * This funcition allocates a \ref log_target and initializes it
1027 * with some default values. The newly created target is not
1028 * registered yet.
1029 */
Harald Welte3ae27582010-03-26 21:24:24 +08001030struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001031{
Harald Welte3ae27582010-03-26 21:24:24 +08001032 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +08001033 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001034
Max68bf16a2018-01-10 17:00:43 +01001035 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001036
Harald Welte3ae27582010-03-26 21:24:24 +08001037 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001038 if (!target)
1039 return NULL;
1040
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +02001041 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +02001042 struct log_category,
1043 osmo_log_info->num_cat);
1044 if (!target->categories) {
1045 talloc_free(target);
1046 return NULL;
1047 }
1048
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001049 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +08001050
1051 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +02001052 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +08001053 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +02001054 cat->enabled = osmo_log_info->cat[i].enabled;
1055 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +08001056 }
1057
1058 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001059 target->use_color = 1;
1060 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +01001061 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001062 target->print_filename2 = LOG_FILENAME_PATH;
1063 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +08001064
1065 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001066 target->loglevel = 0;
1067 return target;
1068}
1069
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001070/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001071 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +08001072struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001073{
Harald Weltea3b844c2010-03-27 00:04:40 +08001074/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +02001075#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +08001076 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001077
Harald Welte3ae27582010-03-26 21:24:24 +08001078 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001079 if (!target)
1080 return NULL;
1081
Harald Welte28222962011-02-18 20:37:04 +01001082 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +02001083 target->tgt_file.out = stderr;
Harald Welteb72867f2020-09-26 21:45:16 +02001084 target->output = _file_output_stream;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001085 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +08001086#else
1087 return NULL;
1088#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001089}
1090
Harald Welte44c0f632017-01-15 17:58:29 +01001091#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001092/*! Create a new file-based log target using buffered, blocking stream output
Harald Welte18fc4652011-08-17 14:14:17 +02001093 * \param[in] fname File name of the new log file
1094 * \returns Log target in case of success, NULL otherwise
1095 */
Harald Welteb72867f2020-09-26 21:45:16 +02001096struct log_target *log_target_create_file_stream(const char *fname)
Harald Welte3086c392010-08-25 19:10:50 +02001097{
1098 struct log_target *target;
1099
1100 target = log_target_create();
1101 if (!target)
1102 return NULL;
1103
Harald Welte28222962011-02-18 20:37:04 +01001104 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +02001105 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001106 if (!target->tgt_file.out) {
1107 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +02001108 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001109 }
Harald Welteb72867f2020-09-26 21:45:16 +02001110 target->output = _file_output_stream;
1111 target->tgt_file.fname = talloc_strdup(target, fname);
Harald Welte3086c392010-08-25 19:10:50 +02001112
Harald Welteb72867f2020-09-26 21:45:16 +02001113 return target;
1114}
1115
1116/*! switch from non-blocking/write-queue to blocking + buffered stream output
1117 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001118 * \return 0 on success; 1 if already switched before; negative on error
1119 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1120 */
Harald Welteb72867f2020-09-26 21:45:16 +02001121int log_target_file_switch_to_stream(struct log_target *target)
1122{
1123 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001124
1125 if (!target)
1126 return -ENODEV;
1127
Harald Welteb72867f2020-09-26 21:45:16 +02001128 if (target->tgt_file.out) {
1129 /* target has already been switched over */
1130 return 1;
1131 }
1132
Harald Welteb72867f2020-09-26 21:45:16 +02001133 wq = target->tgt_file.wqueue;
1134 OSMO_ASSERT(wq);
1135
1136 /* re-open output as stream */
1137 if (target->type == LOG_TGT_TYPE_STDERR)
1138 target->tgt_file.out = stderr;
1139 else
1140 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1141 if (!target->tgt_file.out) {
Harald Welteb72867f2020-09-26 21:45:16 +02001142 return -EIO;
1143 }
1144
1145 /* synchronously write anything left in the queue */
1146 while (!llist_empty(&wq->msg_queue)) {
1147 struct msgb *msg = msgb_dequeue(&wq->msg_queue);
1148 fwrite(msgb_data(msg), msgb_length(msg), 1, target->tgt_file.out);
1149 msgb_free(msg);
1150 }
1151
1152 /* now that everything succeeded, we can finally close the old output fd */
1153 if (target->type == LOG_TGT_TYPE_FILE) {
1154 osmo_fd_unregister(&wq->bfd);
1155 close(wq->bfd.fd);
1156 }
1157
1158 /* release the queue itself */
1159 talloc_free(wq);
1160 target->tgt_file.wqueue = NULL;
1161 target->output = _file_output_stream;
1162 target->raw_output = NULL;
1163
1164 return 0;
1165}
1166
1167/*! switch from blocking + buffered file output to non-blocking write-queue based output.
1168 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001169 * \return 0 on success; 1 if already switched before; negative on error
1170 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1171 */
Harald Welteb72867f2020-09-26 21:45:16 +02001172int log_target_file_switch_to_wqueue(struct log_target *target)
1173{
1174 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001175 int rc;
1176
1177 if (!target)
1178 return -ENODEV;
1179
Harald Welteb72867f2020-09-26 21:45:16 +02001180 if (!target->tgt_file.out) {
1181 /* target has already been switched over */
1182 return 1;
1183 }
1184
Harald Welteb72867f2020-09-26 21:45:16 +02001185 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1186 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1187 * reduce the OOM probability within logging, as the pool is already allocated */
1188 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001189 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001190 if (!wq)
1191 return -ENOMEM;
1192 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1193
1194 fflush(target->tgt_file.out);
1195 if (target->type == LOG_TGT_TYPE_FILE) {
1196 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1197 if (rc < 0) {
Harald Welteb72867f2020-09-26 21:45:16 +02001198 talloc_free(wq);
1199 return -errno;
1200 }
1201 } else {
1202 rc = STDERR_FILENO;
1203 }
1204 wq->bfd.fd = rc;
1205 wq->bfd.when = OSMO_FD_WRITE;
1206 wq->write_cb = _file_wq_write_cb;
1207
1208 rc = osmo_fd_register(&wq->bfd);
1209 if (rc < 0) {
1210 talloc_free(wq);
1211 return -EIO;
1212 }
1213 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001214 target->raw_output = _file_raw_output;
1215 target->output = NULL;
Harald Welte3086c392010-08-25 19:10:50 +02001216
Harald Welteb72867f2020-09-26 21:45:16 +02001217 /* now that everything succeeded, we can finally close the old output stream */
1218 if (target->type == LOG_TGT_TYPE_FILE)
1219 fclose(target->tgt_file.out);
1220 target->tgt_file.out = NULL;
1221
1222 return 0;
1223}
1224
1225/*! Create a new file-based log target using non-blocking write_queue
1226 * \param[in] fname File name of the new log file
1227 * \returns Log target in case of success, NULL otherwise
1228 */
1229struct log_target *log_target_create_file(const char *fname)
1230{
1231 struct log_target *target;
1232 struct osmo_wqueue *wq;
1233 int rc;
1234
1235 target = log_target_create();
1236 if (!target)
1237 return NULL;
1238
1239 target->type = LOG_TGT_TYPE_FILE;
1240 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1241 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1242 * reduce the OOM probability within logging, as the pool is already allocated */
1243 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001244 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001245 if (!wq) {
1246 log_target_destroy(target);
1247 return NULL;
1248 }
1249 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1250 wq->bfd.fd = open(fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1251 if (wq->bfd.fd < 0) {
1252 talloc_free(wq);
1253 log_target_destroy(target);
1254 return NULL;
1255 }
1256 wq->bfd.when = OSMO_FD_WRITE;
1257 wq->write_cb = _file_wq_write_cb;
1258
1259 rc = osmo_fd_register(&wq->bfd);
1260 if (rc < 0) {
1261 talloc_free(wq);
1262 log_target_destroy(target);
1263 return NULL;
1264 }
1265
1266 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001267 target->raw_output = _file_raw_output;
Harald Welte3086c392010-08-25 19:10:50 +02001268 target->tgt_file.fname = talloc_strdup(target, fname);
1269
1270 return target;
1271}
Harald Welte44c0f632017-01-15 17:58:29 +01001272#endif
Harald Welte3086c392010-08-25 19:10:50 +02001273
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001274/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +02001275 * \param[in] type Log target type
1276 * \param[in] fname File name
1277 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001278 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001279 */
Harald Weltee6fb8902022-01-09 12:00:03 +01001280struct log_target *log_target_find(enum log_target_type type, const char *fname)
Harald Welte28222962011-02-18 20:37:04 +01001281{
1282 struct log_target *tgt;
1283
1284 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1285 if (tgt->type != type)
1286 continue;
Maxc90f40a2018-01-11 10:52:28 +01001287 switch (tgt->type) {
1288 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001289 if (!strcmp(fname, tgt->tgt_file.fname))
1290 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001291 break;
1292 case LOG_TGT_TYPE_GSMTAP:
1293 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1294 return tgt;
1295 break;
1296 default:
Harald Welte28222962011-02-18 20:37:04 +01001297 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001298 }
Harald Welte28222962011-02-18 20:37:04 +01001299 }
1300 return NULL;
1301}
1302
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001303/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001304 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001305void log_target_destroy(struct log_target *target)
1306{
Harald Welte3086c392010-08-25 19:10:50 +02001307 /* just in case, to make sure we don't have any references */
1308 log_del_target(target);
1309
Harald Welte44c0f632017-01-15 17:58:29 +01001310#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001311 struct osmo_wqueue *wq;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001312 switch (target->type) {
1313 case LOG_TGT_TYPE_FILE:
Harald Welteb72867f2020-09-26 21:45:16 +02001314 case LOG_TGT_TYPE_STDERR:
1315 if (target->tgt_file.out) {
1316 if (target->type == LOG_TGT_TYPE_FILE)
1317 fclose(target->tgt_file.out);
1318 target->tgt_file.out = NULL;
1319 }
1320 wq = target->tgt_file.wqueue;
1321 if (wq) {
1322 if (wq->bfd.fd >= 0) {
1323 if (target->type == LOG_TGT_TYPE_FILE)
1324 close(wq->bfd.fd);
1325 wq->bfd.fd = -1;
1326 }
1327 osmo_fd_unregister(&wq->bfd);
1328 osmo_wqueue_clear(wq);
1329 talloc_free(wq);
1330 target->tgt_file.wqueue = NULL;
1331 }
1332 talloc_free((void *)target->tgt_file.fname);
1333 target->tgt_file.fname = NULL;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001334 break;
Vadim Yanitskiy2f4186a2021-12-29 21:58:19 +06001335 case LOG_TGT_TYPE_GSMTAP:
1336 gsmtap_source_free(target->tgt_gsmtap.gsmtap_inst);
1337 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001338#ifdef HAVE_SYSLOG_H
1339 case LOG_TGT_TYPE_SYSLOG:
1340 closelog();
1341 break;
1342#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001343 default:
1344 /* make GCC happy */
1345 break;
Harald Welte3086c392010-08-25 19:10:50 +02001346 }
Harald Welte44c0f632017-01-15 17:58:29 +01001347#endif
Harald Welte3086c392010-08-25 19:10:50 +02001348
1349 talloc_free(target);
1350}
1351
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001352/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001353 * \param[in] target log target to re-open
1354 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001355int log_target_file_reopen(struct log_target *target)
1356{
Harald Welteb72867f2020-09-26 21:45:16 +02001357 struct osmo_wqueue *wq;
1358 int rc;
Harald Welte3086c392010-08-25 19:10:50 +02001359
Harald Welteb72867f2020-09-26 21:45:16 +02001360 OSMO_ASSERT(target->type == LOG_TGT_TYPE_FILE || target->type == LOG_TGT_TYPE_STDERR);
1361 OSMO_ASSERT(target->tgt_file.out || target->tgt_file.wqueue);
Harald Welte3086c392010-08-25 19:10:50 +02001362
Harald Welteb72867f2020-09-26 21:45:16 +02001363 if (target->tgt_file.out) {
1364 fclose(target->tgt_file.out);
1365 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1366 if (!target->tgt_file.out)
1367 return -errno;
1368 } else {
1369 wq = target->tgt_file.wqueue;
1370 osmo_fd_unregister(&wq->bfd);
1371 if (wq->bfd.fd >= 0) {
1372 close(wq->bfd.fd);
1373 wq->bfd.fd = -1;
1374 }
1375
1376 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1377 if (rc < 0)
1378 return -errno;
1379 wq->bfd.fd = rc;
1380 rc = osmo_fd_register(&wq->bfd);
1381 if (rc < 0)
1382 return rc;
1383 }
Harald Welte3086c392010-08-25 19:10:50 +02001384
1385 return 0;
1386}
1387
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001388/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001389 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001390int log_targets_reopen(void)
1391{
1392 struct log_target *tar;
1393 int rc = 0;
1394
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001395 log_tgt_mutex_lock();
1396
Harald Welte4de854d2013-03-18 19:01:40 +01001397 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1398 switch (tar->type) {
1399 case LOG_TGT_TYPE_FILE:
1400 if (log_target_file_reopen(tar) < 0)
1401 rc = -1;
1402 break;
1403 default:
1404 break;
1405 }
1406 }
1407
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001408 log_tgt_mutex_unlock();
1409
Harald Welte4de854d2013-03-18 19:01:40 +01001410 return rc;
1411}
1412
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001413/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001414 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001415 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001416 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001417 *
1418 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001419 */
Harald Welteb43bc042011-06-27 10:29:17 +02001420int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001421{
Harald Welteb43bc042011-06-27 10:29:17 +02001422 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001423 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001424
Philipp Maierdc02c062020-05-12 17:51:25 +02001425 /* Ensure that log_init is not called multiple times */
1426 OSMO_ASSERT(tall_log_ctx == NULL)
1427
Harald Welteb43bc042011-06-27 10:29:17 +02001428 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1429 if (!tall_log_ctx)
1430 return -ENOMEM;
1431
1432 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1433 if (!osmo_log_info)
1434 return -ENOMEM;
1435
Max72dfd432018-12-04 11:24:18 +01001436 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1437
1438 if (inf) {
1439 osmo_log_info->filter_fn = inf->filter_fn;
1440 osmo_log_info->num_cat_user = inf->num_cat;
1441 osmo_log_info->num_cat += inf->num_cat;
1442 }
Harald Welteb43bc042011-06-27 10:29:17 +02001443
Philipp Maierdcad1c52020-03-25 11:25:59 +01001444 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1445 osmo_log_info->num_cat);
1446 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001447 talloc_free(osmo_log_info);
1448 osmo_log_info = NULL;
1449 return -ENOMEM;
1450 }
1451
Philipp Maierdcad1c52020-03-25 11:25:59 +01001452 /* copy over the user part and sanitize loglevel */
1453 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001454 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001455 memcpy(&cat_ptr[i], &inf->cat[i],
1456 sizeof(struct log_info_cat));
1457
1458 /* Make sure that the loglevel is set to NOTICE in case
1459 * no loglevel has been preset. */
1460 if (!cat_ptr[i].loglevel) {
1461 cat_ptr[i].loglevel = LOGL_NOTICE;
1462 }
Max72dfd432018-12-04 11:24:18 +01001463 }
Harald Welteb43bc042011-06-27 10:29:17 +02001464 }
1465
1466 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001467 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001468 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001469 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001470 }
1471
Philipp Maierdcad1c52020-03-25 11:25:59 +01001472 osmo_log_info->cat = cat_ptr;
1473
Harald Welte9fe16522011-06-27 14:00:03 +02001474 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001475}
Harald Welte18fc4652011-08-17 14:14:17 +02001476
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001477/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001478 * This function destroys all targets and releases associated memory */
1479void log_fini(void)
1480{
1481 struct log_target *tar, *tar2;
1482
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001483 log_tgt_mutex_lock();
1484
Harald Welte69e6c3c2016-04-20 10:41:27 +02001485 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1486 log_target_destroy(tar);
1487
1488 talloc_free(osmo_log_info);
1489 osmo_log_info = NULL;
1490 talloc_free(tall_log_ctx);
1491 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001492
1493 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001494}
1495
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001496/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001497 * \returns != 0 if a log entry might get generated by at least one target */
1498int log_check_level(int subsys, unsigned int level)
1499{
1500 struct log_target *tar;
1501
Max68bf16a2018-01-10 17:00:43 +01001502 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001503
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001504 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001505
1506 /* TODO: The following could/should be cached (update on config) */
1507
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001508 log_tgt_mutex_lock();
1509
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001510 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001511 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001512 continue;
1513
1514 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001515 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001516 return 1;
1517 }
1518
1519 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001520 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001521 return 0;
1522}
1523
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001524/*! @} */