blob: 147b1fb76e014350d6485ee007c47a696b67924e [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 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100171 [INT2IDX(DLJIBUF)] = {
172 .name = "DLJIBUF",
173 .description = "libosmo-netif Jitter Buffer",
174 .enabled = 1, .loglevel = LOGL_NOTICE,
175 },
Harald Welteb43bc042011-06-27 10:29:17 +0200176};
177
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200178/*! descriptive string for each log level */
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100179/* You have to keep this in sync with the structure loglevel_strs. */
Maxc65c5b42017-03-15 13:20:23 +0100180static const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
Holger Hans Peter Freyther6ec6bd92014-12-28 18:27:38 +0100181 "Don't use. It doesn't log anything",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100182 "Log debug messages and higher levels",
183 "Log informational messages and higher levels",
Ruben Undheim029f5a12015-09-16 19:02:35 +0200184 "Log noticeable messages and higher levels",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100185 "Log error messages and higher levels",
186 "Log only fatal messages",
187 NULL,
188};
189
Max68bf16a2018-01-10 17:00:43 +0100190static void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100191{
192 if (!osmo_log_info) {
193 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100194 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100195 OSMO_ASSERT(osmo_log_info);
196 }
197}
198
Harald Welteb43bc042011-06-27 10:29:17 +0200199/* special magic for negative (library-internal) log subsystem numbers */
200static int subsys_lib2index(int subsys)
201{
202 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
203}
204
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200205/*! Parse a human-readable log level into a numeric value
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200206 * \param lvl[in] zero-terminated string containing log level name
207 * \returns numeric log level
208 */
Harald Welte3ae27582010-03-26 21:24:24 +0800209int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800210{
211 return get_string_value(loglevel_strs, lvl);
212}
213
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200214/*! convert a numeric log level into human-readable string
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200215 * \param lvl[in] numeric log level
216 * \returns zero-terminated string (log level name)
217 */
Harald Welte9ac22252010-05-11 11:19:40 +0200218const char *log_level_str(unsigned int lvl)
219{
220 return get_value_string(loglevel_strs, lvl);
221}
222
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200224 * \param[in] category human-readable log category name
225 * \returns numeric category value, or -EINVAL otherwise
226 */
Harald Welte3ae27582010-03-26 21:24:24 +0800227int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800228{
229 int i;
230
Max68bf16a2018-01-10 17:00:43 +0100231 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100232
Harald Welte4ebdf742010-05-19 19:54:00 +0200233 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200234 if (osmo_log_info->cat[i].name == NULL)
235 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200236 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800237 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800238 }
239
240 return -EINVAL;
241}
242
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200243/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200244 * \param[in] target log target to be configured
245 * \param[in] _mask log category mask string
246 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800247 * The format can be this: category1:category2:category3
248 * or category1,2:category2,3:...
249 */
Harald Welte3ae27582010-03-26 21:24:24 +0800250void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800251{
252 int i = 0;
253 char *mask = strdup(_mask);
254 char *category_token = NULL;
255
Max68bf16a2018-01-10 17:00:43 +0100256 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100257
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800258 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200259 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800260 target->categories[i].enabled = 0;
261
262 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200263 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800264 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200265 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200266 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800267 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200268
269 if (!osmo_log_info->cat[i].name)
270 continue;
271
272 length = strlen(category_token);
273 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200274
275 /* Use longest length not to match subocurrences. */
276 if (cat_length > length)
277 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800278
279 if (colon)
280 length = colon - category_token;
281
Harald Welte4ebdf742010-05-19 19:54:00 +0200282 if (strncasecmp(osmo_log_info->cat[i].name,
283 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800284 int level = 0;
285
286 if (colon)
287 level = atoi(colon+1);
288
Harald Weltefaadfe22010-03-26 21:05:43 +0800289 target->categories[i].enabled = 1;
290 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800291 }
292 }
293 } while ((category_token = strtok(NULL, ":")));
294
295 free(mask);
296}
297
298static const char* color(int subsys)
299{
Harald Welte4ebdf742010-05-19 19:54:00 +0200300 if (subsys < osmo_log_info->num_cat)
301 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800302
Harald Welted788f662010-03-26 09:45:03 +0800303 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800304}
305
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100306static const struct value_string level_colors[] = {
307 { LOGL_DEBUG, "\033[1;34m" },
308 { LOGL_INFO, "\033[1;32m" },
309 { LOGL_NOTICE, "\033[1;33m" },
310 { LOGL_ERROR, "\033[1;31m" },
311 { LOGL_FATAL, "\033[1;31m" },
312 { 0, NULL }
313};
314
315static const char *level_color(int level)
316{
317 const char *c = get_value_string_or_null(level_colors, level);
318 if (!c)
319 return get_value_string(level_colors, LOGL_FATAL);
320 return c;
321}
322
Harald Welteaa00f992016-12-02 15:30:02 +0100323const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100324{
325 if (subsys < osmo_log_info->num_cat)
326 return osmo_log_info->cat[subsys].name;
327
328 return NULL;
329}
330
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100331static const char *const_basename(const char *path)
332{
333 const char *bn = strrchr(path, '/');
334 if (!bn || !bn[1])
335 return path;
336 return bn + 1;
337}
338
Harald Welte3ae27582010-03-26 21:24:24 +0800339static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200340 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100341 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800343 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200344 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100345 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800346
347 /* are we using color */
348 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100349 c_subsys = color(subsys);
350 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100351 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200352 if (ret < 0)
353 goto err;
354 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800355 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800356 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800357 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100358 if (target->print_ext_timestamp) {
359 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100360 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200361 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100362 localtime_r(&tv.tv_sec, &tm);
363 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100364 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100365 tm.tm_hour, tm.tm_min, tm.tm_sec,
366 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100367 if (ret < 0)
368 goto err;
369 OSMO_SNPRINTF_RET(ret, rem, offset, len);
370 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800371 char *timestr;
372 time_t tm;
373 tm = time(NULL);
374 timestr = ctime(&tm);
375 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200376 ret = snprintf(buf + offset, rem, "%s ", timestr);
377 if (ret < 0)
378 goto err;
379 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800380 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100381 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100382 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
383 target->use_color ? level_color(level) : "",
384 log_category_name(subsys),
385 target->use_color ? "\033[0;m" : "",
386 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100387 if (ret < 0)
388 goto err;
389 OSMO_SNPRINTF_RET(ret, rem, offset, len);
390 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100391 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100392 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
393 target->use_color ? level_color(level) : "",
394 log_level_str(level),
395 target->use_color ? "\033[0;m" : "",
396 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100397 if (ret < 0)
398 goto err;
399 OSMO_SNPRINTF_RET(ret, rem, offset, len);
400 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100401 if (target->print_category_hex) {
402 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200403 if (ret < 0)
404 goto err;
405 OSMO_SNPRINTF_RET(ret, rem, offset, len);
406 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100407 switch (target->print_filename2) {
408 case LOG_FILENAME_NONE:
409 break;
410 case LOG_FILENAME_PATH:
411 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
412 if (ret < 0)
413 goto err;
414 OSMO_SNPRINTF_RET(ret, rem, offset, len);
415 break;
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100416 case LOG_FILENAME_BASENAME:
417 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
418 if (ret < 0)
419 goto err;
420 OSMO_SNPRINTF_RET(ret, rem, offset, len);
421 break;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100422 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800423 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200424 ret = vsnprintf(buf + offset, rem, format, ap);
425 if (ret < 0)
426 goto err;
427 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800428
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100429 if (target->use_color) {
430 ret = snprintf(buf + offset, rem, "\033[0;m");
431 if (ret < 0)
432 goto err;
433 OSMO_SNPRINTF_RET(ret, rem, offset, len);
434 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200435err:
436 buf[sizeof(buf)-1] = '\0';
437 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800438}
439
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100440/* Catch internal logging category indexes as well as out-of-bounds indexes.
441 * For internal categories, the ID is negative starting with -1; and internal
442 * logging categories are added behind the user categories. For out-of-bounds
443 * indexes, return the index of DLGLOBAL. The returned category index is
444 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
445 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100446static inline int map_subsys(int subsys)
447{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100448 /* Note: comparing signed and unsigned integers */
449
450 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
451 subsys = DLGLOBAL;
452
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100453 if (subsys < 0)
454 subsys = subsys_lib2index(subsys);
455
Neels Hofmeyrca135742016-12-12 14:18:54 +0100456 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100457 subsys = subsys_lib2index(DLGLOBAL);
458
Neels Hofmeyrca135742016-12-12 14:18:54 +0100459 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
460
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100461 return subsys;
462}
463
Maxc65c5b42017-03-15 13:20:23 +0100464static inline bool should_log_to_target(struct log_target *tar, int subsys,
465 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100466{
467 struct log_category *category;
468
469 category = &tar->categories[subsys];
470
471 /* subsystem is not supposed to be logged */
472 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100473 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100474
475 /* Check the global log level */
476 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100477 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100478
479 /* Check the category log level */
480 if (tar->loglevel == 0 && category->loglevel != 0 &&
481 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100482 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100483
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100484 /* Apply filters here... if that becomes messy we will
485 * need to put filters in a list and each filter will
486 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100487 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100488 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100489
490 if (osmo_log_info->filter_fn)
491 return osmo_log_info->filter_fn(&log_context, tar);
492
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100493 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100494 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100495}
496
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200497/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200498 * \param[in] subsys Logging sub-system
499 * \param[in] level Log level
500 * \param[in] file name of source code file
501 * \param[in] cont continuation (1) or new line (0)
502 * \param[in] format format string
503 * \param[in] ap vararg-list containing format string arguments
504 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200505void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200506 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800507{
Harald Welte3ae27582010-03-26 21:24:24 +0800508 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800509
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100510 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200511
Harald Welte28222962011-02-18 20:37:04 +0100512 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200513 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800514
Maxc65c5b42017-03-15 13:20:23 +0100515 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800516 continue;
517
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200518 /* According to the manpage, vsnprintf leaves the value of ap
519 * in undefined state. Since _output uses vsnprintf and it may
520 * be called several times, we have to pass a copy of ap. */
521 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100522 if (tar->raw_output)
523 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
524 else
525 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200526 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800527 }
528}
529
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200530/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200531 * \param[in] subsys Logging sub-system
532 * \param[in] file name of source code file
533 * \param[in] cont continuation (1) or new line (0)
534 * \param[in] format format string
535 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200536void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800537 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800538{
539 va_list ap;
540
541 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200542 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800543 va_end(ap);
544}
545
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200546/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200547 * \param[in] subsys Logging sub-system
548 * \param[in] level Log level
549 * \param[in] file name of source code file
550 * \param[in] cont continuation (1) or new line (0)
551 * \param[in] format format string
552 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200553void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800554{
555 va_list ap;
556
557 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200558 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800559 va_end(ap);
560}
561
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200562/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200563 * \param[in] target Log target to be registered
564 */
Harald Welte3ae27582010-03-26 21:24:24 +0800565void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800566{
Harald Welte28222962011-02-18 20:37:04 +0100567 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800568}
569
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200570/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200571 * \param[in] target Log target to be unregistered
572 */
Harald Welte3ae27582010-03-26 21:24:24 +0800573void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800574{
575 llist_del(&target->entry);
576}
577
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200578/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800579void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800580{
Harald Welte3ae27582010-03-26 21:24:24 +0800581 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800582}
583
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200584/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200585 * \param[in] ctx_nr logging context number
586 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200587 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200588 *
589 * A logging context is something like the subscriber identity to which
590 * the currently processed message relates, or the BTS through which it
591 * was received. As soon as this data is known, it can be set using
592 * this function. The main use of context information is for logging
593 * filters.
594 */
Harald Welte3ae27582010-03-26 21:24:24 +0800595int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800596{
Harald Welte3ae27582010-03-26 21:24:24 +0800597 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800598 return -EINVAL;
599
Harald Welte3ae27582010-03-26 21:24:24 +0800600 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800601
602 return 0;
603}
604
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200605/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200606 * \param[in] target Log target to be affected
607 * \param[in] all enable (1) or disable (0) the ALL filter
608 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100609 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100610 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
611 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200612 */
Harald Welte3ae27582010-03-26 21:24:24 +0800613void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800614{
615 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100616 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800617 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100618 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800619}
620
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200621/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200622 * \param[in] target Log target to be affected
623 * \param[in] use_color Use color (1) or don't use color (0)
624 */
Harald Welte3ae27582010-03-26 21:24:24 +0800625void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800626{
627 target->use_color = use_color;
628}
629
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200630/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200631 * \param[in] target Log target to be affected
632 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
633 */
Harald Welte3ae27582010-03-26 21:24:24 +0800634void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800635{
636 target->print_timestamp = print_timestamp;
637}
638
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200639/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100640 * \param[in] target Log target to be affected
641 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
642 *
643 * When both timestamp and extended timestamp is enabled then only
644 * the extended timestamp will be used. The format of the timestamp
645 * is YYYYMMDDhhmmssnnn.
646 */
647void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
648{
649 target->print_ext_timestamp = print_timestamp;
650}
651
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100652/*! Use log_set_print_filename2() instead.
653 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
654 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
655 * behavior, which combined the category in hex with the filename. For example, if the category-hex
656 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
657 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200658 * \param[in] target Log target to be affected
659 * \param[in] print_filename Enable (1) or disable (0) filenames
660 */
661void log_set_print_filename(struct log_target *target, int print_filename)
662{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100663 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
664 log_set_print_category_hex(target, print_filename);
665}
666
667/*! Enable or disable printing of the filename while logging.
668 * \param[in] target Log target to be affected.
669 * \param[in] print_filename An LOG_FILENAME_* enum value.
670 * LOG_FILENAME_NONE omits the source file and line information from logs.
671 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
672 */
673void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
674{
675 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200676}
677
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200678/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100679 * \param[in] target Log target to be affected
680 * \param[in] print_catname Enable (1) or disable (0) filenames
681 *
682 * Print the category/subsys name in front of every log message.
683 */
684void log_set_print_category(struct log_target *target, int print_category)
685{
686 target->print_category = print_category;
687}
688
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100689/*! Enable or disable printing of the category number in hex ('<000b>').
690 * \param[in] target Log target to be affected.
691 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
692 */
693void log_set_print_category_hex(struct log_target *target, int print_category_hex)
694{
695 target->print_category_hex = print_category_hex;
696}
697
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100698/*! Enable or disable printing of the log level name.
699 * \param[in] target Log target to be affected
700 * \param[in] print_catname Enable (1) or disable (0) filenames
701 *
702 * Print the log level name in front of every log message.
703 */
704void log_set_print_level(struct log_target *target, int print_level)
705{
706 target->print_level = (bool)print_level;
707}
708
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200709/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200710 * \param[in] target Log target to be affected
711 * \param[in] log_level New global log level
712 */
Harald Welte3ae27582010-03-26 21:24:24 +0800713void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800714{
715 target->loglevel = log_level;
716}
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100719 * \param[in] target Log target to be affected
720 * \param[in] category Log category to be affected
721 * \param[in] enable whether to enable or disable the filter
722 * \param[in] level Log level of the filter
723 */
Harald Welte3ae27582010-03-26 21:24:24 +0800724void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800725 int enable, int level)
726{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100727 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800728 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100729 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800730 target->categories[category].enabled = !!enable;
731 target->categories[category].loglevel = level;
732}
733
Harald Welte44c0f632017-01-15 17:58:29 +0100734#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100735static void _file_output(struct log_target *target, unsigned int level,
736 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800737{
Harald Welte0083cd32010-08-25 14:55:44 +0200738 fprintf(target->tgt_file.out, "%s", log);
739 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800740}
Harald Welte44c0f632017-01-15 17:58:29 +0100741#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800742
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200743/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200744 * \returns dynamically-allocated log target
745 * This funcition allocates a \ref log_target and initializes it
746 * with some default values. The newly created target is not
747 * registered yet.
748 */
Harald Welte3ae27582010-03-26 21:24:24 +0800749struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800750{
Harald Welte3ae27582010-03-26 21:24:24 +0800751 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800752 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800753
Max68bf16a2018-01-10 17:00:43 +0100754 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100755
Harald Welte3ae27582010-03-26 21:24:24 +0800756 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800757 if (!target)
758 return NULL;
759
Harald Welteb43bc042011-06-27 10:29:17 +0200760 target->categories = talloc_zero_array(target,
761 struct log_category,
762 osmo_log_info->num_cat);
763 if (!target->categories) {
764 talloc_free(target);
765 return NULL;
766 }
767
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800768 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800769
770 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200771 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800772 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200773 cat->enabled = osmo_log_info->cat[i].enabled;
774 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800775 }
776
777 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800778 target->use_color = 1;
779 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100780 target->print_filename2 = LOG_FILENAME_PATH;
781 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800782
783 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800784 target->loglevel = 0;
785 return target;
786}
787
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200788/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200789 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800790struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800791{
Harald Weltea3b844c2010-03-27 00:04:40 +0800792/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200793#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800794 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800795
Harald Welte3ae27582010-03-26 21:24:24 +0800796 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800797 if (!target)
798 return NULL;
799
Harald Welte28222962011-02-18 20:37:04 +0100800 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200801 target->tgt_file.out = stderr;
802 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800803 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800804#else
805 return NULL;
806#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800807}
808
Harald Welte44c0f632017-01-15 17:58:29 +0100809#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200810/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200811 * \param[in] fname File name of the new log file
812 * \returns Log target in case of success, NULL otherwise
813 */
Harald Welte3086c392010-08-25 19:10:50 +0200814struct log_target *log_target_create_file(const char *fname)
815{
816 struct log_target *target;
817
818 target = log_target_create();
819 if (!target)
820 return NULL;
821
Harald Welte28222962011-02-18 20:37:04 +0100822 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200823 target->tgt_file.out = fopen(fname, "a");
824 if (!target->tgt_file.out)
825 return NULL;
826
827 target->output = _file_output;
828
829 target->tgt_file.fname = talloc_strdup(target, fname);
830
831 return target;
832}
Harald Welte44c0f632017-01-15 17:58:29 +0100833#endif
Harald Welte3086c392010-08-25 19:10:50 +0200834
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200835/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200836 * \param[in] type Log target type
837 * \param[in] fname File name
838 * \returns Log target (if found), NULL otherwise
839 */
Harald Welte28222962011-02-18 20:37:04 +0100840struct log_target *log_target_find(int type, const char *fname)
841{
842 struct log_target *tgt;
843
844 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
845 if (tgt->type != type)
846 continue;
Maxc90f40a2018-01-11 10:52:28 +0100847 switch (tgt->type) {
848 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100849 if (!strcmp(fname, tgt->tgt_file.fname))
850 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100851 break;
852 case LOG_TGT_TYPE_GSMTAP:
853 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
854 return tgt;
855 break;
856 default:
Harald Welte28222962011-02-18 20:37:04 +0100857 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100858 }
Harald Welte28222962011-02-18 20:37:04 +0100859 }
860 return NULL;
861}
862
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200863/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200864 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200865void log_target_destroy(struct log_target *target)
866{
867
868 /* just in case, to make sure we don't have any references */
869 log_del_target(target);
870
Harald Welte44c0f632017-01-15 17:58:29 +0100871#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200872 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200873/* since C89/C99 says stderr is a macro, we can safely do this! */
874#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200875 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200876 if (target->tgt_file.out != stderr)
877#endif
878 {
Harald Welte3086c392010-08-25 19:10:50 +0200879 fclose(target->tgt_file.out);
880 target->tgt_file.out = NULL;
881 }
882 }
Harald Welte44c0f632017-01-15 17:58:29 +0100883#endif
Harald Welte3086c392010-08-25 19:10:50 +0200884
885 talloc_free(target);
886}
887
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200888/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200889 * \param[in] target log target to re-open
890 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200891int log_target_file_reopen(struct log_target *target)
892{
893 fclose(target->tgt_file.out);
894
895 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
896 if (!target->tgt_file.out)
897 return -errno;
898
899 /* we assume target->output already to be set */
900
901 return 0;
902}
903
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200904/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200905 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100906int log_targets_reopen(void)
907{
908 struct log_target *tar;
909 int rc = 0;
910
911 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
912 switch (tar->type) {
913 case LOG_TGT_TYPE_FILE:
914 if (log_target_file_reopen(tar) < 0)
915 rc = -1;
916 break;
917 default:
918 break;
919 }
920 }
921
922 return rc;
923}
924
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200925/*! Generates the logging command string for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200926 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200927 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200928 */
Maxc65c5b42017-03-15 13:20:23 +0100929const char *log_vty_command_string()
Harald Welte7638af92010-05-11 16:39:22 +0200930{
Harald Weltece9fec32011-06-27 14:19:16 +0200931 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100932 int len = 0, offset = 0, ret, i, rem;
933 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200934 char *str;
935
Max68bf16a2018-01-10 17:00:43 +0100936 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100937
Harald Welteb43bc042011-06-27 10:29:17 +0200938 for (i = 0; i < info->num_cat; i++) {
939 if (info->cat[i].name == NULL)
940 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100941 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200942 }
Harald Welte7638af92010-05-11 16:39:22 +0200943
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100944 for (i = 0; i < LOGLEVEL_DEFS; i++)
945 size += strlen(loglevel_strs[i].str) + 1;
946
947 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200948 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200949 if (!str)
950 return NULL;
951
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200952 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100953 if (ret < 0)
954 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200955 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200956
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100957 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200958 if (info->cat[i].name) {
959 int j, name_len = strlen(info->cat[i].name)+1;
960 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100961
Harald Welteb43bc042011-06-27 10:29:17 +0200962 for (j = 0; j < name_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200963 name[j] = tolower((unsigned char)info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100964
Harald Welteb43bc042011-06-27 10:29:17 +0200965 name[name_len-1] = '\0';
966 ret = snprintf(str + offset, rem, "%s|", name+1);
967 if (ret < 0)
968 goto err;
969 OSMO_SNPRINTF_RET(ret, rem, offset, len);
970 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100971 }
972 offset--; /* to remove the trailing | */
973 rem++;
974
975 ret = snprintf(str + offset, rem, ") (");
976 if (ret < 0)
977 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200978 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100979
980 for (i = 0; i < LOGLEVEL_DEFS; i++) {
981 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
982 char loglevel_str[loglevel_str_len];
983
984 for (j = 0; j < loglevel_str_len; j++)
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200985 loglevel_str[j] = tolower((unsigned char)loglevel_strs[i].str[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100986
987 loglevel_str[loglevel_str_len-1] = '\0';
988 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
989 if (ret < 0)
990 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200991 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100992 }
993 offset--; /* to remove the trailing | */
994 rem++;
995
996 ret = snprintf(str + offset, rem, ")");
997 if (ret < 0)
998 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200999 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001000err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +02001001 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001002 return str;
1003}
1004
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001005/*! Generates the logging command description for VTY
Harald Welte18fc4652011-08-17 14:14:17 +02001006 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +02001007 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +02001008 */
Maxc65c5b42017-03-15 13:20:23 +01001009const char *log_vty_command_description()
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001010{
Harald Weltece9fec32011-06-27 14:19:16 +02001011 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001012 char *str;
1013 int i, ret, len = 0, offset = 0, rem;
1014 unsigned int size =
1015 strlen(LOGGING_STR
1016 "Set the log level for a specified category\n") + 1;
1017
Max68bf16a2018-01-10 17:00:43 +01001018 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001019
Harald Welteb43bc042011-06-27 10:29:17 +02001020 for (i = 0; i < info->num_cat; i++) {
1021 if (info->cat[i].name == NULL)
1022 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001023 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +02001024 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001025
1026 for (i = 0; i < LOGLEVEL_DEFS; i++)
1027 size += strlen(loglevel_descriptions[i]) + 1;
1028
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +02001029 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001030 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +02001031 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001032 if (!str)
1033 return NULL;
1034
1035 ret = snprintf(str + offset, rem, LOGGING_STR
1036 "Set the log level for a specified category\n");
1037 if (ret < 0)
1038 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001039 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001040
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +02001041 ret = snprintf(str + offset, rem,
1042 "Global setting for all subsystems\n");
1043 if (ret < 0)
1044 goto err;
1045 OSMO_SNPRINTF_RET(ret, rem, offset, len);
1046
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001047 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +02001048 if (info->cat[i].name == NULL)
1049 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001050 ret = snprintf(str + offset, rem, "%s\n",
1051 info->cat[i].description);
1052 if (ret < 0)
1053 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001054 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001055 }
1056 for (i = 0; i < LOGLEVEL_DEFS; i++) {
1057 ret = snprintf(str + offset, rem, "%s\n",
1058 loglevel_descriptions[i]);
1059 if (ret < 0)
1060 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +02001061 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +01001062 }
1063err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +02001064 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +02001065 return str;
1066}
1067
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001068/*! Initialize the Osmocom logging core
Harald Welte18fc4652011-08-17 14:14:17 +02001069 * \param[in] inf Information regarding logging categories
1070 * \param[in] ctx \ref talloc context for logging allocations
1071 * \returns 0 in case of success, negative in case of error
1072 */
Harald Welteb43bc042011-06-27 10:29:17 +02001073int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001074{
Harald Welteb43bc042011-06-27 10:29:17 +02001075 int i;
1076
1077 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
1078 if (!tall_log_ctx)
1079 return -ENOMEM;
1080
1081 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
1082 if (!osmo_log_info)
1083 return -ENOMEM;
1084
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +01001085 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +02001086 osmo_log_info->num_cat_user = inf->num_cat;
1087 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +02001088 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +02001089
1090 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
1091 struct log_info_cat,
1092 osmo_log_info->num_cat);
1093 if (!osmo_log_info->cat) {
1094 talloc_free(osmo_log_info);
1095 osmo_log_info = NULL;
1096 return -ENOMEM;
1097 }
1098
1099 /* copy over the user part */
1100 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001101 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
1102 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +02001103 sizeof(struct log_info_cat));
1104 }
1105
1106 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001107 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001108 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001109 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001110 &internal_cat[i], sizeof(struct log_info_cat));
1111 }
1112
1113 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001114}
Harald Welte18fc4652011-08-17 14:14:17 +02001115
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001116/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001117 * This function destroys all targets and releases associated memory */
1118void log_fini(void)
1119{
1120 struct log_target *tar, *tar2;
1121
1122 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1123 log_target_destroy(tar);
1124
1125 talloc_free(osmo_log_info);
1126 osmo_log_info = NULL;
1127 talloc_free(tall_log_ctx);
1128 tall_log_ctx = NULL;
1129}
1130
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001131/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001132 * \returns != 0 if a log entry might get generated by at least one target */
1133int log_check_level(int subsys, unsigned int level)
1134{
1135 struct log_target *tar;
1136
Max68bf16a2018-01-10 17:00:43 +01001137 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001138
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001139 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001140
1141 /* TODO: The following could/should be cached (update on config) */
1142
1143 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001144 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001145 continue;
1146
1147 /* This might get logged (ignoring filters) */
1148 return 1;
1149 }
1150
1151 /* We are sure, that this will not be logged. */
1152 return 0;
1153}
1154
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001155/*! @} */