blob: 5c6b993e4615683341cf414e77487f77c7a7f74c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging.c
2 * Debugging/Logging support code. */
3/*
4 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte4a2bb9e2010-03-26 09:33:40 +08005 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +080010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Harald Weltef01b2382017-10-16 13:55:34 +020026/*! \addtogroup logging
Harald Welte18fc4652011-08-17 14:14:17 +020027 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 * libosmocore Logging sub-system
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 *
30 * \file logging.c */
Harald Welte18fc4652011-08-17 14:14:17 +020031
Harald Welte01fd5cb2010-03-26 23:51:31 +080032#include "../config.h"
33
Harald Welte4a2bb9e2010-03-26 09:33:40 +080034#include <stdarg.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
Harald Welteb72867f2020-09-26 21:45:16 +020038#include <unistd.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080039
40#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080041#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080042#endif
Vadim Yanitskiy04f42712020-09-09 04:47:25 +070043
44#ifdef HAVE_SYSLOG_H
45#include <syslog.h>
46#endif
47
Harald Welte433005c2020-09-26 11:51:32 +020048#ifdef HAVE_SYSTEMTAP
49/* include the generated probes header and put markers in code */
50#include "probes.h"
51#define TRACE(probe) probe
52#define TRACE_ENABLED(probe) probe ## _ENABLED()
53#else
54/* Wrap the probe to allow it to be removed when no systemtap available */
55#define TRACE(probe)
56#define TRACE_ENABLED(probe) (0)
57#endif /* HAVE_SYSTEMTAP */
58
Harald Welte4a2bb9e2010-03-26 09:33:40 +080059#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010060#include <sys/time.h>
Harald Welteb72867f2020-09-26 21:45:16 +020061#include <sys/types.h>
62#include <sys/stat.h>
63#include <fcntl.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080064#include <errno.h>
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020065#include <pthread.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080066
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010067#include <osmocom/core/talloc.h>
68#include <osmocom/core/utils.h>
69#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020070#include <osmocom/core/timer.h>
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010071#include <osmocom/core/thread.h>
Harald Welteb72867f2020-09-26 21:45:16 +020072#include <osmocom/core/select.h>
73#include <osmocom/core/write_queue.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080074
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010075#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
76
Harald Weltea8b1b212020-09-27 17:21:07 +020077/* maximum length of the log string of a single log event (typically line) */
78#define MAX_LOG_SIZE 4096
79
Harald Welteb72867f2020-09-26 21:45:16 +020080/* maximum number of log statements we queue in file/stderr target write queue */
81#define LOG_WQUEUE_LEN 1024
82
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010083osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010084 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010085osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010086 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010087osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010088 enum_logging_filters_fit_in_log_target_filter_map);
89
Harald Welteb43bc042011-06-27 10:29:17 +020090struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080091
Harald Welte3ae27582010-03-26 21:24:24 +080092static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020093void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010094LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080095
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +010096static __thread long int logging_tid;
97
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020098#if (!EMBEDDED)
99/*! This mutex must be held while using osmo_log_target_list or any of its
100 log_targets in a multithread program. Prevents race conditions between threads
101 like producing unordered timestamps or VTY deleting a target while another
102 thread is writing to it */
103static pthread_mutex_t osmo_log_tgt_mutex;
104static bool osmo_log_tgt_mutex_on = false;
105
106/*! Enable multithread support (mutex) in libosmocore logging system.
107 * Must be called by processes willing to use logging subsystem from several
108 * threads. Once enabled, it's not possible to disable it again.
109 */
110void log_enable_multithread(void) {
111 if (osmo_log_tgt_mutex_on)
112 return;
113 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
114 osmo_log_tgt_mutex_on = true;
115}
116
117/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
118 * macro log_tgt_mutex_lock() instead.
119 */
120void log_tgt_mutex_lock_impl(void) {
121 /* These lines are useful to debug scenarios where there's only 1 thread
122 and a double lock appears, for instance during startup and some
123 unlock() missing somewhere:
124 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
125 osmo_panic("acquiring already locked mutex!\n");
126 return;
127 */
128
129 if (osmo_log_tgt_mutex_on)
130 pthread_mutex_lock(&osmo_log_tgt_mutex);
131}
132
133/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
134 * macro log_tgt_mutex_unlock() instead.
135 */
136void log_tgt_mutex_unlock_impl(void) {
137 if (osmo_log_tgt_mutex_on)
138 pthread_mutex_unlock(&osmo_log_tgt_mutex);
139}
140
141#else /* if (!EMBEDDED) */
142#pragma message ("logging multithread support disabled in embedded build")
143void log_enable_multithread(void) {}
144void log_tgt_mutex_lock_impl(void) {}
145void log_tgt_mutex_unlock_impl(void) {}
146#endif /* if (!EMBEDDED) */
147
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200148const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800149 { LOGL_DEBUG, "DEBUG" },
150 { LOGL_INFO, "INFO" },
151 { LOGL_NOTICE, "NOTICE" },
152 { LOGL_ERROR, "ERROR" },
153 { LOGL_FATAL, "FATAL" },
154 { 0, NULL },
155};
156
Harald Welte4c0e1a12020-12-07 22:03:09 +0100157/* 256 color palette see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit */
Harald Welteb43bc042011-06-27 10:29:17 +0200158#define INT2IDX(x) (-1*(x)-1)
159static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
160 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
161 .name = "DLGLOBAL",
162 .description = "Library-internal global log family",
163 .loglevel = LOGL_NOTICE,
164 .enabled = 1,
165 },
root8a996b42011-09-26 11:22:21 +0200166 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
167 .name = "DLLAPD",
168 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200169 .loglevel = LOGL_NOTICE,
170 .enabled = 1,
Pau Espin Pedrol1573add2021-10-14 18:13:48 +0200171 .color = "\033[38;5;12m",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200172 },
Harald Welte892e6212011-07-19 14:31:44 +0200173 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200174 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200175 .description = "A-bis Intput Subsystem",
176 .loglevel = LOGL_NOTICE,
177 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100178 .color = "\033[38;5;23m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200179 },
Harald Welte892e6212011-07-19 14:31:44 +0200180 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200181 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200182 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
183 .loglevel = LOGL_NOTICE,
184 .enabled = 1,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100185 .color = "\033[38;5;25m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200186 },
Harald Welte892e6212011-07-19 14:31:44 +0200187 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200188 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200189 .description = "A-bis Input Driver for Signalling",
190 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100191 .color = "\033[38;5;27m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200192 },
Harald Welte892e6212011-07-19 14:31:44 +0200193 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200194 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200195 .description = "A-bis Input Driver for B-Channels (voice)",
196 .enabled = 0, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100197 .color = "\033[38;5;29m",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200198 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200199 [INT2IDX(DLSMS)] = {
200 .name = "DLSMS",
201 .description = "Layer3 Short Message Service (SMS)",
202 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100203 .color = "\033[38;5;31m",
Andreas Eversbergc626da92011-10-28 03:53:50 +0200204 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200205 [INT2IDX(DLCTRL)] = {
206 .name = "DLCTRL",
207 .description = "Control Interface",
208 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100209 .color = "\033[38;5;33m",
Harald Welte7fd0c832014-08-20 19:58:13 +0200210 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100211 [INT2IDX(DLGTP)] = {
212 .name = "DLGTP",
213 .description = "GPRS GTP library",
214 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100215 .color = "\033[38;5;35m",
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100216 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100217 [INT2IDX(DLSTATS)] = {
218 .name = "DLSTATS",
219 .description = "Statistics messages and logging",
220 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100221 .color = "\033[38;5;37m",
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100222 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100223 [INT2IDX(DLGSUP)] = {
224 .name = "DLGSUP",
225 .description = "Generic Subscriber Update Protocol",
226 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100227 .color = "\033[38;5;39m",
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100228 },
Harald Weltec0f00072016-04-27 18:32:35 +0200229 [INT2IDX(DLOAP)] = {
230 .name = "DLOAP",
231 .description = "Osmocom Authentication Protocol",
232 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100233 .color = "\033[38;5;41m",
Harald Weltec0f00072016-04-27 18:32:35 +0200234 },
Harald Welte059c4042017-04-03 22:20:49 +0200235 [INT2IDX(DLSS7)] = {
236 .name = "DLSS7",
237 .description = "libosmo-sigtran Signalling System 7",
238 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100239 .color = "\033[38;5;43m",
Harald Welte059c4042017-04-03 22:20:49 +0200240 },
241 [INT2IDX(DLSCCP)] = {
242 .name = "DLSCCP",
243 .description = "libosmo-sigtran SCCP Implementation",
244 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100245 .color = "\033[38;5;45m",
Harald Welte059c4042017-04-03 22:20:49 +0200246 },
247 [INT2IDX(DLSUA)] = {
248 .name = "DLSUA",
249 .description = "libosmo-sigtran SCCP User Adaptation",
250 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100251 .color = "\033[38;5;47m",
Harald Welte059c4042017-04-03 22:20:49 +0200252 },
253 [INT2IDX(DLM3UA)] = {
254 .name = "DLM3UA",
255 .description = "libosmo-sigtran MTP3 User Adaptation",
256 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100257 .color = "\033[38;5;49m",
Harald Welte059c4042017-04-03 22:20:49 +0200258 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200259 [INT2IDX(DLMGCP)] = {
260 .name = "DLMGCP",
261 .description = "libosmo-mgcp Media Gateway Control Protocol",
262 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100263 .color = "\033[38;5;51m",
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200264 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100265 [INT2IDX(DLJIBUF)] = {
266 .name = "DLJIBUF",
267 .description = "libosmo-netif Jitter Buffer",
268 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100269 .color = "\033[38;5;53m",
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100270 },
Max450f5ac2019-02-14 19:12:03 +0100271 [INT2IDX(DLRSPRO)] = {
272 .name = "DLRSPRO",
273 .description = "Remote SIM protocol",
274 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100275 .color = "\033[38;5;55m",
Max450f5ac2019-02-14 19:12:03 +0100276 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200277 [INT2IDX(DLNS)] = {
278 .name = "DLNS",
279 .description = "GPRS NS layer",
280 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100281 .color = "\033[38;5;57m",
Alexander Couzens6a161492020-07-12 13:45:50 +0200282 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100283 [INT2IDX(DLBSSGP)] = {
284 .name = "DLBSSGP",
285 .description = "GPRS BSSGP layer",
286 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte4c0e1a12020-12-07 22:03:09 +0100287 .color = "\033[38;5;59m",
Harald Weltefde19ed2020-12-07 21:43:51 +0100288 },
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100289 [INT2IDX(DLNSDATA)] = {
290 .name = "DLNSDATA",
291 .description = "GPRS NS layer data PDU",
292 .enabled = 1, .loglevel = LOGL_NOTICE,
293 .color = "\033[38;5;61m",
294 },
295 [INT2IDX(DLNSSIGNAL)] = {
296 .name = "DLNSSIGNAL",
297 .description = "GPRS NS layer signal PDU",
298 .enabled = 1, .loglevel = LOGL_NOTICE,
299 .color = "\033[38;5;63m",
300 },
Harald Welteb43bc042011-06-27 10:29:17 +0200301};
302
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200303void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100304{
305 if (!osmo_log_info) {
306 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100307 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100308 OSMO_ASSERT(osmo_log_info);
309 }
310}
311
Harald Welteb43bc042011-06-27 10:29:17 +0200312/* special magic for negative (library-internal) log subsystem numbers */
313static int subsys_lib2index(int subsys)
314{
315 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
316}
317
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200318/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700319 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200320 * \returns numeric log level
321 */
Harald Welte3ae27582010-03-26 21:24:24 +0800322int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800323{
324 return get_string_value(loglevel_strs, lvl);
325}
326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700328 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200329 * \returns zero-terminated string (log level name)
330 */
Harald Welte9ac22252010-05-11 11:19:40 +0200331const char *log_level_str(unsigned int lvl)
332{
333 return get_value_string(loglevel_strs, lvl);
334}
335
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200336/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200337 * \param[in] category human-readable log category name
338 * \returns numeric category value, or -EINVAL otherwise
339 */
Harald Welte3ae27582010-03-26 21:24:24 +0800340int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800341{
342 int i;
343
Max68bf16a2018-01-10 17:00:43 +0100344 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100345
Harald Welte4ebdf742010-05-19 19:54:00 +0200346 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200347 if (osmo_log_info->cat[i].name == NULL)
348 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200349 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800350 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800351 }
352
353 return -EINVAL;
354}
355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200357 * \param[in] target log target to be configured
358 * \param[in] _mask log category mask string
359 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800360 * The format can be this: category1:category2:category3
361 * or category1,2:category2,3:...
362 */
Harald Welte3ae27582010-03-26 21:24:24 +0800363void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800364{
365 int i = 0;
366 char *mask = strdup(_mask);
367 char *category_token = NULL;
368
Max68bf16a2018-01-10 17:00:43 +0100369 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100370
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800371 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200372 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800373 target->categories[i].enabled = 0;
374
375 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200376 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800377 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200378 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200379 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800380 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200381
382 if (!osmo_log_info->cat[i].name)
383 continue;
384
385 length = strlen(category_token);
386 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200387
388 /* Use longest length not to match subocurrences. */
389 if (cat_length > length)
390 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800391
392 if (colon)
393 length = colon - category_token;
394
Harald Welte4ebdf742010-05-19 19:54:00 +0200395 if (strncasecmp(osmo_log_info->cat[i].name,
396 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800397 int level = 0;
398
399 if (colon)
400 level = atoi(colon+1);
401
Harald Weltefaadfe22010-03-26 21:05:43 +0800402 target->categories[i].enabled = 1;
403 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800404 }
405 }
406 } while ((category_token = strtok(NULL, ":")));
407
408 free(mask);
409}
410
411static const char* color(int subsys)
412{
Harald Welte4ebdf742010-05-19 19:54:00 +0200413 if (subsys < osmo_log_info->num_cat)
414 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800415
Harald Welted788f662010-03-26 09:45:03 +0800416 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800417}
418
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100419static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100420 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
421 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
422 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
423 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
424 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100425 { 0, NULL }
426};
427
428static const char *level_color(int level)
429{
430 const char *c = get_value_string_or_null(level_colors, level);
431 if (!c)
432 return get_value_string(level_colors, LOGL_FATAL);
433 return c;
434}
435
Harald Welteaa00f992016-12-02 15:30:02 +0100436const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100437{
438 if (subsys < osmo_log_info->num_cat)
439 return osmo_log_info->cat[subsys].name;
440
441 return NULL;
442}
443
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100444static const char *const_basename(const char *path)
445{
446 const char *bn = strrchr(path, '/');
447 if (!bn || !bn[1])
448 return path;
449 return bn + 1;
450}
451
Harald Welte3ae27582010-03-26 21:24:24 +0800452static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200453 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100454 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800455{
Harald Weltea8b1b212020-09-27 17:21:07 +0200456 char buf[MAX_LOG_SIZE];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200457 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100458 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800459
460 /* are we using color */
461 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100462 c_subsys = color(subsys);
463 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100464 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200465 if (ret < 0)
466 goto err;
467 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800468 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800469 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800470 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100471 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200472#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100473 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100474 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200475 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100476 localtime_r(&tv.tv_sec, &tm);
477 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100478 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100479 tm.tm_hour, tm.tm_min, tm.tm_sec,
480 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100481 if (ret < 0)
482 goto err;
483 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200484#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100485 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800486 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200487 if ((tm = time(NULL)) == (time_t) -1)
488 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200489 /* Get human-readable representation of time.
490 man ctime: we need at least 26 bytes in buf */
491 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200492 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200493 ret = strlen(buf + offset);
494 if (ret <= 0)
495 goto err;
496 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
497 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200498 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800499 }
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100500 if (target->print_tid) {
501 if (logging_tid == 0)
502 logging_tid = (long int)osmo_gettid();
503 ret = snprintf(buf + offset, rem, "%ld ", logging_tid);
504 if (ret < 0)
505 goto err;
506 OSMO_SNPRINTF_RET(ret, rem, offset, len);
507 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100508 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100509 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
510 target->use_color ? level_color(level) : "",
511 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100512 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100513 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100514 if (ret < 0)
515 goto err;
516 OSMO_SNPRINTF_RET(ret, rem, offset, len);
517 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100518 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100519 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
520 target->use_color ? level_color(level) : "",
521 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100522 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100523 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100524 if (ret < 0)
525 goto err;
526 OSMO_SNPRINTF_RET(ret, rem, offset, len);
527 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100528 if (target->print_category_hex) {
529 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200530 if (ret < 0)
531 goto err;
532 OSMO_SNPRINTF_RET(ret, rem, offset, len);
533 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200534
535 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
536 switch (target->print_filename2) {
537 case LOG_FILENAME_NONE:
538 break;
539 case LOG_FILENAME_PATH:
540 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
541 if (ret < 0)
542 goto err;
543 OSMO_SNPRINTF_RET(ret, rem, offset, len);
544 break;
545 case LOG_FILENAME_BASENAME:
546 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
547 if (ret < 0)
548 goto err;
549 OSMO_SNPRINTF_RET(ret, rem, offset, len);
550 break;
551 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100552 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800553 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200554 ret = vsnprintf(buf + offset, rem, format, ap);
555 if (ret < 0)
556 goto err;
557 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800558
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200559 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
560 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
561 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
562 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
563 && offset > 0 && buf[offset-1] == '\n') {
564 switch (target->print_filename2) {
565 case LOG_FILENAME_NONE:
566 break;
567 case LOG_FILENAME_PATH:
568 offset --;
569 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
570 if (ret < 0)
571 goto err;
572 OSMO_SNPRINTF_RET(ret, rem, offset, len);
573 break;
574 case LOG_FILENAME_BASENAME:
575 offset --;
576 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
577 if (ret < 0)
578 goto err;
579 OSMO_SNPRINTF_RET(ret, rem, offset, len);
580 break;
581 }
582 }
583
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200584 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100585 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100586 if (ret < 0)
587 goto err;
588 OSMO_SNPRINTF_RET(ret, rem, offset, len);
589 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200590err:
591 buf[sizeof(buf)-1] = '\0';
592 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800593}
594
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100595/* Catch internal logging category indexes as well as out-of-bounds indexes.
596 * For internal categories, the ID is negative starting with -1; and internal
597 * logging categories are added behind the user categories. For out-of-bounds
598 * indexes, return the index of DLGLOBAL. The returned category index is
599 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
600 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100601static inline int map_subsys(int subsys)
602{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100603 /* Note: comparing signed and unsigned integers */
604
605 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
606 subsys = DLGLOBAL;
607
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100608 if (subsys < 0)
609 subsys = subsys_lib2index(subsys);
610
Neels Hofmeyrca135742016-12-12 14:18:54 +0100611 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100612 subsys = subsys_lib2index(DLGLOBAL);
613
Neels Hofmeyrca135742016-12-12 14:18:54 +0100614 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
615
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100616 return subsys;
617}
618
Maxc65c5b42017-03-15 13:20:23 +0100619static inline bool should_log_to_target(struct log_target *tar, int subsys,
620 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100621{
622 struct log_category *category;
623
624 category = &tar->categories[subsys];
625
626 /* subsystem is not supposed to be logged */
627 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100628 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100629
630 /* Check the global log level */
631 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100632 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100633
634 /* Check the category log level */
635 if (tar->loglevel == 0 && category->loglevel != 0 &&
636 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100637 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100638
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100639 /* Apply filters here... if that becomes messy we will
640 * need to put filters in a list and each filter will
641 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100642 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100643 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100644
645 if (osmo_log_info->filter_fn)
646 return osmo_log_info->filter_fn(&log_context, tar);
647
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100648 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100649 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100650}
651
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200652/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200653 * \param[in] subsys Logging sub-system
654 * \param[in] level Log level
655 * \param[in] file name of source code file
656 * \param[in] cont continuation (1) or new line (0)
657 * \param[in] format format string
658 * \param[in] ap vararg-list containing format string arguments
659 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200660void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200661 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800662{
Harald Welte3ae27582010-03-26 21:24:24 +0800663 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800664
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100665 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200666
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200667 log_tgt_mutex_lock();
668
Harald Welte28222962011-02-18 20:37:04 +0100669 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200670 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800671
Maxc65c5b42017-03-15 13:20:23 +0100672 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800673 continue;
674
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200675 /* According to the manpage, vsnprintf leaves the value of ap
676 * in undefined state. Since _output uses vsnprintf and it may
677 * be called several times, we have to pass a copy of ap. */
678 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100679 if (tar->raw_output)
680 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
681 else
682 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200683 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800684 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200685
686 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800687}
688
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200689/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200690 * \param[in] subsys Logging sub-system
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 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200695void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800696 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800697{
698 va_list ap;
699
700 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200701 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800702 va_end(ap);
703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200706 * \param[in] subsys Logging sub-system
707 * \param[in] level Log level
708 * \param[in] file name of source code file
709 * \param[in] cont continuation (1) or new line (0)
710 * \param[in] format format string
711 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200712void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800713{
714 va_list ap;
715
Harald Welte433005c2020-09-26 11:51:32 +0200716 TRACE(LIBOSMOCORE_LOG_START());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800717 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200718 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800719 va_end(ap);
Harald Welte433005c2020-09-26 11:51:32 +0200720 TRACE(LIBOSMOCORE_LOG_DONE());
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800721}
722
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200723/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200724 * \param[in] target Log target to be registered
725 */
Harald Welte3ae27582010-03-26 21:24:24 +0800726void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800727{
Harald Welte28222962011-02-18 20:37:04 +0100728 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800729}
730
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200731/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200732 * \param[in] target Log target to be unregistered
733 */
Harald Welte3ae27582010-03-26 21:24:24 +0800734void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800735{
736 llist_del(&target->entry);
737}
738
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200739/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800740void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800741{
Harald Welte3ae27582010-03-26 21:24:24 +0800742 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800743}
744
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200745/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200746 * \param[in] ctx_nr logging context number
747 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200748 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200749 *
750 * A logging context is something like the subscriber identity to which
751 * the currently processed message relates, or the BTS through which it
752 * was received. As soon as this data is known, it can be set using
753 * this function. The main use of context information is for logging
754 * filters.
755 */
Harald Welte3ae27582010-03-26 21:24:24 +0800756int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800757{
Harald Welte3ae27582010-03-26 21:24:24 +0800758 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800759 return -EINVAL;
760
Harald Welte3ae27582010-03-26 21:24:24 +0800761 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800762
763 return 0;
764}
765
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200766/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200767 * \param[in] target Log target to be affected
768 * \param[in] all enable (1) or disable (0) the ALL filter
769 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100770 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100771 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
772 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200773 */
Harald Welte3ae27582010-03-26 21:24:24 +0800774void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800775{
776 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100777 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800778 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100779 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800780}
781
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200782/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200783 * \param[in] target Log target to be affected
784 * \param[in] use_color Use color (1) or don't use color (0)
785 */
Harald Welte3ae27582010-03-26 21:24:24 +0800786void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800787{
788 target->use_color = use_color;
789}
790
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200791/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200792 * \param[in] target Log target to be affected
793 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
794 */
Harald Welte3ae27582010-03-26 21:24:24 +0800795void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800796{
797 target->print_timestamp = print_timestamp;
798}
799
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200800/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100801 * \param[in] target Log target to be affected
802 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
803 *
804 * When both timestamp and extended timestamp is enabled then only
805 * the extended timestamp will be used. The format of the timestamp
806 * is YYYYMMDDhhmmssnnn.
807 */
808void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
809{
810 target->print_ext_timestamp = print_timestamp;
811}
812
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100813/*! Enable or disable printing of timestamps while logging
814 * \param[in] target Log target to be affected
815 * \param[in] print_tid Enable (1) or disable (0) Thread ID logging
816 */
817void log_set_print_tid(struct log_target *target, int print_tid)
818{
819 target->print_tid = print_tid;
820}
821
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100822/*! Use log_set_print_filename2() instead.
823 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
824 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
825 * behavior, which combined the category in hex with the filename. For example, if the category-hex
826 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
827 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200828 * \param[in] target Log target to be affected
829 * \param[in] print_filename Enable (1) or disable (0) filenames
830 */
831void log_set_print_filename(struct log_target *target, int print_filename)
832{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100833 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
834 log_set_print_category_hex(target, print_filename);
835}
836
837/*! Enable or disable printing of the filename while logging.
838 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700839 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100840 * LOG_FILENAME_NONE omits the source file and line information from logs.
841 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
842 */
843void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
844{
845 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200846}
847
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200848/*! Set the position where on a log line the source file info should be logged.
849 * \param[in] target Log target to be affected.
850 * \param[in] pos A LOG_FILENAME_POS_* enum value.
851 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
852 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
853 */
854void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
855{
856 target->print_filename_pos = pos;
857}
858
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200859/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100860 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700861 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100862 *
863 * Print the category/subsys name in front of every log message.
864 */
865void log_set_print_category(struct log_target *target, int print_category)
866{
867 target->print_category = print_category;
868}
869
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100870/*! Enable or disable printing of the category number in hex ('<000b>').
871 * \param[in] target Log target to be affected.
872 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
873 */
874void log_set_print_category_hex(struct log_target *target, int print_category_hex)
875{
876 target->print_category_hex = print_category_hex;
877}
878
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100879/*! Enable or disable printing of the log level name.
880 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700881 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100882 *
883 * Print the log level name in front of every log message.
884 */
885void log_set_print_level(struct log_target *target, int print_level)
886{
887 target->print_level = (bool)print_level;
888}
889
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200890/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200891 * \param[in] target Log target to be affected
892 * \param[in] log_level New global log level
893 */
Harald Welte3ae27582010-03-26 21:24:24 +0800894void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800895{
896 target->loglevel = log_level;
897}
898
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200899/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100900 * \param[in] target Log target to be affected
901 * \param[in] category Log category to be affected
902 * \param[in] enable whether to enable or disable the filter
903 * \param[in] level Log level of the filter
904 */
Harald Welte3ae27582010-03-26 21:24:24 +0800905void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800906 int enable, int level)
907{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100908 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800909 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100910 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800911 target->categories[category].enabled = !!enable;
912 target->categories[category].loglevel = level;
913}
914
Harald Welte44c0f632017-01-15 17:58:29 +0100915#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +0200916static void _file_output_stream(struct log_target *target, unsigned int level,
917 const char *log)
918{
919 OSMO_ASSERT(target->tgt_file.out);
920 fputs(log, target->tgt_file.out);
921 fflush(target->tgt_file.out);
922}
923
924/* output via non-blocking write_queue, doing internal buffering */
Harald Welte76e72ab2011-02-17 15:52:39 +0100925static void _file_output(struct log_target *target, unsigned int level,
926 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800927{
Harald Welteb72867f2020-09-26 21:45:16 +0200928 int len = strlen(log);
929 struct msgb *msg;
930
931 OSMO_ASSERT(target->tgt_file.wqueue);
932 msg = msgb_alloc_c(target->tgt_file.wqueue, len, "log_file_msg");
933 if (!msg)
934 return;
935
936 /* we simply enqueue the log message to a write queue here, to avoid any blocking
937 * writes on the output file. The write queue will tell us once the file is writable
938 * and call _file_wq_write_cb() */
939 memcpy(msg->data, log, len);
940 msgb_put(msg, len);
941 osmo_wqueue_enqueue_quiet(target->tgt_file.wqueue, msg);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800942}
Harald Welte44c0f632017-01-15 17:58:29 +0100943#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800944
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200945/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200946 * \returns dynamically-allocated log target
947 * This funcition allocates a \ref log_target and initializes it
948 * with some default values. The newly created target is not
949 * registered yet.
950 */
Harald Welte3ae27582010-03-26 21:24:24 +0800951struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800952{
Harald Welte3ae27582010-03-26 21:24:24 +0800953 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800954 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800955
Max68bf16a2018-01-10 17:00:43 +0100956 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100957
Harald Welte3ae27582010-03-26 21:24:24 +0800958 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800959 if (!target)
960 return NULL;
961
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200962 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200963 struct log_category,
964 osmo_log_info->num_cat);
965 if (!target->categories) {
966 talloc_free(target);
967 return NULL;
968 }
969
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800970 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800971
972 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200973 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800974 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200975 cat->enabled = osmo_log_info->cat[i].enabled;
976 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800977 }
978
979 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800980 target->use_color = 1;
981 target->print_timestamp = 0;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100982 target->print_tid = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100983 target->print_filename2 = LOG_FILENAME_PATH;
984 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800985
986 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800987 target->loglevel = 0;
988 return target;
989}
990
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200991/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200992 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800993struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800994{
Harald Weltea3b844c2010-03-27 00:04:40 +0800995/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200996#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800997 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800998
Harald Welte3ae27582010-03-26 21:24:24 +0800999 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001000 if (!target)
1001 return NULL;
1002
Harald Welte28222962011-02-18 20:37:04 +01001003 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +02001004 target->tgt_file.out = stderr;
Harald Welteb72867f2020-09-26 21:45:16 +02001005 target->output = _file_output_stream;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001006 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +08001007#else
1008 return NULL;
1009#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001010}
1011
Harald Welte44c0f632017-01-15 17:58:29 +01001012#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001013/* write-queue tells us we should write another msgb (log line) to the output fd */
1014static int _file_wq_write_cb(struct osmo_fd *ofd, struct msgb *msg)
1015{
1016 int rc;
1017
1018 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
1019 if (rc < 0)
1020 return rc;
1021 if (rc != msgb_length(msg)) {
1022 /* pull the number of bytes we have already written */
1023 msgb_pull(msg, rc);
1024 /* ask write_queue to re-insert the msgb at the head of the queue */
1025 return -EAGAIN;
1026 }
1027 return 0;
1028}
1029
1030/*! Create a new file-based log target using buffered, blocking stream output
Harald Welte18fc4652011-08-17 14:14:17 +02001031 * \param[in] fname File name of the new log file
1032 * \returns Log target in case of success, NULL otherwise
1033 */
Harald Welteb72867f2020-09-26 21:45:16 +02001034struct log_target *log_target_create_file_stream(const char *fname)
Harald Welte3086c392010-08-25 19:10:50 +02001035{
1036 struct log_target *target;
1037
1038 target = log_target_create();
1039 if (!target)
1040 return NULL;
1041
Harald Welte28222962011-02-18 20:37:04 +01001042 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +02001043 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001044 if (!target->tgt_file.out) {
1045 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +02001046 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +07001047 }
Harald Welteb72867f2020-09-26 21:45:16 +02001048 target->output = _file_output_stream;
1049 target->tgt_file.fname = talloc_strdup(target, fname);
Harald Welte3086c392010-08-25 19:10:50 +02001050
Harald Welteb72867f2020-09-26 21:45:16 +02001051 return target;
1052}
1053
1054/*! switch from non-blocking/write-queue to blocking + buffered stream output
1055 * \param[in] target log target which we should switch
1056 * \return 0 on success; 1 if already switched before; negative on error */
1057int log_target_file_switch_to_stream(struct log_target *target)
1058{
1059 struct osmo_wqueue *wq;
1060 const char *name;
1061
1062 if (!target)
1063 return -ENODEV;
1064
1065 /* this only works for file/stderr targets */
1066 switch (target->type) {
1067 case LOG_TGT_TYPE_FILE:
1068 name = target->tgt_file.fname;
1069 break;
1070 case LOG_TGT_TYPE_STDERR:
1071 name = "stderr";
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
1076
1077 if (target->tgt_file.out) {
1078 /* target has already been switched over */
1079 return 1;
1080 }
1081
1082 LOGP(DLGLOBAL, LOGL_INFO, "Switching log target '%s' to blocking stream I/O\n", name);
1083
1084 wq = target->tgt_file.wqueue;
1085 OSMO_ASSERT(wq);
1086
1087 /* re-open output as stream */
1088 if (target->type == LOG_TGT_TYPE_STDERR)
1089 target->tgt_file.out = stderr;
1090 else
1091 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1092 if (!target->tgt_file.out) {
1093 LOGP(DLGLOBAL, LOGL_ERROR, "Cannot open log target '%s' as blocking stream I/O: %s\n",
1094 name, strerror(errno));
1095 return -EIO;
1096 }
1097
1098 /* synchronously write anything left in the queue */
1099 while (!llist_empty(&wq->msg_queue)) {
1100 struct msgb *msg = msgb_dequeue(&wq->msg_queue);
1101 fwrite(msgb_data(msg), msgb_length(msg), 1, target->tgt_file.out);
1102 msgb_free(msg);
1103 }
1104
1105 /* now that everything succeeded, we can finally close the old output fd */
1106 if (target->type == LOG_TGT_TYPE_FILE) {
1107 osmo_fd_unregister(&wq->bfd);
1108 close(wq->bfd.fd);
1109 }
1110
1111 /* release the queue itself */
1112 talloc_free(wq);
1113 target->tgt_file.wqueue = NULL;
1114 target->output = _file_output_stream;
1115 target->raw_output = NULL;
1116
1117 return 0;
1118}
1119
1120/*! switch from blocking + buffered file output to non-blocking write-queue based output.
1121 * \param[in] target log target which we should switch
1122 * \return 0 on success; 1 if already switched before; negative on error */
1123int log_target_file_switch_to_wqueue(struct log_target *target)
1124{
1125 struct osmo_wqueue *wq;
1126 const char *name;
1127 int rc;
1128
1129 if (!target)
1130 return -ENODEV;
1131
1132 /* this only works for file/stderr targets */
1133 switch (target->type) {
1134 case LOG_TGT_TYPE_FILE:
1135 name = target->tgt_file.fname;
1136 break;
1137 case LOG_TGT_TYPE_STDERR:
1138 name = "stderr";
1139 break;
1140 default:
1141 return -EINVAL;
1142 }
1143
1144 if (!target->tgt_file.out) {
1145 /* target has already been switched over */
1146 return 1;
1147 }
1148
1149 LOGP(DLGLOBAL, LOGL_INFO, "Switching log target '%s' to non-blocking I/O\n", name);
1150
1151 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1152 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1153 * reduce the OOM probability within logging, as the pool is already allocated */
1154 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
1155 LOG_WQUEUE_LEN*(sizeof(struct msgb)+512));
1156 if (!wq)
1157 return -ENOMEM;
1158 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1159
1160 fflush(target->tgt_file.out);
1161 if (target->type == LOG_TGT_TYPE_FILE) {
1162 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1163 if (rc < 0) {
1164 LOGP(DLGLOBAL, LOGL_ERROR, "Cannot open log target '%s' as non-blocking I/O: %s\n",
1165 name, strerror(errno));
1166 talloc_free(wq);
1167 return -errno;
1168 }
1169 } else {
1170 rc = STDERR_FILENO;
1171 }
1172 wq->bfd.fd = rc;
1173 wq->bfd.when = OSMO_FD_WRITE;
1174 wq->write_cb = _file_wq_write_cb;
1175
1176 rc = osmo_fd_register(&wq->bfd);
1177 if (rc < 0) {
1178 talloc_free(wq);
1179 return -EIO;
1180 }
1181 target->tgt_file.wqueue = wq;
Harald Welte3086c392010-08-25 19:10:50 +02001182 target->output = _file_output;
1183
Harald Welteb72867f2020-09-26 21:45:16 +02001184 /* now that everything succeeded, we can finally close the old output stream */
1185 if (target->type == LOG_TGT_TYPE_FILE)
1186 fclose(target->tgt_file.out);
1187 target->tgt_file.out = NULL;
1188
1189 return 0;
1190}
1191
1192/*! Create a new file-based log target using non-blocking write_queue
1193 * \param[in] fname File name of the new log file
1194 * \returns Log target in case of success, NULL otherwise
1195 */
1196struct log_target *log_target_create_file(const char *fname)
1197{
1198 struct log_target *target;
1199 struct osmo_wqueue *wq;
1200 int rc;
1201
1202 target = log_target_create();
1203 if (!target)
1204 return NULL;
1205
1206 target->type = LOG_TGT_TYPE_FILE;
1207 /* we create a ~640kB sized talloc pool within the write-queue to ensure individual
1208 * log lines (stored as msgbs) will not put result in malloc() calls, and also to
1209 * reduce the OOM probability within logging, as the pool is already allocated */
1210 wq = talloc_pooled_object(target, struct osmo_wqueue, LOG_WQUEUE_LEN,
1211 LOG_WQUEUE_LEN*(sizeof(struct msgb)+512));
1212 if (!wq) {
1213 log_target_destroy(target);
1214 return NULL;
1215 }
1216 osmo_wqueue_init(wq, LOG_WQUEUE_LEN);
1217 wq->bfd.fd = open(fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1218 if (wq->bfd.fd < 0) {
1219 talloc_free(wq);
1220 log_target_destroy(target);
1221 return NULL;
1222 }
1223 wq->bfd.when = OSMO_FD_WRITE;
1224 wq->write_cb = _file_wq_write_cb;
1225
1226 rc = osmo_fd_register(&wq->bfd);
1227 if (rc < 0) {
1228 talloc_free(wq);
1229 log_target_destroy(target);
1230 return NULL;
1231 }
1232
1233 target->tgt_file.wqueue = wq;
1234 target->output = _file_output;
Harald Welte3086c392010-08-25 19:10:50 +02001235 target->tgt_file.fname = talloc_strdup(target, fname);
1236
1237 return target;
1238}
Harald Welte44c0f632017-01-15 17:58:29 +01001239#endif
Harald Welte3086c392010-08-25 19:10:50 +02001240
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001241/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +02001242 * \param[in] type Log target type
1243 * \param[in] fname File name
1244 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001245 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +02001246 */
Harald Welte28222962011-02-18 20:37:04 +01001247struct log_target *log_target_find(int type, const char *fname)
1248{
1249 struct log_target *tgt;
1250
1251 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
1252 if (tgt->type != type)
1253 continue;
Maxc90f40a2018-01-11 10:52:28 +01001254 switch (tgt->type) {
1255 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +01001256 if (!strcmp(fname, tgt->tgt_file.fname))
1257 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001258 break;
1259 case LOG_TGT_TYPE_GSMTAP:
1260 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
1261 return tgt;
1262 break;
1263 default:
Harald Welte28222962011-02-18 20:37:04 +01001264 return tgt;
Maxc90f40a2018-01-11 10:52:28 +01001265 }
Harald Welte28222962011-02-18 20:37:04 +01001266 }
1267 return NULL;
1268}
1269
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001270/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001271 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +02001272void log_target_destroy(struct log_target *target)
1273{
Harald Welte3086c392010-08-25 19:10:50 +02001274 /* just in case, to make sure we don't have any references */
1275 log_del_target(target);
1276
Harald Welte44c0f632017-01-15 17:58:29 +01001277#if (!EMBEDDED)
Harald Welteb72867f2020-09-26 21:45:16 +02001278 struct osmo_wqueue *wq;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001279 switch (target->type) {
1280 case LOG_TGT_TYPE_FILE:
Harald Welteb72867f2020-09-26 21:45:16 +02001281 case LOG_TGT_TYPE_STDERR:
1282 if (target->tgt_file.out) {
1283 if (target->type == LOG_TGT_TYPE_FILE)
1284 fclose(target->tgt_file.out);
1285 target->tgt_file.out = NULL;
1286 }
1287 wq = target->tgt_file.wqueue;
1288 if (wq) {
1289 if (wq->bfd.fd >= 0) {
1290 if (target->type == LOG_TGT_TYPE_FILE)
1291 close(wq->bfd.fd);
1292 wq->bfd.fd = -1;
1293 }
1294 osmo_fd_unregister(&wq->bfd);
1295 osmo_wqueue_clear(wq);
1296 talloc_free(wq);
1297 target->tgt_file.wqueue = NULL;
1298 }
1299 talloc_free((void *)target->tgt_file.fname);
1300 target->tgt_file.fname = NULL;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001301 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +07001302#ifdef HAVE_SYSLOG_H
1303 case LOG_TGT_TYPE_SYSLOG:
1304 closelog();
1305 break;
1306#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +07001307 default:
1308 /* make GCC happy */
1309 break;
Harald Welte3086c392010-08-25 19:10:50 +02001310 }
Harald Welte44c0f632017-01-15 17:58:29 +01001311#endif
Harald Welte3086c392010-08-25 19:10:50 +02001312
1313 talloc_free(target);
1314}
1315
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001316/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001317 * \param[in] target log target to re-open
1318 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001319int log_target_file_reopen(struct log_target *target)
1320{
Harald Welteb72867f2020-09-26 21:45:16 +02001321 struct osmo_wqueue *wq;
1322 int rc;
Harald Welte3086c392010-08-25 19:10:50 +02001323
Harald Welteb72867f2020-09-26 21:45:16 +02001324 OSMO_ASSERT(target->type == LOG_TGT_TYPE_FILE || target->type == LOG_TGT_TYPE_STDERR);
1325 OSMO_ASSERT(target->tgt_file.out || target->tgt_file.wqueue);
Harald Welte3086c392010-08-25 19:10:50 +02001326
Harald Welteb72867f2020-09-26 21:45:16 +02001327 if (target->tgt_file.out) {
1328 fclose(target->tgt_file.out);
1329 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1330 if (!target->tgt_file.out)
1331 return -errno;
1332 } else {
1333 wq = target->tgt_file.wqueue;
1334 osmo_fd_unregister(&wq->bfd);
1335 if (wq->bfd.fd >= 0) {
1336 close(wq->bfd.fd);
1337 wq->bfd.fd = -1;
1338 }
1339
1340 rc = open(target->tgt_file.fname, O_WRONLY|O_APPEND|O_CREAT|O_NONBLOCK, 0660);
1341 if (rc < 0)
1342 return -errno;
1343 wq->bfd.fd = rc;
1344 rc = osmo_fd_register(&wq->bfd);
1345 if (rc < 0)
1346 return rc;
1347 }
Harald Welte3086c392010-08-25 19:10:50 +02001348
1349 return 0;
1350}
1351
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001352/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001353 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001354int log_targets_reopen(void)
1355{
1356 struct log_target *tar;
1357 int rc = 0;
1358
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001359 log_tgt_mutex_lock();
1360
Harald Welte4de854d2013-03-18 19:01:40 +01001361 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1362 switch (tar->type) {
1363 case LOG_TGT_TYPE_FILE:
1364 if (log_target_file_reopen(tar) < 0)
1365 rc = -1;
1366 break;
1367 default:
1368 break;
1369 }
1370 }
1371
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001372 log_tgt_mutex_unlock();
1373
Harald Welte4de854d2013-03-18 19:01:40 +01001374 return rc;
1375}
1376
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001377/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001378 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001379 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001380 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001381 *
1382 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001383 */
Harald Welteb43bc042011-06-27 10:29:17 +02001384int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001385{
Harald Welteb43bc042011-06-27 10:29:17 +02001386 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001387 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001388
Philipp Maierdc02c062020-05-12 17:51:25 +02001389 /* Ensure that log_init is not called multiple times */
1390 OSMO_ASSERT(tall_log_ctx == NULL)
1391
Harald Welteb43bc042011-06-27 10:29:17 +02001392 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1393 if (!tall_log_ctx)
1394 return -ENOMEM;
1395
1396 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1397 if (!osmo_log_info)
1398 return -ENOMEM;
1399
Max72dfd432018-12-04 11:24:18 +01001400 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1401
1402 if (inf) {
1403 osmo_log_info->filter_fn = inf->filter_fn;
1404 osmo_log_info->num_cat_user = inf->num_cat;
1405 osmo_log_info->num_cat += inf->num_cat;
1406 }
Harald Welteb43bc042011-06-27 10:29:17 +02001407
Philipp Maierdcad1c52020-03-25 11:25:59 +01001408 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1409 osmo_log_info->num_cat);
1410 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001411 talloc_free(osmo_log_info);
1412 osmo_log_info = NULL;
1413 return -ENOMEM;
1414 }
1415
Philipp Maierdcad1c52020-03-25 11:25:59 +01001416 /* copy over the user part and sanitize loglevel */
1417 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001418 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001419 memcpy(&cat_ptr[i], &inf->cat[i],
1420 sizeof(struct log_info_cat));
1421
1422 /* Make sure that the loglevel is set to NOTICE in case
1423 * no loglevel has been preset. */
1424 if (!cat_ptr[i].loglevel) {
1425 cat_ptr[i].loglevel = LOGL_NOTICE;
1426 }
Max72dfd432018-12-04 11:24:18 +01001427 }
Harald Welteb43bc042011-06-27 10:29:17 +02001428 }
1429
1430 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001431 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001432 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001433 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001434 }
1435
Philipp Maierdcad1c52020-03-25 11:25:59 +01001436 osmo_log_info->cat = cat_ptr;
1437
Harald Welte9fe16522011-06-27 14:00:03 +02001438 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001439}
Harald Welte18fc4652011-08-17 14:14:17 +02001440
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001441/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001442 * This function destroys all targets and releases associated memory */
1443void log_fini(void)
1444{
1445 struct log_target *tar, *tar2;
1446
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001447 log_tgt_mutex_lock();
1448
Harald Welte69e6c3c2016-04-20 10:41:27 +02001449 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1450 log_target_destroy(tar);
1451
1452 talloc_free(osmo_log_info);
1453 osmo_log_info = NULL;
1454 talloc_free(tall_log_ctx);
1455 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001456
1457 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001458}
1459
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001460/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001461 * \returns != 0 if a log entry might get generated by at least one target */
1462int log_check_level(int subsys, unsigned int level)
1463{
1464 struct log_target *tar;
1465
Max68bf16a2018-01-10 17:00:43 +01001466 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001467
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001468 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001469
1470 /* TODO: The following could/should be cached (update on config) */
1471
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001472 log_tgt_mutex_lock();
1473
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001474 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001475 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001476 continue;
1477
1478 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001479 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001480 return 1;
1481 }
1482
1483 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001484 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001485 return 0;
1486}
1487
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001488/*! @} */