blob: 9b37bf530906d009e2425ff8c1deccd70f792117 [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>
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010038#include <ctype.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080039
40#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080041#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080042#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080043#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010044#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080045#include <errno.h>
46
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010047#include <osmocom/core/talloc.h>
48#include <osmocom/core/utils.h>
49#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020050#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080051
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010052#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
53
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010054osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010055 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010056osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010057 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010058osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010059 enum_logging_filters_fit_in_log_target_filter_map);
60
Harald Welteb43bc042011-06-27 10:29:17 +020061struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080062
Harald Welte3ae27582010-03-26 21:24:24 +080063static struct log_context log_context;
64static void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010065LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080066
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010067#define LOGLEVEL_DEFS 6 /* Number of loglevels.*/
68
69static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = {
Harald Welte9b186702013-03-19 09:55:28 +010070 { 0, "EVERYTHING" },
Harald Welte4a2bb9e2010-03-26 09:33:40 +080071 { LOGL_DEBUG, "DEBUG" },
72 { LOGL_INFO, "INFO" },
73 { LOGL_NOTICE, "NOTICE" },
74 { LOGL_ERROR, "ERROR" },
75 { LOGL_FATAL, "FATAL" },
76 { 0, NULL },
77};
78
Harald Welteb43bc042011-06-27 10:29:17 +020079#define INT2IDX(x) (-1*(x)-1)
80static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
81 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
82 .name = "DLGLOBAL",
83 .description = "Library-internal global log family",
84 .loglevel = LOGL_NOTICE,
85 .enabled = 1,
86 },
root8a996b42011-09-26 11:22:21 +020087 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
88 .name = "DLLAPD",
89 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020090 .loglevel = LOGL_NOTICE,
91 .enabled = 1,
92 },
Harald Welte892e6212011-07-19 14:31:44 +020093 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020094 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020095 .description = "A-bis Intput Subsystem",
96 .loglevel = LOGL_NOTICE,
97 .enabled = 1,
98 },
Harald Welte892e6212011-07-19 14:31:44 +020099 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200100 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200101 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
102 .loglevel = LOGL_NOTICE,
103 .enabled = 1,
104 },
Harald Welte892e6212011-07-19 14:31:44 +0200105 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200106 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200107 .description = "A-bis Input Driver for Signalling",
108 .enabled = 0, .loglevel = LOGL_NOTICE,
109 },
Harald Welte892e6212011-07-19 14:31:44 +0200110 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200111 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200112 .description = "A-bis Input Driver for B-Channels (voice)",
113 .enabled = 0, .loglevel = LOGL_NOTICE,
114 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200115 [INT2IDX(DLSMS)] = {
116 .name = "DLSMS",
117 .description = "Layer3 Short Message Service (SMS)",
118 .enabled = 1, .loglevel = LOGL_NOTICE,
119 .color = "\033[1;38m",
120 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200121 [INT2IDX(DLCTRL)] = {
122 .name = "DLCTRL",
123 .description = "Control Interface",
124 .enabled = 1, .loglevel = LOGL_NOTICE,
125 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100126 [INT2IDX(DLGTP)] = {
127 .name = "DLGTP",
128 .description = "GPRS GTP library",
129 .enabled = 1, .loglevel = LOGL_NOTICE,
130 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100131 [INT2IDX(DLSTATS)] = {
132 .name = "DLSTATS",
133 .description = "Statistics messages and logging",
134 .enabled = 1, .loglevel = LOGL_NOTICE,
135 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100136 [INT2IDX(DLGSUP)] = {
137 .name = "DLGSUP",
138 .description = "Generic Subscriber Update Protocol",
139 .enabled = 1, .loglevel = LOGL_NOTICE,
140 },
Harald Weltec0f00072016-04-27 18:32:35 +0200141 [INT2IDX(DLOAP)] = {
142 .name = "DLOAP",
143 .description = "Osmocom Authentication Protocol",
144 .enabled = 1, .loglevel = LOGL_NOTICE,
145 },
Harald Welte059c4042017-04-03 22:20:49 +0200146 [INT2IDX(DLSS7)] = {
147 .name = "DLSS7",
148 .description = "libosmo-sigtran Signalling System 7",
149 .enabled = 1, .loglevel = LOGL_NOTICE,
150 },
151 [INT2IDX(DLSCCP)] = {
152 .name = "DLSCCP",
153 .description = "libosmo-sigtran SCCP Implementation",
154 .enabled = 1, .loglevel = LOGL_NOTICE,
155 },
156 [INT2IDX(DLSUA)] = {
157 .name = "DLSUA",
158 .description = "libosmo-sigtran SCCP User Adaptation",
159 .enabled = 1, .loglevel = LOGL_NOTICE,
160 },
161 [INT2IDX(DLM3UA)] = {
162 .name = "DLM3UA",
163 .description = "libosmo-sigtran MTP3 User Adaptation",
164 .enabled = 1, .loglevel = LOGL_NOTICE,
165 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200166 [INT2IDX(DLMGCP)] = {
167 .name = "DLMGCP",
168 .description = "libosmo-mgcp Media Gateway Control Protocol",
169 .enabled = 1, .loglevel = LOGL_NOTICE,
170 },
Harald Welteb43bc042011-06-27 10:29:17 +0200171};
172
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173/*! descriptive string for each log level */
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100174/* You have to keep this in sync with the structure loglevel_strs. */
Maxc65c5b42017-03-15 13:20:23 +0100175static const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
Holger Hans Peter Freyther6ec6bd92014-12-28 18:27:38 +0100176 "Don't use. It doesn't log anything",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100177 "Log debug messages and higher levels",
178 "Log informational messages and higher levels",
Ruben Undheim029f5a12015-09-16 19:02:35 +0200179 "Log noticeable messages and higher levels",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100180 "Log error messages and higher levels",
181 "Log only fatal messages",
182 NULL,
183};
184
Max68bf16a2018-01-10 17:00:43 +0100185static void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100186{
187 if (!osmo_log_info) {
188 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100189 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100190 OSMO_ASSERT(osmo_log_info);
191 }
192}
193
Harald Welteb43bc042011-06-27 10:29:17 +0200194/* special magic for negative (library-internal) log subsystem numbers */
195static int subsys_lib2index(int subsys)
196{
197 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
198}
199
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200200/*! Parse a human-readable log level into a numeric value
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200201 * \param lvl[in] zero-terminated string containing log level name
202 * \returns numeric log level
203 */
Harald Welte3ae27582010-03-26 21:24:24 +0800204int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800205{
206 return get_string_value(loglevel_strs, lvl);
207}
208
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200209/*! convert a numeric log level into human-readable string
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200210 * \param lvl[in] numeric log level
211 * \returns zero-terminated string (log level name)
212 */
Harald Welte9ac22252010-05-11 11:19:40 +0200213const char *log_level_str(unsigned int lvl)
214{
215 return get_value_string(loglevel_strs, lvl);
216}
217
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200219 * \param[in] category human-readable log category name
220 * \returns numeric category value, or -EINVAL otherwise
221 */
Harald Welte3ae27582010-03-26 21:24:24 +0800222int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800223{
224 int i;
225
Max68bf16a2018-01-10 17:00:43 +0100226 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100227
Harald Welte4ebdf742010-05-19 19:54:00 +0200228 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200229 if (osmo_log_info->cat[i].name == NULL)
230 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200231 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800232 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800233 }
234
235 return -EINVAL;
236}
237
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200238/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200239 * \param[in] target log target to be configured
240 * \param[in] _mask log category mask string
241 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800242 * The format can be this: category1:category2:category3
243 * or category1,2:category2,3:...
244 */
Harald Welte3ae27582010-03-26 21:24:24 +0800245void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800246{
247 int i = 0;
248 char *mask = strdup(_mask);
249 char *category_token = NULL;
250
Max68bf16a2018-01-10 17:00:43 +0100251 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100252
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800253 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200254 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800255 target->categories[i].enabled = 0;
256
257 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200258 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800259 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200260 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200261 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800262 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200263
264 if (!osmo_log_info->cat[i].name)
265 continue;
266
267 length = strlen(category_token);
268 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200269
270 /* Use longest length not to match subocurrences. */
271 if (cat_length > length)
272 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800273
274 if (colon)
275 length = colon - category_token;
276
Harald Welte4ebdf742010-05-19 19:54:00 +0200277 if (strncasecmp(osmo_log_info->cat[i].name,
278 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800279 int level = 0;
280
281 if (colon)
282 level = atoi(colon+1);
283
Harald Weltefaadfe22010-03-26 21:05:43 +0800284 target->categories[i].enabled = 1;
285 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800286 }
287 }
288 } while ((category_token = strtok(NULL, ":")));
289
290 free(mask);
291}
292
293static const char* color(int subsys)
294{
Harald Welte4ebdf742010-05-19 19:54:00 +0200295 if (subsys < osmo_log_info->num_cat)
296 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800297
Harald Welted788f662010-03-26 09:45:03 +0800298 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800299}
300
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100301static const struct value_string level_colors[] = {
302 { LOGL_DEBUG, "\033[1;34m" },
303 { LOGL_INFO, "\033[1;32m" },
304 { LOGL_NOTICE, "\033[1;33m" },
305 { LOGL_ERROR, "\033[1;31m" },
306 { LOGL_FATAL, "\033[1;31m" },
307 { 0, NULL }
308};
309
310static const char *level_color(int level)
311{
312 const char *c = get_value_string_or_null(level_colors, level);
313 if (!c)
314 return get_value_string(level_colors, LOGL_FATAL);
315 return c;
316}
317
Harald Welteaa00f992016-12-02 15:30:02 +0100318const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100319{
320 if (subsys < osmo_log_info->num_cat)
321 return osmo_log_info->cat[subsys].name;
322
323 return NULL;
324}
325
Harald Welte3ae27582010-03-26 21:24:24 +0800326static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200327 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100328 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800329{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800330 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200331 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100332 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800333
334 /* are we using color */
335 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100336 c_subsys = color(subsys);
337 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100338 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200339 if (ret < 0)
340 goto err;
341 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800342 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800343 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800344 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100345 if (target->print_ext_timestamp) {
346 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100347 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200348 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100349 localtime_r(&tv.tv_sec, &tm);
350 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100351 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100352 tm.tm_hour, tm.tm_min, tm.tm_sec,
353 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100354 if (ret < 0)
355 goto err;
356 OSMO_SNPRINTF_RET(ret, rem, offset, len);
357 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800358 char *timestr;
359 time_t tm;
360 tm = time(NULL);
361 timestr = ctime(&tm);
362 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200363 ret = snprintf(buf + offset, rem, "%s ", timestr);
364 if (ret < 0)
365 goto err;
366 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800367 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100368 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100369 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
370 target->use_color ? level_color(level) : "",
371 log_category_name(subsys),
372 target->use_color ? "\033[0;m" : "",
373 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100374 if (ret < 0)
375 goto err;
376 OSMO_SNPRINTF_RET(ret, rem, offset, len);
377 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100378 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100379 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
380 target->use_color ? level_color(level) : "",
381 log_level_str(level),
382 target->use_color ? "\033[0;m" : "",
383 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100384 if (ret < 0)
385 goto err;
386 OSMO_SNPRINTF_RET(ret, rem, offset, len);
387 }
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200388 if (target->print_filename) {
389 ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
390 subsys, file, line);
391 if (ret < 0)
392 goto err;
393 OSMO_SNPRINTF_RET(ret, rem, offset, len);
394 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800395 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200396 ret = vsnprintf(buf + offset, rem, format, ap);
397 if (ret < 0)
398 goto err;
399 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800400
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200401 ret = snprintf(buf + offset, rem, "%s",
402 target->use_color ? "\033[0;m" : "");
403 if (ret < 0)
404 goto err;
405 OSMO_SNPRINTF_RET(ret, rem, offset, len);
406err:
407 buf[sizeof(buf)-1] = '\0';
408 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800409}
410
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100411/* Catch internal logging category indexes as well as out-of-bounds indexes.
412 * For internal categories, the ID is negative starting with -1; and internal
413 * logging categories are added behind the user categories. For out-of-bounds
414 * indexes, return the index of DLGLOBAL. The returned category index is
415 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
416 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100417static inline int map_subsys(int subsys)
418{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100419 /* Note: comparing signed and unsigned integers */
420
421 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
422 subsys = DLGLOBAL;
423
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100424 if (subsys < 0)
425 subsys = subsys_lib2index(subsys);
426
Neels Hofmeyrca135742016-12-12 14:18:54 +0100427 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100428 subsys = subsys_lib2index(DLGLOBAL);
429
Neels Hofmeyrca135742016-12-12 14:18:54 +0100430 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
431
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100432 return subsys;
433}
434
Maxc65c5b42017-03-15 13:20:23 +0100435static inline bool should_log_to_target(struct log_target *tar, int subsys,
436 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100437{
438 struct log_category *category;
439
440 category = &tar->categories[subsys];
441
442 /* subsystem is not supposed to be logged */
443 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100444 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100445
446 /* Check the global log level */
447 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100448 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100449
450 /* Check the category log level */
451 if (tar->loglevel == 0 && category->loglevel != 0 &&
452 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100453 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100454
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100455 /* Apply filters here... if that becomes messy we will
456 * need to put filters in a list and each filter will
457 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100458 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100459 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100460
461 if (osmo_log_info->filter_fn)
462 return osmo_log_info->filter_fn(&log_context, tar);
463
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100464 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100465 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100466}
467
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200468/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200469 * \param[in] subsys Logging sub-system
470 * \param[in] level Log level
471 * \param[in] file name of source code file
472 * \param[in] cont continuation (1) or new line (0)
473 * \param[in] format format string
474 * \param[in] ap vararg-list containing format string arguments
475 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200476void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200477 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800478{
Harald Welte3ae27582010-03-26 21:24:24 +0800479 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800480
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100481 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200482
Harald Welte28222962011-02-18 20:37:04 +0100483 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200484 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800485
Maxc65c5b42017-03-15 13:20:23 +0100486 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800487 continue;
488
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200489 /* According to the manpage, vsnprintf leaves the value of ap
490 * in undefined state. Since _output uses vsnprintf and it may
491 * be called several times, we have to pass a copy of ap. */
492 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100493 if (tar->raw_output)
494 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
495 else
496 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200497 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800498 }
499}
500
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200501/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200502 * \param[in] subsys Logging sub-system
503 * \param[in] file name of source code file
504 * \param[in] cont continuation (1) or new line (0)
505 * \param[in] format format string
506 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200507void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800508 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800509{
510 va_list ap;
511
512 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200513 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800514 va_end(ap);
515}
516
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200517/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200518 * \param[in] subsys Logging sub-system
519 * \param[in] level Log level
520 * \param[in] file name of source code file
521 * \param[in] cont continuation (1) or new line (0)
522 * \param[in] format format string
523 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200524void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800525{
526 va_list ap;
527
528 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200529 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800530 va_end(ap);
531}
532
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200533/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200534 * \param[in] target Log target to be registered
535 */
Harald Welte3ae27582010-03-26 21:24:24 +0800536void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800537{
Harald Welte28222962011-02-18 20:37:04 +0100538 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800539}
540
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200541/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200542 * \param[in] target Log target to be unregistered
543 */
Harald Welte3ae27582010-03-26 21:24:24 +0800544void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800545{
546 llist_del(&target->entry);
547}
548
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200549/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800550void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800551{
Harald Welte3ae27582010-03-26 21:24:24 +0800552 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800553}
554
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200555/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200556 * \param[in] ctx_nr logging context number
557 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200558 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200559 *
560 * A logging context is something like the subscriber identity to which
561 * the currently processed message relates, or the BTS through which it
562 * was received. As soon as this data is known, it can be set using
563 * this function. The main use of context information is for logging
564 * filters.
565 */
Harald Welte3ae27582010-03-26 21:24:24 +0800566int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800567{
Harald Welte3ae27582010-03-26 21:24:24 +0800568 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800569 return -EINVAL;
570
Harald Welte3ae27582010-03-26 21:24:24 +0800571 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800572
573 return 0;
574}
575
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200576/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200577 * \param[in] target Log target to be affected
578 * \param[in] all enable (1) or disable (0) the ALL filter
579 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100580 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100581 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
582 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200583 */
Harald Welte3ae27582010-03-26 21:24:24 +0800584void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800585{
586 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100587 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800588 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100589 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800590}
591
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200592/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200593 * \param[in] target Log target to be affected
594 * \param[in] use_color Use color (1) or don't use color (0)
595 */
Harald Welte3ae27582010-03-26 21:24:24 +0800596void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800597{
598 target->use_color = use_color;
599}
600
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200601/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200602 * \param[in] target Log target to be affected
603 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
604 */
Harald Welte3ae27582010-03-26 21:24:24 +0800605void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800606{
607 target->print_timestamp = print_timestamp;
608}
609
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200610/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100611 * \param[in] target Log target to be affected
612 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
613 *
614 * When both timestamp and extended timestamp is enabled then only
615 * the extended timestamp will be used. The format of the timestamp
616 * is YYYYMMDDhhmmssnnn.
617 */
618void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
619{
620 target->print_ext_timestamp = print_timestamp;
621}
622
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200623/*! Enable or disable printing of the filename while logging
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200624 * \param[in] target Log target to be affected
625 * \param[in] print_filename Enable (1) or disable (0) filenames
626 */
627void log_set_print_filename(struct log_target *target, int print_filename)
628{
629 target->print_filename = print_filename;
630}
631
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200632/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100633 * \param[in] target Log target to be affected
634 * \param[in] print_catname Enable (1) or disable (0) filenames
635 *
636 * Print the category/subsys name in front of every log message.
637 */
638void log_set_print_category(struct log_target *target, int print_category)
639{
640 target->print_category = print_category;
641}
642
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100643/*! Enable or disable printing of the log level name.
644 * \param[in] target Log target to be affected
645 * \param[in] print_catname Enable (1) or disable (0) filenames
646 *
647 * Print the log level name in front of every log message.
648 */
649void log_set_print_level(struct log_target *target, int print_level)
650{
651 target->print_level = (bool)print_level;
652}
653
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200654/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200655 * \param[in] target Log target to be affected
656 * \param[in] log_level New global log level
657 */
Harald Welte3ae27582010-03-26 21:24:24 +0800658void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800659{
660 target->loglevel = log_level;
661}
662
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200663/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100664 * \param[in] target Log target to be affected
665 * \param[in] category Log category to be affected
666 * \param[in] enable whether to enable or disable the filter
667 * \param[in] level Log level of the filter
668 */
Harald Welte3ae27582010-03-26 21:24:24 +0800669void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800670 int enable, int level)
671{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100672 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800673 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100674 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800675 target->categories[category].enabled = !!enable;
676 target->categories[category].loglevel = level;
677}
678
Harald Welte44c0f632017-01-15 17:58:29 +0100679#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100680static void _file_output(struct log_target *target, unsigned int level,
681 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800682{
Harald Welte0083cd32010-08-25 14:55:44 +0200683 fprintf(target->tgt_file.out, "%s", log);
684 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800685}
Harald Welte44c0f632017-01-15 17:58:29 +0100686#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800687
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200688/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200689 * \returns dynamically-allocated log target
690 * This funcition allocates a \ref log_target and initializes it
691 * with some default values. The newly created target is not
692 * registered yet.
693 */
Harald Welte3ae27582010-03-26 21:24:24 +0800694struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800695{
Harald Welte3ae27582010-03-26 21:24:24 +0800696 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800697 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800698
Max68bf16a2018-01-10 17:00:43 +0100699 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100700
Harald Welte3ae27582010-03-26 21:24:24 +0800701 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800702 if (!target)
703 return NULL;
704
Harald Welteb43bc042011-06-27 10:29:17 +0200705 target->categories = talloc_zero_array(target,
706 struct log_category,
707 osmo_log_info->num_cat);
708 if (!target->categories) {
709 talloc_free(target);
710 return NULL;
711 }
712
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800713 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800714
715 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200716 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800717 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200718 cat->enabled = osmo_log_info->cat[i].enabled;
719 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800720 }
721
722 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800723 target->use_color = 1;
724 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200725 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800726
727 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800728 target->loglevel = 0;
729 return target;
730}
731
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200732/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200733 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800734struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800735{
Harald Weltea3b844c2010-03-27 00:04:40 +0800736/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200737#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800738 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800739
Harald Welte3ae27582010-03-26 21:24:24 +0800740 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800741 if (!target)
742 return NULL;
743
Harald Welte28222962011-02-18 20:37:04 +0100744 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200745 target->tgt_file.out = stderr;
746 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800747 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800748#else
749 return NULL;
750#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800751}
752
Harald Welte44c0f632017-01-15 17:58:29 +0100753#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200754/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200755 * \param[in] fname File name of the new log file
756 * \returns Log target in case of success, NULL otherwise
757 */
Harald Welte3086c392010-08-25 19:10:50 +0200758struct log_target *log_target_create_file(const char *fname)
759{
760 struct log_target *target;
761
762 target = log_target_create();
763 if (!target)
764 return NULL;
765
Harald Welte28222962011-02-18 20:37:04 +0100766 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200767 target->tgt_file.out = fopen(fname, "a");
768 if (!target->tgt_file.out)
769 return NULL;
770
771 target->output = _file_output;
772
773 target->tgt_file.fname = talloc_strdup(target, fname);
774
775 return target;
776}
Harald Welte44c0f632017-01-15 17:58:29 +0100777#endif
Harald Welte3086c392010-08-25 19:10:50 +0200778
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200779/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200780 * \param[in] type Log target type
781 * \param[in] fname File name
782 * \returns Log target (if found), NULL otherwise
783 */
Harald Welte28222962011-02-18 20:37:04 +0100784struct log_target *log_target_find(int type, const char *fname)
785{
786 struct log_target *tgt;
787
788 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
789 if (tgt->type != type)
790 continue;
Maxc90f40a2018-01-11 10:52:28 +0100791 switch (tgt->type) {
792 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100793 if (!strcmp(fname, tgt->tgt_file.fname))
794 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100795 break;
796 case LOG_TGT_TYPE_GSMTAP:
797 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
798 return tgt;
799 break;
800 default:
Harald Welte28222962011-02-18 20:37:04 +0100801 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100802 }
Harald Welte28222962011-02-18 20:37:04 +0100803 }
804 return NULL;
805}
806
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200807/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200808 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200809void log_target_destroy(struct log_target *target)
810{
811
812 /* just in case, to make sure we don't have any references */
813 log_del_target(target);
814
Harald Welte44c0f632017-01-15 17:58:29 +0100815#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200816 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200817/* since C89/C99 says stderr is a macro, we can safely do this! */
818#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200819 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200820 if (target->tgt_file.out != stderr)
821#endif
822 {
Harald Welte3086c392010-08-25 19:10:50 +0200823 fclose(target->tgt_file.out);
824 target->tgt_file.out = NULL;
825 }
826 }
Harald Welte44c0f632017-01-15 17:58:29 +0100827#endif
Harald Welte3086c392010-08-25 19:10:50 +0200828
829 talloc_free(target);
830}
831
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200832/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200833 * \param[in] target log target to re-open
834 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200835int log_target_file_reopen(struct log_target *target)
836{
837 fclose(target->tgt_file.out);
838
839 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
840 if (!target->tgt_file.out)
841 return -errno;
842
843 /* we assume target->output already to be set */
844
845 return 0;
846}
847
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200848/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200849 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100850int log_targets_reopen(void)
851{
852 struct log_target *tar;
853 int rc = 0;
854
855 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
856 switch (tar->type) {
857 case LOG_TGT_TYPE_FILE:
858 if (log_target_file_reopen(tar) < 0)
859 rc = -1;
860 break;
861 default:
862 break;
863 }
864 }
865
866 return rc;
867}
868
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200869/*! Generates the logging command string for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200870 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200871 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200872 */
Maxc65c5b42017-03-15 13:20:23 +0100873const char *log_vty_command_string()
Harald Welte7638af92010-05-11 16:39:22 +0200874{
Harald Weltece9fec32011-06-27 14:19:16 +0200875 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100876 int len = 0, offset = 0, ret, i, rem;
877 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200878 char *str;
879
Max68bf16a2018-01-10 17:00:43 +0100880 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100881
Harald Welteb43bc042011-06-27 10:29:17 +0200882 for (i = 0; i < info->num_cat; i++) {
883 if (info->cat[i].name == NULL)
884 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100885 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200886 }
Harald Welte7638af92010-05-11 16:39:22 +0200887
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100888 for (i = 0; i < LOGLEVEL_DEFS; i++)
889 size += strlen(loglevel_strs[i].str) + 1;
890
891 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200892 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200893 if (!str)
894 return NULL;
895
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200896 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100897 if (ret < 0)
898 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200899 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200900
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100901 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200902 if (info->cat[i].name) {
903 int j, name_len = strlen(info->cat[i].name)+1;
904 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100905
Harald Welteb43bc042011-06-27 10:29:17 +0200906 for (j = 0; j < name_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200907 name[j] = tolower((unsigned char)info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100908
Harald Welteb43bc042011-06-27 10:29:17 +0200909 name[name_len-1] = '\0';
910 ret = snprintf(str + offset, rem, "%s|", name+1);
911 if (ret < 0)
912 goto err;
913 OSMO_SNPRINTF_RET(ret, rem, offset, len);
914 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100915 }
916 offset--; /* to remove the trailing | */
917 rem++;
918
919 ret = snprintf(str + offset, rem, ") (");
920 if (ret < 0)
921 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200922 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100923
924 for (i = 0; i < LOGLEVEL_DEFS; i++) {
925 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
926 char loglevel_str[loglevel_str_len];
927
928 for (j = 0; j < loglevel_str_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200929 loglevel_str[j] = tolower((unsigned char)loglevel_strs[i].str[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100930
931 loglevel_str[loglevel_str_len-1] = '\0';
932 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
933 if (ret < 0)
934 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200935 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100936 }
937 offset--; /* to remove the trailing | */
938 rem++;
939
940 ret = snprintf(str + offset, rem, ")");
941 if (ret < 0)
942 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200943 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100944err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200945 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100946 return str;
947}
948
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200949/*! Generates the logging command description for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200950 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200951 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200952 */
Maxc65c5b42017-03-15 13:20:23 +0100953const char *log_vty_command_description()
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100954{
Harald Weltece9fec32011-06-27 14:19:16 +0200955 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100956 char *str;
957 int i, ret, len = 0, offset = 0, rem;
958 unsigned int size =
959 strlen(LOGGING_STR
960 "Set the log level for a specified category\n") + 1;
961
Max68bf16a2018-01-10 17:00:43 +0100962 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100963
Harald Welteb43bc042011-06-27 10:29:17 +0200964 for (i = 0; i < info->num_cat; i++) {
965 if (info->cat[i].name == NULL)
966 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100967 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200968 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100969
970 for (i = 0; i < LOGLEVEL_DEFS; i++)
971 size += strlen(loglevel_descriptions[i]) + 1;
972
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200973 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100974 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200975 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100976 if (!str)
977 return NULL;
978
979 ret = snprintf(str + offset, rem, LOGGING_STR
980 "Set the log level for a specified category\n");
981 if (ret < 0)
982 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200983 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100984
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200985 ret = snprintf(str + offset, rem,
986 "Global setting for all subsystems\n");
987 if (ret < 0)
988 goto err;
989 OSMO_SNPRINTF_RET(ret, rem, offset, len);
990
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100991 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200992 if (info->cat[i].name == NULL)
993 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100994 ret = snprintf(str + offset, rem, "%s\n",
995 info->cat[i].description);
996 if (ret < 0)
997 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200998 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100999 }
1000 for (i = 0; i < LOGLEVEL_DEFS; i++) {
1001 ret = snprintf(str + offset, rem, "%s\n",
1002 loglevel_descriptions[i]);
1003 if (ret < 0)
1004 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001005 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001006 }
1007err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +02001008 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +02001009 return str;
1010}
1011
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001012/*! Initialize the Osmocom logging core
Harald Welte18fc4652011-08-17 14:14:17 +02001013 * \param[in] inf Information regarding logging categories
1014 * \param[in] ctx \ref talloc context for logging allocations
1015 * \returns 0 in case of success, negative in case of error
1016 */
Harald Welteb43bc042011-06-27 10:29:17 +02001017int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001018{
Harald Welteb43bc042011-06-27 10:29:17 +02001019 int i;
1020
1021 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1022 if (!tall_log_ctx)
1023 return -ENOMEM;
1024
1025 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1026 if (!osmo_log_info)
1027 return -ENOMEM;
1028
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +01001029 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +02001030 osmo_log_info->num_cat_user = inf->num_cat;
1031 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +02001032 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +02001033
1034 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
1035 struct log_info_cat,
1036 osmo_log_info->num_cat);
1037 if (!osmo_log_info->cat) {
1038 talloc_free(osmo_log_info);
1039 osmo_log_info = NULL;
1040 return -ENOMEM;
1041 }
1042
1043 /* copy over the user part */
1044 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001045 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
1046 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +02001047 sizeof(struct log_info_cat));
1048 }
1049
1050 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001051 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001052 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001053 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001054 &internal_cat[i], sizeof(struct log_info_cat));
1055 }
1056
1057 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001058}
Harald Welte18fc4652011-08-17 14:14:17 +02001059
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001060/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001061 * This function destroys all targets and releases associated memory */
1062void log_fini(void)
1063{
1064 struct log_target *tar, *tar2;
1065
1066 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1067 log_target_destroy(tar);
1068
1069 talloc_free(osmo_log_info);
1070 osmo_log_info = NULL;
1071 talloc_free(tall_log_ctx);
1072 tall_log_ctx = NULL;
1073}
1074
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001075/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001076 * \returns != 0 if a log entry might get generated by at least one target */
1077int log_check_level(int subsys, unsigned int level)
1078{
1079 struct log_target *tar;
1080
Max68bf16a2018-01-10 17:00:43 +01001081 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001082
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001083 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001084
1085 /* TODO: The following could/should be cached (update on config) */
1086
1087 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001088 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001089 continue;
1090
1091 /* This might get logged (ignoring filters) */
1092 return 1;
1093 }
1094
1095 /* We are sure, that this will not be logged. */
1096 return 0;
1097}
1098
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001099/*! @} */