blob: 1a0abc2f3535b16314dfe10a269f8fe6122acd2e [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
Vadim Yanitskiye56f1b62022-01-29 19:37:39 +0600583 && offset > 0 && buf[offset - 1] == '\n') {
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200584 switch (target->print_filename2) {
585 case LOG_FILENAME_NONE:
586 break;
587 case LOG_FILENAME_PATH:
Vadim Yanitskiye56f1b62022-01-29 19:37:39 +0600588 offset--;
Vadim Yanitskiy83f423b2022-01-29 20:34:45 +0600589 len--;
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200590 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
591 if (ret < 0)
592 goto err;
593 OSMO_SNPRINTF_RET(ret, rem, offset, len);
594 break;
595 case LOG_FILENAME_BASENAME:
Vadim Yanitskiye56f1b62022-01-29 19:37:39 +0600596 offset--;
Vadim Yanitskiy83f423b2022-01-29 20:34:45 +0600597 len--;
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200598 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
599 if (ret < 0)
600 goto err;
601 OSMO_SNPRINTF_RET(ret, rem, offset, len);
602 break;
603 }
604 }
605
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200606 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100607 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100608 if (ret < 0)
609 goto err;
610 OSMO_SNPRINTF_RET(ret, rem, offset, len);
611 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200612err:
Pau Espin Pedrolbeaf2a22022-01-04 13:36:18 +0100613 len = OSMO_MIN(buf_len - 1, len);
614 buf[len] = '\0';
Harald Weltea0b57d02020-09-27 17:15:10 +0200615 return len;
616}
617
618/* Format the log line for given target; use a stack buffer and call target->output */
619static void _output(struct log_target *target, unsigned int subsys,
620 unsigned int level, const char *file, int line, int cont,
621 const char *format, va_list ap)
622{
623 char buf[MAX_LOG_SIZE];
624 int rc;
625
626 rc = _output_buf(buf, sizeof(buf), target, subsys, level, file, line, cont, format, ap);
627 if (rc > 0)
628 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800629}
630
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100631/* Catch internal logging category indexes as well as out-of-bounds indexes.
632 * For internal categories, the ID is negative starting with -1; and internal
633 * logging categories are added behind the user categories. For out-of-bounds
634 * indexes, return the index of DLGLOBAL. The returned category index is
635 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
636 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100637static inline int map_subsys(int subsys)
638{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100639 /* Note: comparing signed and unsigned integers */
640
641 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
642 subsys = DLGLOBAL;
643
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100644 if (subsys < 0)
645 subsys = subsys_lib2index(subsys);
646
Neels Hofmeyrca135742016-12-12 14:18:54 +0100647 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100648 subsys = subsys_lib2index(DLGLOBAL);
649
Neels Hofmeyrca135742016-12-12 14:18:54 +0100650 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
651
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100652 return subsys;
653}
654
Maxc65c5b42017-03-15 13:20:23 +0100655static inline bool should_log_to_target(struct log_target *tar, int subsys,
656 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100657{
658 struct log_category *category;
659
660 category = &tar->categories[subsys];
661
662 /* subsystem is not supposed to be logged */
663 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100664 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100665
666 /* Check the global log level */
667 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100668 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100669
670 /* Check the category log level */
671 if (tar->loglevel == 0 && category->loglevel != 0 &&
672 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100673 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100674
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100675 /* Apply filters here... if that becomes messy we will
676 * need to put filters in a list and each filter will
677 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100678 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100679 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100680
681 if (osmo_log_info->filter_fn)
682 return osmo_log_info->filter_fn(&log_context, tar);
683
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100684 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100685 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100686}
687
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200688/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200689 * \param[in] subsys Logging sub-system
690 * \param[in] level Log level
691 * \param[in] file name of source code file
692 * \param[in] cont continuation (1) or new line (0)
693 * \param[in] format format string
694 * \param[in] ap vararg-list containing format string arguments
695 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200696void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200697 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800698{
Harald Welte3ae27582010-03-26 21:24:24 +0800699 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800700
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100701 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200702
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200703 log_tgt_mutex_lock();
704
Harald Welte28222962011-02-18 20:37:04 +0100705 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200706 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800707
Maxc65c5b42017-03-15 13:20:23 +0100708 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800709 continue;
710
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200711 /* According to the manpage, vsnprintf leaves the value of ap
712 * in undefined state. Since _output uses vsnprintf and it may
713 * be called several times, we have to pass a copy of ap. */
714 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100715 if (tar->raw_output)
716 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
717 else
718 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200719 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800720 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200721
722 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800723}
724
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200725/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200726 * \param[in] subsys Logging sub-system
727 * \param[in] file name of source code file
728 * \param[in] cont continuation (1) or new line (0)
729 * \param[in] format format string
730 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200731void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800732 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800733{
734 va_list ap;
735
736 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200737 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800738 va_end(ap);
739}
740
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200741/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200742 * \param[in] subsys Logging sub-system
743 * \param[in] level Log level
744 * \param[in] file name of source code file
745 * \param[in] cont continuation (1) or new line (0)
746 * \param[in] format format string
747 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200748void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800749{
750 va_list ap;
751
Harald Welte433005c2020-09-26 11:51:32 +0200752 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800753 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200754 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200756 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800757}
758
Philipp Maier4c44d462022-01-19 11:43:05 +0100759/* This logging function is used as a fallback when the logging framework is
760 * not is not properly initialized. */
761void logp_stub(const char *file, int line, int cont, const char *format, ...)
762{
763 va_list ap;
764 if (!cont)
765 fprintf(stderr, "%s:%d ", file, line);
766 va_start(ap, format);
767 vfprintf(stderr, format, ap);
768 va_end(ap);
769}
770
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200771/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200772 * \param[in] target Log target to be registered
773 */
Harald Welte3ae27582010-03-26 21:24:24 +0800774void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800775{
Harald Welte28222962011-02-18 20:37:04 +0100776 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800777}
778
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200779/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200780 * \param[in] target Log target to be unregistered
781 */
Harald Welte3ae27582010-03-26 21:24:24 +0800782void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800783{
784 llist_del(&target->entry);
785}
786
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200787/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800788void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800789{
Harald Welte3ae27582010-03-26 21:24:24 +0800790 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800791}
792
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200793/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200794 * \param[in] ctx_nr logging context number
795 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200796 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200797 *
798 * A logging context is something like the subscriber identity to which
799 * the currently processed message relates, or the BTS through which it
800 * was received. As soon as this data is known, it can be set using
801 * this function. The main use of context information is for logging
802 * filters.
803 */
Harald Welte3ae27582010-03-26 21:24:24 +0800804int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800805{
Harald Welte3ae27582010-03-26 21:24:24 +0800806 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800807 return -EINVAL;
808
Harald Welte3ae27582010-03-26 21:24:24 +0800809 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800810
811 return 0;
812}
813
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200814/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200815 * \param[in] target Log target to be affected
816 * \param[in] all enable (1) or disable (0) the ALL filter
817 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100818 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100819 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
820 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200821 */
Harald Welte3ae27582010-03-26 21:24:24 +0800822void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800823{
824 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100825 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800826 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100827 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800828}
829
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200830/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200831 * \param[in] target Log target to be affected
832 * \param[in] use_color Use color (1) or don't use color (0)
833 */
Harald Welte3ae27582010-03-26 21:24:24 +0800834void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800835{
836 target->use_color = use_color;
837}
838
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200839/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200840 * \param[in] target Log target to be affected
841 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
842 */
Harald Welte3ae27582010-03-26 21:24:24 +0800843void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800844{
845 target->print_timestamp = print_timestamp;
846}
847
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200848/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100849 * \param[in] target Log target to be affected
850 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
851 *
852 * When both timestamp and extended timestamp is enabled then only
853 * the extended timestamp will be used. The format of the timestamp
854 * is YYYYMMDDhhmmssnnn.
855 */
856void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
857{
858 target->print_ext_timestamp = print_timestamp;
859}
860
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100861/*! Enable or disable printing of timestamps while logging
862 * \param[in] target Log target to be affected
863 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
864 */
865void log_set_print_tid(struct log_target *target, int print_tid)
866{
867 target->print_tid = print_tid;
868}
869
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100870/*! Use log_set_print_filename2() instead.
871 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
872 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
873 * behavior, which combined the category in hex with the filename. For example, if the category-hex
874 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
875 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200876 * \param[in] target Log target to be affected
877 * \param[in] print_filename Enable (1) or disable (0) filenames
878 */
879void log_set_print_filename(struct log_target *target, int print_filename)
880{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100881 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
882 log_set_print_category_hex(target, print_filename);
883}
884
885/*! Enable or disable printing of the filename while logging.
886 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700887 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100888 * LOG_FILENAME_NONE omits the source file and line information from logs.
889 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
890 */
891void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
892{
893 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200894}
895
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200896/*! Set the position where on a log line the source file info should be logged.
897 * \param[in] target Log target to be affected.
898 * \param[in] pos A LOG_FILENAME_POS_* enum value.
899 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
900 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
901 */
902void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
903{
904 target->print_filename_pos = pos;
905}
906
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200907/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100908 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700909 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100910 *
911 * Print the category/subsys name in front of every log message.
912 */
913void log_set_print_category(struct log_target *target, int print_category)
914{
915 target->print_category = print_category;
916}
917
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100918/*! Enable or disable printing of the category number in hex ('<000b>').
919 * \param[in] target Log target to be affected.
920 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
921 */
922void log_set_print_category_hex(struct log_target *target, int print_category_hex)
923{
924 target->print_category_hex = print_category_hex;
925}
926
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100927/*! Enable or disable printing of the log level name.
928 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700929 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100930 *
931 * Print the log level name in front of every log message.
932 */
933void log_set_print_level(struct log_target *target, int print_level)
934{
935 target->print_level = (bool)print_level;
936}
937
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200938/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200939 * \param[in] target Log target to be affected
940 * \param[in] log_level New global log level
941 */
Harald Welte3ae27582010-03-26 21:24:24 +0800942void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800943{
944 target->loglevel = log_level;
945}
946
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200947/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100948 * \param[in] target Log target to be affected
949 * \param[in] category Log category to be affected
950 * \param[in] enable whether to enable or disable the filter
951 * \param[in] level Log level of the filter
952 */
Harald Welte3ae27582010-03-26 21:24:24 +0800953void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800954 int enable, int level)
955{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100956 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800957 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100958 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800959 target->categories[category].enabled = !!enable;
960 target->categories[category].loglevel = level;
961}
962
Harald Welte44c0f632017-01-15 17:58:29 +0100963#if (!EMBEDDED)
Harald Welte78d73672020-09-29 11:32:35 +0200964/* write-queue tells us we should write another msgb (log line) to the output fd */
965static int _file_wq_write_cb(struct osmo_fd *ofd, struct msgb *msg)
966{
967 int rc;
968
969 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
970 if (rc < 0)
971 return rc;
972 if (rc != msgb_length(msg)) {
973 /* pull the number of bytes we have already written */
974 msgb_pull(msg, rc);
975 /* ask write_queue to re-insert the msgb at the head of the queue */
976 return -EAGAIN;
977 }
978 return 0;
979}
980
981/* output via buffered, blocking stdio streams */
Harald Welteb72867f2020-09-26 21:45:16 +0200982static void _file_output_stream(struct log_target *target, unsigned int level,
983 const char *log)
984{
985 OSMO_ASSERT(target->tgt_file.out);
986 fputs(log, target->tgt_file.out);
987 fflush(target->tgt_file.out);
988}
989
990/* output via non-blocking write_queue, doing internal buffering */
Harald Weltea0b57d02020-09-27 17:15:10 +0200991static void _file_raw_output(struct log_target *target, int subsys, unsigned int level, const char *file,
992 int line, int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800993{
Harald Welteb72867f2020-09-26 21:45:16 +0200994 struct msgb *msg;
Harald Weltea0b57d02020-09-27 17:15:10 +0200995 int rc;
Harald Welteb72867f2020-09-26 21:45:16 +0200996
997 OSMO_ASSERT(target->tgt_file.wqueue);
Harald Weltea0b57d02020-09-27 17:15:10 +0200998 msg = msgb_alloc_c(target->tgt_file.wqueue, MAX_LOG_SIZE, "log_file_msg");
Harald Welteb72867f2020-09-26 21:45:16 +0200999 if (!msg)
1000 return;
1001
1002 /* we simply enqueue the log message to a write queue here, to avoid any blocking
1003 * writes on the output file. The write queue will tell us once the file is writable
1004 * and call _file_wq_write_cb() */
Harald Weltea0b57d02020-09-27 17:15:10 +02001005 rc = _output_buf((char *)msgb_data(msg), msgb_tailroom(msg), target, subsys, level, file, line, cont, format, ap);
1006 msgb_put(msg, rc);
Harald Welte78d73672020-09-29 11:32:35 +02001007
1008 /* attempt a synchronous, non-blocking write, if the write queue is empty */
1009 if (target->tgt_file.wqueue->current_length == 0) {
1010 rc = _file_wq_write_cb(&target->tgt_file.wqueue->bfd, msg);
1011 if (rc == 0) {
1012 /* the write was complete, we can exit early */
1013 msgb_free(msg);
1014 return;
1015 }
1016 }
1017 /* if we reach here, either we already had elements in the write_queue, or the synchronous write
1018 * failed: enqueue the message to the write_queue (backlog) */
Harald Welte9a9627e2021-11-25 13:38:19 +01001019 if (osmo_wqueue_enqueue_quiet(target->tgt_file.wqueue, msg) < 0) {
1020 msgb_free(msg);
1021 /* TODO: increment some counter so we can see that messages were dropped */
1022 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001023}
Harald Welte44c0f632017-01-15 17:58:29 +01001024#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001025
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001026/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001027 * \returns dynamically-allocated log target
1028 * This funcition allocates a \ref log_target and initializes it
1029 * with some default values. The newly created target is not
1030 * registered yet.
1031 */
Harald Welte3ae27582010-03-26 21:24:24 +08001032struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001033{
Harald Welte3ae27582010-03-26 21:24:24 +08001034 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +08001035 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001036
Max68bf16a2018-01-10 17:00:43 +01001037 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001038
Harald Welte3ae27582010-03-26 21:24:24 +08001039 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001040 if (!target)
1041 return NULL;
1042
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +02001043 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +02001044 struct log_category,
1045 osmo_log_info->num_cat);
1046 if (!target->categories) {
1047 talloc_free(target);
1048 return NULL;
1049 }
1050
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001051 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +08001052
1053 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +02001054 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +08001055 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +02001056 cat->enabled = osmo_log_info->cat[i].enabled;
1057 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +08001058 }
1059
1060 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001061 target->use_color = 1;
1062 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +01001063 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001064 target->print_filename2 = LOG_FILENAME_PATH;
1065 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +08001066
1067 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001068 target->loglevel = 0;
1069 return target;
1070}
1071
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001072/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001073 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +08001074struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001075{
Harald Weltea3b844c2010-03-27 00:04:40 +08001076/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +02001077#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +08001078 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001079
Harald Welte3ae27582010-03-26 21:24:24 +08001080 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001081 if (!target)
1082 return NULL;
1083
Harald Welte28222962011-02-18 20:37:04 +01001084 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +02001085 target->tgt_file.out = stderr;
Harald Welteb72867f2020-09-26 21:45:16 +02001086 target->output = _file_output_stream;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001087 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +08001088#else
1089 return NULL;
1090#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001091}
1092
Harald Welte44c0f632017-01-15 17:58:29 +01001093#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001094/*! Create a new file-based log target using buffered, blocking stream output
Harald Welte18fc4652011-08-17 14:14:17 +02001095 * \param[in] fname File name of the new log file
1096 * \returns Log target in case of success, NULL otherwise
1097 */
Harald Welteb72867f2020-09-26 21:45:16 +02001098struct log_target *log_target_create_file_stream(const char *fname)
Harald Welte3086c392010-08-25 19:10:50 +02001099{
1100 struct log_target *target;
1101
1102 target = log_target_create();
1103 if (!target)
1104 return NULL;
1105
Harald Welte28222962011-02-18 20:37:04 +01001106 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +02001107 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001108 if (!target->tgt_file.out) {
1109 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +02001110 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001111 }
Harald Welteb72867f2020-09-26 21:45:16 +02001112 target->output = _file_output_stream;
1113 target->tgt_file.fname = talloc_strdup(target, fname);
Harald Welte3086c392010-08-25 19:10:50 +02001114
Harald Welteb72867f2020-09-26 21:45:16 +02001115 return target;
1116}
1117
1118/*! switch from non-blocking/write-queue to blocking + buffered stream output
1119 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001120 * \return 0 on success; 1 if already switched before; negative on error
1121 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1122 */
Harald Welteb72867f2020-09-26 21:45:16 +02001123int log_target_file_switch_to_stream(struct log_target *target)
1124{
1125 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001126
1127 if (!target)
1128 return -ENODEV;
1129
Harald Welteb72867f2020-09-26 21:45:16 +02001130 if (target->tgt_file.out) {
1131 /* target has already been switched over */
1132 return 1;
1133 }
1134
Harald Welteb72867f2020-09-26 21:45:16 +02001135 wq = target->tgt_file.wqueue;
1136 OSMO_ASSERT(wq);
1137
1138 /* re-open output as stream */
1139 if (target->type == LOG_TGT_TYPE_STDERR)
1140 target->tgt_file.out = stderr;
1141 else
1142 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1143 if (!target->tgt_file.out) {
Harald Welteb72867f2020-09-26 21:45:16 +02001144 return -EIO;
1145 }
1146
1147 /* synchronously write anything left in the queue */
1148 while (!llist_empty(&wq->msg_queue)) {
1149 struct msgb *msg = msgb_dequeue(&wq->msg_queue);
1150 fwrite(msgb_data(msg), msgb_length(msg), 1, target->tgt_file.out);
1151 msgb_free(msg);
1152 }
1153
1154 /* now that everything succeeded, we can finally close the old output fd */
1155 if (target->type == LOG_TGT_TYPE_FILE) {
1156 osmo_fd_unregister(&wq->bfd);
1157 close(wq->bfd.fd);
1158 }
1159
1160 /* release the queue itself */
1161 talloc_free(wq);
1162 target->tgt_file.wqueue = NULL;
1163 target->output = _file_output_stream;
1164 target->raw_output = NULL;
1165
1166 return 0;
1167}
1168
1169/*! switch from blocking + buffered file output to non-blocking write-queue based output.
1170 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001171 * \return 0 on success; 1 if already switched before; negative on error
1172 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1173 */
Harald Welteb72867f2020-09-26 21:45:16 +02001174int log_target_file_switch_to_wqueue(struct log_target *target)
1175{
1176 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001177 int rc;
1178
1179 if (!target)
1180 return -ENODEV;
1181
Harald Welteb72867f2020-09-26 21:45:16 +02001182 if (!target->tgt_file.out) {
1183 /* target has already been switched over */
1184 return 1;
1185 }
1186
Harald Welteb72867f2020-09-26 21:45:16 +02001187 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1188 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1189 * reduce the OOM probability within logging, as the pool is already allocated */
1190 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001191 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001192 if (!wq)
1193 return -ENOMEM;
1194 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1195
1196 fflush(target->tgt_file.out);
1197 if (target->type == LOG_TGT_TYPE_FILE) {
1198 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1199 if (rc < 0) {
Harald Welteb72867f2020-09-26 21:45:16 +02001200 talloc_free(wq);
1201 return -errno;
1202 }
1203 } else {
1204 rc = STDERR_FILENO;
1205 }
1206 wq->bfd.fd = rc;
1207 wq->bfd.when = OSMO_FD_WRITE;
1208 wq->write_cb = _file_wq_write_cb;
1209
1210 rc = osmo_fd_register(&wq->bfd);
1211 if (rc < 0) {
1212 talloc_free(wq);
1213 return -EIO;
1214 }
1215 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001216 target->raw_output = _file_raw_output;
1217 target->output = NULL;
Harald Welte3086c392010-08-25 19:10:50 +02001218
Harald Welteb72867f2020-09-26 21:45:16 +02001219 /* now that everything succeeded, we can finally close the old output stream */
1220 if (target->type == LOG_TGT_TYPE_FILE)
1221 fclose(target->tgt_file.out);
1222 target->tgt_file.out = NULL;
1223
1224 return 0;
1225}
1226
1227/*! Create a new file-based log target using non-blocking write_queue
1228 * \param[in] fname File name of the new log file
1229 * \returns Log target in case of success, NULL otherwise
1230 */
1231struct log_target *log_target_create_file(const char *fname)
1232{
1233 struct log_target *target;
1234 struct osmo_wqueue *wq;
1235 int rc;
1236
1237 target = log_target_create();
1238 if (!target)
1239 return NULL;
1240
1241 target->type = LOG_TGT_TYPE_FILE;
1242 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1243 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1244 * reduce the OOM probability within logging, as the pool is already allocated */
1245 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001246 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001247 if (!wq) {
1248 log_target_destroy(target);
1249 return NULL;
1250 }
1251 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1252 wq->bfd.fd = open(fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1253 if (wq->bfd.fd < 0) {
1254 talloc_free(wq);
1255 log_target_destroy(target);
1256 return NULL;
1257 }
1258 wq->bfd.when = OSMO_FD_WRITE;
1259 wq->write_cb = _file_wq_write_cb;
1260
1261 rc = osmo_fd_register(&wq->bfd);
1262 if (rc < 0) {
1263 talloc_free(wq);
1264 log_target_destroy(target);
1265 return NULL;
1266 }
1267
1268 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001269 target->raw_output = _file_raw_output;
Harald Welte3086c392010-08-25 19:10:50 +02001270 target->tgt_file.fname = talloc_strdup(target, fname);
1271
1272 return target;
1273}
Harald Welte44c0f632017-01-15 17:58:29 +01001274#endif
Harald Welte3086c392010-08-25 19:10:50 +02001275
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001276/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +02001277 * \param[in] type Log target type
1278 * \param[in] fname File name
1279 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001280 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001281 */
Harald Weltee6fb8902022-01-09 12:00:03 +01001282struct log_target *log_target_find(enum log_target_type type, const char *fname)
Harald Welte28222962011-02-18 20:37:04 +01001283{
1284 struct log_target *tgt;
1285
1286 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1287 if (tgt->type != type)
1288 continue;
Maxc90f40a2018-01-11 10:52:28 +01001289 switch (tgt->type) {
1290 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001291 if (!strcmp(fname, tgt->tgt_file.fname))
1292 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001293 break;
1294 case LOG_TGT_TYPE_GSMTAP:
1295 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1296 return tgt;
1297 break;
1298 default:
Harald Welte28222962011-02-18 20:37:04 +01001299 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001300 }
Harald Welte28222962011-02-18 20:37:04 +01001301 }
1302 return NULL;
1303}
1304
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001305/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001306 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001307void log_target_destroy(struct log_target *target)
1308{
Harald Welte3086c392010-08-25 19:10:50 +02001309 /* just in case, to make sure we don't have any references */
1310 log_del_target(target);
1311
Harald Welte44c0f632017-01-15 17:58:29 +01001312#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001313 struct osmo_wqueue *wq;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001314 switch (target->type) {
1315 case LOG_TGT_TYPE_FILE:
Harald Welteb72867f2020-09-26 21:45:16 +02001316 case LOG_TGT_TYPE_STDERR:
1317 if (target->tgt_file.out) {
1318 if (target->type == LOG_TGT_TYPE_FILE)
1319 fclose(target->tgt_file.out);
1320 target->tgt_file.out = NULL;
1321 }
1322 wq = target->tgt_file.wqueue;
1323 if (wq) {
1324 if (wq->bfd.fd >= 0) {
1325 if (target->type == LOG_TGT_TYPE_FILE)
1326 close(wq->bfd.fd);
1327 wq->bfd.fd = -1;
1328 }
1329 osmo_fd_unregister(&wq->bfd);
1330 osmo_wqueue_clear(wq);
1331 talloc_free(wq);
1332 target->tgt_file.wqueue = NULL;
1333 }
1334 talloc_free((void *)target->tgt_file.fname);
1335 target->tgt_file.fname = NULL;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001336 break;
Vadim Yanitskiy2f4186a2021-12-29 21:58:19 +06001337 case LOG_TGT_TYPE_GSMTAP:
1338 gsmtap_source_free(target->tgt_gsmtap.gsmtap_inst);
1339 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001340#ifdef HAVE_SYSLOG_H
1341 case LOG_TGT_TYPE_SYSLOG:
1342 closelog();
1343 break;
1344#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001345 default:
1346 /* make GCC happy */
1347 break;
Harald Welte3086c392010-08-25 19:10:50 +02001348 }
Harald Welte44c0f632017-01-15 17:58:29 +01001349#endif
Harald Welte3086c392010-08-25 19:10:50 +02001350
1351 talloc_free(target);
1352}
1353
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001354/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001355 * \param[in] target log target to re-open
1356 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001357int log_target_file_reopen(struct log_target *target)
1358{
Harald Welteb72867f2020-09-26 21:45:16 +02001359 struct osmo_wqueue *wq;
1360 int rc;
Harald Welte3086c392010-08-25 19:10:50 +02001361
Harald Welteb72867f2020-09-26 21:45:16 +02001362 OSMO_ASSERT(target->type == LOG_TGT_TYPE_FILE || target->type == LOG_TGT_TYPE_STDERR);
1363 OSMO_ASSERT(target->tgt_file.out || target->tgt_file.wqueue);
Harald Welte3086c392010-08-25 19:10:50 +02001364
Harald Welteb72867f2020-09-26 21:45:16 +02001365 if (target->tgt_file.out) {
1366 fclose(target->tgt_file.out);
1367 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1368 if (!target->tgt_file.out)
1369 return -errno;
1370 } else {
1371 wq = target->tgt_file.wqueue;
1372 osmo_fd_unregister(&wq->bfd);
1373 if (wq->bfd.fd >= 0) {
1374 close(wq->bfd.fd);
1375 wq->bfd.fd = -1;
1376 }
1377
1378 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1379 if (rc < 0)
1380 return -errno;
1381 wq->bfd.fd = rc;
1382 rc = osmo_fd_register(&wq->bfd);
1383 if (rc < 0)
1384 return rc;
1385 }
Harald Welte3086c392010-08-25 19:10:50 +02001386
1387 return 0;
1388}
1389
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001390/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001391 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001392int log_targets_reopen(void)
1393{
1394 struct log_target *tar;
1395 int rc = 0;
1396
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001397 log_tgt_mutex_lock();
1398
Harald Welte4de854d2013-03-18 19:01:40 +01001399 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1400 switch (tar->type) {
1401 case LOG_TGT_TYPE_FILE:
1402 if (log_target_file_reopen(tar) < 0)
1403 rc = -1;
1404 break;
1405 default:
1406 break;
1407 }
1408 }
1409
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001410 log_tgt_mutex_unlock();
1411
Harald Welte4de854d2013-03-18 19:01:40 +01001412 return rc;
1413}
1414
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001415/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001416 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001417 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001418 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001419 *
1420 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001421 */
Harald Welteb43bc042011-06-27 10:29:17 +02001422int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001423{
Harald Welteb43bc042011-06-27 10:29:17 +02001424 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001425 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001426
Philipp Maierdc02c062020-05-12 17:51:25 +02001427 /* Ensure that log_init is not called multiple times */
1428 OSMO_ASSERT(tall_log_ctx == NULL)
1429
Harald Welteb43bc042011-06-27 10:29:17 +02001430 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1431 if (!tall_log_ctx)
1432 return -ENOMEM;
1433
1434 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1435 if (!osmo_log_info)
1436 return -ENOMEM;
1437
Max72dfd432018-12-04 11:24:18 +01001438 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1439
1440 if (inf) {
1441 osmo_log_info->filter_fn = inf->filter_fn;
1442 osmo_log_info->num_cat_user = inf->num_cat;
1443 osmo_log_info->num_cat += inf->num_cat;
1444 }
Harald Welteb43bc042011-06-27 10:29:17 +02001445
Philipp Maierdcad1c52020-03-25 11:25:59 +01001446 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1447 osmo_log_info->num_cat);
1448 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001449 talloc_free(osmo_log_info);
1450 osmo_log_info = NULL;
1451 return -ENOMEM;
1452 }
1453
Philipp Maierdcad1c52020-03-25 11:25:59 +01001454 /* copy over the user part and sanitize loglevel */
1455 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001456 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001457 memcpy(&cat_ptr[i], &inf->cat[i],
1458 sizeof(struct log_info_cat));
1459
1460 /* Make sure that the loglevel is set to NOTICE in case
1461 * no loglevel has been preset. */
1462 if (!cat_ptr[i].loglevel) {
1463 cat_ptr[i].loglevel = LOGL_NOTICE;
1464 }
Max72dfd432018-12-04 11:24:18 +01001465 }
Harald Welteb43bc042011-06-27 10:29:17 +02001466 }
1467
1468 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001469 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001470 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001471 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001472 }
1473
Philipp Maierdcad1c52020-03-25 11:25:59 +01001474 osmo_log_info->cat = cat_ptr;
1475
Harald Welte9fe16522011-06-27 14:00:03 +02001476 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001477}
Harald Welte18fc4652011-08-17 14:14:17 +02001478
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001479/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001480 * This function destroys all targets and releases associated memory */
1481void log_fini(void)
1482{
1483 struct log_target *tar, *tar2;
1484
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001485 log_tgt_mutex_lock();
1486
Harald Welte69e6c3c2016-04-20 10:41:27 +02001487 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1488 log_target_destroy(tar);
1489
1490 talloc_free(osmo_log_info);
1491 osmo_log_info = NULL;
1492 talloc_free(tall_log_ctx);
1493 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001494
1495 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001496}
1497
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001498/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001499 * \returns != 0 if a log entry might get generated by at least one target */
1500int log_check_level(int subsys, unsigned int level)
1501{
1502 struct log_target *tar;
1503
Max68bf16a2018-01-10 17:00:43 +01001504 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001505
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001506 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001507
1508 /* TODO: The following could/should be cached (update on config) */
1509
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001510 log_tgt_mutex_lock();
1511
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001512 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001513 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001514 continue;
1515
1516 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001517 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001518 return 1;
1519 }
1520
1521 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001522 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001523 return 0;
1524}
1525
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001526/*! @} */