blob: 2a8bfdc8dfd0f01c07038cab9a0d9c546b66931d [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
Neels Hofmeyrca135742016-12-12 14:18:54 +0100342 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100343 subsys = subsys_lib2index(DLGLOBAL);
344
Neels Hofmeyrca135742016-12-12 14:18:54 +0100345 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
346
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100347 return subsys;
348}
349
350static inline int check_log_to_target(struct log_target *tar, int subsys, int level)
351{
352 struct log_category *category;
353
354 category = &tar->categories[subsys];
355
356 /* subsystem is not supposed to be logged */
357 if (!category->enabled)
358 return 0;
359
360 /* Check the global log level */
361 if (tar->loglevel != 0 && level < tar->loglevel)
362 return 0;
363
364 /* Check the category log level */
365 if (tar->loglevel == 0 && category->loglevel != 0 &&
366 level < category->loglevel)
367 return 0;
368
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100369 /* Apply filters here... if that becomes messy we will
370 * need to put filters in a list and each filter will
371 * say stop, continue, output */
372 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
373 return 1;
374
375 if (osmo_log_info->filter_fn)
376 return osmo_log_info->filter_fn(&log_context, tar);
377
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100378 /* TODO: Check the filter/selector too? */
379 return 1;
380}
381
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200382/*! \brief vararg version of logging function
383 * \param[in] subsys Logging sub-system
384 * \param[in] level Log level
385 * \param[in] file name of source code file
386 * \param[in] cont continuation (1) or new line (0)
387 * \param[in] format format string
388 * \param[in] ap vararg-list containing format string arguments
389 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200390void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200391 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800392{
Harald Welte3ae27582010-03-26 21:24:24 +0800393 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800394
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100395 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200396
Harald Welte28222962011-02-18 20:37:04 +0100397 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200398 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800399
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100400 if (!check_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800401 continue;
402
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200403 /* According to the manpage, vsnprintf leaves the value of ap
404 * in undefined state. Since _output uses vsnprintf and it may
405 * be called several times, we have to pass a copy of ap. */
406 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100407 if (tar->raw_output)
408 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
409 else
410 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200411 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800412 }
413}
414
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200415/*! \brief logging function used by DEBUGP() macro
416 * \param[in] subsys Logging sub-system
417 * \param[in] file name of source code file
418 * \param[in] cont continuation (1) or new line (0)
419 * \param[in] format format string
420 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200421void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800422 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800423{
424 va_list ap;
425
426 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200427 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800428 va_end(ap);
429}
430
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200431/*! \brief logging function used by LOGP() macro
432 * \param[in] subsys Logging sub-system
433 * \param[in] level Log level
434 * \param[in] file name of source code file
435 * \param[in] cont continuation (1) or new line (0)
436 * \param[in] format format string
437 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200438void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800439{
440 va_list ap;
441
442 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200443 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800444 va_end(ap);
445}
446
Harald Welte18fc4652011-08-17 14:14:17 +0200447/*! \brief Register a new log target with the logging core
448 * \param[in] target Log target to be registered
449 */
Harald Welte3ae27582010-03-26 21:24:24 +0800450void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800451{
Harald Welte28222962011-02-18 20:37:04 +0100452 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800453}
454
Harald Welte18fc4652011-08-17 14:14:17 +0200455/*! \brief Unregister a log target from the logging core
456 * \param[in] target Log target to be unregistered
457 */
Harald Welte3ae27582010-03-26 21:24:24 +0800458void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800459{
460 llist_del(&target->entry);
461}
462
Harald Welte18fc4652011-08-17 14:14:17 +0200463/*! \brief Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800464void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800465{
Harald Welte3ae27582010-03-26 21:24:24 +0800466 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800467}
468
Harald Welte18fc4652011-08-17 14:14:17 +0200469/*! \brief Set the logging context
470 * \param[in] ctx_nr logging context number
471 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200472 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200473 *
474 * A logging context is something like the subscriber identity to which
475 * the currently processed message relates, or the BTS through which it
476 * was received. As soon as this data is known, it can be set using
477 * this function. The main use of context information is for logging
478 * filters.
479 */
Harald Welte3ae27582010-03-26 21:24:24 +0800480int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800481{
Harald Welte3ae27582010-03-26 21:24:24 +0800482 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800483 return -EINVAL;
484
Harald Welte3ae27582010-03-26 21:24:24 +0800485 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800486
487 return 0;
488}
489
Harald Welte18fc4652011-08-17 14:14:17 +0200490/*! \brief Enable the \ref LOG_FILTER_ALL log filter
491 * \param[in] target Log target to be affected
492 * \param[in] all enable (1) or disable (0) the ALL filter
493 *
494 * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
495 * be printed. It acts as a wildcard. Setting it to \a 1 means there
496 * is no filtering.
497 */
Harald Welte3ae27582010-03-26 21:24:24 +0800498void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800499{
500 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800501 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800502 else
Harald Welte3ae27582010-03-26 21:24:24 +0800503 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800504}
505
Harald Welte18fc4652011-08-17 14:14:17 +0200506/*! \brief Enable or disable the use of colored output
507 * \param[in] target Log target to be affected
508 * \param[in] use_color Use color (1) or don't use color (0)
509 */
Harald Welte3ae27582010-03-26 21:24:24 +0800510void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800511{
512 target->use_color = use_color;
513}
514
Harald Welte18fc4652011-08-17 14:14:17 +0200515/*! \brief Enable or disable printing of timestamps while logging
516 * \param[in] target Log target to be affected
517 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
518 */
Harald Welte3ae27582010-03-26 21:24:24 +0800519void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800520{
521 target->print_timestamp = print_timestamp;
522}
523
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100524/*! \brief Enable or disable printing of extended timestamps while logging
525 * \param[in] target Log target to be affected
526 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
527 *
528 * When both timestamp and extended timestamp is enabled then only
529 * the extended timestamp will be used. The format of the timestamp
530 * is YYYYMMDDhhmmssnnn.
531 */
532void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
533{
534 target->print_ext_timestamp = print_timestamp;
535}
536
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200537/*! \brief Enable or disable printing of the filename while logging
538 * \param[in] target Log target to be affected
539 * \param[in] print_filename Enable (1) or disable (0) filenames
540 */
541void log_set_print_filename(struct log_target *target, int print_filename)
542{
543 target->print_filename = print_filename;
544}
545
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100546/*! \brief Enable or disable printing of the category name
547 * \param[in] target Log target to be affected
548 * \param[in] print_catname Enable (1) or disable (0) filenames
549 *
550 * Print the category/subsys name in front of every log message.
551 */
552void log_set_print_category(struct log_target *target, int print_category)
553{
554 target->print_category = print_category;
555}
556
Harald Welte18fc4652011-08-17 14:14:17 +0200557/*! \brief Set the global log level for a given log target
558 * \param[in] target Log target to be affected
559 * \param[in] log_level New global log level
560 */
Harald Welte3ae27582010-03-26 21:24:24 +0800561void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800562{
563 target->loglevel = log_level;
564}
565
Harald Weltede6e4982012-12-06 21:25:27 +0100566/*! \brief Set a category filter on a given log target
567 * \param[in] target Log target to be affected
568 * \param[in] category Log category to be affected
569 * \param[in] enable whether to enable or disable the filter
570 * \param[in] level Log level of the filter
571 */
Harald Welte3ae27582010-03-26 21:24:24 +0800572void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800573 int enable, int level)
574{
Harald Welte4ebdf742010-05-19 19:54:00 +0200575 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800576 return;
577 target->categories[category].enabled = !!enable;
578 target->categories[category].loglevel = level;
579}
580
Harald Welte76e72ab2011-02-17 15:52:39 +0100581static void _file_output(struct log_target *target, unsigned int level,
582 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800583{
Harald Welte0083cd32010-08-25 14:55:44 +0200584 fprintf(target->tgt_file.out, "%s", log);
585 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800586}
587
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200588/*! \brief Create a new log target skeleton
589 * \returns dynamically-allocated log target
590 * This funcition allocates a \ref log_target and initializes it
591 * with some default values. The newly created target is not
592 * registered yet.
593 */
Harald Welte3ae27582010-03-26 21:24:24 +0800594struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800595{
Harald Welte3ae27582010-03-26 21:24:24 +0800596 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800597 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800598
Harald Welte3ae27582010-03-26 21:24:24 +0800599 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800600 if (!target)
601 return NULL;
602
Harald Welteb43bc042011-06-27 10:29:17 +0200603 target->categories = talloc_zero_array(target,
604 struct log_category,
605 osmo_log_info->num_cat);
606 if (!target->categories) {
607 talloc_free(target);
608 return NULL;
609 }
610
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800611 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800612
613 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200614 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800615 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200616 cat->enabled = osmo_log_info->cat[i].enabled;
617 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800618 }
619
620 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621 target->use_color = 1;
622 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200623 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800624
625 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800626 target->loglevel = 0;
627 return target;
628}
629
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200630/*! \brief Create the STDERR log target
631 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800632struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800633{
Harald Weltea3b844c2010-03-27 00:04:40 +0800634/* since C89/C99 says stderr is a macro, we can safely do this! */
635#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800636 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637
Harald Welte3ae27582010-03-26 21:24:24 +0800638 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800639 if (!target)
640 return NULL;
641
Harald Welte28222962011-02-18 20:37:04 +0100642 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200643 target->tgt_file.out = stderr;
644 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800645 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800646#else
647 return NULL;
648#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800649}
650
Harald Welte18fc4652011-08-17 14:14:17 +0200651/*! \brief Create a new file-based log target
652 * \param[in] fname File name of the new log file
653 * \returns Log target in case of success, NULL otherwise
654 */
Harald Welte3086c392010-08-25 19:10:50 +0200655struct log_target *log_target_create_file(const char *fname)
656{
657 struct log_target *target;
658
659 target = log_target_create();
660 if (!target)
661 return NULL;
662
Harald Welte28222962011-02-18 20:37:04 +0100663 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200664 target->tgt_file.out = fopen(fname, "a");
665 if (!target->tgt_file.out)
666 return NULL;
667
668 target->output = _file_output;
669
670 target->tgt_file.fname = talloc_strdup(target, fname);
671
672 return target;
673}
674
Harald Welte18fc4652011-08-17 14:14:17 +0200675/*! \brief Find a registered log target
676 * \param[in] type Log target type
677 * \param[in] fname File name
678 * \returns Log target (if found), NULL otherwise
679 */
Harald Welte28222962011-02-18 20:37:04 +0100680struct log_target *log_target_find(int type, const char *fname)
681{
682 struct log_target *tgt;
683
684 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
685 if (tgt->type != type)
686 continue;
687 if (tgt->type == LOG_TGT_TYPE_FILE) {
688 if (!strcmp(fname, tgt->tgt_file.fname))
689 return tgt;
690 } else
691 return tgt;
692 }
693 return NULL;
694}
695
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200696/*! \brief Unregister, close and delete a log target
697 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200698void log_target_destroy(struct log_target *target)
699{
700
701 /* just in case, to make sure we don't have any references */
702 log_del_target(target);
703
704 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200705/* since C89/C99 says stderr is a macro, we can safely do this! */
706#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200707 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200708 if (target->tgt_file.out != stderr)
709#endif
710 {
Harald Welte3086c392010-08-25 19:10:50 +0200711 fclose(target->tgt_file.out);
712 target->tgt_file.out = NULL;
713 }
714 }
715
716 talloc_free(target);
717}
718
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200719/*! \brief close and re-open a log file (for log file rotation)
720 * \param[in] target log target to re-open
721 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200722int log_target_file_reopen(struct log_target *target)
723{
724 fclose(target->tgt_file.out);
725
726 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
727 if (!target->tgt_file.out)
728 return -errno;
729
730 /* we assume target->output already to be set */
731
732 return 0;
733}
734
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200735/*! \brief close and re-open all log files (for log file rotation)
736 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100737int log_targets_reopen(void)
738{
739 struct log_target *tar;
740 int rc = 0;
741
742 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
743 switch (tar->type) {
744 case LOG_TGT_TYPE_FILE:
745 if (log_target_file_reopen(tar) < 0)
746 rc = -1;
747 break;
748 default:
749 break;
750 }
751 }
752
753 return rc;
754}
755
Harald Welte18fc4652011-08-17 14:14:17 +0200756/*! \brief Generates the logging command string for VTY
757 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200758 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200759 */
Harald Weltece9fec32011-06-27 14:19:16 +0200760const char *log_vty_command_string(const struct log_info *unused_info)
Harald Welte7638af92010-05-11 16:39:22 +0200761{
Harald Weltece9fec32011-06-27 14:19:16 +0200762 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100763 int len = 0, offset = 0, ret, i, rem;
764 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200765 char *str;
766
Harald Welteb43bc042011-06-27 10:29:17 +0200767 for (i = 0; i < info->num_cat; i++) {
768 if (info->cat[i].name == NULL)
769 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100770 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200771 }
Harald Welte7638af92010-05-11 16:39:22 +0200772
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100773 for (i = 0; i < LOGLEVEL_DEFS; i++)
774 size += strlen(loglevel_strs[i].str) + 1;
775
776 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200777 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200778 if (!str)
779 return NULL;
780
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200781 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100782 if (ret < 0)
783 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200784 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200785
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100786 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200787 if (info->cat[i].name) {
788 int j, name_len = strlen(info->cat[i].name)+1;
789 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100790
Harald Welteb43bc042011-06-27 10:29:17 +0200791 for (j = 0; j < name_len; j++)
792 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100793
Harald Welteb43bc042011-06-27 10:29:17 +0200794 name[name_len-1] = '\0';
795 ret = snprintf(str + offset, rem, "%s|", name+1);
796 if (ret < 0)
797 goto err;
798 OSMO_SNPRINTF_RET(ret, rem, offset, len);
799 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100800 }
801 offset--; /* to remove the trailing | */
802 rem++;
803
804 ret = snprintf(str + offset, rem, ") (");
805 if (ret < 0)
806 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200807 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100808
809 for (i = 0; i < LOGLEVEL_DEFS; i++) {
810 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
811 char loglevel_str[loglevel_str_len];
812
813 for (j = 0; j < loglevel_str_len; j++)
814 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
815
816 loglevel_str[loglevel_str_len-1] = '\0';
817 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
818 if (ret < 0)
819 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200820 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100821 }
822 offset--; /* to remove the trailing | */
823 rem++;
824
825 ret = snprintf(str + offset, rem, ")");
826 if (ret < 0)
827 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200828 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100829err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200830 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100831 return str;
832}
833
Harald Welte18fc4652011-08-17 14:14:17 +0200834/*! \brief Generates the logging command description for VTY
835 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200836 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200837 */
Harald Weltece9fec32011-06-27 14:19:16 +0200838const char *log_vty_command_description(const struct log_info *unused_info)
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100839{
Harald Weltece9fec32011-06-27 14:19:16 +0200840 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100841 char *str;
842 int i, ret, len = 0, offset = 0, rem;
843 unsigned int size =
844 strlen(LOGGING_STR
845 "Set the log level for a specified category\n") + 1;
846
Harald Welteb43bc042011-06-27 10:29:17 +0200847 for (i = 0; i < info->num_cat; i++) {
848 if (info->cat[i].name == NULL)
849 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100850 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200851 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100852
853 for (i = 0; i < LOGLEVEL_DEFS; i++)
854 size += strlen(loglevel_descriptions[i]) + 1;
855
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200856 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100857 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200858 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100859 if (!str)
860 return NULL;
861
862 ret = snprintf(str + offset, rem, LOGGING_STR
863 "Set the log level for a specified category\n");
864 if (ret < 0)
865 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200866 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100867
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200868 ret = snprintf(str + offset, rem,
869 "Global setting for all subsystems\n");
870 if (ret < 0)
871 goto err;
872 OSMO_SNPRINTF_RET(ret, rem, offset, len);
873
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100874 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200875 if (info->cat[i].name == NULL)
876 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100877 ret = snprintf(str + offset, rem, "%s\n",
878 info->cat[i].description);
879 if (ret < 0)
880 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200881 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100882 }
883 for (i = 0; i < LOGLEVEL_DEFS; i++) {
884 ret = snprintf(str + offset, rem, "%s\n",
885 loglevel_descriptions[i]);
886 if (ret < 0)
887 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200888 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100889 }
890err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200891 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200892 return str;
893}
894
Harald Welte18fc4652011-08-17 14:14:17 +0200895/*! \brief Initialize the Osmocom logging core
896 * \param[in] inf Information regarding logging categories
897 * \param[in] ctx \ref talloc context for logging allocations
898 * \returns 0 in case of success, negative in case of error
899 */
Harald Welteb43bc042011-06-27 10:29:17 +0200900int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800901{
Harald Welteb43bc042011-06-27 10:29:17 +0200902 int i;
903
904 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
905 if (!tall_log_ctx)
906 return -ENOMEM;
907
908 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
909 if (!osmo_log_info)
910 return -ENOMEM;
911
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100912 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200913 osmo_log_info->num_cat_user = inf->num_cat;
914 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200915 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200916
917 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
918 struct log_info_cat,
919 osmo_log_info->num_cat);
920 if (!osmo_log_info->cat) {
921 talloc_free(osmo_log_info);
922 osmo_log_info = NULL;
923 return -ENOMEM;
924 }
925
926 /* copy over the user part */
927 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200928 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
929 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200930 sizeof(struct log_info_cat));
931 }
932
933 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200934 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200935 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200936 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200937 &internal_cat[i], sizeof(struct log_info_cat));
938 }
939
940 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800941}
Harald Welte18fc4652011-08-17 14:14:17 +0200942
Harald Welte69e6c3c2016-04-20 10:41:27 +0200943/* \brief De-initialize the Osmocom logging core
944 * This function destroys all targets and releases associated memory */
945void log_fini(void)
946{
947 struct log_target *tar, *tar2;
948
949 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
950 log_target_destroy(tar);
951
952 talloc_free(osmo_log_info);
953 osmo_log_info = NULL;
954 talloc_free(tall_log_ctx);
955 tall_log_ctx = NULL;
956}
957
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100958/*! \brief Check whether a log entry will be generated.
959 * \returns != 0 if a log entry might get generated by at least one target */
960int log_check_level(int subsys, unsigned int level)
961{
962 struct log_target *tar;
963
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100964 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100965
966 /* TODO: The following could/should be cached (update on config) */
967
968 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100969 if (!check_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100970 continue;
971
972 /* This might get logged (ignoring filters) */
973 return 1;
974 }
975
976 /* We are sure, that this will not be logged. */
977 return 0;
978}
979
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200980/*! @} */