blob: 9e30d5f875ac365447bda9d4e84320ab5f944f5d [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 },
Harald Welteb43bc042011-06-27 10:29:17 +0200126};
127
Harald Weltede6e4982012-12-06 21:25:27 +0100128/*! \brief descriptive string for each log level */
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100129/* You have to keep this in sync with the structure loglevel_strs. */
130const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
Holger Hans Peter Freyther6ec6bd92014-12-28 18:27:38 +0100131 "Don't use. It doesn't log anything",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100132 "Log debug messages and higher levels",
133 "Log informational messages and higher levels",
Ruben Undheim029f5a12015-09-16 19:02:35 +0200134 "Log noticeable messages and higher levels",
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100135 "Log error messages and higher levels",
136 "Log only fatal messages",
137 NULL,
138};
139
Harald Welteb43bc042011-06-27 10:29:17 +0200140/* special magic for negative (library-internal) log subsystem numbers */
141static int subsys_lib2index(int subsys)
142{
143 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
144}
145
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200146/*! \brief Parse a human-readable log level into a numeric value
147 * \param lvl[in] zero-terminated string containing log level name
148 * \returns numeric log level
149 */
Harald Welte3ae27582010-03-26 21:24:24 +0800150int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800151{
152 return get_string_value(loglevel_strs, lvl);
153}
154
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200155/*! \brief convert a numeric log level into human-readable string
156 * \param lvl[in] numeric log level
157 * \returns zero-terminated string (log level name)
158 */
Harald Welte9ac22252010-05-11 11:19:40 +0200159const char *log_level_str(unsigned int lvl)
160{
161 return get_value_string(loglevel_strs, lvl);
162}
163
Harald Welte18fc4652011-08-17 14:14:17 +0200164/*! \brief parse a human-readable log category into numeric form
165 * \param[in] category human-readable log category name
166 * \returns numeric category value, or -EINVAL otherwise
167 */
Harald Welte3ae27582010-03-26 21:24:24 +0800168int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800169{
170 int i;
171
Harald Welte4ebdf742010-05-19 19:54:00 +0200172 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200173 if (osmo_log_info->cat[i].name == NULL)
174 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200175 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800176 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800177 }
178
179 return -EINVAL;
180}
181
Harald Welte18fc4652011-08-17 14:14:17 +0200182/*! \brief parse the log category mask
183 * \param[in] target log target to be configured
184 * \param[in] _mask log category mask string
185 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800186 * The format can be this: category1:category2:category3
187 * or category1,2:category2,3:...
188 */
Harald Welte3ae27582010-03-26 21:24:24 +0800189void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800190{
191 int i = 0;
192 char *mask = strdup(_mask);
193 char *category_token = NULL;
194
195 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200196 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800197 target->categories[i].enabled = 0;
198
199 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200200 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800201 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200202 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200203 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800204 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200205
206 if (!osmo_log_info->cat[i].name)
207 continue;
208
209 length = strlen(category_token);
210 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200211
212 /* Use longest length not to match subocurrences. */
213 if (cat_length > length)
214 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800215
216 if (colon)
217 length = colon - category_token;
218
Harald Welte4ebdf742010-05-19 19:54:00 +0200219 if (strncasecmp(osmo_log_info->cat[i].name,
220 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800221 int level = 0;
222
223 if (colon)
224 level = atoi(colon+1);
225
Harald Weltefaadfe22010-03-26 21:05:43 +0800226 target->categories[i].enabled = 1;
227 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800228 }
229 }
230 } while ((category_token = strtok(NULL, ":")));
231
232 free(mask);
233}
234
235static const char* color(int subsys)
236{
Harald Welte4ebdf742010-05-19 19:54:00 +0200237 if (subsys < osmo_log_info->num_cat)
238 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800239
Harald Welted788f662010-03-26 09:45:03 +0800240 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800241}
242
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100243static const char* category_name(int subsys)
244{
245 if (subsys < osmo_log_info->num_cat)
246 return osmo_log_info->cat[subsys].name;
247
248 return NULL;
249}
250
Harald Welte3ae27582010-03-26 21:24:24 +0800251static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200252 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100253 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800254{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800255 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200256 int ret, len = 0, offset = 0, rem = sizeof(buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800257
258 /* are we using color */
259 if (target->use_color) {
Harald Welted788f662010-03-26 09:45:03 +0800260 const char *c = color(subsys);
261 if (c) {
Holger Hans Peter Freyther5f91a402014-12-05 10:29:45 +0100262 ret = snprintf(buf + offset, rem, "%s", c);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200263 if (ret < 0)
264 goto err;
265 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800266 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800267 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800268 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100269 if (target->print_ext_timestamp) {
270 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100271 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200272 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100273 localtime_r(&tv.tv_sec, &tm);
274 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100275 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100276 tm.tm_hour, tm.tm_min, tm.tm_sec,
277 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100278 if (ret < 0)
279 goto err;
280 OSMO_SNPRINTF_RET(ret, rem, offset, len);
281 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800282 char *timestr;
283 time_t tm;
284 tm = time(NULL);
285 timestr = ctime(&tm);
286 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200287 ret = snprintf(buf + offset, rem, "%s ", timestr);
288 if (ret < 0)
289 goto err;
290 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800291 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100292 if (target->print_category) {
293 ret = snprintf(buf + offset, rem, "%s ", category_name(subsys));
294 if (ret < 0)
295 goto err;
296 OSMO_SNPRINTF_RET(ret, rem, offset, len);
297 }
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200298 if (target->print_filename) {
299 ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ",
300 subsys, file, line);
301 if (ret < 0)
302 goto err;
303 OSMO_SNPRINTF_RET(ret, rem, offset, len);
304 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800305 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200306 ret = vsnprintf(buf + offset, rem, format, ap);
307 if (ret < 0)
308 goto err;
309 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800310
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200311 ret = snprintf(buf + offset, rem, "%s",
312 target->use_color ? "\033[0;m" : "");
313 if (ret < 0)
314 goto err;
315 OSMO_SNPRINTF_RET(ret, rem, offset, len);
316err:
317 buf[sizeof(buf)-1] = '\0';
318 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800319}
320
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100321static inline int map_subsys(int subsys)
322{
323 if (subsys < 0)
324 subsys = subsys_lib2index(subsys);
325
326 if (subsys > osmo_log_info->num_cat)
327 subsys = DLGLOBAL;
328 return subsys;
329}
330
331static inline int check_log_to_target(struct log_target *tar, int subsys, int level)
332{
333 struct log_category *category;
334
335 category = &tar->categories[subsys];
336
337 /* subsystem is not supposed to be logged */
338 if (!category->enabled)
339 return 0;
340
341 /* Check the global log level */
342 if (tar->loglevel != 0 && level < tar->loglevel)
343 return 0;
344
345 /* Check the category log level */
346 if (tar->loglevel == 0 && category->loglevel != 0 &&
347 level < category->loglevel)
348 return 0;
349
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100350 /* Apply filters here... if that becomes messy we will
351 * need to put filters in a list and each filter will
352 * say stop, continue, output */
353 if ((tar->filter_map & LOG_FILTER_ALL) != 0)
354 return 1;
355
356 if (osmo_log_info->filter_fn)
357 return osmo_log_info->filter_fn(&log_context, tar);
358
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100359 /* TODO: Check the filter/selector too? */
360 return 1;
361}
362
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200363/*! \brief vararg version of logging function
364 * \param[in] subsys Logging sub-system
365 * \param[in] level Log level
366 * \param[in] file name of source code file
367 * \param[in] cont continuation (1) or new line (0)
368 * \param[in] format format string
369 * \param[in] ap vararg-list containing format string arguments
370 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200371void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200372 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800373{
Harald Welte3ae27582010-03-26 21:24:24 +0800374 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800375
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100376 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200377
Harald Welte28222962011-02-18 20:37:04 +0100378 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200379 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800380
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100381 if (!check_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800382 continue;
383
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200384 /* According to the manpage, vsnprintf leaves the value of ap
385 * in undefined state. Since _output uses vsnprintf and it may
386 * be called several times, we have to pass a copy of ap. */
387 va_copy(bp, ap);
Harald Welteda127cb2011-07-02 21:51:32 +0200388 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200389 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800390 }
391}
392
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200393/*! \brief logging function used by DEBUGP() macro
394 * \param[in] subsys Logging sub-system
395 * \param[in] file name of source code file
396 * \param[in] cont continuation (1) or new line (0)
397 * \param[in] format format string
398 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200399void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800400 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800401{
402 va_list ap;
403
404 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200405 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800406 va_end(ap);
407}
408
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200409/*! \brief logging function used by LOGP() macro
410 * \param[in] subsys Logging sub-system
411 * \param[in] level Log level
412 * \param[in] file name of source code file
413 * \param[in] cont continuation (1) or new line (0)
414 * \param[in] format format string
415 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200416void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800417{
418 va_list ap;
419
420 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200421 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800422 va_end(ap);
423}
424
Harald Welte18fc4652011-08-17 14:14:17 +0200425/*! \brief Register a new log target with the logging core
426 * \param[in] target Log target to be registered
427 */
Harald Welte3ae27582010-03-26 21:24:24 +0800428void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800429{
Harald Welte28222962011-02-18 20:37:04 +0100430 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800431}
432
Harald Welte18fc4652011-08-17 14:14:17 +0200433/*! \brief Unregister a log target from the logging core
434 * \param[in] target Log target to be unregistered
435 */
Harald Welte3ae27582010-03-26 21:24:24 +0800436void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800437{
438 llist_del(&target->entry);
439}
440
Harald Welte18fc4652011-08-17 14:14:17 +0200441/*! \brief Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800442void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800443{
Harald Welte3ae27582010-03-26 21:24:24 +0800444 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800445}
446
Harald Welte18fc4652011-08-17 14:14:17 +0200447/*! \brief Set the logging context
448 * \param[in] ctx_nr logging context number
449 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200450 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200451 *
452 * A logging context is something like the subscriber identity to which
453 * the currently processed message relates, or the BTS through which it
454 * was received. As soon as this data is known, it can be set using
455 * this function. The main use of context information is for logging
456 * filters.
457 */
Harald Welte3ae27582010-03-26 21:24:24 +0800458int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800459{
Harald Welte3ae27582010-03-26 21:24:24 +0800460 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800461 return -EINVAL;
462
Harald Welte3ae27582010-03-26 21:24:24 +0800463 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800464
465 return 0;
466}
467
Harald Welte18fc4652011-08-17 14:14:17 +0200468/*! \brief Enable the \ref LOG_FILTER_ALL log filter
469 * \param[in] target Log target to be affected
470 * \param[in] all enable (1) or disable (0) the ALL filter
471 *
472 * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
473 * be printed. It acts as a wildcard. Setting it to \a 1 means there
474 * is no filtering.
475 */
Harald Welte3ae27582010-03-26 21:24:24 +0800476void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800477{
478 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800479 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800480 else
Harald Welte3ae27582010-03-26 21:24:24 +0800481 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800482}
483
Harald Welte18fc4652011-08-17 14:14:17 +0200484/*! \brief Enable or disable the use of colored output
485 * \param[in] target Log target to be affected
486 * \param[in] use_color Use color (1) or don't use color (0)
487 */
Harald Welte3ae27582010-03-26 21:24:24 +0800488void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800489{
490 target->use_color = use_color;
491}
492
Harald Welte18fc4652011-08-17 14:14:17 +0200493/*! \brief Enable or disable printing of timestamps while logging
494 * \param[in] target Log target to be affected
495 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
496 */
Harald Welte3ae27582010-03-26 21:24:24 +0800497void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800498{
499 target->print_timestamp = print_timestamp;
500}
501
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100502/*! \brief Enable or disable printing of extended timestamps while logging
503 * \param[in] target Log target to be affected
504 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
505 *
506 * When both timestamp and extended timestamp is enabled then only
507 * the extended timestamp will be used. The format of the timestamp
508 * is YYYYMMDDhhmmssnnn.
509 */
510void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
511{
512 target->print_ext_timestamp = print_timestamp;
513}
514
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200515/*! \brief Enable or disable printing of the filename while logging
516 * \param[in] target Log target to be affected
517 * \param[in] print_filename Enable (1) or disable (0) filenames
518 */
519void log_set_print_filename(struct log_target *target, int print_filename)
520{
521 target->print_filename = print_filename;
522}
523
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100524/*! \brief Enable or disable printing of the category name
525 * \param[in] target Log target to be affected
526 * \param[in] print_catname Enable (1) or disable (0) filenames
527 *
528 * Print the category/subsys name in front of every log message.
529 */
530void log_set_print_category(struct log_target *target, int print_category)
531{
532 target->print_category = print_category;
533}
534
Harald Welte18fc4652011-08-17 14:14:17 +0200535/*! \brief Set the global log level for a given log target
536 * \param[in] target Log target to be affected
537 * \param[in] log_level New global log level
538 */
Harald Welte3ae27582010-03-26 21:24:24 +0800539void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800540{
541 target->loglevel = log_level;
542}
543
Harald Weltede6e4982012-12-06 21:25:27 +0100544/*! \brief Set a category filter on a given log target
545 * \param[in] target Log target to be affected
546 * \param[in] category Log category to be affected
547 * \param[in] enable whether to enable or disable the filter
548 * \param[in] level Log level of the filter
549 */
Harald Welte3ae27582010-03-26 21:24:24 +0800550void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800551 int enable, int level)
552{
Harald Welte4ebdf742010-05-19 19:54:00 +0200553 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800554 return;
555 target->categories[category].enabled = !!enable;
556 target->categories[category].loglevel = level;
557}
558
Harald Welte76e72ab2011-02-17 15:52:39 +0100559static void _file_output(struct log_target *target, unsigned int level,
560 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800561{
Harald Welte0083cd32010-08-25 14:55:44 +0200562 fprintf(target->tgt_file.out, "%s", log);
563 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800564}
565
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200566/*! \brief Create a new log target skeleton
567 * \returns dynamically-allocated log target
568 * This funcition allocates a \ref log_target and initializes it
569 * with some default values. The newly created target is not
570 * registered yet.
571 */
Harald Welte3ae27582010-03-26 21:24:24 +0800572struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800573{
Harald Welte3ae27582010-03-26 21:24:24 +0800574 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800575 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800576
Harald Welte3ae27582010-03-26 21:24:24 +0800577 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800578 if (!target)
579 return NULL;
580
Harald Welteb43bc042011-06-27 10:29:17 +0200581 target->categories = talloc_zero_array(target,
582 struct log_category,
583 osmo_log_info->num_cat);
584 if (!target->categories) {
585 talloc_free(target);
586 return NULL;
587 }
588
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800589 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800590
591 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200592 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800593 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200594 cat->enabled = osmo_log_info->cat[i].enabled;
595 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800596 }
597
598 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800599 target->use_color = 1;
600 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200601 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800602
603 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800604 target->loglevel = 0;
605 return target;
606}
607
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200608/*! \brief Create the STDERR log target
609 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800610struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800611{
Harald Weltea3b844c2010-03-27 00:04:40 +0800612/* since C89/C99 says stderr is a macro, we can safely do this! */
613#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800614 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800615
Harald Welte3ae27582010-03-26 21:24:24 +0800616 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800617 if (!target)
618 return NULL;
619
Harald Welte28222962011-02-18 20:37:04 +0100620 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200621 target->tgt_file.out = stderr;
622 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800623 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800624#else
625 return NULL;
626#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800627}
628
Harald Welte18fc4652011-08-17 14:14:17 +0200629/*! \brief Create a new file-based log target
630 * \param[in] fname File name of the new log file
631 * \returns Log target in case of success, NULL otherwise
632 */
Harald Welte3086c392010-08-25 19:10:50 +0200633struct log_target *log_target_create_file(const char *fname)
634{
635 struct log_target *target;
636
637 target = log_target_create();
638 if (!target)
639 return NULL;
640
Harald Welte28222962011-02-18 20:37:04 +0100641 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200642 target->tgt_file.out = fopen(fname, "a");
643 if (!target->tgt_file.out)
644 return NULL;
645
646 target->output = _file_output;
647
648 target->tgt_file.fname = talloc_strdup(target, fname);
649
650 return target;
651}
652
Harald Welte18fc4652011-08-17 14:14:17 +0200653/*! \brief Find a registered log target
654 * \param[in] type Log target type
655 * \param[in] fname File name
656 * \returns Log target (if found), NULL otherwise
657 */
Harald Welte28222962011-02-18 20:37:04 +0100658struct log_target *log_target_find(int type, const char *fname)
659{
660 struct log_target *tgt;
661
662 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
663 if (tgt->type != type)
664 continue;
665 if (tgt->type == LOG_TGT_TYPE_FILE) {
666 if (!strcmp(fname, tgt->tgt_file.fname))
667 return tgt;
668 } else
669 return tgt;
670 }
671 return NULL;
672}
673
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200674/*! \brief Unregister, close and delete a log target
675 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200676void log_target_destroy(struct log_target *target)
677{
678
679 /* just in case, to make sure we don't have any references */
680 log_del_target(target);
681
682 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200683/* since C89/C99 says stderr is a macro, we can safely do this! */
684#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200685 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200686 if (target->tgt_file.out != stderr)
687#endif
688 {
Harald Welte3086c392010-08-25 19:10:50 +0200689 fclose(target->tgt_file.out);
690 target->tgt_file.out = NULL;
691 }
692 }
693
694 talloc_free(target);
695}
696
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200697/*! \brief close and re-open a log file (for log file rotation)
698 * \param[in] target log target to re-open
699 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200700int log_target_file_reopen(struct log_target *target)
701{
702 fclose(target->tgt_file.out);
703
704 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
705 if (!target->tgt_file.out)
706 return -errno;
707
708 /* we assume target->output already to be set */
709
710 return 0;
711}
712
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200713/*! \brief close and re-open all log files (for log file rotation)
714 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100715int log_targets_reopen(void)
716{
717 struct log_target *tar;
718 int rc = 0;
719
720 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
721 switch (tar->type) {
722 case LOG_TGT_TYPE_FILE:
723 if (log_target_file_reopen(tar) < 0)
724 rc = -1;
725 break;
726 default:
727 break;
728 }
729 }
730
731 return rc;
732}
733
Harald Welte18fc4652011-08-17 14:14:17 +0200734/*! \brief Generates the logging command string for VTY
735 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200736 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200737 */
Harald Weltece9fec32011-06-27 14:19:16 +0200738const char *log_vty_command_string(const struct log_info *unused_info)
Harald Welte7638af92010-05-11 16:39:22 +0200739{
Harald Weltece9fec32011-06-27 14:19:16 +0200740 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100741 int len = 0, offset = 0, ret, i, rem;
742 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200743 char *str;
744
Harald Welteb43bc042011-06-27 10:29:17 +0200745 for (i = 0; i < info->num_cat; i++) {
746 if (info->cat[i].name == NULL)
747 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100748 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200749 }
Harald Welte7638af92010-05-11 16:39:22 +0200750
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100751 for (i = 0; i < LOGLEVEL_DEFS; i++)
752 size += strlen(loglevel_strs[i].str) + 1;
753
754 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200755 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200756 if (!str)
757 return NULL;
758
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200759 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100760 if (ret < 0)
761 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200762 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200763
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100764 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200765 if (info->cat[i].name) {
766 int j, name_len = strlen(info->cat[i].name)+1;
767 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100768
Harald Welteb43bc042011-06-27 10:29:17 +0200769 for (j = 0; j < name_len; j++)
770 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100771
Harald Welteb43bc042011-06-27 10:29:17 +0200772 name[name_len-1] = '\0';
773 ret = snprintf(str + offset, rem, "%s|", name+1);
774 if (ret < 0)
775 goto err;
776 OSMO_SNPRINTF_RET(ret, rem, offset, len);
777 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100778 }
779 offset--; /* to remove the trailing | */
780 rem++;
781
782 ret = snprintf(str + offset, rem, ") (");
783 if (ret < 0)
784 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200785 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100786
787 for (i = 0; i < LOGLEVEL_DEFS; i++) {
788 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
789 char loglevel_str[loglevel_str_len];
790
791 for (j = 0; j < loglevel_str_len; j++)
792 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
793
794 loglevel_str[loglevel_str_len-1] = '\0';
795 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
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 offset--; /* to remove the trailing | */
801 rem++;
802
803 ret = snprintf(str + offset, rem, ")");
804 if (ret < 0)
805 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200806 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100807err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200808 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100809 return str;
810}
811
Harald Welte18fc4652011-08-17 14:14:17 +0200812/*! \brief Generates the logging command description for VTY
813 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200814 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200815 */
Harald Weltece9fec32011-06-27 14:19:16 +0200816const char *log_vty_command_description(const struct log_info *unused_info)
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100817{
Harald Weltece9fec32011-06-27 14:19:16 +0200818 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100819 char *str;
820 int i, ret, len = 0, offset = 0, rem;
821 unsigned int size =
822 strlen(LOGGING_STR
823 "Set the log level for a specified category\n") + 1;
824
Harald Welteb43bc042011-06-27 10:29:17 +0200825 for (i = 0; i < info->num_cat; i++) {
826 if (info->cat[i].name == NULL)
827 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100828 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200829 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100830
831 for (i = 0; i < LOGLEVEL_DEFS; i++)
832 size += strlen(loglevel_descriptions[i]) + 1;
833
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200834 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100835 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200836 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100837 if (!str)
838 return NULL;
839
840 ret = snprintf(str + offset, rem, LOGGING_STR
841 "Set the log level for a specified category\n");
842 if (ret < 0)
843 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200844 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100845
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200846 ret = snprintf(str + offset, rem,
847 "Global setting for all subsystems\n");
848 if (ret < 0)
849 goto err;
850 OSMO_SNPRINTF_RET(ret, rem, offset, len);
851
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100852 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200853 if (info->cat[i].name == NULL)
854 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100855 ret = snprintf(str + offset, rem, "%s\n",
856 info->cat[i].description);
857 if (ret < 0)
858 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200859 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100860 }
861 for (i = 0; i < LOGLEVEL_DEFS; i++) {
862 ret = snprintf(str + offset, rem, "%s\n",
863 loglevel_descriptions[i]);
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 }
868err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200869 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200870 return str;
871}
872
Harald Welte18fc4652011-08-17 14:14:17 +0200873/*! \brief Initialize the Osmocom logging core
874 * \param[in] inf Information regarding logging categories
875 * \param[in] ctx \ref talloc context for logging allocations
876 * \returns 0 in case of success, negative in case of error
877 */
Harald Welteb43bc042011-06-27 10:29:17 +0200878int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800879{
Harald Welteb43bc042011-06-27 10:29:17 +0200880 int i;
881
882 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
883 if (!tall_log_ctx)
884 return -ENOMEM;
885
886 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
887 if (!osmo_log_info)
888 return -ENOMEM;
889
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100890 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200891 osmo_log_info->num_cat_user = inf->num_cat;
892 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200893 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200894
895 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
896 struct log_info_cat,
897 osmo_log_info->num_cat);
898 if (!osmo_log_info->cat) {
899 talloc_free(osmo_log_info);
900 osmo_log_info = NULL;
901 return -ENOMEM;
902 }
903
904 /* copy over the user part */
905 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200906 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
907 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200908 sizeof(struct log_info_cat));
909 }
910
911 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200912 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200913 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200914 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200915 &internal_cat[i], sizeof(struct log_info_cat));
916 }
917
918 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800919}
Harald Welte18fc4652011-08-17 14:14:17 +0200920
Harald Welte69e6c3c2016-04-20 10:41:27 +0200921/* \brief De-initialize the Osmocom logging core
922 * This function destroys all targets and releases associated memory */
923void log_fini(void)
924{
925 struct log_target *tar, *tar2;
926
927 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
928 log_target_destroy(tar);
929
930 talloc_free(osmo_log_info);
931 osmo_log_info = NULL;
932 talloc_free(tall_log_ctx);
933 tall_log_ctx = NULL;
934}
935
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100936/*! \brief Check whether a log entry will be generated.
937 * \returns != 0 if a log entry might get generated by at least one target */
938int log_check_level(int subsys, unsigned int level)
939{
940 struct log_target *tar;
941
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100942 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100943
944 /* TODO: The following could/should be cached (update on config) */
945
946 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100947 if (!check_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100948 continue;
949
950 /* This might get logged (ignoring filters) */
951 return 1;
952 }
953
954 /* We are sure, that this will not be logged. */
955 return 0;
956}
957
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200958/*! @} */