blob: 24b5553deb000128a04538371c2912dac7d5d599 [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>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080070
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010071#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
72
Harald Weltea8b1b212020-09-27 17:21:07 +020073/* maximum length of the log string of a single log event (typically line) */
74#define MAX_LOG_SIZE 4096
75
Harald Welteb72867f2020-09-26 21:45:16 +020076/* maximum number of log statements we queue in file/stderr target write queue */
Harald Weltea0b57d02020-09-27 17:15:10 +020077#define LOG_WQUEUE_LEN 156
Harald Welteb72867f2020-09-26 21:45:16 +020078
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010079osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010080 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010081osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010082 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010083osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010084 enum_logging_filters_fit_in_log_target_filter_map);
85
Harald Welteb43bc042011-06-27 10:29:17 +020086struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080087
Harald Welte3ae27582010-03-26 21:24:24 +080088static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020089void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010090LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080091
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010092static __thread long int logging_tid;
93
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020094#if (!EMBEDDED)
95/*! This mutex must be held while using osmo_log_target_list or any of its
96 log_targets in a multithread program. Prevents race conditions between threads
97 like producing unordered timestamps or VTY deleting a target while another
98 thread is writing to it */
99static pthread_mutex_t osmo_log_tgt_mutex;
100static bool osmo_log_tgt_mutex_on = false;
101
102/*! Enable multithread support (mutex) in libosmocore logging system.
103 * Must be called by processes willing to use logging subsystem from several
104 * threads. Once enabled, it's not possible to disable it again.
105 */
106void log_enable_multithread(void) {
107 if (osmo_log_tgt_mutex_on)
108 return;
109 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
110 osmo_log_tgt_mutex_on = true;
111}
112
113/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
114 * macro log_tgt_mutex_lock() instead.
115 */
116void log_tgt_mutex_lock_impl(void) {
117 /* These lines are useful to debug scenarios where there's only 1 thread
118 and a double lock appears, for instance during startup and some
119 unlock() missing somewhere:
120 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
121 osmo_panic("acquiring already locked mutex!\n");
122 return;
123 */
124
125 if (osmo_log_tgt_mutex_on)
126 pthread_mutex_lock(&osmo_log_tgt_mutex);
127}
128
129/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
130 * macro log_tgt_mutex_unlock() instead.
131 */
132void log_tgt_mutex_unlock_impl(void) {
133 if (osmo_log_tgt_mutex_on)
134 pthread_mutex_unlock(&osmo_log_tgt_mutex);
135}
136
137#else /* if (!EMBEDDED) */
138#pragma message ("logging multithread support disabled in embedded build")
139void log_enable_multithread(void) {}
140void log_tgt_mutex_lock_impl(void) {}
141void log_tgt_mutex_unlock_impl(void) {}
142#endif /* if (!EMBEDDED) */
143
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200144const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800145 { LOGL_DEBUG, "DEBUG" },
146 { LOGL_INFO, "INFO" },
147 { LOGL_NOTICE, "NOTICE" },
148 { LOGL_ERROR, "ERROR" },
149 { LOGL_FATAL, "FATAL" },
150 { 0, NULL },
151};
152
Harald Welte4c0e1a12020-12-07 22:03:09 +0100153/* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
Harald Welteb43bc042011-06-27 10:29:17 +0200154#define INT2IDX(x) (-1*(x)-1)
155static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
156 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
157 .name = "DLGLOBAL",
158 .description = "Library-internal global log family",
159 .loglevel = LOGL_NOTICE,
160 .enabled = 1,
161 },
root8a996b42011-09-26 11:22:21 +0200162 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
163 .name = "DLLAPD",
164 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200165 .loglevel = LOGL_NOTICE,
166 .enabled = 1,
Pau Espin Pedrol1573add2021-10-14 18:13:48 +0200167 .color = "\033[38;5;12m",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200168 },
Harald Welte892e6212011-07-19 14:31:44 +0200169 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200170 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200171 .description = "A-bis Intput Subsystem",
172 .loglevel = LOGL_NOTICE,
173 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100174 .color = "\033[38;5;23m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200175 },
Harald Welte892e6212011-07-19 14:31:44 +0200176 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200177 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200178 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
179 .loglevel = LOGL_NOTICE,
180 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100181 .color = "\033[38;5;25m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200182 },
Harald Welte892e6212011-07-19 14:31:44 +0200183 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200184 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200185 .description = "A-bis Input Driver for Signalling",
186 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100187 .color = "\033[38;5;27m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200188 },
Harald Welte892e6212011-07-19 14:31:44 +0200189 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200190 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200191 .description = "A-bis Input Driver for B-Channels (voice)",
192 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100193 .color = "\033[38;5;29m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200194 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200195 [INT2IDX(DLSMS)] = {
196 .name = "DLSMS",
197 .description = "Layer3 Short Message Service (SMS)",
198 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100199 .color = "\033[38;5;31m",
Andreas Eversbergc626da92011-10-28 03:53:50 +0200200 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200201 [INT2IDX(DLCTRL)] = {
202 .name = "DLCTRL",
203 .description = "Control Interface",
204 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100205 .color = "\033[38;5;33m",
Harald Welte7fd0c832014-08-20 19:58:13 +0200206 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100207 [INT2IDX(DLGTP)] = {
208 .name = "DLGTP",
209 .description = "GPRS GTP library",
210 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100211 .color = "\033[38;5;35m",
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100212 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100213 [INT2IDX(DLSTATS)] = {
214 .name = "DLSTATS",
215 .description = "Statistics messages and logging",
216 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100217 .color = "\033[38;5;37m",
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100218 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100219 [INT2IDX(DLGSUP)] = {
220 .name = "DLGSUP",
221 .description = "Generic Subscriber Update Protocol",
222 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100223 .color = "\033[38;5;39m",
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100224 },
Harald Weltec0f00072016-04-27 18:32:35 +0200225 [INT2IDX(DLOAP)] = {
226 .name = "DLOAP",
227 .description = "Osmocom Authentication Protocol",
228 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100229 .color = "\033[38;5;41m",
Harald Weltec0f00072016-04-27 18:32:35 +0200230 },
Harald Welte059c4042017-04-03 22:20:49 +0200231 [INT2IDX(DLSS7)] = {
232 .name = "DLSS7",
233 .description = "libosmo-sigtran Signalling System 7",
234 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100235 .color = "\033[38;5;43m",
Harald Welte059c4042017-04-03 22:20:49 +0200236 },
237 [INT2IDX(DLSCCP)] = {
238 .name = "DLSCCP",
239 .description = "libosmo-sigtran SCCP Implementation",
240 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100241 .color = "\033[38;5;45m",
Harald Welte059c4042017-04-03 22:20:49 +0200242 },
243 [INT2IDX(DLSUA)] = {
244 .name = "DLSUA",
245 .description = "libosmo-sigtran SCCP User Adaptation",
246 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100247 .color = "\033[38;5;47m",
Harald Welte059c4042017-04-03 22:20:49 +0200248 },
249 [INT2IDX(DLM3UA)] = {
250 .name = "DLM3UA",
251 .description = "libosmo-sigtran MTP3 User Adaptation",
252 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100253 .color = "\033[38;5;49m",
Harald Welte059c4042017-04-03 22:20:49 +0200254 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200255 [INT2IDX(DLMGCP)] = {
256 .name = "DLMGCP",
257 .description = "libosmo-mgcp Media Gateway Control Protocol",
258 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100259 .color = "\033[38;5;51m",
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200260 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100261 [INT2IDX(DLJIBUF)] = {
262 .name = "DLJIBUF",
263 .description = "libosmo-netif Jitter Buffer",
264 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100265 .color = "\033[38;5;53m",
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100266 },
Max450f5ac2019-02-14 19:12:03 +0100267 [INT2IDX(DLRSPRO)] = {
268 .name = "DLRSPRO",
269 .description = "Remote SIM protocol",
270 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100271 .color = "\033[38;5;55m",
Max450f5ac2019-02-14 19:12:03 +0100272 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200273 [INT2IDX(DLNS)] = {
274 .name = "DLNS",
275 .description = "GPRS NS layer",
276 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100277 .color = "\033[38;5;57m",
Alexander Couzens6a161492020-07-12 13:45:50 +0200278 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100279 [INT2IDX(DLBSSGP)] = {
280 .name = "DLBSSGP",
281 .description = "GPRS BSSGP layer",
282 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100283 .color = "\033[38;5;59m",
Harald Weltefde19ed2020-12-07 21:43:51 +0100284 },
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100285 [INT2IDX(DLNSDATA)] = {
286 .name = "DLNSDATA",
287 .description = "GPRS NS layer data PDU",
288 .enabled = 1, .loglevel = LOGL_NOTICE,
289 .color = "\033[38;5;61m",
290 },
291 [INT2IDX(DLNSSIGNAL)] = {
292 .name = "DLNSSIGNAL",
293 .description = "GPRS NS layer signal PDU",
294 .enabled = 1, .loglevel = LOGL_NOTICE,
295 .color = "\033[38;5;63m",
296 },
Harald Welte9fe1f9f2018-11-29 13:47:39 +0100297 [INT2IDX(DLIUUP)] = {
298 .name = "DLIUUP",
299 .description = "Iu UP layer",
300 .enabled = 1, .loglevel = LOGL_NOTICE,
301 .color = "\033[38;5;65m",
302 },
Harald Welteb43bc042011-06-27 10:29:17 +0200303};
304
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200305void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100306{
307 if (!osmo_log_info) {
308 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100309 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100310 OSMO_ASSERT(osmo_log_info);
311 }
312}
313
Harald Welteb43bc042011-06-27 10:29:17 +0200314/* special magic for negative (library-internal) log subsystem numbers */
315static int subsys_lib2index(int subsys)
316{
317 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
318}
319
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200320/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700321 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200322 * \returns numeric log level
323 */
Harald Welte3ae27582010-03-26 21:24:24 +0800324int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800325{
326 return get_string_value(loglevel_strs, lvl);
327}
328
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200329/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700330 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200331 * \returns zero-terminated string (log level name)
332 */
Harald Welte9ac22252010-05-11 11:19:40 +0200333const char *log_level_str(unsigned int lvl)
334{
335 return get_value_string(loglevel_strs, lvl);
336}
337
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200338/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200339 * \param[in] category human-readable log category name
340 * \returns numeric category value, or -EINVAL otherwise
341 */
Harald Welte3ae27582010-03-26 21:24:24 +0800342int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800343{
344 int i;
345
Max68bf16a2018-01-10 17:00:43 +0100346 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100347
Harald Welte4ebdf742010-05-19 19:54:00 +0200348 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200349 if (osmo_log_info->cat[i].name == NULL)
350 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200351 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800352 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800353 }
354
355 return -EINVAL;
356}
357
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200358/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200359 * \param[in] target log target to be configured
360 * \param[in] _mask log category mask string
361 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800362 * The format can be this: category1:category2:category3
363 * or category1,2:category2,3:...
364 */
Harald Welte3ae27582010-03-26 21:24:24 +0800365void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800366{
367 int i = 0;
368 char *mask = strdup(_mask);
369 char *category_token = NULL;
370
Max68bf16a2018-01-10 17:00:43 +0100371 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100372
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800373 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200374 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800375 target->categories[i].enabled = 0;
376
377 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200378 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800379 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200380 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200381 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800382 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200383
384 if (!osmo_log_info->cat[i].name)
385 continue;
386
387 length = strlen(category_token);
388 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200389
390 /* Use longest length not to match subocurrences. */
391 if (cat_length > length)
392 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800393
394 if (colon)
395 length = colon - category_token;
396
Harald Welte4ebdf742010-05-19 19:54:00 +0200397 if (strncasecmp(osmo_log_info->cat[i].name,
398 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800399 int level = 0;
400
401 if (colon)
402 level = atoi(colon+1);
403
Harald Weltefaadfe22010-03-26 21:05:43 +0800404 target->categories[i].enabled = 1;
405 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800406 }
407 }
408 } while ((category_token = strtok(NULL, ":")));
409
410 free(mask);
411}
412
413static const char* color(int subsys)
414{
Harald Welte4ebdf742010-05-19 19:54:00 +0200415 if (subsys < osmo_log_info->num_cat)
416 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800417
Harald Welted788f662010-03-26 09:45:03 +0800418 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800419}
420
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100421static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100422 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
423 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
424 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
425 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
426 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100427 { 0, NULL }
428};
429
430static const char *level_color(int level)
431{
432 const char *c = get_value_string_or_null(level_colors, level);
433 if (!c)
434 return get_value_string(level_colors, LOGL_FATAL);
435 return c;
436}
437
Harald Welteaa00f992016-12-02 15:30:02 +0100438const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100439{
440 if (subsys < osmo_log_info->num_cat)
441 return osmo_log_info->cat[subsys].name;
442
443 return NULL;
444}
445
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100446static const char *const_basename(const char *path)
447{
448 const char *bn = strrchr(path, '/');
449 if (!bn || !bn[1])
450 return path;
451 return bn + 1;
452}
453
Harald Weltea0b57d02020-09-27 17:15:10 +0200454/*! main output formatting function for log lines.
455 * \param[out] buf caller-allocated output buffer for the generated string
456 * \param[in] buf_len number of bytes available in buf
457 * \param[in] target log target for which the string is to be formatted
458 * \param[in] subsys Log sub-system number
459 * \param[in] level Log level
460 * \param[in] file name of source code file generating the log
461 * \param[in] line line source code line number within 'file' generating the log
462 * \param[in] cont is this a continuation (true) or not (false)
463 * \param[in] format format string
464 * \param[in] ap variable argument list for format
465 * \returns number of bytes written to out */
466static int _output_buf(char *buf, int buf_len, struct log_target *target, unsigned int subsys,
467 unsigned int level, const char *file, int line, int cont,
468 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800469{
Harald Weltea0b57d02020-09-27 17:15:10 +0200470 int ret, len = 0, offset = 0, rem = buf_len;
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100471 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800472
473 /* are we using color */
474 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100475 c_subsys = color(subsys);
476 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100477 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200478 if (ret < 0)
479 goto err;
480 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800481 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800482 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800483 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100484 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200485#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100486 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100487 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200488 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100489 localtime_r(&tv.tv_sec, &tm);
490 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100491 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100492 tm.tm_hour, tm.tm_min, tm.tm_sec,
493 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100494 if (ret < 0)
495 goto err;
496 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200497#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100498 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800499 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200500 if ((tm = time(NULL)) == (time_t) -1)
501 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200502 /* Get human-readable representation of time.
503 man ctime: we need at least 26 bytes in buf */
504 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200505 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200506 ret = strlen(buf + offset);
507 if (ret <= 0)
508 goto err;
509 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
510 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200511 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800512 }
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100513 if (target->print_tid) {
514 if (logging_tid == 0)
515 logging_tid = (long int)osmo_gettid();
516 ret = snprintf(buf + offset, rem, "%ld ", logging_tid);
517 if (ret < 0)
518 goto err;
519 OSMO_SNPRINTF_RET(ret, rem, offset, len);
520 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100521 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100522 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
523 target->use_color ? level_color(level) : "",
524 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100525 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100526 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100527 if (ret < 0)
528 goto err;
529 OSMO_SNPRINTF_RET(ret, rem, offset, len);
530 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100531 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100532 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
533 target->use_color ? level_color(level) : "",
534 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100535 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100536 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100537 if (ret < 0)
538 goto err;
539 OSMO_SNPRINTF_RET(ret, rem, offset, len);
540 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100541 if (target->print_category_hex) {
542 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200543 if (ret < 0)
544 goto err;
545 OSMO_SNPRINTF_RET(ret, rem, offset, len);
546 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200547
548 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
549 switch (target->print_filename2) {
550 case LOG_FILENAME_NONE:
551 break;
552 case LOG_FILENAME_PATH:
553 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
554 if (ret < 0)
555 goto err;
556 OSMO_SNPRINTF_RET(ret, rem, offset, len);
557 break;
558 case LOG_FILENAME_BASENAME:
559 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
560 if (ret < 0)
561 goto err;
562 OSMO_SNPRINTF_RET(ret, rem, offset, len);
563 break;
564 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100565 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800566 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200567 ret = vsnprintf(buf + offset, rem, format, ap);
568 if (ret < 0)
569 goto err;
570 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800571
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200572 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
573 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
574 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
575 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
576 && offset > 0 && buf[offset-1] == '\n') {
577 switch (target->print_filename2) {
578 case LOG_FILENAME_NONE:
579 break;
580 case LOG_FILENAME_PATH:
581 offset --;
582 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
583 if (ret < 0)
584 goto err;
585 OSMO_SNPRINTF_RET(ret, rem, offset, len);
586 break;
587 case LOG_FILENAME_BASENAME:
588 offset --;
589 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
590 if (ret < 0)
591 goto err;
592 OSMO_SNPRINTF_RET(ret, rem, offset, len);
593 break;
594 }
595 }
596
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200597 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100598 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100599 if (ret < 0)
600 goto err;
601 OSMO_SNPRINTF_RET(ret, rem, offset, len);
602 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200603err:
Pau Espin Pedrolbeaf2a22022-01-04 13:36:18 +0100604 len = OSMO_MIN(buf_len - 1, len);
605 buf[len] = '\0';
Harald Weltea0b57d02020-09-27 17:15:10 +0200606 return len;
607}
608
609/* Format the log line for given target; use a stack buffer and call target->output */
610static void _output(struct log_target *target, unsigned int subsys,
611 unsigned int level, const char *file, int line, int cont,
612 const char *format, va_list ap)
613{
614 char buf[MAX_LOG_SIZE];
615 int rc;
616
617 rc = _output_buf(buf, sizeof(buf), target, subsys, level, file, line, cont, format, ap);
618 if (rc > 0)
619 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800620}
621
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100622/* Catch internal logging category indexes as well as out-of-bounds indexes.
623 * For internal categories, the ID is negative starting with -1; and internal
624 * logging categories are added behind the user categories. For out-of-bounds
625 * indexes, return the index of DLGLOBAL. The returned category index is
626 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
627 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100628static inline int map_subsys(int subsys)
629{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100630 /* Note: comparing signed and unsigned integers */
631
632 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
633 subsys = DLGLOBAL;
634
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100635 if (subsys < 0)
636 subsys = subsys_lib2index(subsys);
637
Neels Hofmeyrca135742016-12-12 14:18:54 +0100638 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100639 subsys = subsys_lib2index(DLGLOBAL);
640
Neels Hofmeyrca135742016-12-12 14:18:54 +0100641 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
642
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100643 return subsys;
644}
645
Maxc65c5b42017-03-15 13:20:23 +0100646static inline bool should_log_to_target(struct log_target *tar, int subsys,
647 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100648{
649 struct log_category *category;
650
651 category = &tar->categories[subsys];
652
653 /* subsystem is not supposed to be logged */
654 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100655 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100656
657 /* Check the global log level */
658 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100659 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100660
661 /* Check the category log level */
662 if (tar->loglevel == 0 && category->loglevel != 0 &&
663 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100664 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100665
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100666 /* Apply filters here... if that becomes messy we will
667 * need to put filters in a list and each filter will
668 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100669 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100670 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100671
672 if (osmo_log_info->filter_fn)
673 return osmo_log_info->filter_fn(&log_context, tar);
674
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100675 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100676 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100677}
678
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200679/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200680 * \param[in] subsys Logging sub-system
681 * \param[in] level Log level
682 * \param[in] file name of source code file
683 * \param[in] cont continuation (1) or new line (0)
684 * \param[in] format format string
685 * \param[in] ap vararg-list containing format string arguments
686 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200687void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200688 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800689{
Harald Welte3ae27582010-03-26 21:24:24 +0800690 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800691
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100692 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200693
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200694 log_tgt_mutex_lock();
695
Harald Welte28222962011-02-18 20:37:04 +0100696 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200697 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800698
Maxc65c5b42017-03-15 13:20:23 +0100699 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800700 continue;
701
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200702 /* According to the manpage, vsnprintf leaves the value of ap
703 * in undefined state. Since _output uses vsnprintf and it may
704 * be called several times, we have to pass a copy of ap. */
705 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100706 if (tar->raw_output)
707 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
708 else
709 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200710 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800711 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200712
713 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800714}
715
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200716/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200717 * \param[in] subsys Logging sub-system
718 * \param[in] file name of source code file
719 * \param[in] cont continuation (1) or new line (0)
720 * \param[in] format format string
721 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200722void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800723 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800724{
725 va_list ap;
726
727 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200728 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800729 va_end(ap);
730}
731
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200732/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200733 * \param[in] subsys Logging sub-system
734 * \param[in] level Log level
735 * \param[in] file name of source code file
736 * \param[in] cont continuation (1) or new line (0)
737 * \param[in] format format string
738 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200739void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800740{
741 va_list ap;
742
Harald Welte433005c2020-09-26 11:51:32 +0200743 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800744 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200745 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800746 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200747 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800748}
749
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200750/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200751 * \param[in] target Log target to be registered
752 */
Harald Welte3ae27582010-03-26 21:24:24 +0800753void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800754{
Harald Welte28222962011-02-18 20:37:04 +0100755 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800756}
757
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200758/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200759 * \param[in] target Log target to be unregistered
760 */
Harald Welte3ae27582010-03-26 21:24:24 +0800761void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800762{
763 llist_del(&target->entry);
764}
765
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200766/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800767void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800768{
Harald Welte3ae27582010-03-26 21:24:24 +0800769 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800770}
771
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200772/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200773 * \param[in] ctx_nr logging context number
774 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200775 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200776 *
777 * A logging context is something like the subscriber identity to which
778 * the currently processed message relates, or the BTS through which it
779 * was received. As soon as this data is known, it can be set using
780 * this function. The main use of context information is for logging
781 * filters.
782 */
Harald Welte3ae27582010-03-26 21:24:24 +0800783int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800784{
Harald Welte3ae27582010-03-26 21:24:24 +0800785 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800786 return -EINVAL;
787
Harald Welte3ae27582010-03-26 21:24:24 +0800788 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800789
790 return 0;
791}
792
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200793/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200794 * \param[in] target Log target to be affected
795 * \param[in] all enable (1) or disable (0) the ALL filter
796 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100797 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100798 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
799 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200800 */
Harald Welte3ae27582010-03-26 21:24:24 +0800801void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800802{
803 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100804 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800805 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100806 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800807}
808
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200809/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200810 * \param[in] target Log target to be affected
811 * \param[in] use_color Use color (1) or don't use color (0)
812 */
Harald Welte3ae27582010-03-26 21:24:24 +0800813void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800814{
815 target->use_color = use_color;
816}
817
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200818/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200819 * \param[in] target Log target to be affected
820 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
821 */
Harald Welte3ae27582010-03-26 21:24:24 +0800822void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800823{
824 target->print_timestamp = print_timestamp;
825}
826
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200827/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100828 * \param[in] target Log target to be affected
829 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
830 *
831 * When both timestamp and extended timestamp is enabled then only
832 * the extended timestamp will be used. The format of the timestamp
833 * is YYYYMMDDhhmmssnnn.
834 */
835void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
836{
837 target->print_ext_timestamp = print_timestamp;
838}
839
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100840/*! Enable or disable printing of timestamps while logging
841 * \param[in] target Log target to be affected
842 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
843 */
844void log_set_print_tid(struct log_target *target, int print_tid)
845{
846 target->print_tid = print_tid;
847}
848
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100849/*! Use log_set_print_filename2() instead.
850 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
851 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
852 * behavior, which combined the category in hex with the filename. For example, if the category-hex
853 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
854 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200855 * \param[in] target Log target to be affected
856 * \param[in] print_filename Enable (1) or disable (0) filenames
857 */
858void log_set_print_filename(struct log_target *target, int print_filename)
859{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100860 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
861 log_set_print_category_hex(target, print_filename);
862}
863
864/*! Enable or disable printing of the filename while logging.
865 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700866 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100867 * LOG_FILENAME_NONE omits the source file and line information from logs.
868 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
869 */
870void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
871{
872 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200873}
874
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200875/*! Set the position where on a log line the source file info should be logged.
876 * \param[in] target Log target to be affected.
877 * \param[in] pos A LOG_FILENAME_POS_* enum value.
878 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
879 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
880 */
881void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
882{
883 target->print_filename_pos = pos;
884}
885
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200886/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100887 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700888 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100889 *
890 * Print the category/subsys name in front of every log message.
891 */
892void log_set_print_category(struct log_target *target, int print_category)
893{
894 target->print_category = print_category;
895}
896
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100897/*! Enable or disable printing of the category number in hex ('<000b>').
898 * \param[in] target Log target to be affected.
899 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
900 */
901void log_set_print_category_hex(struct log_target *target, int print_category_hex)
902{
903 target->print_category_hex = print_category_hex;
904}
905
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100906/*! Enable or disable printing of the log level name.
907 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700908 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100909 *
910 * Print the log level name in front of every log message.
911 */
912void log_set_print_level(struct log_target *target, int print_level)
913{
914 target->print_level = (bool)print_level;
915}
916
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200917/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200918 * \param[in] target Log target to be affected
919 * \param[in] log_level New global log level
920 */
Harald Welte3ae27582010-03-26 21:24:24 +0800921void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800922{
923 target->loglevel = log_level;
924}
925
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200926/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100927 * \param[in] target Log target to be affected
928 * \param[in] category Log category to be affected
929 * \param[in] enable whether to enable or disable the filter
930 * \param[in] level Log level of the filter
931 */
Harald Welte3ae27582010-03-26 21:24:24 +0800932void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800933 int enable, int level)
934{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100935 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800936 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100937 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800938 target->categories[category].enabled = !!enable;
939 target->categories[category].loglevel = level;
940}
941
Harald Welte44c0f632017-01-15 17:58:29 +0100942#if (!EMBEDDED)
Harald Welte78d73672020-09-29 11:32:35 +0200943/* write-queue tells us we should write another msgb (log line) to the output fd */
944static int _file_wq_write_cb(struct osmo_fd *ofd, struct msgb *msg)
945{
946 int rc;
947
948 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
949 if (rc < 0)
950 return rc;
951 if (rc != msgb_length(msg)) {
952 /* pull the number of bytes we have already written */
953 msgb_pull(msg, rc);
954 /* ask write_queue to re-insert the msgb at the head of the queue */
955 return -EAGAIN;
956 }
957 return 0;
958}
959
960/* output via buffered, blocking stdio streams */
Harald Welteb72867f2020-09-26 21:45:16 +0200961static void _file_output_stream(struct log_target *target, unsigned int level,
962 const char *log)
963{
964 OSMO_ASSERT(target->tgt_file.out);
965 fputs(log, target->tgt_file.out);
966 fflush(target->tgt_file.out);
967}
968
969/* output via non-blocking write_queue, doing internal buffering */
Harald Weltea0b57d02020-09-27 17:15:10 +0200970static void _file_raw_output(struct log_target *target, int subsys, unsigned int level, const char *file,
971 int line, int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800972{
Harald Welteb72867f2020-09-26 21:45:16 +0200973 struct msgb *msg;
Harald Weltea0b57d02020-09-27 17:15:10 +0200974 int rc;
Harald Welteb72867f2020-09-26 21:45:16 +0200975
976 OSMO_ASSERT(target->tgt_file.wqueue);
Harald Weltea0b57d02020-09-27 17:15:10 +0200977 msg = msgb_alloc_c(target->tgt_file.wqueue, MAX_LOG_SIZE, "log_file_msg");
Harald Welteb72867f2020-09-26 21:45:16 +0200978 if (!msg)
979 return;
980
981 /* we simply enqueue the log message to a write queue here, to avoid any blocking
982 * writes on the output file. The write queue will tell us once the file is writable
983 * and call _file_wq_write_cb() */
Harald Weltea0b57d02020-09-27 17:15:10 +0200984 rc = _output_buf((char *)msgb_data(msg), msgb_tailroom(msg), target, subsys, level, file, line, cont, format, ap);
985 msgb_put(msg, rc);
Harald Welte78d73672020-09-29 11:32:35 +0200986
987 /* attempt a synchronous, non-blocking write, if the write queue is empty */
988 if (target->tgt_file.wqueue->current_length == 0) {
989 rc = _file_wq_write_cb(&target->tgt_file.wqueue->bfd, msg);
990 if (rc == 0) {
991 /* the write was complete, we can exit early */
992 msgb_free(msg);
993 return;
994 }
995 }
996 /* if we reach here, either we already had elements in the write_queue, or the synchronous write
997 * failed: enqueue the message to the write_queue (backlog) */
Harald Welte9a9627e2021-11-25 13:38:19 +0100998 if (osmo_wqueue_enqueue_quiet(target->tgt_file.wqueue, msg) < 0) {
999 msgb_free(msg);
1000 /* TODO: increment some counter so we can see that messages were dropped */
1001 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001002}
Harald Welte44c0f632017-01-15 17:58:29 +01001003#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001004
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001005/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001006 * \returns dynamically-allocated log target
1007 * This funcition allocates a \ref log_target and initializes it
1008 * with some default values. The newly created target is not
1009 * registered yet.
1010 */
Harald Welte3ae27582010-03-26 21:24:24 +08001011struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001012{
Harald Welte3ae27582010-03-26 21:24:24 +08001013 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +08001014 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001015
Max68bf16a2018-01-10 17:00:43 +01001016 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001017
Harald Welte3ae27582010-03-26 21:24:24 +08001018 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001019 if (!target)
1020 return NULL;
1021
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +02001022 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +02001023 struct log_category,
1024 osmo_log_info->num_cat);
1025 if (!target->categories) {
1026 talloc_free(target);
1027 return NULL;
1028 }
1029
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001030 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +08001031
1032 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +02001033 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +08001034 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +02001035 cat->enabled = osmo_log_info->cat[i].enabled;
1036 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +08001037 }
1038
1039 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001040 target->use_color = 1;
1041 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +01001042 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +01001043 target->print_filename2 = LOG_FILENAME_PATH;
1044 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +08001045
1046 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001047 target->loglevel = 0;
1048 return target;
1049}
1050
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001051/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001052 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +08001053struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001054{
Harald Weltea3b844c2010-03-27 00:04:40 +08001055/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +02001056#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +08001057 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001058
Harald Welte3ae27582010-03-26 21:24:24 +08001059 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001060 if (!target)
1061 return NULL;
1062
Harald Welte28222962011-02-18 20:37:04 +01001063 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +02001064 target->tgt_file.out = stderr;
Harald Welteb72867f2020-09-26 21:45:16 +02001065 target->output = _file_output_stream;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001066 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +08001067#else
1068 return NULL;
1069#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001070}
1071
Harald Welte44c0f632017-01-15 17:58:29 +01001072#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001073/*! Create a new file-based log target using buffered, blocking stream output
Harald Welte18fc4652011-08-17 14:14:17 +02001074 * \param[in] fname File name of the new log file
1075 * \returns Log target in case of success, NULL otherwise
1076 */
Harald Welteb72867f2020-09-26 21:45:16 +02001077struct log_target *log_target_create_file_stream(const char *fname)
Harald Welte3086c392010-08-25 19:10:50 +02001078{
1079 struct log_target *target;
1080
1081 target = log_target_create();
1082 if (!target)
1083 return NULL;
1084
Harald Welte28222962011-02-18 20:37:04 +01001085 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +02001086 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001087 if (!target->tgt_file.out) {
1088 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +02001089 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001090 }
Harald Welteb72867f2020-09-26 21:45:16 +02001091 target->output = _file_output_stream;
1092 target->tgt_file.fname = talloc_strdup(target, fname);
Harald Welte3086c392010-08-25 19:10:50 +02001093
Harald Welteb72867f2020-09-26 21:45:16 +02001094 return target;
1095}
1096
1097/*! switch from non-blocking/write-queue to blocking + buffered stream output
1098 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001099 * \return 0 on success; 1 if already switched before; negative on error
1100 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1101 */
Harald Welteb72867f2020-09-26 21:45:16 +02001102int log_target_file_switch_to_stream(struct log_target *target)
1103{
1104 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001105
1106 if (!target)
1107 return -ENODEV;
1108
Harald Welteb72867f2020-09-26 21:45:16 +02001109 if (target->tgt_file.out) {
1110 /* target has already been switched over */
1111 return 1;
1112 }
1113
Harald Welteb72867f2020-09-26 21:45:16 +02001114 wq = target->tgt_file.wqueue;
1115 OSMO_ASSERT(wq);
1116
1117 /* re-open output as stream */
1118 if (target->type == LOG_TGT_TYPE_STDERR)
1119 target->tgt_file.out = stderr;
1120 else
1121 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1122 if (!target->tgt_file.out) {
Harald Welteb72867f2020-09-26 21:45:16 +02001123 return -EIO;
1124 }
1125
1126 /* synchronously write anything left in the queue */
1127 while (!llist_empty(&wq->msg_queue)) {
1128 struct msgb *msg = msgb_dequeue(&wq->msg_queue);
1129 fwrite(msgb_data(msg), msgb_length(msg), 1, target->tgt_file.out);
1130 msgb_free(msg);
1131 }
1132
1133 /* now that everything succeeded, we can finally close the old output fd */
1134 if (target->type == LOG_TGT_TYPE_FILE) {
1135 osmo_fd_unregister(&wq->bfd);
1136 close(wq->bfd.fd);
1137 }
1138
1139 /* release the queue itself */
1140 talloc_free(wq);
1141 target->tgt_file.wqueue = NULL;
1142 target->output = _file_output_stream;
1143 target->raw_output = NULL;
1144
1145 return 0;
1146}
1147
1148/*! switch from blocking + buffered file output to non-blocking write-queue based output.
1149 * \param[in] target log target which we should switch
Pau Espin Pedrol843a84c2021-11-03 17:31:47 +01001150 * \return 0 on success; 1 if already switched before; negative on error
1151 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
1152 */
Harald Welteb72867f2020-09-26 21:45:16 +02001153int log_target_file_switch_to_wqueue(struct log_target *target)
1154{
1155 struct osmo_wqueue *wq;
Harald Welteb72867f2020-09-26 21:45:16 +02001156 int rc;
1157
1158 if (!target)
1159 return -ENODEV;
1160
Harald Welteb72867f2020-09-26 21:45:16 +02001161 if (!target->tgt_file.out) {
1162 /* target has already been switched over */
1163 return 1;
1164 }
1165
Harald Welteb72867f2020-09-26 21:45:16 +02001166 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1167 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1168 * reduce the OOM probability within logging, as the pool is already allocated */
1169 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001170 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001171 if (!wq)
1172 return -ENOMEM;
1173 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1174
1175 fflush(target->tgt_file.out);
1176 if (target->type == LOG_TGT_TYPE_FILE) {
1177 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1178 if (rc < 0) {
Harald Welteb72867f2020-09-26 21:45:16 +02001179 talloc_free(wq);
1180 return -errno;
1181 }
1182 } else {
1183 rc = STDERR_FILENO;
1184 }
1185 wq->bfd.fd = rc;
1186 wq->bfd.when = OSMO_FD_WRITE;
1187 wq->write_cb = _file_wq_write_cb;
1188
1189 rc = osmo_fd_register(&wq->bfd);
1190 if (rc < 0) {
1191 talloc_free(wq);
1192 return -EIO;
1193 }
1194 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001195 target->raw_output = _file_raw_output;
1196 target->output = NULL;
Harald Welte3086c392010-08-25 19:10:50 +02001197
Harald Welteb72867f2020-09-26 21:45:16 +02001198 /* now that everything succeeded, we can finally close the old output stream */
1199 if (target->type == LOG_TGT_TYPE_FILE)
1200 fclose(target->tgt_file.out);
1201 target->tgt_file.out = NULL;
1202
1203 return 0;
1204}
1205
1206/*! Create a new file-based log target using non-blocking write_queue
1207 * \param[in] fname File name of the new log file
1208 * \returns Log target in case of success, NULL otherwise
1209 */
1210struct log_target *log_target_create_file(const char *fname)
1211{
1212 struct log_target *target;
1213 struct osmo_wqueue *wq;
1214 int rc;
1215
1216 target = log_target_create();
1217 if (!target)
1218 return NULL;
1219
1220 target->type = LOG_TGT_TYPE_FILE;
1221 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1222 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1223 * reduce the OOM probability within logging, as the pool is already allocated */
1224 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
Harald Weltea0b57d02020-09-27 17:15:10 +02001225 LOG_WQUEUE_LEN*(sizeof(struct msgb)+MAX_LOG_SIZE));
Harald Welteb72867f2020-09-26 21:45:16 +02001226 if (!wq) {
1227 log_target_destroy(target);
1228 return NULL;
1229 }
1230 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1231 wq->bfd.fd = open(fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1232 if (wq->bfd.fd < 0) {
1233 talloc_free(wq);
1234 log_target_destroy(target);
1235 return NULL;
1236 }
1237 wq->bfd.when = OSMO_FD_WRITE;
1238 wq->write_cb = _file_wq_write_cb;
1239
1240 rc = osmo_fd_register(&wq->bfd);
1241 if (rc < 0) {
1242 talloc_free(wq);
1243 log_target_destroy(target);
1244 return NULL;
1245 }
1246
1247 target->tgt_file.wqueue = wq;
Harald Weltea0b57d02020-09-27 17:15:10 +02001248 target->raw_output = _file_raw_output;
Harald Welte3086c392010-08-25 19:10:50 +02001249 target->tgt_file.fname = talloc_strdup(target, fname);
1250
1251 return target;
1252}
Harald Welte44c0f632017-01-15 17:58:29 +01001253#endif
Harald Welte3086c392010-08-25 19:10:50 +02001254
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001255/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +02001256 * \param[in] type Log target type
1257 * \param[in] fname File name
1258 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001259 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001260 */
Harald Welte28222962011-02-18 20:37:04 +01001261struct log_target *log_target_find(int type, const char *fname)
1262{
1263 struct log_target *tgt;
1264
1265 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1266 if (tgt->type != type)
1267 continue;
Maxc90f40a2018-01-11 10:52:28 +01001268 switch (tgt->type) {
1269 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001270 if (!strcmp(fname, tgt->tgt_file.fname))
1271 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001272 break;
1273 case LOG_TGT_TYPE_GSMTAP:
1274 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1275 return tgt;
1276 break;
1277 default:
Harald Welte28222962011-02-18 20:37:04 +01001278 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001279 }
Harald Welte28222962011-02-18 20:37:04 +01001280 }
1281 return NULL;
1282}
1283
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001284/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001285 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001286void log_target_destroy(struct log_target *target)
1287{
Harald Welte3086c392010-08-25 19:10:50 +02001288 /* just in case, to make sure we don't have any references */
1289 log_del_target(target);
1290
Harald Welte44c0f632017-01-15 17:58:29 +01001291#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001292 struct osmo_wqueue *wq;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001293 switch (target->type) {
1294 case LOG_TGT_TYPE_FILE:
Harald Welteb72867f2020-09-26 21:45:16 +02001295 case LOG_TGT_TYPE_STDERR:
1296 if (target->tgt_file.out) {
1297 if (target->type == LOG_TGT_TYPE_FILE)
1298 fclose(target->tgt_file.out);
1299 target->tgt_file.out = NULL;
1300 }
1301 wq = target->tgt_file.wqueue;
1302 if (wq) {
1303 if (wq->bfd.fd >= 0) {
1304 if (target->type == LOG_TGT_TYPE_FILE)
1305 close(wq->bfd.fd);
1306 wq->bfd.fd = -1;
1307 }
1308 osmo_fd_unregister(&wq->bfd);
1309 osmo_wqueue_clear(wq);
1310 talloc_free(wq);
1311 target->tgt_file.wqueue = NULL;
1312 }
1313 talloc_free((void *)target->tgt_file.fname);
1314 target->tgt_file.fname = NULL;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001315 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001316#ifdef HAVE_SYSLOG_H
1317 case LOG_TGT_TYPE_SYSLOG:
1318 closelog();
1319 break;
1320#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001321 default:
1322 /* make GCC happy */
1323 break;
Harald Welte3086c392010-08-25 19:10:50 +02001324 }
Harald Welte44c0f632017-01-15 17:58:29 +01001325#endif
Harald Welte3086c392010-08-25 19:10:50 +02001326
1327 talloc_free(target);
1328}
1329
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001330/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001331 * \param[in] target log target to re-open
1332 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001333int log_target_file_reopen(struct log_target *target)
1334{
Harald Welteb72867f2020-09-26 21:45:16 +02001335 struct osmo_wqueue *wq;
1336 int rc;
Harald Welte3086c392010-08-25 19:10:50 +02001337
Harald Welteb72867f2020-09-26 21:45:16 +02001338 OSMO_ASSERT(target->type == LOG_TGT_TYPE_FILE || target->type == LOG_TGT_TYPE_STDERR);
1339 OSMO_ASSERT(target->tgt_file.out || target->tgt_file.wqueue);
Harald Welte3086c392010-08-25 19:10:50 +02001340
Harald Welteb72867f2020-09-26 21:45:16 +02001341 if (target->tgt_file.out) {
1342 fclose(target->tgt_file.out);
1343 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1344 if (!target->tgt_file.out)
1345 return -errno;
1346 } else {
1347 wq = target->tgt_file.wqueue;
1348 osmo_fd_unregister(&wq->bfd);
1349 if (wq->bfd.fd >= 0) {
1350 close(wq->bfd.fd);
1351 wq->bfd.fd = -1;
1352 }
1353
1354 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1355 if (rc < 0)
1356 return -errno;
1357 wq->bfd.fd = rc;
1358 rc = osmo_fd_register(&wq->bfd);
1359 if (rc < 0)
1360 return rc;
1361 }
Harald Welte3086c392010-08-25 19:10:50 +02001362
1363 return 0;
1364}
1365
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001366/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001367 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001368int log_targets_reopen(void)
1369{
1370 struct log_target *tar;
1371 int rc = 0;
1372
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001373 log_tgt_mutex_lock();
1374
Harald Welte4de854d2013-03-18 19:01:40 +01001375 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1376 switch (tar->type) {
1377 case LOG_TGT_TYPE_FILE:
1378 if (log_target_file_reopen(tar) < 0)
1379 rc = -1;
1380 break;
1381 default:
1382 break;
1383 }
1384 }
1385
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001386 log_tgt_mutex_unlock();
1387
Harald Welte4de854d2013-03-18 19:01:40 +01001388 return rc;
1389}
1390
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001391/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001392 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001393 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001394 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001395 *
1396 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001397 */
Harald Welteb43bc042011-06-27 10:29:17 +02001398int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001399{
Harald Welteb43bc042011-06-27 10:29:17 +02001400 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001401 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001402
Philipp Maierdc02c062020-05-12 17:51:25 +02001403 /* Ensure that log_init is not called multiple times */
1404 OSMO_ASSERT(tall_log_ctx == NULL)
1405
Harald Welteb43bc042011-06-27 10:29:17 +02001406 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1407 if (!tall_log_ctx)
1408 return -ENOMEM;
1409
1410 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1411 if (!osmo_log_info)
1412 return -ENOMEM;
1413
Max72dfd432018-12-04 11:24:18 +01001414 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1415
1416 if (inf) {
1417 osmo_log_info->filter_fn = inf->filter_fn;
1418 osmo_log_info->num_cat_user = inf->num_cat;
1419 osmo_log_info->num_cat += inf->num_cat;
1420 }
Harald Welteb43bc042011-06-27 10:29:17 +02001421
Philipp Maierdcad1c52020-03-25 11:25:59 +01001422 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1423 osmo_log_info->num_cat);
1424 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001425 talloc_free(osmo_log_info);
1426 osmo_log_info = NULL;
1427 return -ENOMEM;
1428 }
1429
Philipp Maierdcad1c52020-03-25 11:25:59 +01001430 /* copy over the user part and sanitize loglevel */
1431 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001432 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001433 memcpy(&cat_ptr[i], &inf->cat[i],
1434 sizeof(struct log_info_cat));
1435
1436 /* Make sure that the loglevel is set to NOTICE in case
1437 * no loglevel has been preset. */
1438 if (!cat_ptr[i].loglevel) {
1439 cat_ptr[i].loglevel = LOGL_NOTICE;
1440 }
Max72dfd432018-12-04 11:24:18 +01001441 }
Harald Welteb43bc042011-06-27 10:29:17 +02001442 }
1443
1444 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001445 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001446 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001447 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001448 }
1449
Philipp Maierdcad1c52020-03-25 11:25:59 +01001450 osmo_log_info->cat = cat_ptr;
1451
Harald Welte9fe16522011-06-27 14:00:03 +02001452 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001453}
Harald Welte18fc4652011-08-17 14:14:17 +02001454
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001455/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001456 * This function destroys all targets and releases associated memory */
1457void log_fini(void)
1458{
1459 struct log_target *tar, *tar2;
1460
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001461 log_tgt_mutex_lock();
1462
Harald Welte69e6c3c2016-04-20 10:41:27 +02001463 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1464 log_target_destroy(tar);
1465
1466 talloc_free(osmo_log_info);
1467 osmo_log_info = NULL;
1468 talloc_free(tall_log_ctx);
1469 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001470
1471 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001472}
1473
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001474/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001475 * \returns != 0 if a log entry might get generated by at least one target */
1476int log_check_level(int subsys, unsigned int level)
1477{
1478 struct log_target *tar;
1479
Max68bf16a2018-01-10 17:00:43 +01001480 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001481
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001482 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001483
1484 /* TODO: The following could/should be cached (update on config) */
1485
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001486 log_tgt_mutex_lock();
1487
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001488 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001489 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001490 continue;
1491
1492 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001493 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001494 return 1;
1495 }
1496
1497 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001498 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001499 return 0;
1500}
1501
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001502/*! @} */