blob: 92852c9c461ba06adfbbed180cb0616b8424959b [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
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100331static inline int map_subsys(int subsys)
332{
333 if (subsys < 0)
334 subsys = subsys_lib2index(subsys);
335
336 if (subsys > osmo_log_info->num_cat)
337 subsys = DLGLOBAL;
338 return subsys;
339}
340
341static inline int check_log_to_target(struct log_target *tar, int subsys, int level)
342{
343 struct log_category *category;
344
345 category = &tar->categories[subsys];
346
347 /* subsystem is not supposed to be logged */
348 if (!category->enabled)
349 return 0;
350
351 /* Check the global log level */
352 if (tar->loglevel != 0 && level < tar->loglevel)
353 return 0;
354
355 /* Check the category log level */
356 if (tar->loglevel == 0 && category->loglevel != 0 &&
357 level < category->loglevel)
358 return 0;
359
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100360 /* Apply filters here... if that becomes messy we will
361 * need to put filters in a list and each filter will
362 * say stop, continue, output */
363 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
364 return 1;
365
366 if (osmo_log_info->filter_fn)
367 return osmo_log_info->filter_fn(&log_context, tar);
368
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100369 /* TODO: Check the filter/selector too? */
370 return 1;
371}
372
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200373/*! \brief vararg version of logging function
374 * \param[in] subsys Logging sub-system
375 * \param[in] level Log level
376 * \param[in] file name of source code file
377 * \param[in] cont continuation (1) or new line (0)
378 * \param[in] format format string
379 * \param[in] ap vararg-list containing format string arguments
380 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200381void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200382 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800383{
Harald Welte3ae27582010-03-26 21:24:24 +0800384 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800385
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100386 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200387
Harald Welte28222962011-02-18 20:37:04 +0100388 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200389 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800390
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100391 if (!check_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800392 continue;
393
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200394 /* According to the manpage, vsnprintf leaves the value of ap
395 * in undefined state. Since _output uses vsnprintf and it may
396 * be called several times, we have to pass a copy of ap. */
397 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100398 if (tar->raw_output)
399 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
400 else
401 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200402 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800403 }
404}
405
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200406/*! \brief logging function used by DEBUGP() macro
407 * \param[in] subsys Logging sub-system
408 * \param[in] file name of source code file
409 * \param[in] cont continuation (1) or new line (0)
410 * \param[in] format format string
411 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200412void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800413 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800414{
415 va_list ap;
416
417 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200418 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800419 va_end(ap);
420}
421
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200422/*! \brief logging function used by LOGP() macro
423 * \param[in] subsys Logging sub-system
424 * \param[in] level Log level
425 * \param[in] file name of source code file
426 * \param[in] cont continuation (1) or new line (0)
427 * \param[in] format format string
428 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200429void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800430{
431 va_list ap;
432
433 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200434 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800435 va_end(ap);
436}
437
Harald Welte18fc4652011-08-17 14:14:17 +0200438/*! \brief Register a new log target with the logging core
439 * \param[in] target Log target to be registered
440 */
Harald Welte3ae27582010-03-26 21:24:24 +0800441void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800442{
Harald Welte28222962011-02-18 20:37:04 +0100443 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800444}
445
Harald Welte18fc4652011-08-17 14:14:17 +0200446/*! \brief Unregister a log target from the logging core
447 * \param[in] target Log target to be unregistered
448 */
Harald Welte3ae27582010-03-26 21:24:24 +0800449void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800450{
451 llist_del(&target->entry);
452}
453
Harald Welte18fc4652011-08-17 14:14:17 +0200454/*! \brief Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800455void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800456{
Harald Welte3ae27582010-03-26 21:24:24 +0800457 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800458}
459
Harald Welte18fc4652011-08-17 14:14:17 +0200460/*! \brief Set the logging context
461 * \param[in] ctx_nr logging context number
462 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200463 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200464 *
465 * A logging context is something like the subscriber identity to which
466 * the currently processed message relates, or the BTS through which it
467 * was received. As soon as this data is known, it can be set using
468 * this function. The main use of context information is for logging
469 * filters.
470 */
Harald Welte3ae27582010-03-26 21:24:24 +0800471int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800472{
Harald Welte3ae27582010-03-26 21:24:24 +0800473 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800474 return -EINVAL;
475
Harald Welte3ae27582010-03-26 21:24:24 +0800476 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800477
478 return 0;
479}
480
Harald Welte18fc4652011-08-17 14:14:17 +0200481/*! \brief Enable the \ref LOG_FILTER_ALL log filter
482 * \param[in] target Log target to be affected
483 * \param[in] all enable (1) or disable (0) the ALL filter
484 *
485 * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
486 * be printed. It acts as a wildcard. Setting it to \a 1 means there
487 * is no filtering.
488 */
Harald Welte3ae27582010-03-26 21:24:24 +0800489void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800490{
491 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800492 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800493 else
Harald Welte3ae27582010-03-26 21:24:24 +0800494 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800495}
496
Harald Welte18fc4652011-08-17 14:14:17 +0200497/*! \brief Enable or disable the use of colored output
498 * \param[in] target Log target to be affected
499 * \param[in] use_color Use color (1) or don't use color (0)
500 */
Harald Welte3ae27582010-03-26 21:24:24 +0800501void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800502{
503 target->use_color = use_color;
504}
505
Harald Welte18fc4652011-08-17 14:14:17 +0200506/*! \brief Enable or disable printing of timestamps while logging
507 * \param[in] target Log target to be affected
508 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
509 */
Harald Welte3ae27582010-03-26 21:24:24 +0800510void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800511{
512 target->print_timestamp = print_timestamp;
513}
514
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100515/*! \brief Enable or disable printing of extended timestamps while logging
516 * \param[in] target Log target to be affected
517 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
518 *
519 * When both timestamp and extended timestamp is enabled then only
520 * the extended timestamp will be used. The format of the timestamp
521 * is YYYYMMDDhhmmssnnn.
522 */
523void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
524{
525 target->print_ext_timestamp = print_timestamp;
526}
527
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200528/*! \brief Enable or disable printing of the filename while logging
529 * \param[in] target Log target to be affected
530 * \param[in] print_filename Enable (1) or disable (0) filenames
531 */
532void log_set_print_filename(struct log_target *target, int print_filename)
533{
534 target->print_filename = print_filename;
535}
536
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100537/*! \brief Enable or disable printing of the category name
538 * \param[in] target Log target to be affected
539 * \param[in] print_catname Enable (1) or disable (0) filenames
540 *
541 * Print the category/subsys name in front of every log message.
542 */
543void log_set_print_category(struct log_target *target, int print_category)
544{
545 target->print_category = print_category;
546}
547
Harald Welte18fc4652011-08-17 14:14:17 +0200548/*! \brief Set the global log level for a given log target
549 * \param[in] target Log target to be affected
550 * \param[in] log_level New global log level
551 */
Harald Welte3ae27582010-03-26 21:24:24 +0800552void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800553{
554 target->loglevel = log_level;
555}
556
Harald Weltede6e4982012-12-06 21:25:27 +0100557/*! \brief Set a category filter on a given log target
558 * \param[in] target Log target to be affected
559 * \param[in] category Log category to be affected
560 * \param[in] enable whether to enable or disable the filter
561 * \param[in] level Log level of the filter
562 */
Harald Welte3ae27582010-03-26 21:24:24 +0800563void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800564 int enable, int level)
565{
Harald Welte4ebdf742010-05-19 19:54:00 +0200566 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800567 return;
568 target->categories[category].enabled = !!enable;
569 target->categories[category].loglevel = level;
570}
571
Harald Welte76e72ab2011-02-17 15:52:39 +0100572static void _file_output(struct log_target *target, unsigned int level,
573 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800574{
Harald Welte0083cd32010-08-25 14:55:44 +0200575 fprintf(target->tgt_file.out, "%s", log);
576 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800577}
578
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200579/*! \brief Create a new log target skeleton
580 * \returns dynamically-allocated log target
581 * This funcition allocates a \ref log_target and initializes it
582 * with some default values. The newly created target is not
583 * registered yet.
584 */
Harald Welte3ae27582010-03-26 21:24:24 +0800585struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800586{
Harald Welte3ae27582010-03-26 21:24:24 +0800587 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800588 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800589
Harald Welte3ae27582010-03-26 21:24:24 +0800590 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800591 if (!target)
592 return NULL;
593
Harald Welteb43bc042011-06-27 10:29:17 +0200594 target->categories = talloc_zero_array(target,
595 struct log_category,
596 osmo_log_info->num_cat);
597 if (!target->categories) {
598 talloc_free(target);
599 return NULL;
600 }
601
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800602 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800603
604 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200605 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800606 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200607 cat->enabled = osmo_log_info->cat[i].enabled;
608 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800609 }
610
611 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800612 target->use_color = 1;
613 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200614 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800615
616 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800617 target->loglevel = 0;
618 return target;
619}
620
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200621/*! \brief Create the STDERR log target
622 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800623struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800624{
Harald Weltea3b844c2010-03-27 00:04:40 +0800625/* since C89/C99 says stderr is a macro, we can safely do this! */
626#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800627 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800628
Harald Welte3ae27582010-03-26 21:24:24 +0800629 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800630 if (!target)
631 return NULL;
632
Harald Welte28222962011-02-18 20:37:04 +0100633 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200634 target->tgt_file.out = stderr;
635 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800636 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800637#else
638 return NULL;
639#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800640}
641
Harald Welte18fc4652011-08-17 14:14:17 +0200642/*! \brief Create a new file-based log target
643 * \param[in] fname File name of the new log file
644 * \returns Log target in case of success, NULL otherwise
645 */
Harald Welte3086c392010-08-25 19:10:50 +0200646struct log_target *log_target_create_file(const char *fname)
647{
648 struct log_target *target;
649
650 target = log_target_create();
651 if (!target)
652 return NULL;
653
Harald Welte28222962011-02-18 20:37:04 +0100654 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200655 target->tgt_file.out = fopen(fname, "a");
656 if (!target->tgt_file.out)
657 return NULL;
658
659 target->output = _file_output;
660
661 target->tgt_file.fname = talloc_strdup(target, fname);
662
663 return target;
664}
665
Harald Welte18fc4652011-08-17 14:14:17 +0200666/*! \brief Find a registered log target
667 * \param[in] type Log target type
668 * \param[in] fname File name
669 * \returns Log target (if found), NULL otherwise
670 */
Harald Welte28222962011-02-18 20:37:04 +0100671struct log_target *log_target_find(int type, const char *fname)
672{
673 struct log_target *tgt;
674
675 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
676 if (tgt->type != type)
677 continue;
678 if (tgt->type == LOG_TGT_TYPE_FILE) {
679 if (!strcmp(fname, tgt->tgt_file.fname))
680 return tgt;
681 } else
682 return tgt;
683 }
684 return NULL;
685}
686
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200687/*! \brief Unregister, close and delete a log target
688 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200689void log_target_destroy(struct log_target *target)
690{
691
692 /* just in case, to make sure we don't have any references */
693 log_del_target(target);
694
695 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200696/* since C89/C99 says stderr is a macro, we can safely do this! */
697#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200698 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200699 if (target->tgt_file.out != stderr)
700#endif
701 {
Harald Welte3086c392010-08-25 19:10:50 +0200702 fclose(target->tgt_file.out);
703 target->tgt_file.out = NULL;
704 }
705 }
706
707 talloc_free(target);
708}
709
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200710/*! \brief close and re-open a log file (for log file rotation)
711 * \param[in] target log target to re-open
712 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200713int log_target_file_reopen(struct log_target *target)
714{
715 fclose(target->tgt_file.out);
716
717 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
718 if (!target->tgt_file.out)
719 return -errno;
720
721 /* we assume target->output already to be set */
722
723 return 0;
724}
725
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200726/*! \brief close and re-open all log files (for log file rotation)
727 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100728int log_targets_reopen(void)
729{
730 struct log_target *tar;
731 int rc = 0;
732
733 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
734 switch (tar->type) {
735 case LOG_TGT_TYPE_FILE:
736 if (log_target_file_reopen(tar) < 0)
737 rc = -1;
738 break;
739 default:
740 break;
741 }
742 }
743
744 return rc;
745}
746
Harald Welte18fc4652011-08-17 14:14:17 +0200747/*! \brief Generates the logging command string for VTY
748 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200749 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200750 */
Harald Weltece9fec32011-06-27 14:19:16 +0200751const char *log_vty_command_string(const struct log_info *unused_info)
Harald Welte7638af92010-05-11 16:39:22 +0200752{
Harald Weltece9fec32011-06-27 14:19:16 +0200753 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100754 int len = 0, offset = 0, ret, i, rem;
755 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200756 char *str;
757
Harald Welteb43bc042011-06-27 10:29:17 +0200758 for (i = 0; i < info->num_cat; i++) {
759 if (info->cat[i].name == NULL)
760 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100761 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200762 }
Harald Welte7638af92010-05-11 16:39:22 +0200763
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100764 for (i = 0; i < LOGLEVEL_DEFS; i++)
765 size += strlen(loglevel_strs[i].str) + 1;
766
767 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200768 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200769 if (!str)
770 return NULL;
771
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200772 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100773 if (ret < 0)
774 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200775 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200776
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100777 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200778 if (info->cat[i].name) {
779 int j, name_len = strlen(info->cat[i].name)+1;
780 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100781
Harald Welteb43bc042011-06-27 10:29:17 +0200782 for (j = 0; j < name_len; j++)
783 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100784
Harald Welteb43bc042011-06-27 10:29:17 +0200785 name[name_len-1] = '\0';
786 ret = snprintf(str + offset, rem, "%s|", name+1);
787 if (ret < 0)
788 goto err;
789 OSMO_SNPRINTF_RET(ret, rem, offset, len);
790 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100791 }
792 offset--; /* to remove the trailing | */
793 rem++;
794
795 ret = snprintf(str + offset, rem, ") (");
796 if (ret < 0)
797 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200798 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100799
800 for (i = 0; i < LOGLEVEL_DEFS; i++) {
801 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
802 char loglevel_str[loglevel_str_len];
803
804 for (j = 0; j < loglevel_str_len; j++)
805 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
806
807 loglevel_str[loglevel_str_len-1] = '\0';
808 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
809 if (ret < 0)
810 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200811 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100812 }
813 offset--; /* to remove the trailing | */
814 rem++;
815
816 ret = snprintf(str + offset, rem, ")");
817 if (ret < 0)
818 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200819 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100820err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200821 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100822 return str;
823}
824
Harald Welte18fc4652011-08-17 14:14:17 +0200825/*! \brief Generates the logging command description for VTY
826 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200827 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200828 */
Harald Weltece9fec32011-06-27 14:19:16 +0200829const char *log_vty_command_description(const struct log_info *unused_info)
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100830{
Harald Weltece9fec32011-06-27 14:19:16 +0200831 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100832 char *str;
833 int i, ret, len = 0, offset = 0, rem;
834 unsigned int size =
835 strlen(LOGGING_STR
836 "Set the log level for a specified category\n") + 1;
837
Harald Welteb43bc042011-06-27 10:29:17 +0200838 for (i = 0; i < info->num_cat; i++) {
839 if (info->cat[i].name == NULL)
840 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100841 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200842 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100843
844 for (i = 0; i < LOGLEVEL_DEFS; i++)
845 size += strlen(loglevel_descriptions[i]) + 1;
846
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200847 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100848 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200849 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100850 if (!str)
851 return NULL;
852
853 ret = snprintf(str + offset, rem, LOGGING_STR
854 "Set the log level for a specified category\n");
855 if (ret < 0)
856 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200857 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100858
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200859 ret = snprintf(str + offset, rem,
860 "Global setting for all subsystems\n");
861 if (ret < 0)
862 goto err;
863 OSMO_SNPRINTF_RET(ret, rem, offset, len);
864
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100865 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200866 if (info->cat[i].name == NULL)
867 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100868 ret = snprintf(str + offset, rem, "%s\n",
869 info->cat[i].description);
870 if (ret < 0)
871 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200872 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100873 }
874 for (i = 0; i < LOGLEVEL_DEFS; i++) {
875 ret = snprintf(str + offset, rem, "%s\n",
876 loglevel_descriptions[i]);
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 }
881err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200882 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200883 return str;
884}
885
Harald Welte18fc4652011-08-17 14:14:17 +0200886/*! \brief Initialize the Osmocom logging core
887 * \param[in] inf Information regarding logging categories
888 * \param[in] ctx \ref talloc context for logging allocations
889 * \returns 0 in case of success, negative in case of error
890 */
Harald Welteb43bc042011-06-27 10:29:17 +0200891int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800892{
Harald Welteb43bc042011-06-27 10:29:17 +0200893 int i;
894
895 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
896 if (!tall_log_ctx)
897 return -ENOMEM;
898
899 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
900 if (!osmo_log_info)
901 return -ENOMEM;
902
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100903 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200904 osmo_log_info->num_cat_user = inf->num_cat;
905 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200906 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200907
908 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
909 struct log_info_cat,
910 osmo_log_info->num_cat);
911 if (!osmo_log_info->cat) {
912 talloc_free(osmo_log_info);
913 osmo_log_info = NULL;
914 return -ENOMEM;
915 }
916
917 /* copy over the user part */
918 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200919 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
920 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200921 sizeof(struct log_info_cat));
922 }
923
924 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200925 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200926 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200927 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200928 &internal_cat[i], sizeof(struct log_info_cat));
929 }
930
931 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800932}
Harald Welte18fc4652011-08-17 14:14:17 +0200933
Harald Welte69e6c3c2016-04-20 10:41:27 +0200934/* \brief De-initialize the Osmocom logging core
935 * This function destroys all targets and releases associated memory */
936void log_fini(void)
937{
938 struct log_target *tar, *tar2;
939
940 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
941 log_target_destroy(tar);
942
943 talloc_free(osmo_log_info);
944 osmo_log_info = NULL;
945 talloc_free(tall_log_ctx);
946 tall_log_ctx = NULL;
947}
948
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100949/*! \brief Check whether a log entry will be generated.
950 * \returns != 0 if a log entry might get generated by at least one target */
951int log_check_level(int subsys, unsigned int level)
952{
953 struct log_target *tar;
954
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100955 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100956
957 /* TODO: The following could/should be cached (update on config) */
958
959 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100960 if (!check_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100961 continue;
962
963 /* This might get logged (ignoring filters) */
964 return 1;
965 }
966
967 /* We are sure, that this will not be logged. */
968 return 0;
969}
970
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200971/*! @} */