blob: 66074ea076a9573df5f8387d57358495a7279841 [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
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100326static const char *const_basename(const char *path)
327{
328 const char *bn = strrchr(path, '/');
329 if (!bn || !bn[1])
330 return path;
331 return bn + 1;
332}
333
Harald Welte3ae27582010-03-26 21:24:24 +0800334static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200335 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100336 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800337{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800338 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200339 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100340 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800341
342 /* are we using color */
343 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100344 c_subsys = color(subsys);
345 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100346 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200347 if (ret < 0)
348 goto err;
349 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800350 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800351 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800352 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100353 if (target->print_ext_timestamp) {
354 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100355 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200356 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100357 localtime_r(&tv.tv_sec, &tm);
358 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100359 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100360 tm.tm_hour, tm.tm_min, tm.tm_sec,
361 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100362 if (ret < 0)
363 goto err;
364 OSMO_SNPRINTF_RET(ret, rem, offset, len);
365 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800366 char *timestr;
367 time_t tm;
368 tm = time(NULL);
369 timestr = ctime(&tm);
370 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200371 ret = snprintf(buf + offset, rem, "%s ", timestr);
372 if (ret < 0)
373 goto err;
374 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800375 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100376 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100377 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
378 target->use_color ? level_color(level) : "",
379 log_category_name(subsys),
380 target->use_color ? "\033[0;m" : "",
381 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100382 if (ret < 0)
383 goto err;
384 OSMO_SNPRINTF_RET(ret, rem, offset, len);
385 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100386 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100387 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
388 target->use_color ? level_color(level) : "",
389 log_level_str(level),
390 target->use_color ? "\033[0;m" : "",
391 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100392 if (ret < 0)
393 goto err;
394 OSMO_SNPRINTF_RET(ret, rem, offset, len);
395 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100396 if (target->print_category_hex) {
397 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200398 if (ret < 0)
399 goto err;
400 OSMO_SNPRINTF_RET(ret, rem, offset, len);
401 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100402 switch (target->print_filename2) {
403 case LOG_FILENAME_NONE:
404 break;
405 case LOG_FILENAME_PATH:
406 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
407 if (ret < 0)
408 goto err;
409 OSMO_SNPRINTF_RET(ret, rem, offset, len);
410 break;
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100411 case LOG_FILENAME_BASENAME:
412 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
413 if (ret < 0)
414 goto err;
415 OSMO_SNPRINTF_RET(ret, rem, offset, len);
416 break;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100417 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800418 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200419 ret = vsnprintf(buf + offset, rem, format, ap);
420 if (ret < 0)
421 goto err;
422 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800423
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100424 if (target->use_color) {
425 ret = snprintf(buf + offset, rem, "\033[0;m");
426 if (ret < 0)
427 goto err;
428 OSMO_SNPRINTF_RET(ret, rem, offset, len);
429 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200430err:
431 buf[sizeof(buf)-1] = '\0';
432 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800433}
434
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100435/* Catch internal logging category indexes as well as out-of-bounds indexes.
436 * For internal categories, the ID is negative starting with -1; and internal
437 * logging categories are added behind the user categories. For out-of-bounds
438 * indexes, return the index of DLGLOBAL. The returned category index is
439 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
440 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100441static inline int map_subsys(int subsys)
442{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100443 /* Note: comparing signed and unsigned integers */
444
445 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
446 subsys = DLGLOBAL;
447
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100448 if (subsys < 0)
449 subsys = subsys_lib2index(subsys);
450
Neels Hofmeyrca135742016-12-12 14:18:54 +0100451 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100452 subsys = subsys_lib2index(DLGLOBAL);
453
Neels Hofmeyrca135742016-12-12 14:18:54 +0100454 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
455
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100456 return subsys;
457}
458
Maxc65c5b42017-03-15 13:20:23 +0100459static inline bool should_log_to_target(struct log_target *tar, int subsys,
460 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100461{
462 struct log_category *category;
463
464 category = &tar->categories[subsys];
465
466 /* subsystem is not supposed to be logged */
467 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100468 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100469
470 /* Check the global log level */
471 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100472 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100473
474 /* Check the category log level */
475 if (tar->loglevel == 0 && category->loglevel != 0 &&
476 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100477 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100478
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100479 /* Apply filters here... if that becomes messy we will
480 * need to put filters in a list and each filter will
481 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100482 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100483 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100484
485 if (osmo_log_info->filter_fn)
486 return osmo_log_info->filter_fn(&log_context, tar);
487
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100488 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100489 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100490}
491
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200492/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200493 * \param[in] subsys Logging sub-system
494 * \param[in] level Log level
495 * \param[in] file name of source code file
496 * \param[in] cont continuation (1) or new line (0)
497 * \param[in] format format string
498 * \param[in] ap vararg-list containing format string arguments
499 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200500void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200501 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800502{
Harald Welte3ae27582010-03-26 21:24:24 +0800503 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800504
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100505 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200506
Harald Welte28222962011-02-18 20:37:04 +0100507 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200508 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800509
Maxc65c5b42017-03-15 13:20:23 +0100510 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800511 continue;
512
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200513 /* According to the manpage, vsnprintf leaves the value of ap
514 * in undefined state. Since _output uses vsnprintf and it may
515 * be called several times, we have to pass a copy of ap. */
516 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100517 if (tar->raw_output)
518 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
519 else
520 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200521 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800522 }
523}
524
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200525/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200526 * \param[in] subsys Logging sub-system
527 * \param[in] file name of source code file
528 * \param[in] cont continuation (1) or new line (0)
529 * \param[in] format format string
530 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200531void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800532 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800533{
534 va_list ap;
535
536 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200537 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800538 va_end(ap);
539}
540
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200541/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200542 * \param[in] subsys Logging sub-system
543 * \param[in] level Log level
544 * \param[in] file name of source code file
545 * \param[in] cont continuation (1) or new line (0)
546 * \param[in] format format string
547 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200548void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800549{
550 va_list ap;
551
552 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200553 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800554 va_end(ap);
555}
556
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200557/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200558 * \param[in] target Log target to be registered
559 */
Harald Welte3ae27582010-03-26 21:24:24 +0800560void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800561{
Harald Welte28222962011-02-18 20:37:04 +0100562 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800563}
564
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200565/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200566 * \param[in] target Log target to be unregistered
567 */
Harald Welte3ae27582010-03-26 21:24:24 +0800568void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800569{
570 llist_del(&target->entry);
571}
572
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200573/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800574void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800575{
Harald Welte3ae27582010-03-26 21:24:24 +0800576 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800577}
578
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200579/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200580 * \param[in] ctx_nr logging context number
581 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200582 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200583 *
584 * A logging context is something like the subscriber identity to which
585 * the currently processed message relates, or the BTS through which it
586 * was received. As soon as this data is known, it can be set using
587 * this function. The main use of context information is for logging
588 * filters.
589 */
Harald Welte3ae27582010-03-26 21:24:24 +0800590int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800591{
Harald Welte3ae27582010-03-26 21:24:24 +0800592 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800593 return -EINVAL;
594
Harald Welte3ae27582010-03-26 21:24:24 +0800595 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800596
597 return 0;
598}
599
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200600/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200601 * \param[in] target Log target to be affected
602 * \param[in] all enable (1) or disable (0) the ALL filter
603 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100604 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100605 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
606 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200607 */
Harald Welte3ae27582010-03-26 21:24:24 +0800608void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800609{
610 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100611 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800612 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100613 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800614}
615
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200616/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200617 * \param[in] target Log target to be affected
618 * \param[in] use_color Use color (1) or don't use color (0)
619 */
Harald Welte3ae27582010-03-26 21:24:24 +0800620void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621{
622 target->use_color = use_color;
623}
624
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200625/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200626 * \param[in] target Log target to be affected
627 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
628 */
Harald Welte3ae27582010-03-26 21:24:24 +0800629void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800630{
631 target->print_timestamp = print_timestamp;
632}
633
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200634/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100635 * \param[in] target Log target to be affected
636 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
637 *
638 * When both timestamp and extended timestamp is enabled then only
639 * the extended timestamp will be used. The format of the timestamp
640 * is YYYYMMDDhhmmssnnn.
641 */
642void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
643{
644 target->print_ext_timestamp = print_timestamp;
645}
646
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100647/*! Use log_set_print_filename2() instead.
648 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
649 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
650 * behavior, which combined the category in hex with the filename. For example, if the category-hex
651 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
652 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200653 * \param[in] target Log target to be affected
654 * \param[in] print_filename Enable (1) or disable (0) filenames
655 */
656void log_set_print_filename(struct log_target *target, int print_filename)
657{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100658 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
659 log_set_print_category_hex(target, print_filename);
660}
661
662/*! Enable or disable printing of the filename while logging.
663 * \param[in] target Log target to be affected.
664 * \param[in] print_filename An LOG_FILENAME_* enum value.
665 * LOG_FILENAME_NONE omits the source file and line information from logs.
666 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
667 */
668void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
669{
670 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200671}
672
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200673/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100674 * \param[in] target Log target to be affected
675 * \param[in] print_catname Enable (1) or disable (0) filenames
676 *
677 * Print the category/subsys name in front of every log message.
678 */
679void log_set_print_category(struct log_target *target, int print_category)
680{
681 target->print_category = print_category;
682}
683
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100684/*! Enable or disable printing of the category number in hex ('<000b>').
685 * \param[in] target Log target to be affected.
686 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
687 */
688void log_set_print_category_hex(struct log_target *target, int print_category_hex)
689{
690 target->print_category_hex = print_category_hex;
691}
692
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100693/*! Enable or disable printing of the log level name.
694 * \param[in] target Log target to be affected
695 * \param[in] print_catname Enable (1) or disable (0) filenames
696 *
697 * Print the log level name in front of every log message.
698 */
699void log_set_print_level(struct log_target *target, int print_level)
700{
701 target->print_level = (bool)print_level;
702}
703
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200704/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200705 * \param[in] target Log target to be affected
706 * \param[in] log_level New global log level
707 */
Harald Welte3ae27582010-03-26 21:24:24 +0800708void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800709{
710 target->loglevel = log_level;
711}
712
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200713/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100714 * \param[in] target Log target to be affected
715 * \param[in] category Log category to be affected
716 * \param[in] enable whether to enable or disable the filter
717 * \param[in] level Log level of the filter
718 */
Harald Welte3ae27582010-03-26 21:24:24 +0800719void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800720 int enable, int level)
721{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100722 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800723 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100724 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800725 target->categories[category].enabled = !!enable;
726 target->categories[category].loglevel = level;
727}
728
Harald Welte44c0f632017-01-15 17:58:29 +0100729#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100730static void _file_output(struct log_target *target, unsigned int level,
731 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800732{
Harald Welte0083cd32010-08-25 14:55:44 +0200733 fprintf(target->tgt_file.out, "%s", log);
734 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800735}
Harald Welte44c0f632017-01-15 17:58:29 +0100736#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800737
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200738/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200739 * \returns dynamically-allocated log target
740 * This funcition allocates a \ref log_target and initializes it
741 * with some default values. The newly created target is not
742 * registered yet.
743 */
Harald Welte3ae27582010-03-26 21:24:24 +0800744struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800745{
Harald Welte3ae27582010-03-26 21:24:24 +0800746 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800747 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800748
Max68bf16a2018-01-10 17:00:43 +0100749 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100750
Harald Welte3ae27582010-03-26 21:24:24 +0800751 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800752 if (!target)
753 return NULL;
754
Harald Welteb43bc042011-06-27 10:29:17 +0200755 target->categories = talloc_zero_array(target,
756 struct log_category,
757 osmo_log_info->num_cat);
758 if (!target->categories) {
759 talloc_free(target);
760 return NULL;
761 }
762
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800763 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800764
765 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200766 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800767 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200768 cat->enabled = osmo_log_info->cat[i].enabled;
769 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800770 }
771
772 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800773 target->use_color = 1;
774 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100775 target->print_filename2 = LOG_FILENAME_PATH;
776 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800777
778 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800779 target->loglevel = 0;
780 return target;
781}
782
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200783/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200784 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800785struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800786{
Harald Weltea3b844c2010-03-27 00:04:40 +0800787/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200788#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800789 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800790
Harald Welte3ae27582010-03-26 21:24:24 +0800791 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800792 if (!target)
793 return NULL;
794
Harald Welte28222962011-02-18 20:37:04 +0100795 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200796 target->tgt_file.out = stderr;
797 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800798 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800799#else
800 return NULL;
801#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800802}
803
Harald Welte44c0f632017-01-15 17:58:29 +0100804#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200805/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200806 * \param[in] fname File name of the new log file
807 * \returns Log target in case of success, NULL otherwise
808 */
Harald Welte3086c392010-08-25 19:10:50 +0200809struct log_target *log_target_create_file(const char *fname)
810{
811 struct log_target *target;
812
813 target = log_target_create();
814 if (!target)
815 return NULL;
816
Harald Welte28222962011-02-18 20:37:04 +0100817 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200818 target->tgt_file.out = fopen(fname, "a");
819 if (!target->tgt_file.out)
820 return NULL;
821
822 target->output = _file_output;
823
824 target->tgt_file.fname = talloc_strdup(target, fname);
825
826 return target;
827}
Harald Welte44c0f632017-01-15 17:58:29 +0100828#endif
Harald Welte3086c392010-08-25 19:10:50 +0200829
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200830/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200831 * \param[in] type Log target type
832 * \param[in] fname File name
833 * \returns Log target (if found), NULL otherwise
834 */
Harald Welte28222962011-02-18 20:37:04 +0100835struct log_target *log_target_find(int type, const char *fname)
836{
837 struct log_target *tgt;
838
839 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
840 if (tgt->type != type)
841 continue;
Maxc90f40a2018-01-11 10:52:28 +0100842 switch (tgt->type) {
843 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100844 if (!strcmp(fname, tgt->tgt_file.fname))
845 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100846 break;
847 case LOG_TGT_TYPE_GSMTAP:
848 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
849 return tgt;
850 break;
851 default:
Harald Welte28222962011-02-18 20:37:04 +0100852 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100853 }
Harald Welte28222962011-02-18 20:37:04 +0100854 }
855 return NULL;
856}
857
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200858/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200859 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200860void log_target_destroy(struct log_target *target)
861{
862
863 /* just in case, to make sure we don't have any references */
864 log_del_target(target);
865
Harald Welte44c0f632017-01-15 17:58:29 +0100866#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200867 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200868/* since C89/C99 says stderr is a macro, we can safely do this! */
869#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200870 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200871 if (target->tgt_file.out != stderr)
872#endif
873 {
Harald Welte3086c392010-08-25 19:10:50 +0200874 fclose(target->tgt_file.out);
875 target->tgt_file.out = NULL;
876 }
877 }
Harald Welte44c0f632017-01-15 17:58:29 +0100878#endif
Harald Welte3086c392010-08-25 19:10:50 +0200879
880 talloc_free(target);
881}
882
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200883/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200884 * \param[in] target log target to re-open
885 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200886int log_target_file_reopen(struct log_target *target)
887{
888 fclose(target->tgt_file.out);
889
890 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
891 if (!target->tgt_file.out)
892 return -errno;
893
894 /* we assume target->output already to be set */
895
896 return 0;
897}
898
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200899/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200900 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100901int log_targets_reopen(void)
902{
903 struct log_target *tar;
904 int rc = 0;
905
906 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
907 switch (tar->type) {
908 case LOG_TGT_TYPE_FILE:
909 if (log_target_file_reopen(tar) < 0)
910 rc = -1;
911 break;
912 default:
913 break;
914 }
915 }
916
917 return rc;
918}
919
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200920/*! Generates the logging command string for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200921 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200922 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200923 */
Maxc65c5b42017-03-15 13:20:23 +0100924const char *log_vty_command_string()
Harald Welte7638af92010-05-11 16:39:22 +0200925{
Harald Weltece9fec32011-06-27 14:19:16 +0200926 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100927 int len = 0, offset = 0, ret, i, rem;
928 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200929 char *str;
930
Max68bf16a2018-01-10 17:00:43 +0100931 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100932
Harald Welteb43bc042011-06-27 10:29:17 +0200933 for (i = 0; i < info->num_cat; i++) {
934 if (info->cat[i].name == NULL)
935 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100936 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200937 }
Harald Welte7638af92010-05-11 16:39:22 +0200938
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100939 for (i = 0; i < LOGLEVEL_DEFS; i++)
940 size += strlen(loglevel_strs[i].str) + 1;
941
942 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200943 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200944 if (!str)
945 return NULL;
946
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200947 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100948 if (ret < 0)
949 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200950 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200951
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100952 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200953 if (info->cat[i].name) {
954 int j, name_len = strlen(info->cat[i].name)+1;
955 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100956
Harald Welteb43bc042011-06-27 10:29:17 +0200957 for (j = 0; j < name_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200958 name[j] = tolower((unsigned char)info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100959
Harald Welteb43bc042011-06-27 10:29:17 +0200960 name[name_len-1] = '\0';
961 ret = snprintf(str + offset, rem, "%s|", name+1);
962 if (ret < 0)
963 goto err;
964 OSMO_SNPRINTF_RET(ret, rem, offset, len);
965 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100966 }
967 offset--; /* to remove the trailing | */
968 rem++;
969
970 ret = snprintf(str + offset, rem, ") (");
971 if (ret < 0)
972 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200973 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100974
975 for (i = 0; i < LOGLEVEL_DEFS; i++) {
976 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
977 char loglevel_str[loglevel_str_len];
978
979 for (j = 0; j < loglevel_str_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200980 loglevel_str[j] = tolower((unsigned char)loglevel_strs[i].str[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100981
982 loglevel_str[loglevel_str_len-1] = '\0';
983 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
984 if (ret < 0)
985 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200986 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100987 }
988 offset--; /* to remove the trailing | */
989 rem++;
990
991 ret = snprintf(str + offset, rem, ")");
992 if (ret < 0)
993 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200994 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100995err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200996 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100997 return str;
998}
999
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001000/*! Generates the logging command description for VTY
Harald Welte18fc4652011-08-17 14:14:17 +02001001 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001002 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +02001003 */
Maxc65c5b42017-03-15 13:20:23 +01001004const char *log_vty_command_description()
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001005{
Harald Weltece9fec32011-06-27 14:19:16 +02001006 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001007 char *str;
1008 int i, ret, len = 0, offset = 0, rem;
1009 unsigned int size =
1010 strlen(LOGGING_STR
1011 "Set the log level for a specified category\n") + 1;
1012
Max68bf16a2018-01-10 17:00:43 +01001013 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001014
Harald Welteb43bc042011-06-27 10:29:17 +02001015 for (i = 0; i < info->num_cat; i++) {
1016 if (info->cat[i].name == NULL)
1017 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001018 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +02001019 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001020
1021 for (i = 0; i < LOGLEVEL_DEFS; i++)
1022 size += strlen(loglevel_descriptions[i]) + 1;
1023
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +02001024 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001025 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +02001026 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001027 if (!str)
1028 return NULL;
1029
1030 ret = snprintf(str + offset, rem, LOGGING_STR
1031 "Set the log level for a specified category\n");
1032 if (ret < 0)
1033 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001034 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001035
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +02001036 ret = snprintf(str + offset, rem,
1037 "Global setting for all subsystems\n");
1038 if (ret < 0)
1039 goto err;
1040 OSMO_SNPRINTF_RET(ret, rem, offset, len);
1041
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001042 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +02001043 if (info->cat[i].name == NULL)
1044 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001045 ret = snprintf(str + offset, rem, "%s\n",
1046 info->cat[i].description);
1047 if (ret < 0)
1048 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001049 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001050 }
1051 for (i = 0; i < LOGLEVEL_DEFS; i++) {
1052 ret = snprintf(str + offset, rem, "%s\n",
1053 loglevel_descriptions[i]);
1054 if (ret < 0)
1055 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001056 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001057 }
1058err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +02001059 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +02001060 return str;
1061}
1062
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001063/*! Initialize the Osmocom logging core
Harald Welte18fc4652011-08-17 14:14:17 +02001064 * \param[in] inf Information regarding logging categories
1065 * \param[in] ctx \ref talloc context for logging allocations
1066 * \returns 0 in case of success, negative in case of error
1067 */
Harald Welteb43bc042011-06-27 10:29:17 +02001068int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001069{
Harald Welteb43bc042011-06-27 10:29:17 +02001070 int i;
1071
1072 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1073 if (!tall_log_ctx)
1074 return -ENOMEM;
1075
1076 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1077 if (!osmo_log_info)
1078 return -ENOMEM;
1079
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +01001080 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +02001081 osmo_log_info->num_cat_user = inf->num_cat;
1082 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +02001083 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +02001084
1085 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
1086 struct log_info_cat,
1087 osmo_log_info->num_cat);
1088 if (!osmo_log_info->cat) {
1089 talloc_free(osmo_log_info);
1090 osmo_log_info = NULL;
1091 return -ENOMEM;
1092 }
1093
1094 /* copy over the user part */
1095 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001096 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
1097 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +02001098 sizeof(struct log_info_cat));
1099 }
1100
1101 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001102 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001103 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001104 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001105 &internal_cat[i], sizeof(struct log_info_cat));
1106 }
1107
1108 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001109}
Harald Welte18fc4652011-08-17 14:14:17 +02001110
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001111/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001112 * This function destroys all targets and releases associated memory */
1113void log_fini(void)
1114{
1115 struct log_target *tar, *tar2;
1116
1117 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1118 log_target_destroy(tar);
1119
1120 talloc_free(osmo_log_info);
1121 osmo_log_info = NULL;
1122 talloc_free(tall_log_ctx);
1123 tall_log_ctx = NULL;
1124}
1125
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001126/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001127 * \returns != 0 if a log entry might get generated by at least one target */
1128int log_check_level(int subsys, unsigned int level)
1129{
1130 struct log_target *tar;
1131
Max68bf16a2018-01-10 17:00:43 +01001132 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001133
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001134 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001135
1136 /* TODO: The following could/should be cached (update on config) */
1137
1138 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001139 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001140 continue;
1141
1142 /* This might get logged (ignoring filters) */
1143 return 1;
1144 }
1145
1146 /* We are sure, that this will not be logged. */
1147 return 0;
1148}
1149
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001150/*! @} */