blob: 71025432894cb692efc8e3d67d18e9f235fea557 [file] [log] [blame]
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001/* Debugging/Logging support code */
2
3/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte18fc4652011-08-17 14:14:17 +020023/* \addtogroup logging
24 * @{
25 */
26
27/* \file logging.c */
28
Harald Welte01fd5cb2010-03-26 23:51:31 +080029#include "../config.h"
30
Harald Welte4a2bb9e2010-03-26 09:33:40 +080031#include <stdarg.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <string.h>
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010035#include <ctype.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080036
37#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080038#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080039#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080040#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010041#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080042#include <errno.h>
43
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010044#include <osmocom/core/talloc.h>
45#include <osmocom/core/utils.h>
46#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020047#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080048
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010049#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
50
Harald Welteb43bc042011-06-27 10:29:17 +020051struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080052
Harald Welte3ae27582010-03-26 21:24:24 +080053static struct log_context log_context;
54static void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010055LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080056
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010057#define LOGLEVEL_DEFS 6 /* Number of loglevels.*/
58
59static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = {
Harald Welte9b186702013-03-19 09:55:28 +010060 { 0, "EVERYTHING" },
Harald Welte4a2bb9e2010-03-26 09:33:40 +080061 { LOGL_DEBUG, "DEBUG" },
62 { LOGL_INFO, "INFO" },
63 { LOGL_NOTICE, "NOTICE" },
64 { LOGL_ERROR, "ERROR" },
65 { LOGL_FATAL, "FATAL" },
66 { 0, NULL },
67};
68
Harald Welteb43bc042011-06-27 10:29:17 +020069#define INT2IDX(x) (-1*(x)-1)
70static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
71 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
72 .name = "DLGLOBAL",
73 .description = "Library-internal global log family",
74 .loglevel = LOGL_NOTICE,
75 .enabled = 1,
76 },
root8a996b42011-09-26 11:22:21 +020077 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
78 .name = "DLLAPD",
79 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020080 .loglevel = LOGL_NOTICE,
81 .enabled = 1,
82 },
Harald Welte892e6212011-07-19 14:31:44 +020083 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020084 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020085 .description = "A-bis Intput Subsystem",
86 .loglevel = LOGL_NOTICE,
87 .enabled = 1,
88 },
Harald Welte892e6212011-07-19 14:31:44 +020089 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +020090 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020091 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
92 .loglevel = LOGL_NOTICE,
93 .enabled = 1,
94 },
Harald Welte892e6212011-07-19 14:31:44 +020095 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +020096 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020097 .description = "A-bis Input Driver for Signalling",
98 .enabled = 0, .loglevel = LOGL_NOTICE,
99 },
Harald Welte892e6212011-07-19 14:31:44 +0200100 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200101 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200102 .description = "A-bis Input Driver for B-Channels (voice)",
103 .enabled = 0, .loglevel = LOGL_NOTICE,
104 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200105 [INT2IDX(DLSMS)] = {
106 .name = "DLSMS",
107 .description = "Layer3 Short Message Service (SMS)",
108 .enabled = 1, .loglevel = LOGL_NOTICE,
109 .color = "\033[1;38m",
110 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200111 [INT2IDX(DLCTRL)] = {
112 .name = "DLCTRL",
113 .description = "Control Interface",
114 .enabled = 1, .loglevel = LOGL_NOTICE,
115 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100116 [INT2IDX(DLGTP)] = {
117 .name = "DLGTP",
118 .description = "GPRS GTP library",
119 .enabled = 1, .loglevel = LOGL_NOTICE,
120 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100121 [INT2IDX(DLSTATS)] = {
122 .name = "DLSTATS",
123 .description = "Statistics messages and logging",
124 .enabled = 1, .loglevel = LOGL_NOTICE,
125 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100126 [INT2IDX(DLGSUP)] = {
127 .name = "DLGSUP",
128 .description = "Generic Subscriber Update Protocol",
129 .enabled = 1, .loglevel = LOGL_NOTICE,
130 },
Harald Weltec0f00072016-04-27 18:32:35 +0200131 [INT2IDX(DLOAP)] = {
132 .name = "DLOAP",
133 .description = "Osmocom Authentication Protocol",
134 .enabled = 1, .loglevel = LOGL_NOTICE,
135 },
Harald Welteb43bc042011-06-27 10:29:17 +0200136};
137
Harald Weltede6e4982012-12-06 21:25:27 +0100138/*! \brief descriptive string for each log level */
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100139/* You have to keep this in sync with the structure loglevel_strs. */
140const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
Holger Hans Peter Freyther6ec6bd92014-12-28 18:27:38 +0100141 "Don't use. It doesn't log anything",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100142 "Log debug messages and higher levels",
143 "Log informational messages and higher levels",
Ruben Undheim029f5a12015-09-16 19:02:35 +0200144 "Log noticeable messages and higher levels",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100145 "Log error messages and higher levels",
146 "Log only fatal messages",
147 NULL,
148};
149
Harald Welteb43bc042011-06-27 10:29:17 +0200150/* special magic for negative (library-internal) log subsystem numbers */
151static int subsys_lib2index(int subsys)
152{
153 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
154}
155
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200156/*! \brief Parse a human-readable log level into a numeric value
157 * \param lvl[in] zero-terminated string containing log level name
158 * \returns numeric log level
159 */
Harald Welte3ae27582010-03-26 21:24:24 +0800160int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800161{
162 return get_string_value(loglevel_strs, lvl);
163}
164
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200165/*! \brief convert a numeric log level into human-readable string
166 * \param lvl[in] numeric log level
167 * \returns zero-terminated string (log level name)
168 */
Harald Welte9ac22252010-05-11 11:19:40 +0200169const char *log_level_str(unsigned int lvl)
170{
171 return get_value_string(loglevel_strs, lvl);
172}
173
Harald Welte18fc4652011-08-17 14:14:17 +0200174/*! \brief parse a human-readable log category into numeric form
175 * \param[in] category human-readable log category name
176 * \returns numeric category value, or -EINVAL otherwise
177 */
Harald Welte3ae27582010-03-26 21:24:24 +0800178int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800179{
180 int i;
181
Harald Welte4ebdf742010-05-19 19:54:00 +0200182 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200183 if (osmo_log_info->cat[i].name == NULL)
184 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200185 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800186 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800187 }
188
189 return -EINVAL;
190}
191
Harald Welte18fc4652011-08-17 14:14:17 +0200192/*! \brief parse the log category mask
193 * \param[in] target log target to be configured
194 * \param[in] _mask log category mask string
195 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800196 * The format can be this: category1:category2:category3
197 * or category1,2:category2,3:...
198 */
Harald Welte3ae27582010-03-26 21:24:24 +0800199void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800200{
201 int i = 0;
202 char *mask = strdup(_mask);
203 char *category_token = NULL;
204
205 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200206 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800207 target->categories[i].enabled = 0;
208
209 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200210 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800211 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200212 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200213 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800214 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200215
216 if (!osmo_log_info->cat[i].name)
217 continue;
218
219 length = strlen(category_token);
220 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200221
222 /* Use longest length not to match subocurrences. */
223 if (cat_length > length)
224 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800225
226 if (colon)
227 length = colon - category_token;
228
Harald Welte4ebdf742010-05-19 19:54:00 +0200229 if (strncasecmp(osmo_log_info->cat[i].name,
230 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800231 int level = 0;
232
233 if (colon)
234 level = atoi(colon+1);
235
Harald Weltefaadfe22010-03-26 21:05:43 +0800236 target->categories[i].enabled = 1;
237 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800238 }
239 }
240 } while ((category_token = strtok(NULL, ":")));
241
242 free(mask);
243}
244
245static const char* color(int subsys)
246{
Harald Welte4ebdf742010-05-19 19:54:00 +0200247 if (subsys < osmo_log_info->num_cat)
248 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800249
Harald Welted788f662010-03-26 09:45:03 +0800250 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800251}
252
Harald Welteaa00f992016-12-02 15:30:02 +0100253const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100254{
255 if (subsys < osmo_log_info->num_cat)
256 return osmo_log_info->cat[subsys].name;
257
258 return NULL;
259}
260
Harald Welte3ae27582010-03-26 21:24:24 +0800261static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200262 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100263 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800264{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800265 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200266 int ret, len = 0, offset = 0, rem = sizeof(buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800267
268 /* are we using color */
269 if (target->use_color) {
Harald Welted788f662010-03-26 09:45:03 +0800270 const char *c = color(subsys);
271 if (c) {
Holger Hans Peter Freyther5f91a402014-12-05 10:29:45 +0100272 ret = snprintf(buf + offset, rem, "%s", c);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200273 if (ret < 0)
274 goto err;
275 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800276 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800277 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800278 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100279 if (target->print_ext_timestamp) {
280 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100281 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200282 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100283 localtime_r(&tv.tv_sec, &tm);
284 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100285 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100286 tm.tm_hour, tm.tm_min, tm.tm_sec,
287 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100288 if (ret < 0)
289 goto err;
290 OSMO_SNPRINTF_RET(ret, rem, offset, len);
291 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800292 char *timestr;
293 time_t tm;
294 tm = time(NULL);
295 timestr = ctime(&tm);
296 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200297 ret = snprintf(buf + offset, rem, "%s ", timestr);
298 if (ret < 0)
299 goto err;
300 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800301 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100302 if (target->print_category) {
Harald Welteaa00f992016-12-02 15:30:02 +0100303 ret = snprintf(buf + offset, rem, "%s ", log_category_name(subsys));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100304 if (ret < 0)
305 goto err;
306 OSMO_SNPRINTF_RET(ret, rem, offset, len);
307 }
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200308 if (target->print_filename) {
309 ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
310 subsys, file, line);
311 if (ret < 0)
312 goto err;
313 OSMO_SNPRINTF_RET(ret, rem, offset, len);
314 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800315 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200316 ret = vsnprintf(buf + offset, rem, format, ap);
317 if (ret < 0)
318 goto err;
319 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800320
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200321 ret = snprintf(buf + offset, rem, "%s",
322 target->use_color ? "\033[0;m" : "");
323 if (ret < 0)
324 goto err;
325 OSMO_SNPRINTF_RET(ret, rem, offset, len);
326err:
327 buf[sizeof(buf)-1] = '\0';
328 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800329}
330
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100331/* Catch internal logging category indexes as well as out-of-bounds indexes.
332 * For internal categories, the ID is negative starting with -1; and internal
333 * logging categories are added behind the user categories. For out-of-bounds
334 * indexes, return the index of DLGLOBAL. The returned category index is
335 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
336 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100337static inline int map_subsys(int subsys)
338{
339 if (subsys < 0)
340 subsys = subsys_lib2index(subsys);
341
342 if (subsys > osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100343 subsys = subsys_lib2index(DLGLOBAL);
344
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100345 return subsys;
346}
347
348static inline int check_log_to_target(struct log_target *tar, int subsys, int level)
349{
350 struct log_category *category;
351
352 category = &tar->categories[subsys];
353
354 /* subsystem is not supposed to be logged */
355 if (!category->enabled)
356 return 0;
357
358 /* Check the global log level */
359 if (tar->loglevel != 0 && level < tar->loglevel)
360 return 0;
361
362 /* Check the category log level */
363 if (tar->loglevel == 0 && category->loglevel != 0 &&
364 level < category->loglevel)
365 return 0;
366
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100367 /* Apply filters here... if that becomes messy we will
368 * need to put filters in a list and each filter will
369 * say stop, continue, output */
370 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
371 return 1;
372
373 if (osmo_log_info->filter_fn)
374 return osmo_log_info->filter_fn(&log_context, tar);
375
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100376 /* TODO: Check the filter/selector too? */
377 return 1;
378}
379
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200380/*! \brief vararg version of logging function
381 * \param[in] subsys Logging sub-system
382 * \param[in] level Log level
383 * \param[in] file name of source code file
384 * \param[in] cont continuation (1) or new line (0)
385 * \param[in] format format string
386 * \param[in] ap vararg-list containing format string arguments
387 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200388void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200389 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800390{
Harald Welte3ae27582010-03-26 21:24:24 +0800391 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800392
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100393 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200394
Harald Welte28222962011-02-18 20:37:04 +0100395 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200396 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800397
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100398 if (!check_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800399 continue;
400
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200401 /* According to the manpage, vsnprintf leaves the value of ap
402 * in undefined state. Since _output uses vsnprintf and it may
403 * be called several times, we have to pass a copy of ap. */
404 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100405 if (tar->raw_output)
406 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
407 else
408 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200409 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800410 }
411}
412
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200413/*! \brief logging function used by DEBUGP() macro
414 * \param[in] subsys Logging sub-system
415 * \param[in] file name of source code file
416 * \param[in] cont continuation (1) or new line (0)
417 * \param[in] format format string
418 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200419void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800420 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800421{
422 va_list ap;
423
424 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200425 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800426 va_end(ap);
427}
428
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200429/*! \brief logging function used by LOGP() macro
430 * \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 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200436void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800437{
438 va_list ap;
439
440 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200441 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800442 va_end(ap);
443}
444
Harald Welte18fc4652011-08-17 14:14:17 +0200445/*! \brief Register a new log target with the logging core
446 * \param[in] target Log target to be registered
447 */
Harald Welte3ae27582010-03-26 21:24:24 +0800448void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800449{
Harald Welte28222962011-02-18 20:37:04 +0100450 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800451}
452
Harald Welte18fc4652011-08-17 14:14:17 +0200453/*! \brief Unregister a log target from the logging core
454 * \param[in] target Log target to be unregistered
455 */
Harald Welte3ae27582010-03-26 21:24:24 +0800456void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800457{
458 llist_del(&target->entry);
459}
460
Harald Welte18fc4652011-08-17 14:14:17 +0200461/*! \brief Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800462void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800463{
Harald Welte3ae27582010-03-26 21:24:24 +0800464 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800465}
466
Harald Welte18fc4652011-08-17 14:14:17 +0200467/*! \brief Set the logging context
468 * \param[in] ctx_nr logging context number
469 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200470 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200471 *
472 * A logging context is something like the subscriber identity to which
473 * the currently processed message relates, or the BTS through which it
474 * was received. As soon as this data is known, it can be set using
475 * this function. The main use of context information is for logging
476 * filters.
477 */
Harald Welte3ae27582010-03-26 21:24:24 +0800478int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800479{
Harald Welte3ae27582010-03-26 21:24:24 +0800480 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800481 return -EINVAL;
482
Harald Welte3ae27582010-03-26 21:24:24 +0800483 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800484
485 return 0;
486}
487
Harald Welte18fc4652011-08-17 14:14:17 +0200488/*! \brief Enable the \ref LOG_FILTER_ALL log filter
489 * \param[in] target Log target to be affected
490 * \param[in] all enable (1) or disable (0) the ALL filter
491 *
492 * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
493 * be printed. It acts as a wildcard. Setting it to \a 1 means there
494 * is no filtering.
495 */
Harald Welte3ae27582010-03-26 21:24:24 +0800496void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800497{
498 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800499 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800500 else
Harald Welte3ae27582010-03-26 21:24:24 +0800501 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800502}
503
Harald Welte18fc4652011-08-17 14:14:17 +0200504/*! \brief Enable or disable the use of colored output
505 * \param[in] target Log target to be affected
506 * \param[in] use_color Use color (1) or don't use color (0)
507 */
Harald Welte3ae27582010-03-26 21:24:24 +0800508void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800509{
510 target->use_color = use_color;
511}
512
Harald Welte18fc4652011-08-17 14:14:17 +0200513/*! \brief Enable or disable printing of timestamps while logging
514 * \param[in] target Log target to be affected
515 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
516 */
Harald Welte3ae27582010-03-26 21:24:24 +0800517void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800518{
519 target->print_timestamp = print_timestamp;
520}
521
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100522/*! \brief Enable or disable printing of extended timestamps while logging
523 * \param[in] target Log target to be affected
524 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
525 *
526 * When both timestamp and extended timestamp is enabled then only
527 * the extended timestamp will be used. The format of the timestamp
528 * is YYYYMMDDhhmmssnnn.
529 */
530void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
531{
532 target->print_ext_timestamp = print_timestamp;
533}
534
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200535/*! \brief Enable or disable printing of the filename while logging
536 * \param[in] target Log target to be affected
537 * \param[in] print_filename Enable (1) or disable (0) filenames
538 */
539void log_set_print_filename(struct log_target *target, int print_filename)
540{
541 target->print_filename = print_filename;
542}
543
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100544/*! \brief Enable or disable printing of the category name
545 * \param[in] target Log target to be affected
546 * \param[in] print_catname Enable (1) or disable (0) filenames
547 *
548 * Print the category/subsys name in front of every log message.
549 */
550void log_set_print_category(struct log_target *target, int print_category)
551{
552 target->print_category = print_category;
553}
554
Harald Welte18fc4652011-08-17 14:14:17 +0200555/*! \brief Set the global log level for a given log target
556 * \param[in] target Log target to be affected
557 * \param[in] log_level New global log level
558 */
Harald Welte3ae27582010-03-26 21:24:24 +0800559void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800560{
561 target->loglevel = log_level;
562}
563
Harald Weltede6e4982012-12-06 21:25:27 +0100564/*! \brief Set a category filter on a given log target
565 * \param[in] target Log target to be affected
566 * \param[in] category Log category to be affected
567 * \param[in] enable whether to enable or disable the filter
568 * \param[in] level Log level of the filter
569 */
Harald Welte3ae27582010-03-26 21:24:24 +0800570void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800571 int enable, int level)
572{
Harald Welte4ebdf742010-05-19 19:54:00 +0200573 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800574 return;
575 target->categories[category].enabled = !!enable;
576 target->categories[category].loglevel = level;
577}
578
Harald Welte76e72ab2011-02-17 15:52:39 +0100579static void _file_output(struct log_target *target, unsigned int level,
580 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800581{
Harald Welte0083cd32010-08-25 14:55:44 +0200582 fprintf(target->tgt_file.out, "%s", log);
583 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800584}
585
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200586/*! \brief Create a new log target skeleton
587 * \returns dynamically-allocated log target
588 * This funcition allocates a \ref log_target and initializes it
589 * with some default values. The newly created target is not
590 * registered yet.
591 */
Harald Welte3ae27582010-03-26 21:24:24 +0800592struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800593{
Harald Welte3ae27582010-03-26 21:24:24 +0800594 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800595 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800596
Harald Welte3ae27582010-03-26 21:24:24 +0800597 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800598 if (!target)
599 return NULL;
600
Harald Welteb43bc042011-06-27 10:29:17 +0200601 target->categories = talloc_zero_array(target,
602 struct log_category,
603 osmo_log_info->num_cat);
604 if (!target->categories) {
605 talloc_free(target);
606 return NULL;
607 }
608
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800609 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800610
611 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200612 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800613 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200614 cat->enabled = osmo_log_info->cat[i].enabled;
615 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800616 }
617
618 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800619 target->use_color = 1;
620 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200621 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800622
623 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800624 target->loglevel = 0;
625 return target;
626}
627
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200628/*! \brief Create the STDERR log target
629 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800630struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800631{
Harald Weltea3b844c2010-03-27 00:04:40 +0800632/* since C89/C99 says stderr is a macro, we can safely do this! */
633#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800634 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800635
Harald Welte3ae27582010-03-26 21:24:24 +0800636 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637 if (!target)
638 return NULL;
639
Harald Welte28222962011-02-18 20:37:04 +0100640 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200641 target->tgt_file.out = stderr;
642 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800643 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800644#else
645 return NULL;
646#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800647}
648
Harald Welte18fc4652011-08-17 14:14:17 +0200649/*! \brief Create a new file-based log target
650 * \param[in] fname File name of the new log file
651 * \returns Log target in case of success, NULL otherwise
652 */
Harald Welte3086c392010-08-25 19:10:50 +0200653struct log_target *log_target_create_file(const char *fname)
654{
655 struct log_target *target;
656
657 target = log_target_create();
658 if (!target)
659 return NULL;
660
Harald Welte28222962011-02-18 20:37:04 +0100661 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200662 target->tgt_file.out = fopen(fname, "a");
663 if (!target->tgt_file.out)
664 return NULL;
665
666 target->output = _file_output;
667
668 target->tgt_file.fname = talloc_strdup(target, fname);
669
670 return target;
671}
672
Harald Welte18fc4652011-08-17 14:14:17 +0200673/*! \brief Find a registered log target
674 * \param[in] type Log target type
675 * \param[in] fname File name
676 * \returns Log target (if found), NULL otherwise
677 */
Harald Welte28222962011-02-18 20:37:04 +0100678struct log_target *log_target_find(int type, const char *fname)
679{
680 struct log_target *tgt;
681
682 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
683 if (tgt->type != type)
684 continue;
685 if (tgt->type == LOG_TGT_TYPE_FILE) {
686 if (!strcmp(fname, tgt->tgt_file.fname))
687 return tgt;
688 } else
689 return tgt;
690 }
691 return NULL;
692}
693
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200694/*! \brief Unregister, close and delete a log target
695 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200696void log_target_destroy(struct log_target *target)
697{
698
699 /* just in case, to make sure we don't have any references */
700 log_del_target(target);
701
702 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200703/* since C89/C99 says stderr is a macro, we can safely do this! */
704#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200705 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200706 if (target->tgt_file.out != stderr)
707#endif
708 {
Harald Welte3086c392010-08-25 19:10:50 +0200709 fclose(target->tgt_file.out);
710 target->tgt_file.out = NULL;
711 }
712 }
713
714 talloc_free(target);
715}
716
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200717/*! \brief close and re-open a log file (for log file rotation)
718 * \param[in] target log target to re-open
719 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200720int log_target_file_reopen(struct log_target *target)
721{
722 fclose(target->tgt_file.out);
723
724 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
725 if (!target->tgt_file.out)
726 return -errno;
727
728 /* we assume target->output already to be set */
729
730 return 0;
731}
732
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200733/*! \brief close and re-open all log files (for log file rotation)
734 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100735int log_targets_reopen(void)
736{
737 struct log_target *tar;
738 int rc = 0;
739
740 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
741 switch (tar->type) {
742 case LOG_TGT_TYPE_FILE:
743 if (log_target_file_reopen(tar) < 0)
744 rc = -1;
745 break;
746 default:
747 break;
748 }
749 }
750
751 return rc;
752}
753
Harald Welte18fc4652011-08-17 14:14:17 +0200754/*! \brief Generates the logging command string for VTY
755 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200756 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200757 */
Harald Weltece9fec32011-06-27 14:19:16 +0200758const char *log_vty_command_string(const struct log_info *unused_info)
Harald Welte7638af92010-05-11 16:39:22 +0200759{
Harald Weltece9fec32011-06-27 14:19:16 +0200760 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100761 int len = 0, offset = 0, ret, i, rem;
762 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200763 char *str;
764
Harald Welteb43bc042011-06-27 10:29:17 +0200765 for (i = 0; i < info->num_cat; i++) {
766 if (info->cat[i].name == NULL)
767 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100768 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200769 }
Harald Welte7638af92010-05-11 16:39:22 +0200770
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100771 for (i = 0; i < LOGLEVEL_DEFS; i++)
772 size += strlen(loglevel_strs[i].str) + 1;
773
774 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200775 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200776 if (!str)
777 return NULL;
778
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200779 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100780 if (ret < 0)
781 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200782 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200783
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100784 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200785 if (info->cat[i].name) {
786 int j, name_len = strlen(info->cat[i].name)+1;
787 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100788
Harald Welteb43bc042011-06-27 10:29:17 +0200789 for (j = 0; j < name_len; j++)
790 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100791
Harald Welteb43bc042011-06-27 10:29:17 +0200792 name[name_len-1] = '\0';
793 ret = snprintf(str + offset, rem, "%s|", name+1);
794 if (ret < 0)
795 goto err;
796 OSMO_SNPRINTF_RET(ret, rem, offset, len);
797 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100798 }
799 offset--; /* to remove the trailing | */
800 rem++;
801
802 ret = snprintf(str + offset, rem, ") (");
803 if (ret < 0)
804 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200805 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100806
807 for (i = 0; i < LOGLEVEL_DEFS; i++) {
808 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
809 char loglevel_str[loglevel_str_len];
810
811 for (j = 0; j < loglevel_str_len; j++)
812 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
813
814 loglevel_str[loglevel_str_len-1] = '\0';
815 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
816 if (ret < 0)
817 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200818 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100819 }
820 offset--; /* to remove the trailing | */
821 rem++;
822
823 ret = snprintf(str + offset, rem, ")");
824 if (ret < 0)
825 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200826 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100827err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200828 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100829 return str;
830}
831
Harald Welte18fc4652011-08-17 14:14:17 +0200832/*! \brief Generates the logging command description for VTY
833 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200834 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200835 */
Harald Weltece9fec32011-06-27 14:19:16 +0200836const char *log_vty_command_description(const struct log_info *unused_info)
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100837{
Harald Weltece9fec32011-06-27 14:19:16 +0200838 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100839 char *str;
840 int i, ret, len = 0, offset = 0, rem;
841 unsigned int size =
842 strlen(LOGGING_STR
843 "Set the log level for a specified category\n") + 1;
844
Harald Welteb43bc042011-06-27 10:29:17 +0200845 for (i = 0; i < info->num_cat; i++) {
846 if (info->cat[i].name == NULL)
847 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100848 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200849 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100850
851 for (i = 0; i < LOGLEVEL_DEFS; i++)
852 size += strlen(loglevel_descriptions[i]) + 1;
853
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200854 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100855 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200856 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100857 if (!str)
858 return NULL;
859
860 ret = snprintf(str + offset, rem, LOGGING_STR
861 "Set the log level for a specified category\n");
862 if (ret < 0)
863 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200864 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100865
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200866 ret = snprintf(str + offset, rem,
867 "Global setting for all subsystems\n");
868 if (ret < 0)
869 goto err;
870 OSMO_SNPRINTF_RET(ret, rem, offset, len);
871
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100872 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200873 if (info->cat[i].name == NULL)
874 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100875 ret = snprintf(str + offset, rem, "%s\n",
876 info->cat[i].description);
877 if (ret < 0)
878 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200879 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100880 }
881 for (i = 0; i < LOGLEVEL_DEFS; i++) {
882 ret = snprintf(str + offset, rem, "%s\n",
883 loglevel_descriptions[i]);
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 +0100887 }
888err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200889 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200890 return str;
891}
892
Harald Welte18fc4652011-08-17 14:14:17 +0200893/*! \brief Initialize the Osmocom logging core
894 * \param[in] inf Information regarding logging categories
895 * \param[in] ctx \ref talloc context for logging allocations
896 * \returns 0 in case of success, negative in case of error
897 */
Harald Welteb43bc042011-06-27 10:29:17 +0200898int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800899{
Harald Welteb43bc042011-06-27 10:29:17 +0200900 int i;
901
902 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
903 if (!tall_log_ctx)
904 return -ENOMEM;
905
906 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
907 if (!osmo_log_info)
908 return -ENOMEM;
909
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100910 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200911 osmo_log_info->num_cat_user = inf->num_cat;
912 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200913 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200914
915 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
916 struct log_info_cat,
917 osmo_log_info->num_cat);
918 if (!osmo_log_info->cat) {
919 talloc_free(osmo_log_info);
920 osmo_log_info = NULL;
921 return -ENOMEM;
922 }
923
924 /* copy over the user part */
925 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200926 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
927 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200928 sizeof(struct log_info_cat));
929 }
930
931 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200932 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200933 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200934 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200935 &internal_cat[i], sizeof(struct log_info_cat));
936 }
937
938 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800939}
Harald Welte18fc4652011-08-17 14:14:17 +0200940
Harald Welte69e6c3c2016-04-20 10:41:27 +0200941/* \brief De-initialize the Osmocom logging core
942 * This function destroys all targets and releases associated memory */
943void log_fini(void)
944{
945 struct log_target *tar, *tar2;
946
947 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
948 log_target_destroy(tar);
949
950 talloc_free(osmo_log_info);
951 osmo_log_info = NULL;
952 talloc_free(tall_log_ctx);
953 tall_log_ctx = NULL;
954}
955
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100956/*! \brief Check whether a log entry will be generated.
957 * \returns != 0 if a log entry might get generated by at least one target */
958int log_check_level(int subsys, unsigned int level)
959{
960 struct log_target *tar;
961
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100962 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100963
964 /* TODO: The following could/should be cached (update on config) */
965
966 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100967 if (!check_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100968 continue;
969
970 /* This might get logged (ignoring filters) */
971 return 1;
972 }
973
974 /* We are sure, that this will not be logged. */
975 return 0;
976}
977
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200978/*! @} */