blob: 0b4c1ff32f5c7e5b808be3605efb4a3a898e6bde [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
Vadim Yanitskiy04f42712020-09-09 04:47:25 +070042
43#ifdef HAVE_SYSLOG_H
44#include <syslog.h>
45#endif
46
Harald Welte4a2bb9e2010-03-26 09:33:40 +080047#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010048#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080049#include <errno.h>
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020050#include <pthread.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080051
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010052#include <osmocom/core/talloc.h>
53#include <osmocom/core/utils.h>
54#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020055#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080056
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010057#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
58
Harald Weltea8b1b212020-09-27 17:21:07 +020059/* maximum length of the log string of a single log event (typically line) */
60#define MAX_LOG_SIZE 4096
61
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010062osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010063 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010064osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010065 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010066osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010067 enum_logging_filters_fit_in_log_target_filter_map);
68
Harald Welteb43bc042011-06-27 10:29:17 +020069struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080070
Harald Welte3ae27582010-03-26 21:24:24 +080071static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020072void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010073LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080074
Pau Espin Pedrold12f6982019-09-17 18:38:58 +020075#if (!EMBEDDED)
76/*! This mutex must be held while using osmo_log_target_list or any of its
77 log_targets in a multithread program. Prevents race conditions between threads
78 like producing unordered timestamps or VTY deleting a target while another
79 thread is writing to it */
80static pthread_mutex_t osmo_log_tgt_mutex;
81static bool osmo_log_tgt_mutex_on = false;
82
83/*! Enable multithread support (mutex) in libosmocore logging system.
84 * Must be called by processes willing to use logging subsystem from several
85 * threads. Once enabled, it's not possible to disable it again.
86 */
87void log_enable_multithread(void) {
88 if (osmo_log_tgt_mutex_on)
89 return;
90 pthread_mutex_init(&osmo_log_tgt_mutex, NULL);
91 osmo_log_tgt_mutex_on = true;
92}
93
94/*! Acquire the osmo_log_tgt_mutex. Don't use this function directly, always use
95 * macro log_tgt_mutex_lock() instead.
96 */
97void log_tgt_mutex_lock_impl(void) {
98 /* These lines are useful to debug scenarios where there's only 1 thread
99 and a double lock appears, for instance during startup and some
100 unlock() missing somewhere:
101 if (osmo_log_tgt_mutex_on && pthread_mutex_trylock(&osmo_log_tgt_mutex) != 0)
102 osmo_panic("acquiring already locked mutex!\n");
103 return;
104 */
105
106 if (osmo_log_tgt_mutex_on)
107 pthread_mutex_lock(&osmo_log_tgt_mutex);
108}
109
110/*! Release the osmo_log_tgt_mutex. Don't use this function directly, always use
111 * macro log_tgt_mutex_unlock() instead.
112 */
113void log_tgt_mutex_unlock_impl(void) {
114 if (osmo_log_tgt_mutex_on)
115 pthread_mutex_unlock(&osmo_log_tgt_mutex);
116}
117
118#else /* if (!EMBEDDED) */
119#pragma message ("logging multithread support disabled in embedded build")
120void log_enable_multithread(void) {}
121void log_tgt_mutex_lock_impl(void) {}
122void log_tgt_mutex_unlock_impl(void) {}
123#endif /* if (!EMBEDDED) */
124
Neels Hofmeyr098038a2018-09-11 23:49:13 +0200125const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800126 { LOGL_DEBUG, "DEBUG" },
127 { LOGL_INFO, "INFO" },
128 { LOGL_NOTICE, "NOTICE" },
129 { LOGL_ERROR, "ERROR" },
130 { LOGL_FATAL, "FATAL" },
131 { 0, NULL },
132};
133
Harald Welteb43bc042011-06-27 10:29:17 +0200134#define INT2IDX(x) (-1*(x)-1)
135static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
136 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
137 .name = "DLGLOBAL",
138 .description = "Library-internal global log family",
139 .loglevel = LOGL_NOTICE,
140 .enabled = 1,
141 },
root8a996b42011-09-26 11:22:21 +0200142 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
143 .name = "DLLAPD",
144 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +0200145 .loglevel = LOGL_NOTICE,
146 .enabled = 1,
147 },
Harald Welte892e6212011-07-19 14:31:44 +0200148 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200149 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200150 .description = "A-bis Intput Subsystem",
151 .loglevel = LOGL_NOTICE,
152 .enabled = 1,
153 },
Harald Welte892e6212011-07-19 14:31:44 +0200154 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200155 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200156 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
157 .loglevel = LOGL_NOTICE,
158 .enabled = 1,
159 },
Harald Welte892e6212011-07-19 14:31:44 +0200160 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200161 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200162 .description = "A-bis Input Driver for Signalling",
163 .enabled = 0, .loglevel = LOGL_NOTICE,
164 },
Harald Welte892e6212011-07-19 14:31:44 +0200165 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200166 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200167 .description = "A-bis Input Driver for B-Channels (voice)",
168 .enabled = 0, .loglevel = LOGL_NOTICE,
169 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200170 [INT2IDX(DLSMS)] = {
171 .name = "DLSMS",
172 .description = "Layer3 Short Message Service (SMS)",
173 .enabled = 1, .loglevel = LOGL_NOTICE,
Neels Hofmeyrc5b71752019-11-20 04:50:52 +0100174 .color = OSMO_LOGCOLOR_BRIGHTWHITE,
Andreas Eversbergc626da92011-10-28 03:53:50 +0200175 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200176 [INT2IDX(DLCTRL)] = {
177 .name = "DLCTRL",
178 .description = "Control Interface",
179 .enabled = 1, .loglevel = LOGL_NOTICE,
180 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100181 [INT2IDX(DLGTP)] = {
182 .name = "DLGTP",
183 .description = "GPRS GTP library",
184 .enabled = 1, .loglevel = LOGL_NOTICE,
185 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100186 [INT2IDX(DLSTATS)] = {
187 .name = "DLSTATS",
188 .description = "Statistics messages and logging",
189 .enabled = 1, .loglevel = LOGL_NOTICE,
190 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100191 [INT2IDX(DLGSUP)] = {
192 .name = "DLGSUP",
193 .description = "Generic Subscriber Update Protocol",
194 .enabled = 1, .loglevel = LOGL_NOTICE,
195 },
Harald Weltec0f00072016-04-27 18:32:35 +0200196 [INT2IDX(DLOAP)] = {
197 .name = "DLOAP",
198 .description = "Osmocom Authentication Protocol",
199 .enabled = 1, .loglevel = LOGL_NOTICE,
200 },
Harald Welte059c4042017-04-03 22:20:49 +0200201 [INT2IDX(DLSS7)] = {
202 .name = "DLSS7",
203 .description = "libosmo-sigtran Signalling System 7",
204 .enabled = 1, .loglevel = LOGL_NOTICE,
205 },
206 [INT2IDX(DLSCCP)] = {
207 .name = "DLSCCP",
208 .description = "libosmo-sigtran SCCP Implementation",
209 .enabled = 1, .loglevel = LOGL_NOTICE,
210 },
211 [INT2IDX(DLSUA)] = {
212 .name = "DLSUA",
213 .description = "libosmo-sigtran SCCP User Adaptation",
214 .enabled = 1, .loglevel = LOGL_NOTICE,
215 },
216 [INT2IDX(DLM3UA)] = {
217 .name = "DLM3UA",
218 .description = "libosmo-sigtran MTP3 User Adaptation",
219 .enabled = 1, .loglevel = LOGL_NOTICE,
220 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200221 [INT2IDX(DLMGCP)] = {
222 .name = "DLMGCP",
223 .description = "libosmo-mgcp Media Gateway Control Protocol",
224 .enabled = 1, .loglevel = LOGL_NOTICE,
225 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100226 [INT2IDX(DLJIBUF)] = {
227 .name = "DLJIBUF",
228 .description = "libosmo-netif Jitter Buffer",
229 .enabled = 1, .loglevel = LOGL_NOTICE,
230 },
Max450f5ac2019-02-14 19:12:03 +0100231 [INT2IDX(DLRSPRO)] = {
232 .name = "DLRSPRO",
233 .description = "Remote SIM protocol",
234 .enabled = 1, .loglevel = LOGL_NOTICE,
235 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200236 [INT2IDX(DLNS)] = {
237 .name = "DLNS",
238 .description = "GPRS NS layer",
239 .enabled = 1, .loglevel = LOGL_NOTICE,
240 },
Harald Weltefde19ed2020-12-07 21:43:51 +0100241 [INT2IDX(DLBSSGP)] = {
242 .name = "DLBSSGP",
243 .description = "GPRS BSSGP layer",
244 .enabled = 1, .loglevel = LOGL_NOTICE,
245 },
Harald Welteb43bc042011-06-27 10:29:17 +0200246};
247
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200248void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100249{
250 if (!osmo_log_info) {
251 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100252 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100253 OSMO_ASSERT(osmo_log_info);
254 }
255}
256
Harald Welteb43bc042011-06-27 10:29:17 +0200257/* special magic for negative (library-internal) log subsystem numbers */
258static int subsys_lib2index(int subsys)
259{
260 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
261}
262
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200263/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700264 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200265 * \returns numeric log level
266 */
Harald Welte3ae27582010-03-26 21:24:24 +0800267int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800268{
269 return get_string_value(loglevel_strs, lvl);
270}
271
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200272/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700273 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200274 * \returns zero-terminated string (log level name)
275 */
Harald Welte9ac22252010-05-11 11:19:40 +0200276const char *log_level_str(unsigned int lvl)
277{
278 return get_value_string(loglevel_strs, lvl);
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200282 * \param[in] category human-readable log category name
283 * \returns numeric category value, or -EINVAL otherwise
284 */
Harald Welte3ae27582010-03-26 21:24:24 +0800285int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800286{
287 int i;
288
Max68bf16a2018-01-10 17:00:43 +0100289 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100290
Harald Welte4ebdf742010-05-19 19:54:00 +0200291 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200292 if (osmo_log_info->cat[i].name == NULL)
293 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200294 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800295 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800296 }
297
298 return -EINVAL;
299}
300
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200301/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200302 * \param[in] target log target to be configured
303 * \param[in] _mask log category mask string
304 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800305 * The format can be this: category1:category2:category3
306 * or category1,2:category2,3:...
307 */
Harald Welte3ae27582010-03-26 21:24:24 +0800308void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800309{
310 int i = 0;
311 char *mask = strdup(_mask);
312 char *category_token = NULL;
313
Max68bf16a2018-01-10 17:00:43 +0100314 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100315
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800316 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200317 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800318 target->categories[i].enabled = 0;
319
320 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200321 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800322 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200323 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200324 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800325 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200326
327 if (!osmo_log_info->cat[i].name)
328 continue;
329
330 length = strlen(category_token);
331 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200332
333 /* Use longest length not to match subocurrences. */
334 if (cat_length > length)
335 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800336
337 if (colon)
338 length = colon - category_token;
339
Harald Welte4ebdf742010-05-19 19:54:00 +0200340 if (strncasecmp(osmo_log_info->cat[i].name,
341 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342 int level = 0;
343
344 if (colon)
345 level = atoi(colon+1);
346
Harald Weltefaadfe22010-03-26 21:05:43 +0800347 target->categories[i].enabled = 1;
348 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800349 }
350 }
351 } while ((category_token = strtok(NULL, ":")));
352
353 free(mask);
354}
355
356static const char* color(int subsys)
357{
Harald Welte4ebdf742010-05-19 19:54:00 +0200358 if (subsys < osmo_log_info->num_cat)
359 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800360
Harald Welted788f662010-03-26 09:45:03 +0800361 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800362}
363
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100364static const struct value_string level_colors[] = {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100365 { LOGL_DEBUG, OSMO_LOGCOLOR_BLUE },
366 { LOGL_INFO, OSMO_LOGCOLOR_GREEN },
367 { LOGL_NOTICE, OSMO_LOGCOLOR_YELLOW },
368 { LOGL_ERROR, OSMO_LOGCOLOR_RED },
369 { LOGL_FATAL, OSMO_LOGCOLOR_RED },
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100370 { 0, NULL }
371};
372
373static const char *level_color(int level)
374{
375 const char *c = get_value_string_or_null(level_colors, level);
376 if (!c)
377 return get_value_string(level_colors, LOGL_FATAL);
378 return c;
379}
380
Harald Welteaa00f992016-12-02 15:30:02 +0100381const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100382{
383 if (subsys < osmo_log_info->num_cat)
384 return osmo_log_info->cat[subsys].name;
385
386 return NULL;
387}
388
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100389static const char *const_basename(const char *path)
390{
391 const char *bn = strrchr(path, '/');
392 if (!bn || !bn[1])
393 return path;
394 return bn + 1;
395}
396
Harald Welte3ae27582010-03-26 21:24:24 +0800397static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200398 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100399 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800400{
Harald Weltea8b1b212020-09-27 17:21:07 +0200401 char buf[MAX_LOG_SIZE];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200402 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100403 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800404
405 /* are we using color */
406 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100407 c_subsys = color(subsys);
408 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100409 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200410 if (ret < 0)
411 goto err;
412 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800413 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800414 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800415 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100416 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200417#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100418 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100419 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200420 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100421 localtime_r(&tv.tv_sec, &tm);
422 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100423 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100424 tm.tm_hour, tm.tm_min, tm.tm_sec,
425 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100426 if (ret < 0)
427 goto err;
428 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200429#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100430 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800431 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200432 if ((tm = time(NULL)) == (time_t) -1)
433 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200434 /* Get human-readable representation of time.
435 man ctime: we need at least 26 bytes in buf */
436 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200437 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200438 ret = strlen(buf + offset);
439 if (ret <= 0)
440 goto err;
441 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
442 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200443 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800444 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100445 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100446 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
447 target->use_color ? level_color(level) : "",
448 log_category_name(subsys),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100449 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyre6534722018-01-16 03:02:06 +0100450 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100451 if (ret < 0)
452 goto err;
453 OSMO_SNPRINTF_RET(ret, rem, offset, len);
454 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100455 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100456 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
457 target->use_color ? level_color(level) : "",
458 log_level_str(level),
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100459 target->use_color ? OSMO_LOGCOLOR_END : "",
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100460 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100461 if (ret < 0)
462 goto err;
463 OSMO_SNPRINTF_RET(ret, rem, offset, len);
464 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100465 if (target->print_category_hex) {
466 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200467 if (ret < 0)
468 goto err;
469 OSMO_SNPRINTF_RET(ret, rem, offset, len);
470 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200471
472 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
473 switch (target->print_filename2) {
474 case LOG_FILENAME_NONE:
475 break;
476 case LOG_FILENAME_PATH:
477 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
478 if (ret < 0)
479 goto err;
480 OSMO_SNPRINTF_RET(ret, rem, offset, len);
481 break;
482 case LOG_FILENAME_BASENAME:
483 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
484 if (ret < 0)
485 goto err;
486 OSMO_SNPRINTF_RET(ret, rem, offset, len);
487 break;
488 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100489 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800490 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200491 ret = vsnprintf(buf + offset, rem, format, ap);
492 if (ret < 0)
493 goto err;
494 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800495
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200496 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
497 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
498 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
499 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
500 && offset > 0 && buf[offset-1] == '\n') {
501 switch (target->print_filename2) {
502 case LOG_FILENAME_NONE:
503 break;
504 case LOG_FILENAME_PATH:
505 offset --;
506 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
507 if (ret < 0)
508 goto err;
509 OSMO_SNPRINTF_RET(ret, rem, offset, len);
510 break;
511 case LOG_FILENAME_BASENAME:
512 offset --;
513 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
514 if (ret < 0)
515 goto err;
516 OSMO_SNPRINTF_RET(ret, rem, offset, len);
517 break;
518 }
519 }
520
Pau Espin Pedrol6407c822020-07-20 16:41:44 +0200521 if (target->use_color && c_subsys) {
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100522 ret = snprintf(buf + offset, rem, OSMO_LOGCOLOR_END);
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100523 if (ret < 0)
524 goto err;
525 OSMO_SNPRINTF_RET(ret, rem, offset, len);
526 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200527err:
528 buf[sizeof(buf)-1] = '\0';
529 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800530}
531
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100532/* Catch internal logging category indexes as well as out-of-bounds indexes.
533 * For internal categories, the ID is negative starting with -1; and internal
534 * logging categories are added behind the user categories. For out-of-bounds
535 * indexes, return the index of DLGLOBAL. The returned category index is
536 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
537 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100538static inline int map_subsys(int subsys)
539{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100540 /* Note: comparing signed and unsigned integers */
541
542 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
543 subsys = DLGLOBAL;
544
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100545 if (subsys < 0)
546 subsys = subsys_lib2index(subsys);
547
Neels Hofmeyrca135742016-12-12 14:18:54 +0100548 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100549 subsys = subsys_lib2index(DLGLOBAL);
550
Neels Hofmeyrca135742016-12-12 14:18:54 +0100551 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
552
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100553 return subsys;
554}
555
Maxc65c5b42017-03-15 13:20:23 +0100556static inline bool should_log_to_target(struct log_target *tar, int subsys,
557 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100558{
559 struct log_category *category;
560
561 category = &tar->categories[subsys];
562
563 /* subsystem is not supposed to be logged */
564 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100565 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100566
567 /* Check the global log level */
568 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100569 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100570
571 /* Check the category log level */
572 if (tar->loglevel == 0 && category->loglevel != 0 &&
573 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100574 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100575
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100576 /* Apply filters here... if that becomes messy we will
577 * need to put filters in a list and each filter will
578 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100579 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100580 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100581
582 if (osmo_log_info->filter_fn)
583 return osmo_log_info->filter_fn(&log_context, tar);
584
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100585 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100586 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100587}
588
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200589/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200590 * \param[in] subsys Logging sub-system
591 * \param[in] level Log level
592 * \param[in] file name of source code file
593 * \param[in] cont continuation (1) or new line (0)
594 * \param[in] format format string
595 * \param[in] ap vararg-list containing format string arguments
596 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200597void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200598 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800599{
Harald Welte3ae27582010-03-26 21:24:24 +0800600 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800601
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100602 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200603
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200604 log_tgt_mutex_lock();
605
Harald Welte28222962011-02-18 20:37:04 +0100606 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200607 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800608
Maxc65c5b42017-03-15 13:20:23 +0100609 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800610 continue;
611
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200612 /* According to the manpage, vsnprintf leaves the value of ap
613 * in undefined state. Since _output uses vsnprintf and it may
614 * be called several times, we have to pass a copy of ap. */
615 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100616 if (tar->raw_output)
617 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
618 else
619 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200620 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621 }
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200622
623 log_tgt_mutex_unlock();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800624}
625
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200626/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200627 * \param[in] subsys Logging sub-system
628 * \param[in] file name of source code file
629 * \param[in] cont continuation (1) or new line (0)
630 * \param[in] format format string
631 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200632void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800633 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800634{
635 va_list ap;
636
637 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200638 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800639 va_end(ap);
640}
641
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200642/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200643 * \param[in] subsys Logging sub-system
644 * \param[in] level Log level
645 * \param[in] file name of source code file
646 * \param[in] cont continuation (1) or new line (0)
647 * \param[in] format format string
648 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200649void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800650{
651 va_list ap;
652
653 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200654 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800655 va_end(ap);
656}
657
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200658/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200659 * \param[in] target Log target to be registered
660 */
Harald Welte3ae27582010-03-26 21:24:24 +0800661void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800662{
Harald Welte28222962011-02-18 20:37:04 +0100663 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800664}
665
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200666/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200667 * \param[in] target Log target to be unregistered
668 */
Harald Welte3ae27582010-03-26 21:24:24 +0800669void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800670{
671 llist_del(&target->entry);
672}
673
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200674/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800675void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800676{
Harald Welte3ae27582010-03-26 21:24:24 +0800677 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800678}
679
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200680/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200681 * \param[in] ctx_nr logging context number
682 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200683 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200684 *
685 * A logging context is something like the subscriber identity to which
686 * the currently processed message relates, or the BTS through which it
687 * was received. As soon as this data is known, it can be set using
688 * this function. The main use of context information is for logging
689 * filters.
690 */
Harald Welte3ae27582010-03-26 21:24:24 +0800691int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800692{
Harald Welte3ae27582010-03-26 21:24:24 +0800693 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800694 return -EINVAL;
695
Harald Welte3ae27582010-03-26 21:24:24 +0800696 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800697
698 return 0;
699}
700
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200701/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200702 * \param[in] target Log target to be affected
703 * \param[in] all enable (1) or disable (0) the ALL filter
704 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100705 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100706 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
707 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200708 */
Harald Welte3ae27582010-03-26 21:24:24 +0800709void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800710{
711 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100712 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800713 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100714 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800715}
716
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200717/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200718 * \param[in] target Log target to be affected
719 * \param[in] use_color Use color (1) or don't use color (0)
720 */
Harald Welte3ae27582010-03-26 21:24:24 +0800721void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800722{
723 target->use_color = use_color;
724}
725
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200726/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200727 * \param[in] target Log target to be affected
728 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
729 */
Harald Welte3ae27582010-03-26 21:24:24 +0800730void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800731{
732 target->print_timestamp = print_timestamp;
733}
734
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200735/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100736 * \param[in] target Log target to be affected
737 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
738 *
739 * When both timestamp and extended timestamp is enabled then only
740 * the extended timestamp will be used. The format of the timestamp
741 * is YYYYMMDDhhmmssnnn.
742 */
743void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
744{
745 target->print_ext_timestamp = print_timestamp;
746}
747
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100748/*! Use log_set_print_filename2() instead.
749 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
750 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
751 * behavior, which combined the category in hex with the filename. For example, if the category-hex
752 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
753 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200754 * \param[in] target Log target to be affected
755 * \param[in] print_filename Enable (1) or disable (0) filenames
756 */
757void log_set_print_filename(struct log_target *target, int print_filename)
758{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100759 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
760 log_set_print_category_hex(target, print_filename);
761}
762
763/*! Enable or disable printing of the filename while logging.
764 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700765 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100766 * LOG_FILENAME_NONE omits the source file and line information from logs.
767 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
768 */
769void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
770{
771 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200772}
773
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200774/*! Set the position where on a log line the source file info should be logged.
775 * \param[in] target Log target to be affected.
776 * \param[in] pos A LOG_FILENAME_POS_* enum value.
777 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
778 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
779 */
780void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
781{
782 target->print_filename_pos = pos;
783}
784
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200785/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100786 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700787 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100788 *
789 * Print the category/subsys name in front of every log message.
790 */
791void log_set_print_category(struct log_target *target, int print_category)
792{
793 target->print_category = print_category;
794}
795
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100796/*! Enable or disable printing of the category number in hex ('<000b>').
797 * \param[in] target Log target to be affected.
798 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
799 */
800void log_set_print_category_hex(struct log_target *target, int print_category_hex)
801{
802 target->print_category_hex = print_category_hex;
803}
804
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100805/*! Enable or disable printing of the log level name.
806 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700807 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100808 *
809 * Print the log level name in front of every log message.
810 */
811void log_set_print_level(struct log_target *target, int print_level)
812{
813 target->print_level = (bool)print_level;
814}
815
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200816/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200817 * \param[in] target Log target to be affected
818 * \param[in] log_level New global log level
819 */
Harald Welte3ae27582010-03-26 21:24:24 +0800820void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800821{
822 target->loglevel = log_level;
823}
824
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200825/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100826 * \param[in] target Log target to be affected
827 * \param[in] category Log category to be affected
828 * \param[in] enable whether to enable or disable the filter
829 * \param[in] level Log level of the filter
830 */
Harald Welte3ae27582010-03-26 21:24:24 +0800831void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800832 int enable, int level)
833{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100834 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800835 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100836 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800837 target->categories[category].enabled = !!enable;
838 target->categories[category].loglevel = level;
839}
840
Harald Welte44c0f632017-01-15 17:58:29 +0100841#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100842static void _file_output(struct log_target *target, unsigned int level,
843 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800844{
Harald Welte0083cd32010-08-25 14:55:44 +0200845 fprintf(target->tgt_file.out, "%s", log);
846 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800847}
Harald Welte44c0f632017-01-15 17:58:29 +0100848#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800849
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200850/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200851 * \returns dynamically-allocated log target
852 * This funcition allocates a \ref log_target and initializes it
853 * with some default values. The newly created target is not
854 * registered yet.
855 */
Harald Welte3ae27582010-03-26 21:24:24 +0800856struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800857{
Harald Welte3ae27582010-03-26 21:24:24 +0800858 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800859 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800860
Max68bf16a2018-01-10 17:00:43 +0100861 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100862
Harald Welte3ae27582010-03-26 21:24:24 +0800863 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800864 if (!target)
865 return NULL;
866
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200867 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200868 struct log_category,
869 osmo_log_info->num_cat);
870 if (!target->categories) {
871 talloc_free(target);
872 return NULL;
873 }
874
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800875 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800876
877 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200878 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800879 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200880 cat->enabled = osmo_log_info->cat[i].enabled;
881 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800882 }
883
884 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800885 target->use_color = 1;
886 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100887 target->print_filename2 = LOG_FILENAME_PATH;
888 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800889
890 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800891 target->loglevel = 0;
892 return target;
893}
894
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200895/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200896 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800897struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800898{
Harald Weltea3b844c2010-03-27 00:04:40 +0800899/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200900#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800901 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800902
Harald Welte3ae27582010-03-26 21:24:24 +0800903 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800904 if (!target)
905 return NULL;
906
Harald Welte28222962011-02-18 20:37:04 +0100907 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200908 target->tgt_file.out = stderr;
909 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800910 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800911#else
912 return NULL;
913#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800914}
915
Harald Welte44c0f632017-01-15 17:58:29 +0100916#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200917/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200918 * \param[in] fname File name of the new log file
919 * \returns Log target in case of success, NULL otherwise
920 */
Harald Welte3086c392010-08-25 19:10:50 +0200921struct log_target *log_target_create_file(const char *fname)
922{
923 struct log_target *target;
924
925 target = log_target_create();
926 if (!target)
927 return NULL;
928
Harald Welte28222962011-02-18 20:37:04 +0100929 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200930 target->tgt_file.out = fopen(fname, "a");
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700931 if (!target->tgt_file.out) {
932 log_target_destroy(target);
Harald Welte3086c392010-08-25 19:10:50 +0200933 return NULL;
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700934 }
Harald Welte3086c392010-08-25 19:10:50 +0200935
936 target->output = _file_output;
937
938 target->tgt_file.fname = talloc_strdup(target, fname);
939
940 return target;
941}
Harald Welte44c0f632017-01-15 17:58:29 +0100942#endif
Harald Welte3086c392010-08-25 19:10:50 +0200943
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200944/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200945 * \param[in] type Log target type
946 * \param[in] fname File name
947 * \returns Log target (if found), NULL otherwise
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200948 * Must be called with mutex osmo_log_tgt_mutex held, see log_tgt_mutex_lock.
Harald Welte18fc4652011-08-17 14:14:17 +0200949 */
Harald Welte28222962011-02-18 20:37:04 +0100950struct log_target *log_target_find(int type, const char *fname)
951{
952 struct log_target *tgt;
953
954 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
955 if (tgt->type != type)
956 continue;
Maxc90f40a2018-01-11 10:52:28 +0100957 switch (tgt->type) {
958 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100959 if (!strcmp(fname, tgt->tgt_file.fname))
960 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100961 break;
962 case LOG_TGT_TYPE_GSMTAP:
963 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
964 return tgt;
965 break;
966 default:
Harald Welte28222962011-02-18 20:37:04 +0100967 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100968 }
Harald Welte28222962011-02-18 20:37:04 +0100969 }
970 return NULL;
971}
972
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200973/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700974 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200975void log_target_destroy(struct log_target *target)
976{
977
978 /* just in case, to make sure we don't have any references */
979 log_del_target(target);
980
Harald Welte44c0f632017-01-15 17:58:29 +0100981#if (!EMBEDDED)
Vadim Yanitskiy744236b2020-09-09 04:42:22 +0700982 switch (target->type) {
983 case LOG_TGT_TYPE_FILE:
Vadim Yanitskiyb89114b2020-09-09 04:51:04 +0700984 if (target->tgt_file.out == NULL)
985 break;
Vadim Yanitskiy744236b2020-09-09 04:42:22 +0700986 fclose(target->tgt_file.out);
987 target->tgt_file.out = NULL;
988 break;
Vadim Yanitskiy04f42712020-09-09 04:47:25 +0700989#ifdef HAVE_SYSLOG_H
990 case LOG_TGT_TYPE_SYSLOG:
991 closelog();
992 break;
993#endif /* HAVE_SYSLOG_H */
Vadim Yanitskiy744236b2020-09-09 04:42:22 +0700994 default:
995 /* make GCC happy */
996 break;
Harald Welte3086c392010-08-25 19:10:50 +0200997 }
Harald Welte44c0f632017-01-15 17:58:29 +0100998#endif
Harald Welte3086c392010-08-25 19:10:50 +0200999
1000 talloc_free(target);
1001}
1002
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001003/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001004 * \param[in] target log target to re-open
1005 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +02001006int log_target_file_reopen(struct log_target *target)
1007{
1008 fclose(target->tgt_file.out);
1009
1010 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
1011 if (!target->tgt_file.out)
1012 return -errno;
1013
1014 /* we assume target->output already to be set */
1015
1016 return 0;
1017}
1018
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001019/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001020 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +01001021int log_targets_reopen(void)
1022{
1023 struct log_target *tar;
1024 int rc = 0;
1025
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001026 log_tgt_mutex_lock();
1027
Harald Welte4de854d2013-03-18 19:01:40 +01001028 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
1029 switch (tar->type) {
1030 case LOG_TGT_TYPE_FILE:
1031 if (log_target_file_reopen(tar) < 0)
1032 rc = -1;
1033 break;
1034 default:
1035 break;
1036 }
1037 }
1038
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001039 log_tgt_mutex_unlock();
1040
Harald Welte4de854d2013-03-18 19:01:40 +01001041 return rc;
1042}
1043
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001044/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +01001045 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +07001046 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +02001047 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +01001048 *
1049 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +02001050 */
Harald Welteb43bc042011-06-27 10:29:17 +02001051int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001052{
Harald Welteb43bc042011-06-27 10:29:17 +02001053 int i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001054 struct log_info_cat *cat_ptr;
Harald Welteb43bc042011-06-27 10:29:17 +02001055
Philipp Maierdc02c062020-05-12 17:51:25 +02001056 /* Ensure that log_init is not called multiple times */
1057 OSMO_ASSERT(tall_log_ctx == NULL)
1058
Harald Welteb43bc042011-06-27 10:29:17 +02001059 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1060 if (!tall_log_ctx)
1061 return -ENOMEM;
1062
1063 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1064 if (!osmo_log_info)
1065 return -ENOMEM;
1066
Max72dfd432018-12-04 11:24:18 +01001067 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
1068
1069 if (inf) {
1070 osmo_log_info->filter_fn = inf->filter_fn;
1071 osmo_log_info->num_cat_user = inf->num_cat;
1072 osmo_log_info->num_cat += inf->num_cat;
1073 }
Harald Welteb43bc042011-06-27 10:29:17 +02001074
Philipp Maierdcad1c52020-03-25 11:25:59 +01001075 cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
1076 osmo_log_info->num_cat);
1077 if (!cat_ptr) {
Harald Welteb43bc042011-06-27 10:29:17 +02001078 talloc_free(osmo_log_info);
1079 osmo_log_info = NULL;
1080 return -ENOMEM;
1081 }
1082
Philipp Maierdcad1c52020-03-25 11:25:59 +01001083 /* copy over the user part and sanitize loglevel */
1084 if (inf) {
Max72dfd432018-12-04 11:24:18 +01001085 for (i = 0; i < inf->num_cat; i++) {
Philipp Maierdcad1c52020-03-25 11:25:59 +01001086 memcpy(&cat_ptr[i], &inf->cat[i],
1087 sizeof(struct log_info_cat));
1088
1089 /* Make sure that the loglevel is set to NOTICE in case
1090 * no loglevel has been preset. */
1091 if (!cat_ptr[i].loglevel) {
1092 cat_ptr[i].loglevel = LOGL_NOTICE;
1093 }
Max72dfd432018-12-04 11:24:18 +01001094 }
Harald Welteb43bc042011-06-27 10:29:17 +02001095 }
1096
1097 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001098 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001099 unsigned int cn = osmo_log_info->num_cat_user + i;
Philipp Maierdcad1c52020-03-25 11:25:59 +01001100 memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
Harald Welte9fe16522011-06-27 14:00:03 +02001101 }
1102
Philipp Maierdcad1c52020-03-25 11:25:59 +01001103 osmo_log_info->cat = cat_ptr;
1104
Harald Welte9fe16522011-06-27 14:00:03 +02001105 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001106}
Harald Welte18fc4652011-08-17 14:14:17 +02001107
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001108/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001109 * This function destroys all targets and releases associated memory */
1110void log_fini(void)
1111{
1112 struct log_target *tar, *tar2;
1113
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001114 log_tgt_mutex_lock();
1115
Harald Welte69e6c3c2016-04-20 10:41:27 +02001116 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1117 log_target_destroy(tar);
1118
1119 talloc_free(osmo_log_info);
1120 osmo_log_info = NULL;
1121 talloc_free(tall_log_ctx);
1122 tall_log_ctx = NULL;
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001123
1124 log_tgt_mutex_unlock();
Harald Welte69e6c3c2016-04-20 10:41:27 +02001125}
1126
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001127/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001128 * \returns != 0 if a log entry might get generated by at least one target */
1129int log_check_level(int subsys, unsigned int level)
1130{
1131 struct log_target *tar;
1132
Max68bf16a2018-01-10 17:00:43 +01001133 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001134
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001135 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001136
1137 /* TODO: The following could/should be cached (update on config) */
1138
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001139 log_tgt_mutex_lock();
1140
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001141 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001142 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001143 continue;
1144
1145 /* This might get logged (ignoring filters) */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001146 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001147 return 1;
1148 }
1149
1150 /* We are sure, that this will not be logged. */
Pau Espin Pedrold12f6982019-09-17 18:38:58 +02001151 log_tgt_mutex_unlock();
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001152 return 0;
1153}
1154
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001155/*! @} */