blob: c8b86b1c967bf91d4e43fdf6c00b740029a41873 [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welte18fc4652011-08-17 14:14:17 +020024/* \addtogroup logging
25 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026 * libosmocore Logging sub-system
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 *
28 * \file logging.c */
Harald Welte18fc4652011-08-17 14:14:17 +020029
Harald Welte01fd5cb2010-03-26 23:51:31 +080030#include "../config.h"
31
Harald Welte4a2bb9e2010-03-26 09:33:40 +080032#include <stdarg.h>
33#include <stdlib.h>
34#include <stdio.h>
35#include <string.h>
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010036#include <ctype.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080037
38#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080039#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080040#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080041#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010042#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080043#include <errno.h>
44
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010045#include <osmocom/core/talloc.h>
46#include <osmocom/core/utils.h>
47#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020048#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080049
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010050#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
51
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010052osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010053 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010054osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010055 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010056osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010057 enum_logging_filters_fit_in_log_target_filter_map);
58
Harald Welteb43bc042011-06-27 10:29:17 +020059struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080060
Harald Welte3ae27582010-03-26 21:24:24 +080061static struct log_context log_context;
62static void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010063LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080064
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010065#define LOGLEVEL_DEFS 6 /* Number of loglevels.*/
66
67static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = {
Harald Welte9b186702013-03-19 09:55:28 +010068 { 0, "EVERYTHING" },
Harald Welte4a2bb9e2010-03-26 09:33:40 +080069 { LOGL_DEBUG, "DEBUG" },
70 { LOGL_INFO, "INFO" },
71 { LOGL_NOTICE, "NOTICE" },
72 { LOGL_ERROR, "ERROR" },
73 { LOGL_FATAL, "FATAL" },
74 { 0, NULL },
75};
76
Harald Welteb43bc042011-06-27 10:29:17 +020077#define INT2IDX(x) (-1*(x)-1)
78static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
79 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
80 .name = "DLGLOBAL",
81 .description = "Library-internal global log family",
82 .loglevel = LOGL_NOTICE,
83 .enabled = 1,
84 },
root8a996b42011-09-26 11:22:21 +020085 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
86 .name = "DLLAPD",
87 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020088 .loglevel = LOGL_NOTICE,
89 .enabled = 1,
90 },
Harald Welte892e6212011-07-19 14:31:44 +020091 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020092 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020093 .description = "A-bis Intput Subsystem",
94 .loglevel = LOGL_NOTICE,
95 .enabled = 1,
96 },
Harald Welte892e6212011-07-19 14:31:44 +020097 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +020098 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020099 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
100 .loglevel = LOGL_NOTICE,
101 .enabled = 1,
102 },
Harald Welte892e6212011-07-19 14:31:44 +0200103 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200104 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200105 .description = "A-bis Input Driver for Signalling",
106 .enabled = 0, .loglevel = LOGL_NOTICE,
107 },
Harald Welte892e6212011-07-19 14:31:44 +0200108 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200109 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200110 .description = "A-bis Input Driver for B-Channels (voice)",
111 .enabled = 0, .loglevel = LOGL_NOTICE,
112 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200113 [INT2IDX(DLSMS)] = {
114 .name = "DLSMS",
115 .description = "Layer3 Short Message Service (SMS)",
116 .enabled = 1, .loglevel = LOGL_NOTICE,
117 .color = "\033[1;38m",
118 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200119 [INT2IDX(DLCTRL)] = {
120 .name = "DLCTRL",
121 .description = "Control Interface",
122 .enabled = 1, .loglevel = LOGL_NOTICE,
123 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100124 [INT2IDX(DLGTP)] = {
125 .name = "DLGTP",
126 .description = "GPRS GTP library",
127 .enabled = 1, .loglevel = LOGL_NOTICE,
128 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100129 [INT2IDX(DLSTATS)] = {
130 .name = "DLSTATS",
131 .description = "Statistics messages and logging",
132 .enabled = 1, .loglevel = LOGL_NOTICE,
133 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100134 [INT2IDX(DLGSUP)] = {
135 .name = "DLGSUP",
136 .description = "Generic Subscriber Update Protocol",
137 .enabled = 1, .loglevel = LOGL_NOTICE,
138 },
Harald Weltec0f00072016-04-27 18:32:35 +0200139 [INT2IDX(DLOAP)] = {
140 .name = "DLOAP",
141 .description = "Osmocom Authentication Protocol",
142 .enabled = 1, .loglevel = LOGL_NOTICE,
143 },
Harald Welte059c4042017-04-03 22:20:49 +0200144 [INT2IDX(DLSS7)] = {
145 .name = "DLSS7",
146 .description = "libosmo-sigtran Signalling System 7",
147 .enabled = 1, .loglevel = LOGL_NOTICE,
148 },
149 [INT2IDX(DLSCCP)] = {
150 .name = "DLSCCP",
151 .description = "libosmo-sigtran SCCP Implementation",
152 .enabled = 1, .loglevel = LOGL_NOTICE,
153 },
154 [INT2IDX(DLSUA)] = {
155 .name = "DLSUA",
156 .description = "libosmo-sigtran SCCP User Adaptation",
157 .enabled = 1, .loglevel = LOGL_NOTICE,
158 },
159 [INT2IDX(DLM3UA)] = {
160 .name = "DLM3UA",
161 .description = "libosmo-sigtran MTP3 User Adaptation",
162 .enabled = 1, .loglevel = LOGL_NOTICE,
163 },
Harald Welteb43bc042011-06-27 10:29:17 +0200164};
165
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200166/*! descriptive string for each log level */
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100167/* You have to keep this in sync with the structure loglevel_strs. */
Maxc65c5b42017-03-15 13:20:23 +0100168static const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
Holger Hans Peter Freyther6ec6bd92014-12-28 18:27:38 +0100169 "Don't use. It doesn't log anything",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100170 "Log debug messages and higher levels",
171 "Log informational messages and higher levels",
Ruben Undheim029f5a12015-09-16 19:02:35 +0200172 "Log noticeable messages and higher levels",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100173 "Log error messages and higher levels",
174 "Log only fatal messages",
175 NULL,
176};
177
Harald Welte18a7d812017-03-16 23:54:55 +0100178static void assert_loginfo(void)
179{
180 if (!osmo_log_info) {
181 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
182 "You must call log_init() before using logging!\n");
183 OSMO_ASSERT(osmo_log_info);
184 }
185}
186
Harald Welteb43bc042011-06-27 10:29:17 +0200187/* special magic for negative (library-internal) log subsystem numbers */
188static int subsys_lib2index(int subsys)
189{
190 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
191}
192
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193/*! Parse a human-readable log level into a numeric value
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200194 * \param lvl[in] zero-terminated string containing log level name
195 * \returns numeric log level
196 */
Harald Welte3ae27582010-03-26 21:24:24 +0800197int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800198{
199 return get_string_value(loglevel_strs, lvl);
200}
201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! convert a numeric log level into human-readable string
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200203 * \param lvl[in] numeric log level
204 * \returns zero-terminated string (log level name)
205 */
Harald Welte9ac22252010-05-11 11:19:40 +0200206const char *log_level_str(unsigned int lvl)
207{
208 return get_value_string(loglevel_strs, lvl);
209}
210
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200211/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200212 * \param[in] category human-readable log category name
213 * \returns numeric category value, or -EINVAL otherwise
214 */
Harald Welte3ae27582010-03-26 21:24:24 +0800215int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800216{
217 int i;
218
Harald Welte18a7d812017-03-16 23:54:55 +0100219 assert_loginfo();
220
Harald Welte4ebdf742010-05-19 19:54:00 +0200221 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200222 if (osmo_log_info->cat[i].name == NULL)
223 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200224 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800225 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800226 }
227
228 return -EINVAL;
229}
230
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200231/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200232 * \param[in] target log target to be configured
233 * \param[in] _mask log category mask string
234 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800235 * The format can be this: category1:category2:category3
236 * or category1,2:category2,3:...
237 */
Harald Welte3ae27582010-03-26 21:24:24 +0800238void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800239{
240 int i = 0;
241 char *mask = strdup(_mask);
242 char *category_token = NULL;
243
Harald Welte18a7d812017-03-16 23:54:55 +0100244 assert_loginfo();
245
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800246 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200247 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800248 target->categories[i].enabled = 0;
249
250 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200251 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800252 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200253 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200254 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800255 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200256
257 if (!osmo_log_info->cat[i].name)
258 continue;
259
260 length = strlen(category_token);
261 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200262
263 /* Use longest length not to match subocurrences. */
264 if (cat_length > length)
265 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800266
267 if (colon)
268 length = colon - category_token;
269
Harald Welte4ebdf742010-05-19 19:54:00 +0200270 if (strncasecmp(osmo_log_info->cat[i].name,
271 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800272 int level = 0;
273
274 if (colon)
275 level = atoi(colon+1);
276
Harald Weltefaadfe22010-03-26 21:05:43 +0800277 target->categories[i].enabled = 1;
278 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800279 }
280 }
281 } while ((category_token = strtok(NULL, ":")));
282
283 free(mask);
284}
285
286static const char* color(int subsys)
287{
Harald Welte4ebdf742010-05-19 19:54:00 +0200288 if (subsys < osmo_log_info->num_cat)
289 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800290
Harald Welted788f662010-03-26 09:45:03 +0800291 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800292}
293
Harald Welteaa00f992016-12-02 15:30:02 +0100294const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100295{
296 if (subsys < osmo_log_info->num_cat)
297 return osmo_log_info->cat[subsys].name;
298
299 return NULL;
300}
301
Harald Welte3ae27582010-03-26 21:24:24 +0800302static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200303 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100304 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800305{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800306 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200307 int ret, len = 0, offset = 0, rem = sizeof(buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800308
309 /* are we using color */
310 if (target->use_color) {
Harald Welted788f662010-03-26 09:45:03 +0800311 const char *c = color(subsys);
312 if (c) {
Holger Hans Peter Freyther5f91a402014-12-05 10:29:45 +0100313 ret = snprintf(buf + offset, rem, "%s", c);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200314 if (ret < 0)
315 goto err;
316 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800317 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800318 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800319 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100320 if (target->print_ext_timestamp) {
321 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100322 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200323 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100324 localtime_r(&tv.tv_sec, &tm);
325 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100326 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100327 tm.tm_hour, tm.tm_min, tm.tm_sec,
328 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100329 if (ret < 0)
330 goto err;
331 OSMO_SNPRINTF_RET(ret, rem, offset, len);
332 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800333 char *timestr;
334 time_t tm;
335 tm = time(NULL);
336 timestr = ctime(&tm);
337 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200338 ret = snprintf(buf + offset, rem, "%s ", timestr);
339 if (ret < 0)
340 goto err;
341 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100343 if (target->print_category) {
Harald Welteaa00f992016-12-02 15:30:02 +0100344 ret = snprintf(buf + offset, rem, "%s ", log_category_name(subsys));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100345 if (ret < 0)
346 goto err;
347 OSMO_SNPRINTF_RET(ret, rem, offset, len);
348 }
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200349 if (target->print_filename) {
350 ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
351 subsys, file, line);
352 if (ret < 0)
353 goto err;
354 OSMO_SNPRINTF_RET(ret, rem, offset, len);
355 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800356 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200357 ret = vsnprintf(buf + offset, rem, format, ap);
358 if (ret < 0)
359 goto err;
360 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800361
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200362 ret = snprintf(buf + offset, rem, "%s",
363 target->use_color ? "\033[0;m" : "");
364 if (ret < 0)
365 goto err;
366 OSMO_SNPRINTF_RET(ret, rem, offset, len);
367err:
368 buf[sizeof(buf)-1] = '\0';
369 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800370}
371
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100372/* Catch internal logging category indexes as well as out-of-bounds indexes.
373 * For internal categories, the ID is negative starting with -1; and internal
374 * logging categories are added behind the user categories. For out-of-bounds
375 * indexes, return the index of DLGLOBAL. The returned category index is
376 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
377 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100378static inline int map_subsys(int subsys)
379{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100380 /* Note: comparing signed and unsigned integers */
381
382 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
383 subsys = DLGLOBAL;
384
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100385 if (subsys < 0)
386 subsys = subsys_lib2index(subsys);
387
Neels Hofmeyrca135742016-12-12 14:18:54 +0100388 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100389 subsys = subsys_lib2index(DLGLOBAL);
390
Neels Hofmeyrca135742016-12-12 14:18:54 +0100391 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
392
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100393 return subsys;
394}
395
Maxc65c5b42017-03-15 13:20:23 +0100396static inline bool should_log_to_target(struct log_target *tar, int subsys,
397 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100398{
399 struct log_category *category;
400
401 category = &tar->categories[subsys];
402
403 /* subsystem is not supposed to be logged */
404 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100405 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100406
407 /* Check the global log level */
408 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100409 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100410
411 /* Check the category log level */
412 if (tar->loglevel == 0 && category->loglevel != 0 &&
413 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100414 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100415
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100416 /* Apply filters here... if that becomes messy we will
417 * need to put filters in a list and each filter will
418 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100419 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100420 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100421
422 if (osmo_log_info->filter_fn)
423 return osmo_log_info->filter_fn(&log_context, tar);
424
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100425 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100426 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100427}
428
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200429/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200430 * \param[in] subsys Logging sub-system
431 * \param[in] level Log level
432 * \param[in] file name of source code file
433 * \param[in] cont continuation (1) or new line (0)
434 * \param[in] format format string
435 * \param[in] ap vararg-list containing format string arguments
436 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200437void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200438 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800439{
Harald Welte3ae27582010-03-26 21:24:24 +0800440 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800441
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100442 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200443
Harald Welte28222962011-02-18 20:37:04 +0100444 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200445 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800446
Maxc65c5b42017-03-15 13:20:23 +0100447 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800448 continue;
449
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200450 /* According to the manpage, vsnprintf leaves the value of ap
451 * in undefined state. Since _output uses vsnprintf and it may
452 * be called several times, we have to pass a copy of ap. */
453 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100454 if (tar->raw_output)
455 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
456 else
457 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200458 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800459 }
460}
461
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200462/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200463 * \param[in] subsys Logging sub-system
464 * \param[in] file name of source code file
465 * \param[in] cont continuation (1) or new line (0)
466 * \param[in] format format string
467 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200468void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800469 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800470{
471 va_list ap;
472
473 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200474 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800475 va_end(ap);
476}
477
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200478/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200479 * \param[in] subsys Logging sub-system
480 * \param[in] level Log level
481 * \param[in] file name of source code file
482 * \param[in] cont continuation (1) or new line (0)
483 * \param[in] format format string
484 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200485void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800486{
487 va_list ap;
488
489 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200490 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800491 va_end(ap);
492}
493
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200494/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200495 * \param[in] target Log target to be registered
496 */
Harald Welte3ae27582010-03-26 21:24:24 +0800497void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800498{
Harald Welte28222962011-02-18 20:37:04 +0100499 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800500}
501
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200502/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200503 * \param[in] target Log target to be unregistered
504 */
Harald Welte3ae27582010-03-26 21:24:24 +0800505void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800506{
507 llist_del(&target->entry);
508}
509
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200510/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800511void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800512{
Harald Welte3ae27582010-03-26 21:24:24 +0800513 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800514}
515
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200516/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200517 * \param[in] ctx_nr logging context number
518 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200519 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200520 *
521 * A logging context is something like the subscriber identity to which
522 * the currently processed message relates, or the BTS through which it
523 * was received. As soon as this data is known, it can be set using
524 * this function. The main use of context information is for logging
525 * filters.
526 */
Harald Welte3ae27582010-03-26 21:24:24 +0800527int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800528{
Harald Welte3ae27582010-03-26 21:24:24 +0800529 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800530 return -EINVAL;
531
Harald Welte3ae27582010-03-26 21:24:24 +0800532 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800533
534 return 0;
535}
536
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200537/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200538 * \param[in] target Log target to be affected
539 * \param[in] all enable (1) or disable (0) the ALL filter
540 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100541 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100542 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
543 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200544 */
Harald Welte3ae27582010-03-26 21:24:24 +0800545void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800546{
547 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100548 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800549 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100550 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800551}
552
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200553/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200554 * \param[in] target Log target to be affected
555 * \param[in] use_color Use color (1) or don't use color (0)
556 */
Harald Welte3ae27582010-03-26 21:24:24 +0800557void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800558{
559 target->use_color = use_color;
560}
561
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200562/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200563 * \param[in] target Log target to be affected
564 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
565 */
Harald Welte3ae27582010-03-26 21:24:24 +0800566void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800567{
568 target->print_timestamp = print_timestamp;
569}
570
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200571/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100572 * \param[in] target Log target to be affected
573 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
574 *
575 * When both timestamp and extended timestamp is enabled then only
576 * the extended timestamp will be used. The format of the timestamp
577 * is YYYYMMDDhhmmssnnn.
578 */
579void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
580{
581 target->print_ext_timestamp = print_timestamp;
582}
583
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200584/*! Enable or disable printing of the filename while logging
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200585 * \param[in] target Log target to be affected
586 * \param[in] print_filename Enable (1) or disable (0) filenames
587 */
588void log_set_print_filename(struct log_target *target, int print_filename)
589{
590 target->print_filename = print_filename;
591}
592
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200593/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100594 * \param[in] target Log target to be affected
595 * \param[in] print_catname Enable (1) or disable (0) filenames
596 *
597 * Print the category/subsys name in front of every log message.
598 */
599void log_set_print_category(struct log_target *target, int print_category)
600{
601 target->print_category = print_category;
602}
603
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200604/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200605 * \param[in] target Log target to be affected
606 * \param[in] log_level New global log level
607 */
Harald Welte3ae27582010-03-26 21:24:24 +0800608void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800609{
610 target->loglevel = log_level;
611}
612
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200613/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100614 * \param[in] target Log target to be affected
615 * \param[in] category Log category to be affected
616 * \param[in] enable whether to enable or disable the filter
617 * \param[in] level Log level of the filter
618 */
Harald Welte3ae27582010-03-26 21:24:24 +0800619void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800620 int enable, int level)
621{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100622 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800623 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100624 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800625 target->categories[category].enabled = !!enable;
626 target->categories[category].loglevel = level;
627}
628
Harald Welte44c0f632017-01-15 17:58:29 +0100629#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100630static void _file_output(struct log_target *target, unsigned int level,
631 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800632{
Harald Welte0083cd32010-08-25 14:55:44 +0200633 fprintf(target->tgt_file.out, "%s", log);
634 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800635}
Harald Welte44c0f632017-01-15 17:58:29 +0100636#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200638/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200639 * \returns dynamically-allocated log target
640 * This funcition allocates a \ref log_target and initializes it
641 * with some default values. The newly created target is not
642 * registered yet.
643 */
Harald Welte3ae27582010-03-26 21:24:24 +0800644struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800645{
Harald Welte3ae27582010-03-26 21:24:24 +0800646 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800647 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800648
Harald Welte18a7d812017-03-16 23:54:55 +0100649 assert_loginfo();
650
Harald Welte3ae27582010-03-26 21:24:24 +0800651 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800652 if (!target)
653 return NULL;
654
Harald Welteb43bc042011-06-27 10:29:17 +0200655 target->categories = talloc_zero_array(target,
656 struct log_category,
657 osmo_log_info->num_cat);
658 if (!target->categories) {
659 talloc_free(target);
660 return NULL;
661 }
662
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800663 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800664
665 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200666 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800667 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200668 cat->enabled = osmo_log_info->cat[i].enabled;
669 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800670 }
671
672 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800673 target->use_color = 1;
674 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200675 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800676
677 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800678 target->loglevel = 0;
679 return target;
680}
681
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200682/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200683 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800684struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800685{
Harald Weltea3b844c2010-03-27 00:04:40 +0800686/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200687#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800688 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800689
Harald Welte3ae27582010-03-26 21:24:24 +0800690 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800691 if (!target)
692 return NULL;
693
Harald Welte28222962011-02-18 20:37:04 +0100694 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200695 target->tgt_file.out = stderr;
696 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800697 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800698#else
699 return NULL;
700#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800701}
702
Harald Welte44c0f632017-01-15 17:58:29 +0100703#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200704/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200705 * \param[in] fname File name of the new log file
706 * \returns Log target in case of success, NULL otherwise
707 */
Harald Welte3086c392010-08-25 19:10:50 +0200708struct log_target *log_target_create_file(const char *fname)
709{
710 struct log_target *target;
711
712 target = log_target_create();
713 if (!target)
714 return NULL;
715
Harald Welte28222962011-02-18 20:37:04 +0100716 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200717 target->tgt_file.out = fopen(fname, "a");
718 if (!target->tgt_file.out)
719 return NULL;
720
721 target->output = _file_output;
722
723 target->tgt_file.fname = talloc_strdup(target, fname);
724
725 return target;
726}
Harald Welte44c0f632017-01-15 17:58:29 +0100727#endif
Harald Welte3086c392010-08-25 19:10:50 +0200728
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200729/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200730 * \param[in] type Log target type
731 * \param[in] fname File name
732 * \returns Log target (if found), NULL otherwise
733 */
Harald Welte28222962011-02-18 20:37:04 +0100734struct log_target *log_target_find(int type, const char *fname)
735{
736 struct log_target *tgt;
737
738 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
739 if (tgt->type != type)
740 continue;
741 if (tgt->type == LOG_TGT_TYPE_FILE) {
742 if (!strcmp(fname, tgt->tgt_file.fname))
743 return tgt;
744 } else
745 return tgt;
746 }
747 return NULL;
748}
749
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200750/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200751 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200752void log_target_destroy(struct log_target *target)
753{
754
755 /* just in case, to make sure we don't have any references */
756 log_del_target(target);
757
Harald Welte44c0f632017-01-15 17:58:29 +0100758#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200759 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200760/* since C89/C99 says stderr is a macro, we can safely do this! */
761#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200762 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200763 if (target->tgt_file.out != stderr)
764#endif
765 {
Harald Welte3086c392010-08-25 19:10:50 +0200766 fclose(target->tgt_file.out);
767 target->tgt_file.out = NULL;
768 }
769 }
Harald Welte44c0f632017-01-15 17:58:29 +0100770#endif
Harald Welte3086c392010-08-25 19:10:50 +0200771
772 talloc_free(target);
773}
774
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200775/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200776 * \param[in] target log target to re-open
777 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200778int log_target_file_reopen(struct log_target *target)
779{
780 fclose(target->tgt_file.out);
781
782 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
783 if (!target->tgt_file.out)
784 return -errno;
785
786 /* we assume target->output already to be set */
787
788 return 0;
789}
790
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200791/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200792 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100793int log_targets_reopen(void)
794{
795 struct log_target *tar;
796 int rc = 0;
797
798 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
799 switch (tar->type) {
800 case LOG_TGT_TYPE_FILE:
801 if (log_target_file_reopen(tar) < 0)
802 rc = -1;
803 break;
804 default:
805 break;
806 }
807 }
808
809 return rc;
810}
811
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200812/*! Generates the logging command string for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200813 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200814 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200815 */
Maxc65c5b42017-03-15 13:20:23 +0100816const char *log_vty_command_string()
Harald Welte7638af92010-05-11 16:39:22 +0200817{
Harald Weltece9fec32011-06-27 14:19:16 +0200818 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100819 int len = 0, offset = 0, ret, i, rem;
820 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200821 char *str;
822
Harald Welte18a7d812017-03-16 23:54:55 +0100823 assert_loginfo();
824
Harald Welteb43bc042011-06-27 10:29:17 +0200825 for (i = 0; i < info->num_cat; i++) {
826 if (info->cat[i].name == NULL)
827 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100828 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200829 }
Harald Welte7638af92010-05-11 16:39:22 +0200830
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100831 for (i = 0; i < LOGLEVEL_DEFS; i++)
832 size += strlen(loglevel_strs[i].str) + 1;
833
834 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200835 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200836 if (!str)
837 return NULL;
838
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200839 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100840 if (ret < 0)
841 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200842 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200843
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100844 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200845 if (info->cat[i].name) {
846 int j, name_len = strlen(info->cat[i].name)+1;
847 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100848
Harald Welteb43bc042011-06-27 10:29:17 +0200849 for (j = 0; j < name_len; j++)
850 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100851
Harald Welteb43bc042011-06-27 10:29:17 +0200852 name[name_len-1] = '\0';
853 ret = snprintf(str + offset, rem, "%s|", name+1);
854 if (ret < 0)
855 goto err;
856 OSMO_SNPRINTF_RET(ret, rem, offset, len);
857 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100858 }
859 offset--; /* to remove the trailing | */
860 rem++;
861
862 ret = snprintf(str + offset, rem, ") (");
863 if (ret < 0)
864 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200865 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100866
867 for (i = 0; i < LOGLEVEL_DEFS; i++) {
868 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
869 char loglevel_str[loglevel_str_len];
870
871 for (j = 0; j < loglevel_str_len; j++)
872 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
873
874 loglevel_str[loglevel_str_len-1] = '\0';
875 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
876 if (ret < 0)
877 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200878 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100879 }
880 offset--; /* to remove the trailing | */
881 rem++;
882
883 ret = snprintf(str + offset, rem, ")");
884 if (ret < 0)
885 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200886 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100887err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200888 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100889 return str;
890}
891
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200892/*! Generates the logging command description for VTY
Harald Welte18fc4652011-08-17 14:14:17 +0200893 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200894 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200895 */
Maxc65c5b42017-03-15 13:20:23 +0100896const char *log_vty_command_description()
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100897{
Harald Weltece9fec32011-06-27 14:19:16 +0200898 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100899 char *str;
900 int i, ret, len = 0, offset = 0, rem;
901 unsigned int size =
902 strlen(LOGGING_STR
903 "Set the log level for a specified category\n") + 1;
904
Harald Welte18a7d812017-03-16 23:54:55 +0100905 assert_loginfo();
906
Harald Welteb43bc042011-06-27 10:29:17 +0200907 for (i = 0; i < info->num_cat; i++) {
908 if (info->cat[i].name == NULL)
909 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100910 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200911 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100912
913 for (i = 0; i < LOGLEVEL_DEFS; i++)
914 size += strlen(loglevel_descriptions[i]) + 1;
915
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200916 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100917 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200918 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100919 if (!str)
920 return NULL;
921
922 ret = snprintf(str + offset, rem, LOGGING_STR
923 "Set the log level for a specified category\n");
924 if (ret < 0)
925 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200926 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100927
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200928 ret = snprintf(str + offset, rem,
929 "Global setting for all subsystems\n");
930 if (ret < 0)
931 goto err;
932 OSMO_SNPRINTF_RET(ret, rem, offset, len);
933
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100934 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200935 if (info->cat[i].name == NULL)
936 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100937 ret = snprintf(str + offset, rem, "%s\n",
938 info->cat[i].description);
939 if (ret < 0)
940 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200941 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100942 }
943 for (i = 0; i < LOGLEVEL_DEFS; i++) {
944 ret = snprintf(str + offset, rem, "%s\n",
945 loglevel_descriptions[i]);
946 if (ret < 0)
947 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200948 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100949 }
950err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200951 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200952 return str;
953}
954
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200955/*! Initialize the Osmocom logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200956 * \param[in] inf Information regarding logging categories
957 * \param[in] ctx \ref talloc context for logging allocations
958 * \returns 0 in case of success, negative in case of error
959 */
Harald Welteb43bc042011-06-27 10:29:17 +0200960int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800961{
Harald Welteb43bc042011-06-27 10:29:17 +0200962 int i;
963
964 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
965 if (!tall_log_ctx)
966 return -ENOMEM;
967
968 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
969 if (!osmo_log_info)
970 return -ENOMEM;
971
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100972 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200973 osmo_log_info->num_cat_user = inf->num_cat;
974 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200975 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200976
977 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
978 struct log_info_cat,
979 osmo_log_info->num_cat);
980 if (!osmo_log_info->cat) {
981 talloc_free(osmo_log_info);
982 osmo_log_info = NULL;
983 return -ENOMEM;
984 }
985
986 /* copy over the user part */
987 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200988 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
989 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200990 sizeof(struct log_info_cat));
991 }
992
993 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200994 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200995 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200996 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200997 &internal_cat[i], sizeof(struct log_info_cat));
998 }
999
1000 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001001}
Harald Welte18fc4652011-08-17 14:14:17 +02001002
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001003/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001004 * This function destroys all targets and releases associated memory */
1005void log_fini(void)
1006{
1007 struct log_target *tar, *tar2;
1008
1009 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1010 log_target_destroy(tar);
1011
1012 talloc_free(osmo_log_info);
1013 osmo_log_info = NULL;
1014 talloc_free(tall_log_ctx);
1015 tall_log_ctx = NULL;
1016}
1017
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001018/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001019 * \returns != 0 if a log entry might get generated by at least one target */
1020int log_check_level(int subsys, unsigned int level)
1021{
1022 struct log_target *tar;
1023
Harald Welte18a7d812017-03-16 23:54:55 +01001024 assert_loginfo();
1025
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001026 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001027
1028 /* TODO: The following could/should be cached (update on config) */
1029
1030 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001031 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001032 continue;
1033
1034 /* This might get logged (ignoring filters) */
1035 return 1;
1036 }
1037
1038 /* We are sure, that this will not be logged. */
1039 return 0;
1040}
1041
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001042/*! @} */