blob: 2bb53ae607fdf0951ba86886e4a3da1da2b9e176 [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>
45
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010046#include <osmocom/core/talloc.h>
47#include <osmocom/core/utils.h>
48#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020049#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080050
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010051#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
52
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010053osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010054 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010055osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010056 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010057osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010058 enum_logging_filters_fit_in_log_target_filter_map);
59
Harald Welteb43bc042011-06-27 10:29:17 +020060struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080061
Harald Welte3ae27582010-03-26 21:24:24 +080062static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020063void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010064LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080065
Neels Hofmeyr098038a2018-09-11 23:49:13 +020066const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080067 { LOGL_DEBUG, "DEBUG" },
68 { LOGL_INFO, "INFO" },
69 { LOGL_NOTICE, "NOTICE" },
70 { LOGL_ERROR, "ERROR" },
71 { LOGL_FATAL, "FATAL" },
72 { 0, NULL },
73};
74
Harald Welteb43bc042011-06-27 10:29:17 +020075#define INT2IDX(x) (-1*(x)-1)
76static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
77 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
78 .name = "DLGLOBAL",
79 .description = "Library-internal global log family",
80 .loglevel = LOGL_NOTICE,
81 .enabled = 1,
82 },
root8a996b42011-09-26 11:22:21 +020083 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
84 .name = "DLLAPD",
85 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020086 .loglevel = LOGL_NOTICE,
87 .enabled = 1,
88 },
Harald Welte892e6212011-07-19 14:31:44 +020089 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020090 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020091 .description = "A-bis Intput Subsystem",
92 .loglevel = LOGL_NOTICE,
93 .enabled = 1,
94 },
Harald Welte892e6212011-07-19 14:31:44 +020095 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +020096 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020097 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
98 .loglevel = LOGL_NOTICE,
99 .enabled = 1,
100 },
Harald Welte892e6212011-07-19 14:31:44 +0200101 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200102 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200103 .description = "A-bis Input Driver for Signalling",
104 .enabled = 0, .loglevel = LOGL_NOTICE,
105 },
Harald Welte892e6212011-07-19 14:31:44 +0200106 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200107 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200108 .description = "A-bis Input Driver for B-Channels (voice)",
109 .enabled = 0, .loglevel = LOGL_NOTICE,
110 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200111 [INT2IDX(DLSMS)] = {
112 .name = "DLSMS",
113 .description = "Layer3 Short Message Service (SMS)",
114 .enabled = 1, .loglevel = LOGL_NOTICE,
115 .color = "\033[1;38m",
116 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200117 [INT2IDX(DLCTRL)] = {
118 .name = "DLCTRL",
119 .description = "Control Interface",
120 .enabled = 1, .loglevel = LOGL_NOTICE,
121 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100122 [INT2IDX(DLGTP)] = {
123 .name = "DLGTP",
124 .description = "GPRS GTP library",
125 .enabled = 1, .loglevel = LOGL_NOTICE,
126 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100127 [INT2IDX(DLSTATS)] = {
128 .name = "DLSTATS",
129 .description = "Statistics messages and logging",
130 .enabled = 1, .loglevel = LOGL_NOTICE,
131 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100132 [INT2IDX(DLGSUP)] = {
133 .name = "DLGSUP",
134 .description = "Generic Subscriber Update Protocol",
135 .enabled = 1, .loglevel = LOGL_NOTICE,
136 },
Harald Weltec0f00072016-04-27 18:32:35 +0200137 [INT2IDX(DLOAP)] = {
138 .name = "DLOAP",
139 .description = "Osmocom Authentication Protocol",
140 .enabled = 1, .loglevel = LOGL_NOTICE,
141 },
Harald Welte059c4042017-04-03 22:20:49 +0200142 [INT2IDX(DLSS7)] = {
143 .name = "DLSS7",
144 .description = "libosmo-sigtran Signalling System 7",
145 .enabled = 1, .loglevel = LOGL_NOTICE,
146 },
147 [INT2IDX(DLSCCP)] = {
148 .name = "DLSCCP",
149 .description = "libosmo-sigtran SCCP Implementation",
150 .enabled = 1, .loglevel = LOGL_NOTICE,
151 },
152 [INT2IDX(DLSUA)] = {
153 .name = "DLSUA",
154 .description = "libosmo-sigtran SCCP User Adaptation",
155 .enabled = 1, .loglevel = LOGL_NOTICE,
156 },
157 [INT2IDX(DLM3UA)] = {
158 .name = "DLM3UA",
159 .description = "libosmo-sigtran MTP3 User Adaptation",
160 .enabled = 1, .loglevel = LOGL_NOTICE,
161 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200162 [INT2IDX(DLMGCP)] = {
163 .name = "DLMGCP",
164 .description = "libosmo-mgcp Media Gateway Control Protocol",
165 .enabled = 1, .loglevel = LOGL_NOTICE,
166 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100167 [INT2IDX(DLJIBUF)] = {
168 .name = "DLJIBUF",
169 .description = "libosmo-netif Jitter Buffer",
170 .enabled = 1, .loglevel = LOGL_NOTICE,
171 },
Harald Welteb43bc042011-06-27 10:29:17 +0200172};
173
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200174void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100175{
176 if (!osmo_log_info) {
177 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100178 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100179 OSMO_ASSERT(osmo_log_info);
180 }
181}
182
Harald Welteb43bc042011-06-27 10:29:17 +0200183/* special magic for negative (library-internal) log subsystem numbers */
184static int subsys_lib2index(int subsys)
185{
186 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
187}
188
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189/*! Parse a human-readable log level into a numeric value
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200190 * \param lvl[in] zero-terminated string containing log level name
191 * \returns numeric log level
192 */
Harald Welte3ae27582010-03-26 21:24:24 +0800193int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800194{
195 return get_string_value(loglevel_strs, lvl);
196}
197
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200198/*! convert a numeric log level into human-readable string
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200199 * \param lvl[in] numeric log level
200 * \returns zero-terminated string (log level name)
201 */
Harald Welte9ac22252010-05-11 11:19:40 +0200202const char *log_level_str(unsigned int lvl)
203{
204 return get_value_string(loglevel_strs, lvl);
205}
206
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200207/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200208 * \param[in] category human-readable log category name
209 * \returns numeric category value, or -EINVAL otherwise
210 */
Harald Welte3ae27582010-03-26 21:24:24 +0800211int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800212{
213 int i;
214
Max68bf16a2018-01-10 17:00:43 +0100215 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100216
Harald Welte4ebdf742010-05-19 19:54:00 +0200217 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200218 if (osmo_log_info->cat[i].name == NULL)
219 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200220 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800221 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800222 }
223
224 return -EINVAL;
225}
226
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200227/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200228 * \param[in] target log target to be configured
229 * \param[in] _mask log category mask string
230 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800231 * The format can be this: category1:category2:category3
232 * or category1,2:category2,3:...
233 */
Harald Welte3ae27582010-03-26 21:24:24 +0800234void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800235{
236 int i = 0;
237 char *mask = strdup(_mask);
238 char *category_token = NULL;
239
Max68bf16a2018-01-10 17:00:43 +0100240 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100241
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800242 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200243 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800244 target->categories[i].enabled = 0;
245
246 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200247 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800248 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200249 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200250 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800251 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200252
253 if (!osmo_log_info->cat[i].name)
254 continue;
255
256 length = strlen(category_token);
257 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200258
259 /* Use longest length not to match subocurrences. */
260 if (cat_length > length)
261 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800262
263 if (colon)
264 length = colon - category_token;
265
Harald Welte4ebdf742010-05-19 19:54:00 +0200266 if (strncasecmp(osmo_log_info->cat[i].name,
267 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800268 int level = 0;
269
270 if (colon)
271 level = atoi(colon+1);
272
Harald Weltefaadfe22010-03-26 21:05:43 +0800273 target->categories[i].enabled = 1;
274 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800275 }
276 }
277 } while ((category_token = strtok(NULL, ":")));
278
279 free(mask);
280}
281
282static const char* color(int subsys)
283{
Harald Welte4ebdf742010-05-19 19:54:00 +0200284 if (subsys < osmo_log_info->num_cat)
285 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800286
Harald Welted788f662010-03-26 09:45:03 +0800287 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800288}
289
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100290static const struct value_string level_colors[] = {
291 { LOGL_DEBUG, "\033[1;34m" },
292 { LOGL_INFO, "\033[1;32m" },
293 { LOGL_NOTICE, "\033[1;33m" },
294 { LOGL_ERROR, "\033[1;31m" },
295 { LOGL_FATAL, "\033[1;31m" },
296 { 0, NULL }
297};
298
299static const char *level_color(int level)
300{
301 const char *c = get_value_string_or_null(level_colors, level);
302 if (!c)
303 return get_value_string(level_colors, LOGL_FATAL);
304 return c;
305}
306
Harald Welteaa00f992016-12-02 15:30:02 +0100307const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100308{
309 if (subsys < osmo_log_info->num_cat)
310 return osmo_log_info->cat[subsys].name;
311
312 return NULL;
313}
314
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100315static const char *const_basename(const char *path)
316{
317 const char *bn = strrchr(path, '/');
318 if (!bn || !bn[1])
319 return path;
320 return bn + 1;
321}
322
Harald Welte3ae27582010-03-26 21:24:24 +0800323static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200324 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100325 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800326{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800327 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200328 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100329 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800330
331 /* are we using color */
332 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100333 c_subsys = color(subsys);
334 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100335 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200336 if (ret < 0)
337 goto err;
338 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800339 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800340 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800341 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100342 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200343#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100344 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100345 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200346 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100347 localtime_r(&tv.tv_sec, &tm);
348 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100349 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100350 tm.tm_hour, tm.tm_min, tm.tm_sec,
351 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100352 if (ret < 0)
353 goto err;
354 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200355#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100356 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800357 char *timestr;
358 time_t tm;
359 tm = time(NULL);
360 timestr = ctime(&tm);
361 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200362 ret = snprintf(buf + offset, rem, "%s ", timestr);
363 if (ret < 0)
364 goto err;
365 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800366 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100367 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100368 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
369 target->use_color ? level_color(level) : "",
370 log_category_name(subsys),
371 target->use_color ? "\033[0;m" : "",
372 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100373 if (ret < 0)
374 goto err;
375 OSMO_SNPRINTF_RET(ret, rem, offset, len);
376 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100377 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100378 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
379 target->use_color ? level_color(level) : "",
380 log_level_str(level),
381 target->use_color ? "\033[0;m" : "",
382 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100383 if (ret < 0)
384 goto err;
385 OSMO_SNPRINTF_RET(ret, rem, offset, len);
386 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100387 if (target->print_category_hex) {
388 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200389 if (ret < 0)
390 goto err;
391 OSMO_SNPRINTF_RET(ret, rem, offset, len);
392 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200393
394 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
395 switch (target->print_filename2) {
396 case LOG_FILENAME_NONE:
397 break;
398 case LOG_FILENAME_PATH:
399 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
400 if (ret < 0)
401 goto err;
402 OSMO_SNPRINTF_RET(ret, rem, offset, len);
403 break;
404 case LOG_FILENAME_BASENAME:
405 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
406 if (ret < 0)
407 goto err;
408 OSMO_SNPRINTF_RET(ret, rem, offset, len);
409 break;
410 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100411 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800412 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200413 ret = vsnprintf(buf + offset, rem, format, ap);
414 if (ret < 0)
415 goto err;
416 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800417
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200418 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
419 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
420 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
421 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
422 && offset > 0 && buf[offset-1] == '\n') {
423 switch (target->print_filename2) {
424 case LOG_FILENAME_NONE:
425 break;
426 case LOG_FILENAME_PATH:
427 offset --;
428 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
429 if (ret < 0)
430 goto err;
431 OSMO_SNPRINTF_RET(ret, rem, offset, len);
432 break;
433 case LOG_FILENAME_BASENAME:
434 offset --;
435 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
436 if (ret < 0)
437 goto err;
438 OSMO_SNPRINTF_RET(ret, rem, offset, len);
439 break;
440 }
441 }
442
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100443 if (target->use_color) {
444 ret = snprintf(buf + offset, rem, "\033[0;m");
445 if (ret < 0)
446 goto err;
447 OSMO_SNPRINTF_RET(ret, rem, offset, len);
448 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200449err:
450 buf[sizeof(buf)-1] = '\0';
451 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800452}
453
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100454/* Catch internal logging category indexes as well as out-of-bounds indexes.
455 * For internal categories, the ID is negative starting with -1; and internal
456 * logging categories are added behind the user categories. For out-of-bounds
457 * indexes, return the index of DLGLOBAL. The returned category index is
458 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
459 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100460static inline int map_subsys(int subsys)
461{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100462 /* Note: comparing signed and unsigned integers */
463
464 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
465 subsys = DLGLOBAL;
466
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100467 if (subsys < 0)
468 subsys = subsys_lib2index(subsys);
469
Neels Hofmeyrca135742016-12-12 14:18:54 +0100470 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100471 subsys = subsys_lib2index(DLGLOBAL);
472
Neels Hofmeyrca135742016-12-12 14:18:54 +0100473 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
474
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100475 return subsys;
476}
477
Maxc65c5b42017-03-15 13:20:23 +0100478static inline bool should_log_to_target(struct log_target *tar, int subsys,
479 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100480{
481 struct log_category *category;
482
483 category = &tar->categories[subsys];
484
485 /* subsystem is not supposed to be logged */
486 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100487 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100488
489 /* Check the global log level */
490 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100491 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100492
493 /* Check the category log level */
494 if (tar->loglevel == 0 && category->loglevel != 0 &&
495 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100496 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100497
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100498 /* Apply filters here... if that becomes messy we will
499 * need to put filters in a list and each filter will
500 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100501 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100502 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100503
504 if (osmo_log_info->filter_fn)
505 return osmo_log_info->filter_fn(&log_context, tar);
506
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100507 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100508 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100509}
510
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200511/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200512 * \param[in] subsys Logging sub-system
513 * \param[in] level Log level
514 * \param[in] file name of source code file
515 * \param[in] cont continuation (1) or new line (0)
516 * \param[in] format format string
517 * \param[in] ap vararg-list containing format string arguments
518 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200519void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200520 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800521{
Harald Welte3ae27582010-03-26 21:24:24 +0800522 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800523
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100524 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200525
Harald Welte28222962011-02-18 20:37:04 +0100526 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200527 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800528
Maxc65c5b42017-03-15 13:20:23 +0100529 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800530 continue;
531
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200532 /* According to the manpage, vsnprintf leaves the value of ap
533 * in undefined state. Since _output uses vsnprintf and it may
534 * be called several times, we have to pass a copy of ap. */
535 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100536 if (tar->raw_output)
537 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
538 else
539 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200540 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800541 }
542}
543
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200544/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200545 * \param[in] subsys Logging sub-system
546 * \param[in] file name of source code file
547 * \param[in] cont continuation (1) or new line (0)
548 * \param[in] format format string
549 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200550void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800551 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800552{
553 va_list ap;
554
555 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200556 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800557 va_end(ap);
558}
559
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200560/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200561 * \param[in] subsys Logging sub-system
562 * \param[in] level Log level
563 * \param[in] file name of source code file
564 * \param[in] cont continuation (1) or new line (0)
565 * \param[in] format format string
566 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200567void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800568{
569 va_list ap;
570
571 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200572 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800573 va_end(ap);
574}
575
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200576/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200577 * \param[in] target Log target to be registered
578 */
Harald Welte3ae27582010-03-26 21:24:24 +0800579void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800580{
Harald Welte28222962011-02-18 20:37:04 +0100581 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800582}
583
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200584/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200585 * \param[in] target Log target to be unregistered
586 */
Harald Welte3ae27582010-03-26 21:24:24 +0800587void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800588{
589 llist_del(&target->entry);
590}
591
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200592/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800593void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800594{
Harald Welte3ae27582010-03-26 21:24:24 +0800595 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800596}
597
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200598/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200599 * \param[in] ctx_nr logging context number
600 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200601 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200602 *
603 * A logging context is something like the subscriber identity to which
604 * the currently processed message relates, or the BTS through which it
605 * was received. As soon as this data is known, it can be set using
606 * this function. The main use of context information is for logging
607 * filters.
608 */
Harald Welte3ae27582010-03-26 21:24:24 +0800609int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800610{
Harald Welte3ae27582010-03-26 21:24:24 +0800611 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800612 return -EINVAL;
613
Harald Welte3ae27582010-03-26 21:24:24 +0800614 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800615
616 return 0;
617}
618
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200619/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200620 * \param[in] target Log target to be affected
621 * \param[in] all enable (1) or disable (0) the ALL filter
622 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100623 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100624 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
625 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200626 */
Harald Welte3ae27582010-03-26 21:24:24 +0800627void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800628{
629 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100630 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800631 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100632 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800633}
634
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200635/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200636 * \param[in] target Log target to be affected
637 * \param[in] use_color Use color (1) or don't use color (0)
638 */
Harald Welte3ae27582010-03-26 21:24:24 +0800639void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800640{
641 target->use_color = use_color;
642}
643
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200644/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200645 * \param[in] target Log target to be affected
646 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
647 */
Harald Welte3ae27582010-03-26 21:24:24 +0800648void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800649{
650 target->print_timestamp = print_timestamp;
651}
652
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200653/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100654 * \param[in] target Log target to be affected
655 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
656 *
657 * When both timestamp and extended timestamp is enabled then only
658 * the extended timestamp will be used. The format of the timestamp
659 * is YYYYMMDDhhmmssnnn.
660 */
661void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
662{
663 target->print_ext_timestamp = print_timestamp;
664}
665
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100666/*! Use log_set_print_filename2() instead.
667 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
668 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
669 * behavior, which combined the category in hex with the filename. For example, if the category-hex
670 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
671 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200672 * \param[in] target Log target to be affected
673 * \param[in] print_filename Enable (1) or disable (0) filenames
674 */
675void log_set_print_filename(struct log_target *target, int print_filename)
676{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100677 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
678 log_set_print_category_hex(target, print_filename);
679}
680
681/*! Enable or disable printing of the filename while logging.
682 * \param[in] target Log target to be affected.
683 * \param[in] print_filename An LOG_FILENAME_* enum value.
684 * LOG_FILENAME_NONE omits the source file and line information from logs.
685 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
686 */
687void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
688{
689 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200690}
691
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200692/*! Set the position where on a log line the source file info should be logged.
693 * \param[in] target Log target to be affected.
694 * \param[in] pos A LOG_FILENAME_POS_* enum value.
695 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
696 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
697 */
698void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
699{
700 target->print_filename_pos = pos;
701}
702
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200703/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100704 * \param[in] target Log target to be affected
705 * \param[in] print_catname Enable (1) or disable (0) filenames
706 *
707 * Print the category/subsys name in front of every log message.
708 */
709void log_set_print_category(struct log_target *target, int print_category)
710{
711 target->print_category = print_category;
712}
713
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100714/*! Enable or disable printing of the category number in hex ('<000b>').
715 * \param[in] target Log target to be affected.
716 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
717 */
718void log_set_print_category_hex(struct log_target *target, int print_category_hex)
719{
720 target->print_category_hex = print_category_hex;
721}
722
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100723/*! Enable or disable printing of the log level name.
724 * \param[in] target Log target to be affected
725 * \param[in] print_catname Enable (1) or disable (0) filenames
726 *
727 * Print the log level name in front of every log message.
728 */
729void log_set_print_level(struct log_target *target, int print_level)
730{
731 target->print_level = (bool)print_level;
732}
733
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200734/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200735 * \param[in] target Log target to be affected
736 * \param[in] log_level New global log level
737 */
Harald Welte3ae27582010-03-26 21:24:24 +0800738void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800739{
740 target->loglevel = log_level;
741}
742
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200743/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100744 * \param[in] target Log target to be affected
745 * \param[in] category Log category to be affected
746 * \param[in] enable whether to enable or disable the filter
747 * \param[in] level Log level of the filter
748 */
Harald Welte3ae27582010-03-26 21:24:24 +0800749void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800750 int enable, int level)
751{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100752 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800753 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100754 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755 target->categories[category].enabled = !!enable;
756 target->categories[category].loglevel = level;
757}
758
Harald Welte44c0f632017-01-15 17:58:29 +0100759#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100760static void _file_output(struct log_target *target, unsigned int level,
761 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800762{
Harald Welte0083cd32010-08-25 14:55:44 +0200763 fprintf(target->tgt_file.out, "%s", log);
764 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800765}
Harald Welte44c0f632017-01-15 17:58:29 +0100766#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800767
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200768/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200769 * \returns dynamically-allocated log target
770 * This funcition allocates a \ref log_target and initializes it
771 * with some default values. The newly created target is not
772 * registered yet.
773 */
Harald Welte3ae27582010-03-26 21:24:24 +0800774struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800775{
Harald Welte3ae27582010-03-26 21:24:24 +0800776 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800777 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800778
Max68bf16a2018-01-10 17:00:43 +0100779 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100780
Harald Welte3ae27582010-03-26 21:24:24 +0800781 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800782 if (!target)
783 return NULL;
784
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200785 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200786 struct log_category,
787 osmo_log_info->num_cat);
788 if (!target->categories) {
789 talloc_free(target);
790 return NULL;
791 }
792
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800793 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800794
795 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200796 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800797 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200798 cat->enabled = osmo_log_info->cat[i].enabled;
799 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800800 }
801
802 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800803 target->use_color = 1;
804 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100805 target->print_filename2 = LOG_FILENAME_PATH;
806 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800807
808 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800809 target->loglevel = 0;
810 return target;
811}
812
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200813/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200814 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800815struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800816{
Harald Weltea3b844c2010-03-27 00:04:40 +0800817/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200818#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800819 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800820
Harald Welte3ae27582010-03-26 21:24:24 +0800821 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800822 if (!target)
823 return NULL;
824
Harald Welte28222962011-02-18 20:37:04 +0100825 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200826 target->tgt_file.out = stderr;
827 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800828 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800829#else
830 return NULL;
831#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800832}
833
Harald Welte44c0f632017-01-15 17:58:29 +0100834#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200835/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200836 * \param[in] fname File name of the new log file
837 * \returns Log target in case of success, NULL otherwise
838 */
Harald Welte3086c392010-08-25 19:10:50 +0200839struct log_target *log_target_create_file(const char *fname)
840{
841 struct log_target *target;
842
843 target = log_target_create();
844 if (!target)
845 return NULL;
846
Harald Welte28222962011-02-18 20:37:04 +0100847 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200848 target->tgt_file.out = fopen(fname, "a");
849 if (!target->tgt_file.out)
850 return NULL;
851
852 target->output = _file_output;
853
854 target->tgt_file.fname = talloc_strdup(target, fname);
855
856 return target;
857}
Harald Welte44c0f632017-01-15 17:58:29 +0100858#endif
Harald Welte3086c392010-08-25 19:10:50 +0200859
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200860/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200861 * \param[in] type Log target type
862 * \param[in] fname File name
863 * \returns Log target (if found), NULL otherwise
864 */
Harald Welte28222962011-02-18 20:37:04 +0100865struct log_target *log_target_find(int type, const char *fname)
866{
867 struct log_target *tgt;
868
869 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
870 if (tgt->type != type)
871 continue;
Maxc90f40a2018-01-11 10:52:28 +0100872 switch (tgt->type) {
873 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100874 if (!strcmp(fname, tgt->tgt_file.fname))
875 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100876 break;
877 case LOG_TGT_TYPE_GSMTAP:
878 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
879 return tgt;
880 break;
881 default:
Harald Welte28222962011-02-18 20:37:04 +0100882 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100883 }
Harald Welte28222962011-02-18 20:37:04 +0100884 }
885 return NULL;
886}
887
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200888/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200889 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200890void log_target_destroy(struct log_target *target)
891{
892
893 /* just in case, to make sure we don't have any references */
894 log_del_target(target);
895
Harald Welte44c0f632017-01-15 17:58:29 +0100896#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200897 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200898/* since C89/C99 says stderr is a macro, we can safely do this! */
899#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200900 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200901 if (target->tgt_file.out != stderr)
902#endif
903 {
Harald Welte3086c392010-08-25 19:10:50 +0200904 fclose(target->tgt_file.out);
905 target->tgt_file.out = NULL;
906 }
907 }
Harald Welte44c0f632017-01-15 17:58:29 +0100908#endif
Harald Welte3086c392010-08-25 19:10:50 +0200909
910 talloc_free(target);
911}
912
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200913/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200914 * \param[in] target log target to re-open
915 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200916int log_target_file_reopen(struct log_target *target)
917{
918 fclose(target->tgt_file.out);
919
920 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
921 if (!target->tgt_file.out)
922 return -errno;
923
924 /* we assume target->output already to be set */
925
926 return 0;
927}
928
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200929/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200930 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100931int log_targets_reopen(void)
932{
933 struct log_target *tar;
934 int rc = 0;
935
936 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
937 switch (tar->type) {
938 case LOG_TGT_TYPE_FILE:
939 if (log_target_file_reopen(tar) < 0)
940 rc = -1;
941 break;
942 default:
943 break;
944 }
945 }
946
947 return rc;
948}
949
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200950/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +0100951 * \param[in] inf Information regarding logging categories, could be NULL
Harald Welte18fc4652011-08-17 14:14:17 +0200952 * \param[in] ctx \ref talloc context for logging allocations
953 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +0100954 *
955 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +0200956 */
Harald Welteb43bc042011-06-27 10:29:17 +0200957int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800958{
Harald Welteb43bc042011-06-27 10:29:17 +0200959 int i;
960
961 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
962 if (!tall_log_ctx)
963 return -ENOMEM;
964
965 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
966 if (!osmo_log_info)
967 return -ENOMEM;
968
Max72dfd432018-12-04 11:24:18 +0100969 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
970
971 if (inf) {
972 osmo_log_info->filter_fn = inf->filter_fn;
973 osmo_log_info->num_cat_user = inf->num_cat;
974 osmo_log_info->num_cat += inf->num_cat;
975 }
Harald Welteb43bc042011-06-27 10:29:17 +0200976
977 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
978 struct log_info_cat,
979 osmo_log_info->num_cat);
980 if (!osmo_log_info->cat) {
981 talloc_free(osmo_log_info);
982 osmo_log_info = NULL;
983 return -ENOMEM;
984 }
985
Max72dfd432018-12-04 11:24:18 +0100986 if (inf) { /* copy over the user part */
987 for (i = 0; i < inf->num_cat; i++) {
988 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
989 &inf->cat[i], sizeof(struct log_info_cat));
990 }
Harald Welteb43bc042011-06-27 10:29:17 +0200991 }
992
993 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200994 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200995 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200996 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200997 &internal_cat[i], sizeof(struct log_info_cat));
998 }
999
1000 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001001}
Harald Welte18fc4652011-08-17 14:14:17 +02001002
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001003/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001004 * This function destroys all targets and releases associated memory */
1005void log_fini(void)
1006{
1007 struct log_target *tar, *tar2;
1008
1009 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1010 log_target_destroy(tar);
1011
1012 talloc_free(osmo_log_info);
1013 osmo_log_info = NULL;
1014 talloc_free(tall_log_ctx);
1015 tall_log_ctx = NULL;
1016}
1017
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001018/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001019 * \returns != 0 if a log entry might get generated by at least one target */
1020int log_check_level(int subsys, unsigned int level)
1021{
1022 struct log_target *tar;
1023
Max68bf16a2018-01-10 17:00:43 +01001024 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001025
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001026 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001027
1028 /* TODO: The following could/should be cached (update on config) */
1029
1030 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001031 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001032 continue;
1033
1034 /* This might get logged (ignoring filters) */
1035 return 1;
1036 }
1037
1038 /* We are sure, that this will not be logged. */
1039 return 0;
1040}
1041
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001042/*! @} */