blob: c14e696106aee0c371b7982a726b5df50a08a83c [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 Welte01fd5cb2010-03-26 23:51:31 +080038
39#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080040#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080041#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080042#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010043#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080044#include <errno.h>
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020045#include <pthread.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080046
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010047#include <osmocom/core/talloc.h>
48#include <osmocom/core/utils.h>
49#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020050#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080051
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010052#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
53
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010054osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010055 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010056osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010057 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010058osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010059 enum_logging_filters_fit_in_log_target_filter_map);
60
Harald Welteb43bc042011-06-27 10:29:17 +020061struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080062
Harald Welte3ae27582010-03-26 21:24:24 +080063static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020064void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010065LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080066
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020067#if (!EMBEDDED)
68/*! This mutex must be held while using osmo_log_target_list or any of its
69 log_targets in a multithread program. Prevents race conditions between threads
70 like producing unordered timestamps or VTY deleting a target while another
71 thread is writing to it */
72static pthread_mutex_t osmo_log_tgt_mutex;
73static bool osmo_log_tgt_mutex_on = false;
74
75/*! Enable multithread support (mutex) in libosmocore logging system.
76 * Must be called by processes willing to use logging subsystem from several
77 * threads. Once enabled, it's not possible to disable it again.
78 */
79void log_enable_multithread(void) {
80 if (osmo_log_tgt_mutex_on)
81 return;
82 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
83 osmo_log_tgt_mutex_on = true;
84}
85
86/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
87 * macro log_tgt_mutex_lock() instead.
88 */
89void log_tgt_mutex_lock_impl(void) {
90 /* These lines are useful to debug scenarios where there's only 1 thread
91 and a double lock appears, for instance during startup and some
92 unlock() missing somewhere:
93 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
94 osmo_panic("acquiring already locked mutex!\n");
95 return;
96 */
97
98 if (osmo_log_tgt_mutex_on)
99 pthread_mutex_lock(&osmo_log_tgt_mutex);
100}
101
102/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
103 * macro log_tgt_mutex_unlock() instead.
104 */
105void log_tgt_mutex_unlock_impl(void) {
106 if (osmo_log_tgt_mutex_on)
107 pthread_mutex_unlock(&osmo_log_tgt_mutex);
108}
109
110#else /* if (!EMBEDDED) */
111#pragma message ("logging multithread support disabled in embedded build")
112void log_enable_multithread(void) {}
113void log_tgt_mutex_lock_impl(void) {}
114void log_tgt_mutex_unlock_impl(void) {}
115#endif /* if (!EMBEDDED) */
116
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200117const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800118 { LOGL_DEBUG, "DEBUG" },
119 { LOGL_INFO, "INFO" },
120 { LOGL_NOTICE, "NOTICE" },
121 { LOGL_ERROR, "ERROR" },
122 { LOGL_FATAL, "FATAL" },
123 { 0, NULL },
124};
125
Harald Welteb43bc042011-06-27 10:29:17 +0200126#define INT2IDX(x) (-1*(x)-1)
127static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
128 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
129 .name = "DLGLOBAL",
130 .description = "Library-internal global log family",
131 .loglevel = LOGL_NOTICE,
132 .enabled = 1,
133 },
root8a996b42011-09-26 11:22:21 +0200134 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
135 .name = "DLLAPD",
136 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200137 .loglevel = LOGL_NOTICE,
138 .enabled = 1,
139 },
Harald Welte892e6212011-07-19 14:31:44 +0200140 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200141 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200142 .description = "A-bis Intput Subsystem",
143 .loglevel = LOGL_NOTICE,
144 .enabled = 1,
145 },
Harald Welte892e6212011-07-19 14:31:44 +0200146 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200147 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200148 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
149 .loglevel = LOGL_NOTICE,
150 .enabled = 1,
151 },
Harald Welte892e6212011-07-19 14:31:44 +0200152 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200153 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200154 .description = "A-bis Input Driver for Signalling",
155 .enabled = 0, .loglevel = LOGL_NOTICE,
156 },
Harald Welte892e6212011-07-19 14:31:44 +0200157 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200158 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200159 .description = "A-bis Input Driver for B-Channels (voice)",
160 .enabled = 0, .loglevel = LOGL_NOTICE,
161 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200162 [INT2IDX(DLSMS)] = {
163 .name = "DLSMS",
164 .description = "Layer3 Short Message Service (SMS)",
165 .enabled = 1, .loglevel = LOGL_NOTICE,
Neels Hofmeyrc5b71752019-11-20 04:50:52 +0100166 .color = OSMO_LOGCOLOR_BRIGHTWHITE,
Andreas Eversbergc626da92011-10-28 03:53:50 +0200167 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200168 [INT2IDX(DLCTRL)] = {
169 .name = "DLCTRL",
170 .description = "Control Interface",
171 .enabled = 1, .loglevel = LOGL_NOTICE,
172 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100173 [INT2IDX(DLGTP)] = {
174 .name = "DLGTP",
175 .description = "GPRS GTP library",
176 .enabled = 1, .loglevel = LOGL_NOTICE,
177 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100178 [INT2IDX(DLSTATS)] = {
179 .name = "DLSTATS",
180 .description = "Statistics messages and logging",
181 .enabled = 1, .loglevel = LOGL_NOTICE,
182 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100183 [INT2IDX(DLGSUP)] = {
184 .name = "DLGSUP",
185 .description = "Generic Subscriber Update Protocol",
186 .enabled = 1, .loglevel = LOGL_NOTICE,
187 },
Harald Weltec0f00072016-04-27 18:32:35 +0200188 [INT2IDX(DLOAP)] = {
189 .name = "DLOAP",
190 .description = "Osmocom Authentication Protocol",
191 .enabled = 1, .loglevel = LOGL_NOTICE,
192 },
Harald Welte059c4042017-04-03 22:20:49 +0200193 [INT2IDX(DLSS7)] = {
194 .name = "DLSS7",
195 .description = "libosmo-sigtran Signalling System 7",
196 .enabled = 1, .loglevel = LOGL_NOTICE,
197 },
198 [INT2IDX(DLSCCP)] = {
199 .name = "DLSCCP",
200 .description = "libosmo-sigtran SCCP Implementation",
201 .enabled = 1, .loglevel = LOGL_NOTICE,
202 },
203 [INT2IDX(DLSUA)] = {
204 .name = "DLSUA",
205 .description = "libosmo-sigtran SCCP User Adaptation",
206 .enabled = 1, .loglevel = LOGL_NOTICE,
207 },
208 [INT2IDX(DLM3UA)] = {
209 .name = "DLM3UA",
210 .description = "libosmo-sigtran MTP3 User Adaptation",
211 .enabled = 1, .loglevel = LOGL_NOTICE,
212 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200213 [INT2IDX(DLMGCP)] = {
214 .name = "DLMGCP",
215 .description = "libosmo-mgcp Media Gateway Control Protocol",
216 .enabled = 1, .loglevel = LOGL_NOTICE,
217 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100218 [INT2IDX(DLJIBUF)] = {
219 .name = "DLJIBUF",
220 .description = "libosmo-netif Jitter Buffer",
221 .enabled = 1, .loglevel = LOGL_NOTICE,
222 },
Max450f5ac2019-02-14 19:12:03 +0100223 [INT2IDX(DLRSPRO)] = {
224 .name = "DLRSPRO",
225 .description = "Remote SIM protocol",
226 .enabled = 1, .loglevel = LOGL_NOTICE,
227 },
Harald Welteb43bc042011-06-27 10:29:17 +0200228};
229
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200230void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100231{
232 if (!osmo_log_info) {
233 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100234 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100235 OSMO_ASSERT(osmo_log_info);
236 }
237}
238
Harald Welteb43bc042011-06-27 10:29:17 +0200239/* special magic for negative (library-internal) log subsystem numbers */
240static int subsys_lib2index(int subsys)
241{
242 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
243}
244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700246 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200247 * \returns numeric log level
248 */
Harald Welte3ae27582010-03-26 21:24:24 +0800249int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800250{
251 return get_string_value(loglevel_strs, lvl);
252}
253
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700255 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200256 * \returns zero-terminated string (log level name)
257 */
Harald Welte9ac22252010-05-11 11:19:40 +0200258const char *log_level_str(unsigned int lvl)
259{
260 return get_value_string(loglevel_strs, lvl);
261}
262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200264 * \param[in] category human-readable log category name
265 * \returns numeric category value, or -EINVAL otherwise
266 */
Harald Welte3ae27582010-03-26 21:24:24 +0800267int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800268{
269 int i;
270
Max68bf16a2018-01-10 17:00:43 +0100271 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100272
Harald Welte4ebdf742010-05-19 19:54:00 +0200273 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200274 if (osmo_log_info->cat[i].name == NULL)
275 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200276 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800277 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800278 }
279
280 return -EINVAL;
281}
282
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200284 * \param[in] target log target to be configured
285 * \param[in] _mask log category mask string
286 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800287 * The format can be this: category1:category2:category3
288 * or category1,2:category2,3:...
289 */
Harald Welte3ae27582010-03-26 21:24:24 +0800290void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800291{
292 int i = 0;
293 char *mask = strdup(_mask);
294 char *category_token = NULL;
295
Max68bf16a2018-01-10 17:00:43 +0100296 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100297
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800298 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200299 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800300 target->categories[i].enabled = 0;
301
302 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200303 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800304 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200305 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200306 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800307 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200308
309 if (!osmo_log_info->cat[i].name)
310 continue;
311
312 length = strlen(category_token);
313 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200314
315 /* Use longest length not to match subocurrences. */
316 if (cat_length > length)
317 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800318
319 if (colon)
320 length = colon - category_token;
321
Harald Welte4ebdf742010-05-19 19:54:00 +0200322 if (strncasecmp(osmo_log_info->cat[i].name,
323 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800324 int level = 0;
325
326 if (colon)
327 level = atoi(colon+1);
328
Harald Weltefaadfe22010-03-26 21:05:43 +0800329 target->categories[i].enabled = 1;
330 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800331 }
332 }
333 } while ((category_token = strtok(NULL, ":")));
334
335 free(mask);
336}
337
338static const char* color(int subsys)
339{
Harald Welte4ebdf742010-05-19 19:54:00 +0200340 if (subsys < osmo_log_info->num_cat)
341 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342
Harald Welted788f662010-03-26 09:45:03 +0800343 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800344}
345
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100346static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100347 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
348 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
349 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
350 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
351 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100352 { 0, NULL }
353};
354
355static const char *level_color(int level)
356{
357 const char *c = get_value_string_or_null(level_colors, level);
358 if (!c)
359 return get_value_string(level_colors, LOGL_FATAL);
360 return c;
361}
362
Harald Welteaa00f992016-12-02 15:30:02 +0100363const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100364{
365 if (subsys < osmo_log_info->num_cat)
366 return osmo_log_info->cat[subsys].name;
367
368 return NULL;
369}
370
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100371static const char *const_basename(const char *path)
372{
373 const char *bn = strrchr(path, '/');
374 if (!bn || !bn[1])
375 return path;
376 return bn + 1;
377}
378
Harald Welte3ae27582010-03-26 21:24:24 +0800379static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200380 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100381 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800382{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800383 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200384 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100385 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800386
387 /* are we using color */
388 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100389 c_subsys = color(subsys);
390 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100391 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200392 if (ret < 0)
393 goto err;
394 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800395 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800396 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800397 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100398 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200399#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100400 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100401 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200402 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100403 localtime_r(&tv.tv_sec, &tm);
404 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100405 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100406 tm.tm_hour, tm.tm_min, tm.tm_sec,
407 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100408 if (ret < 0)
409 goto err;
410 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200411#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100412 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800413 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200414 if ((tm = time(NULL)) == (time_t) -1)
415 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200416 /* Get human-readable representation of time.
417 man ctime: we need at least 26 bytes in buf */
418 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200419 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200420 ret = strlen(buf + offset);
421 if (ret <= 0)
422 goto err;
423 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
424 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200425 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800426 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100427 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100428 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
429 target->use_color ? level_color(level) : "",
430 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100431 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100432 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100433 if (ret < 0)
434 goto err;
435 OSMO_SNPRINTF_RET(ret, rem, offset, len);
436 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100437 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100438 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
439 target->use_color ? level_color(level) : "",
440 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100441 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100442 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100443 if (ret < 0)
444 goto err;
445 OSMO_SNPRINTF_RET(ret, rem, offset, len);
446 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100447 if (target->print_category_hex) {
448 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200449 if (ret < 0)
450 goto err;
451 OSMO_SNPRINTF_RET(ret, rem, offset, len);
452 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200453
454 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
455 switch (target->print_filename2) {
456 case LOG_FILENAME_NONE:
457 break;
458 case LOG_FILENAME_PATH:
459 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
460 if (ret < 0)
461 goto err;
462 OSMO_SNPRINTF_RET(ret, rem, offset, len);
463 break;
464 case LOG_FILENAME_BASENAME:
465 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
466 if (ret < 0)
467 goto err;
468 OSMO_SNPRINTF_RET(ret, rem, offset, len);
469 break;
470 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100471 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800472 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200473 ret = vsnprintf(buf + offset, rem, format, ap);
474 if (ret < 0)
475 goto err;
476 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800477
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200478 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
479 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
480 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
481 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
482 && offset > 0 && buf[offset-1] == '\n') {
483 switch (target->print_filename2) {
484 case LOG_FILENAME_NONE:
485 break;
486 case LOG_FILENAME_PATH:
487 offset --;
488 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
489 if (ret < 0)
490 goto err;
491 OSMO_SNPRINTF_RET(ret, rem, offset, len);
492 break;
493 case LOG_FILENAME_BASENAME:
494 offset --;
495 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
496 if (ret < 0)
497 goto err;
498 OSMO_SNPRINTF_RET(ret, rem, offset, len);
499 break;
500 }
501 }
502
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100503 if (target->use_color) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100504 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100505 if (ret < 0)
506 goto err;
507 OSMO_SNPRINTF_RET(ret, rem, offset, len);
508 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200509err:
510 buf[sizeof(buf)-1] = '\0';
511 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800512}
513
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100514/* Catch internal logging category indexes as well as out-of-bounds indexes.
515 * For internal categories, the ID is negative starting with -1; and internal
516 * logging categories are added behind the user categories. For out-of-bounds
517 * indexes, return the index of DLGLOBAL. The returned category index is
518 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
519 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100520static inline int map_subsys(int subsys)
521{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100522 /* Note: comparing signed and unsigned integers */
523
524 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
525 subsys = DLGLOBAL;
526
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100527 if (subsys < 0)
528 subsys = subsys_lib2index(subsys);
529
Neels Hofmeyrca135742016-12-12 14:18:54 +0100530 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100531 subsys = subsys_lib2index(DLGLOBAL);
532
Neels Hofmeyrca135742016-12-12 14:18:54 +0100533 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
534
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100535 return subsys;
536}
537
Maxc65c5b42017-03-15 13:20:23 +0100538static inline bool should_log_to_target(struct log_target *tar, int subsys,
539 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100540{
541 struct log_category *category;
542
543 category = &tar->categories[subsys];
544
545 /* subsystem is not supposed to be logged */
546 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100547 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100548
549 /* Check the global log level */
550 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100551 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100552
553 /* Check the category log level */
554 if (tar->loglevel == 0 && category->loglevel != 0 &&
555 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100556 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100557
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100558 /* Apply filters here... if that becomes messy we will
559 * need to put filters in a list and each filter will
560 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100561 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100562 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100563
564 if (osmo_log_info->filter_fn)
565 return osmo_log_info->filter_fn(&log_context, tar);
566
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100567 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100568 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100569}
570
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200571/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200572 * \param[in] subsys Logging sub-system
573 * \param[in] level Log level
574 * \param[in] file name of source code file
575 * \param[in] cont continuation (1) or new line (0)
576 * \param[in] format format string
577 * \param[in] ap vararg-list containing format string arguments
578 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200579void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200580 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800581{
Harald Welte3ae27582010-03-26 21:24:24 +0800582 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800583
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100584 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200585
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200586 log_tgt_mutex_lock();
587
Harald Welte28222962011-02-18 20:37:04 +0100588 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200589 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800590
Maxc65c5b42017-03-15 13:20:23 +0100591 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800592 continue;
593
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200594 /* According to the manpage, vsnprintf leaves the value of ap
595 * in undefined state. Since _output uses vsnprintf and it may
596 * be called several times, we have to pass a copy of ap. */
597 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100598 if (tar->raw_output)
599 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
600 else
601 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200602 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800603 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200604
605 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800606}
607
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200608/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200609 * \param[in] subsys Logging sub-system
610 * \param[in] file name of source code file
611 * \param[in] cont continuation (1) or new line (0)
612 * \param[in] format format string
613 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200614void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800615 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800616{
617 va_list ap;
618
619 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200620 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621 va_end(ap);
622}
623
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200624/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200625 * \param[in] subsys Logging sub-system
626 * \param[in] level Log level
627 * \param[in] file name of source code file
628 * \param[in] cont continuation (1) or new line (0)
629 * \param[in] format format string
630 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200631void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800632{
633 va_list ap;
634
635 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200636 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637 va_end(ap);
638}
639
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200640/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200641 * \param[in] target Log target to be registered
642 */
Harald Welte3ae27582010-03-26 21:24:24 +0800643void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800644{
Harald Welte28222962011-02-18 20:37:04 +0100645 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800646}
647
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200648/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200649 * \param[in] target Log target to be unregistered
650 */
Harald Welte3ae27582010-03-26 21:24:24 +0800651void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800652{
653 llist_del(&target->entry);
654}
655
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200656/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800657void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800658{
Harald Welte3ae27582010-03-26 21:24:24 +0800659 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800660}
661
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200662/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200663 * \param[in] ctx_nr logging context number
664 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200665 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200666 *
667 * A logging context is something like the subscriber identity to which
668 * the currently processed message relates, or the BTS through which it
669 * was received. As soon as this data is known, it can be set using
670 * this function. The main use of context information is for logging
671 * filters.
672 */
Harald Welte3ae27582010-03-26 21:24:24 +0800673int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800674{
Harald Welte3ae27582010-03-26 21:24:24 +0800675 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800676 return -EINVAL;
677
Harald Welte3ae27582010-03-26 21:24:24 +0800678 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800679
680 return 0;
681}
682
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200683/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200684 * \param[in] target Log target to be affected
685 * \param[in] all enable (1) or disable (0) the ALL filter
686 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100687 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100688 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
689 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200690 */
Harald Welte3ae27582010-03-26 21:24:24 +0800691void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800692{
693 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100694 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800695 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100696 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800697}
698
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200699/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200700 * \param[in] target Log target to be affected
701 * \param[in] use_color Use color (1) or don't use color (0)
702 */
Harald Welte3ae27582010-03-26 21:24:24 +0800703void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800704{
705 target->use_color = use_color;
706}
707
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200708/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200709 * \param[in] target Log target to be affected
710 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
711 */
Harald Welte3ae27582010-03-26 21:24:24 +0800712void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800713{
714 target->print_timestamp = print_timestamp;
715}
716
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200717/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100718 * \param[in] target Log target to be affected
719 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
720 *
721 * When both timestamp and extended timestamp is enabled then only
722 * the extended timestamp will be used. The format of the timestamp
723 * is YYYYMMDDhhmmssnnn.
724 */
725void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
726{
727 target->print_ext_timestamp = print_timestamp;
728}
729
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100730/*! Use log_set_print_filename2() instead.
731 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
732 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
733 * behavior, which combined the category in hex with the filename. For example, if the category-hex
734 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
735 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200736 * \param[in] target Log target to be affected
737 * \param[in] print_filename Enable (1) or disable (0) filenames
738 */
739void log_set_print_filename(struct log_target *target, int print_filename)
740{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100741 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
742 log_set_print_category_hex(target, print_filename);
743}
744
745/*! Enable or disable printing of the filename while logging.
746 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700747 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100748 * LOG_FILENAME_NONE omits the source file and line information from logs.
749 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
750 */
751void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
752{
753 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200754}
755
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200756/*! Set the position where on a log line the source file info should be logged.
757 * \param[in] target Log target to be affected.
758 * \param[in] pos A LOG_FILENAME_POS_* enum value.
759 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
760 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
761 */
762void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
763{
764 target->print_filename_pos = pos;
765}
766
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200767/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100768 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700769 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100770 *
771 * Print the category/subsys name in front of every log message.
772 */
773void log_set_print_category(struct log_target *target, int print_category)
774{
775 target->print_category = print_category;
776}
777
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100778/*! Enable or disable printing of the category number in hex ('<000b>').
779 * \param[in] target Log target to be affected.
780 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
781 */
782void log_set_print_category_hex(struct log_target *target, int print_category_hex)
783{
784 target->print_category_hex = print_category_hex;
785}
786
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100787/*! Enable or disable printing of the log level name.
788 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700789 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100790 *
791 * Print the log level name in front of every log message.
792 */
793void log_set_print_level(struct log_target *target, int print_level)
794{
795 target->print_level = (bool)print_level;
796}
797
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200798/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200799 * \param[in] target Log target to be affected
800 * \param[in] log_level New global log level
801 */
Harald Welte3ae27582010-03-26 21:24:24 +0800802void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800803{
804 target->loglevel = log_level;
805}
806
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200807/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100808 * \param[in] target Log target to be affected
809 * \param[in] category Log category to be affected
810 * \param[in] enable whether to enable or disable the filter
811 * \param[in] level Log level of the filter
812 */
Harald Welte3ae27582010-03-26 21:24:24 +0800813void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800814 int enable, int level)
815{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100816 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800817 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100818 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800819 target->categories[category].enabled = !!enable;
820 target->categories[category].loglevel = level;
821}
822
Harald Welte44c0f632017-01-15 17:58:29 +0100823#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100824static void _file_output(struct log_target *target, unsigned int level,
825 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800826{
Harald Welte0083cd32010-08-25 14:55:44 +0200827 fprintf(target->tgt_file.out, "%s", log);
828 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800829}
Harald Welte44c0f632017-01-15 17:58:29 +0100830#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800831
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200832/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200833 * \returns dynamically-allocated log target
834 * This funcition allocates a \ref log_target and initializes it
835 * with some default values. The newly created target is not
836 * registered yet.
837 */
Harald Welte3ae27582010-03-26 21:24:24 +0800838struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800839{
Harald Welte3ae27582010-03-26 21:24:24 +0800840 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800841 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800842
Max68bf16a2018-01-10 17:00:43 +0100843 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100844
Harald Welte3ae27582010-03-26 21:24:24 +0800845 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800846 if (!target)
847 return NULL;
848
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200849 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200850 struct log_category,
851 osmo_log_info->num_cat);
852 if (!target->categories) {
853 talloc_free(target);
854 return NULL;
855 }
856
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800857 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800858
859 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200860 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800861 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200862 cat->enabled = osmo_log_info->cat[i].enabled;
863 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800864 }
865
866 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800867 target->use_color = 1;
868 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100869 target->print_filename2 = LOG_FILENAME_PATH;
870 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800871
872 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800873 target->loglevel = 0;
874 return target;
875}
876
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200877/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200878 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800879struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800880{
Harald Weltea3b844c2010-03-27 00:04:40 +0800881/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200882#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800883 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800884
Harald Welte3ae27582010-03-26 21:24:24 +0800885 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800886 if (!target)
887 return NULL;
888
Harald Welte28222962011-02-18 20:37:04 +0100889 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200890 target->tgt_file.out = stderr;
891 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800892 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800893#else
894 return NULL;
895#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800896}
897
Harald Welte44c0f632017-01-15 17:58:29 +0100898#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200899/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200900 * \param[in] fname File name of the new log file
901 * \returns Log target in case of success, NULL otherwise
902 */
Harald Welte3086c392010-08-25 19:10:50 +0200903struct log_target *log_target_create_file(const char *fname)
904{
905 struct log_target *target;
906
907 target = log_target_create();
908 if (!target)
909 return NULL;
910
Harald Welte28222962011-02-18 20:37:04 +0100911 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200912 target->tgt_file.out = fopen(fname, "a");
913 if (!target->tgt_file.out)
914 return NULL;
915
916 target->output = _file_output;
917
918 target->tgt_file.fname = talloc_strdup(target, fname);
919
920 return target;
921}
Harald Welte44c0f632017-01-15 17:58:29 +0100922#endif
Harald Welte3086c392010-08-25 19:10:50 +0200923
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200924/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200925 * \param[in] type Log target type
926 * \param[in] fname File name
927 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200928 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +0200929 */
Harald Welte28222962011-02-18 20:37:04 +0100930struct log_target *log_target_find(int type, const char *fname)
931{
932 struct log_target *tgt;
933
934 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
935 if (tgt->type != type)
936 continue;
Maxc90f40a2018-01-11 10:52:28 +0100937 switch (tgt->type) {
938 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100939 if (!strcmp(fname, tgt->tgt_file.fname))
940 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100941 break;
942 case LOG_TGT_TYPE_GSMTAP:
943 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
944 return tgt;
945 break;
946 default:
Harald Welte28222962011-02-18 20:37:04 +0100947 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100948 }
Harald Welte28222962011-02-18 20:37:04 +0100949 }
950 return NULL;
951}
952
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200953/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700954 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200955void log_target_destroy(struct log_target *target)
956{
957
958 /* just in case, to make sure we don't have any references */
959 log_del_target(target);
960
Harald Welte44c0f632017-01-15 17:58:29 +0100961#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200962 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200963/* since C89/C99 says stderr is a macro, we can safely do this! */
964#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200965 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200966 if (target->tgt_file.out != stderr)
967#endif
968 {
Harald Welte3086c392010-08-25 19:10:50 +0200969 fclose(target->tgt_file.out);
970 target->tgt_file.out = NULL;
971 }
972 }
Harald Welte44c0f632017-01-15 17:58:29 +0100973#endif
Harald Welte3086c392010-08-25 19:10:50 +0200974
975 talloc_free(target);
976}
977
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200978/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200979 * \param[in] target log target to re-open
980 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200981int log_target_file_reopen(struct log_target *target)
982{
983 fclose(target->tgt_file.out);
984
985 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
986 if (!target->tgt_file.out)
987 return -errno;
988
989 /* we assume target->output already to be set */
990
991 return 0;
992}
993
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200994/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200995 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100996int log_targets_reopen(void)
997{
998 struct log_target *tar;
999 int rc = 0;
1000
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001001 log_tgt_mutex_lock();
1002
Harald Welte4de854d2013-03-18 19:01:40 +01001003 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1004 switch (tar->type) {
1005 case LOG_TGT_TYPE_FILE:
1006 if (log_target_file_reopen(tar) < 0)
1007 rc = -1;
1008 break;
1009 default:
1010 break;
1011 }
1012 }
1013
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001014 log_tgt_mutex_unlock();
1015
Harald Welte4de854d2013-03-18 19:01:40 +01001016 return rc;
1017}
1018
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001019/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001020 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001021 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001022 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001023 *
1024 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001025 */
Harald Welteb43bc042011-06-27 10:29:17 +02001026int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001027{
Harald Welteb43bc042011-06-27 10:29:17 +02001028 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001029 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001030
Philipp Maierdc02c062020-05-12 17:51:25 +02001031 /* Ensure that log_init is not called multiple times */
1032 OSMO_ASSERT(tall_log_ctx == NULL)
1033
Harald Welteb43bc042011-06-27 10:29:17 +02001034 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1035 if (!tall_log_ctx)
1036 return -ENOMEM;
1037
1038 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1039 if (!osmo_log_info)
1040 return -ENOMEM;
1041
Max72dfd432018-12-04 11:24:18 +01001042 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1043
1044 if (inf) {
1045 osmo_log_info->filter_fn = inf->filter_fn;
1046 osmo_log_info->num_cat_user = inf->num_cat;
1047 osmo_log_info->num_cat += inf->num_cat;
1048 }
Harald Welteb43bc042011-06-27 10:29:17 +02001049
Philipp Maierdcad1c52020-03-25 11:25:59 +01001050 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1051 osmo_log_info->num_cat);
1052 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001053 talloc_free(osmo_log_info);
1054 osmo_log_info = NULL;
1055 return -ENOMEM;
1056 }
1057
Philipp Maierdcad1c52020-03-25 11:25:59 +01001058 /* copy over the user part and sanitize loglevel */
1059 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001060 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001061 memcpy(&cat_ptr[i], &inf->cat[i],
1062 sizeof(struct log_info_cat));
1063
1064 /* Make sure that the loglevel is set to NOTICE in case
1065 * no loglevel has been preset. */
1066 if (!cat_ptr[i].loglevel) {
1067 cat_ptr[i].loglevel = LOGL_NOTICE;
1068 }
Max72dfd432018-12-04 11:24:18 +01001069 }
Harald Welteb43bc042011-06-27 10:29:17 +02001070 }
1071
1072 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001073 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001074 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001075 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001076 }
1077
Philipp Maierdcad1c52020-03-25 11:25:59 +01001078 osmo_log_info->cat = cat_ptr;
1079
Harald Welte9fe16522011-06-27 14:00:03 +02001080 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001081}
Harald Welte18fc4652011-08-17 14:14:17 +02001082
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001083/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001084 * This function destroys all targets and releases associated memory */
1085void log_fini(void)
1086{
1087 struct log_target *tar, *tar2;
1088
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001089 log_tgt_mutex_lock();
1090
Harald Welte69e6c3c2016-04-20 10:41:27 +02001091 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1092 log_target_destroy(tar);
1093
1094 talloc_free(osmo_log_info);
1095 osmo_log_info = NULL;
1096 talloc_free(tall_log_ctx);
1097 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001098
1099 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001100}
1101
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001102/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001103 * \returns != 0 if a log entry might get generated by at least one target */
1104int log_check_level(int subsys, unsigned int level)
1105{
1106 struct log_target *tar;
1107
Max68bf16a2018-01-10 17:00:43 +01001108 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001109
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001110 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001111
1112 /* TODO: The following could/should be cached (update on config) */
1113
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001114 log_tgt_mutex_lock();
1115
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001116 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001117 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001118 continue;
1119
1120 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001121 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001122 return 1;
1123 }
1124
1125 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001126 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001127 return 0;
1128}
1129
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001130/*! @} */