blob: 1516a7d8d33756e723de41d4d2939e818155aaf5 [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
Harald Welteaa00f992016-12-02 15:30:02 +0100243const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100244{
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) {
Harald Welteaa00f992016-12-02 15:30:02 +0100293 ret = snprintf(buf + offset, rem, "%s ", log_category_name(subsys));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100294 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 Welted7c0a372016-12-02 13:52:59 +0100388 if (tar->raw_output)
389 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
390 else
391 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200392 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800393 }
394}
395
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200396/*! \brief logging function used by DEBUGP() macro
397 * \param[in] subsys Logging sub-system
398 * \param[in] file name of source code file
399 * \param[in] cont continuation (1) or new line (0)
400 * \param[in] format format string
401 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200402void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800403 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800404{
405 va_list ap;
406
407 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200408 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800409 va_end(ap);
410}
411
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200412/*! \brief logging function used by LOGP() macro
413 * \param[in] subsys Logging sub-system
414 * \param[in] level Log level
415 * \param[in] file name of source code file
416 * \param[in] cont continuation (1) or new line (0)
417 * \param[in] format format string
418 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200419void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800420{
421 va_list ap;
422
423 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200424 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800425 va_end(ap);
426}
427
Harald Welte18fc4652011-08-17 14:14:17 +0200428/*! \brief Register a new log target with the logging core
429 * \param[in] target Log target to be registered
430 */
Harald Welte3ae27582010-03-26 21:24:24 +0800431void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800432{
Harald Welte28222962011-02-18 20:37:04 +0100433 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800434}
435
Harald Welte18fc4652011-08-17 14:14:17 +0200436/*! \brief Unregister a log target from the logging core
437 * \param[in] target Log target to be unregistered
438 */
Harald Welte3ae27582010-03-26 21:24:24 +0800439void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800440{
441 llist_del(&target->entry);
442}
443
Harald Welte18fc4652011-08-17 14:14:17 +0200444/*! \brief Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800445void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800446{
Harald Welte3ae27582010-03-26 21:24:24 +0800447 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800448}
449
Harald Welte18fc4652011-08-17 14:14:17 +0200450/*! \brief Set the logging context
451 * \param[in] ctx_nr logging context number
452 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200453 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200454 *
455 * A logging context is something like the subscriber identity to which
456 * the currently processed message relates, or the BTS through which it
457 * was received. As soon as this data is known, it can be set using
458 * this function. The main use of context information is for logging
459 * filters.
460 */
Harald Welte3ae27582010-03-26 21:24:24 +0800461int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800462{
Harald Welte3ae27582010-03-26 21:24:24 +0800463 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800464 return -EINVAL;
465
Harald Welte3ae27582010-03-26 21:24:24 +0800466 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800467
468 return 0;
469}
470
Harald Welte18fc4652011-08-17 14:14:17 +0200471/*! \brief Enable the \ref LOG_FILTER_ALL log filter
472 * \param[in] target Log target to be affected
473 * \param[in] all enable (1) or disable (0) the ALL filter
474 *
475 * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will
476 * be printed. It acts as a wildcard. Setting it to \a 1 means there
477 * is no filtering.
478 */
Harald Welte3ae27582010-03-26 21:24:24 +0800479void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800480{
481 if (all)
Harald Welte3ae27582010-03-26 21:24:24 +0800482 target->filter_map |= LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800483 else
Harald Welte3ae27582010-03-26 21:24:24 +0800484 target->filter_map &= ~LOG_FILTER_ALL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800485}
486
Harald Welte18fc4652011-08-17 14:14:17 +0200487/*! \brief Enable or disable the use of colored output
488 * \param[in] target Log target to be affected
489 * \param[in] use_color Use color (1) or don't use color (0)
490 */
Harald Welte3ae27582010-03-26 21:24:24 +0800491void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800492{
493 target->use_color = use_color;
494}
495
Harald Welte18fc4652011-08-17 14:14:17 +0200496/*! \brief Enable or disable printing of timestamps while logging
497 * \param[in] target Log target to be affected
498 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
499 */
Harald Welte3ae27582010-03-26 21:24:24 +0800500void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800501{
502 target->print_timestamp = print_timestamp;
503}
504
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100505/*! \brief Enable or disable printing of extended timestamps while logging
506 * \param[in] target Log target to be affected
507 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
508 *
509 * When both timestamp and extended timestamp is enabled then only
510 * the extended timestamp will be used. The format of the timestamp
511 * is YYYYMMDDhhmmssnnn.
512 */
513void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
514{
515 target->print_ext_timestamp = print_timestamp;
516}
517
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200518/*! \brief Enable or disable printing of the filename while logging
519 * \param[in] target Log target to be affected
520 * \param[in] print_filename Enable (1) or disable (0) filenames
521 */
522void log_set_print_filename(struct log_target *target, int print_filename)
523{
524 target->print_filename = print_filename;
525}
526
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100527/*! \brief Enable or disable printing of the category name
528 * \param[in] target Log target to be affected
529 * \param[in] print_catname Enable (1) or disable (0) filenames
530 *
531 * Print the category/subsys name in front of every log message.
532 */
533void log_set_print_category(struct log_target *target, int print_category)
534{
535 target->print_category = print_category;
536}
537
Harald Welte18fc4652011-08-17 14:14:17 +0200538/*! \brief Set the global log level for a given log target
539 * \param[in] target Log target to be affected
540 * \param[in] log_level New global log level
541 */
Harald Welte3ae27582010-03-26 21:24:24 +0800542void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800543{
544 target->loglevel = log_level;
545}
546
Harald Weltede6e4982012-12-06 21:25:27 +0100547/*! \brief Set a category filter on a given log target
548 * \param[in] target Log target to be affected
549 * \param[in] category Log category to be affected
550 * \param[in] enable whether to enable or disable the filter
551 * \param[in] level Log level of the filter
552 */
Harald Welte3ae27582010-03-26 21:24:24 +0800553void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800554 int enable, int level)
555{
Harald Welte4ebdf742010-05-19 19:54:00 +0200556 if (category >= osmo_log_info->num_cat)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800557 return;
558 target->categories[category].enabled = !!enable;
559 target->categories[category].loglevel = level;
560}
561
Harald Welte76e72ab2011-02-17 15:52:39 +0100562static void _file_output(struct log_target *target, unsigned int level,
563 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800564{
Harald Welte0083cd32010-08-25 14:55:44 +0200565 fprintf(target->tgt_file.out, "%s", log);
566 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800567}
568
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200569/*! \brief Create a new log target skeleton
570 * \returns dynamically-allocated log target
571 * This funcition allocates a \ref log_target and initializes it
572 * with some default values. The newly created target is not
573 * registered yet.
574 */
Harald Welte3ae27582010-03-26 21:24:24 +0800575struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800576{
Harald Welte3ae27582010-03-26 21:24:24 +0800577 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800578 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800579
Harald Welte3ae27582010-03-26 21:24:24 +0800580 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800581 if (!target)
582 return NULL;
583
Harald Welteb43bc042011-06-27 10:29:17 +0200584 target->categories = talloc_zero_array(target,
585 struct log_category,
586 osmo_log_info->num_cat);
587 if (!target->categories) {
588 talloc_free(target);
589 return NULL;
590 }
591
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800592 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800593
594 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200595 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800596 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200597 cat->enabled = osmo_log_info->cat[i].enabled;
598 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800599 }
600
601 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800602 target->use_color = 1;
603 target->print_timestamp = 0;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200604 target->print_filename = 1;
Harald Weltecc6313c2010-03-26 22:04:03 +0800605
606 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800607 target->loglevel = 0;
608 return target;
609}
610
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200611/*! \brief Create the STDERR log target
612 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800613struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800614{
Harald Weltea3b844c2010-03-27 00:04:40 +0800615/* since C89/C99 says stderr is a macro, we can safely do this! */
616#ifdef stderr
Harald Welte3ae27582010-03-26 21:24:24 +0800617 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800618
Harald Welte3ae27582010-03-26 21:24:24 +0800619 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800620 if (!target)
621 return NULL;
622
Harald Welte28222962011-02-18 20:37:04 +0100623 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200624 target->tgt_file.out = stderr;
625 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800626 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800627#else
628 return NULL;
629#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800630}
631
Harald Welte18fc4652011-08-17 14:14:17 +0200632/*! \brief Create a new file-based log target
633 * \param[in] fname File name of the new log file
634 * \returns Log target in case of success, NULL otherwise
635 */
Harald Welte3086c392010-08-25 19:10:50 +0200636struct log_target *log_target_create_file(const char *fname)
637{
638 struct log_target *target;
639
640 target = log_target_create();
641 if (!target)
642 return NULL;
643
Harald Welte28222962011-02-18 20:37:04 +0100644 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200645 target->tgt_file.out = fopen(fname, "a");
646 if (!target->tgt_file.out)
647 return NULL;
648
649 target->output = _file_output;
650
651 target->tgt_file.fname = talloc_strdup(target, fname);
652
653 return target;
654}
655
Harald Welte18fc4652011-08-17 14:14:17 +0200656/*! \brief Find a registered log target
657 * \param[in] type Log target type
658 * \param[in] fname File name
659 * \returns Log target (if found), NULL otherwise
660 */
Harald Welte28222962011-02-18 20:37:04 +0100661struct log_target *log_target_find(int type, const char *fname)
662{
663 struct log_target *tgt;
664
665 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
666 if (tgt->type != type)
667 continue;
668 if (tgt->type == LOG_TGT_TYPE_FILE) {
669 if (!strcmp(fname, tgt->tgt_file.fname))
670 return tgt;
671 } else
672 return tgt;
673 }
674 return NULL;
675}
676
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200677/*! \brief Unregister, close and delete a log target
678 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200679void log_target_destroy(struct log_target *target)
680{
681
682 /* just in case, to make sure we don't have any references */
683 log_del_target(target);
684
685 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200686/* since C89/C99 says stderr is a macro, we can safely do this! */
687#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200688 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200689 if (target->tgt_file.out != stderr)
690#endif
691 {
Harald Welte3086c392010-08-25 19:10:50 +0200692 fclose(target->tgt_file.out);
693 target->tgt_file.out = NULL;
694 }
695 }
696
697 talloc_free(target);
698}
699
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200700/*! \brief close and re-open a log file (for log file rotation)
701 * \param[in] target log target to re-open
702 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200703int log_target_file_reopen(struct log_target *target)
704{
705 fclose(target->tgt_file.out);
706
707 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
708 if (!target->tgt_file.out)
709 return -errno;
710
711 /* we assume target->output already to be set */
712
713 return 0;
714}
715
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200716/*! \brief close and re-open all log files (for log file rotation)
717 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100718int log_targets_reopen(void)
719{
720 struct log_target *tar;
721 int rc = 0;
722
723 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
724 switch (tar->type) {
725 case LOG_TGT_TYPE_FILE:
726 if (log_target_file_reopen(tar) < 0)
727 rc = -1;
728 break;
729 default:
730 break;
731 }
732 }
733
734 return rc;
735}
736
Harald Welte18fc4652011-08-17 14:14:17 +0200737/*! \brief Generates the logging command string for VTY
738 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200739 * \returns vty command string for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200740 */
Harald Weltece9fec32011-06-27 14:19:16 +0200741const char *log_vty_command_string(const struct log_info *unused_info)
Harald Welte7638af92010-05-11 16:39:22 +0200742{
Harald Weltece9fec32011-06-27 14:19:16 +0200743 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100744 int len = 0, offset = 0, ret, i, rem;
745 int size = strlen("logging level () ()") + 1;
Harald Welte7638af92010-05-11 16:39:22 +0200746 char *str;
747
Harald Welteb43bc042011-06-27 10:29:17 +0200748 for (i = 0; i < info->num_cat; i++) {
749 if (info->cat[i].name == NULL)
750 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100751 size += strlen(info->cat[i].name) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200752 }
Harald Welte7638af92010-05-11 16:39:22 +0200753
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100754 for (i = 0; i < LOGLEVEL_DEFS; i++)
755 size += strlen(loglevel_strs[i].str) + 1;
756
757 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200758 str = talloc_zero_size(tall_log_ctx, size);
Harald Welte7638af92010-05-11 16:39:22 +0200759 if (!str)
760 return NULL;
761
Holger Hans Peter Freyther952a18e2011-03-29 17:03:56 +0200762 ret = snprintf(str + offset, rem, "logging level (all|");
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100763 if (ret < 0)
764 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200765 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte7638af92010-05-11 16:39:22 +0200766
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100767 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200768 if (info->cat[i].name) {
769 int j, name_len = strlen(info->cat[i].name)+1;
770 char name[name_len];
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100771
Harald Welteb43bc042011-06-27 10:29:17 +0200772 for (j = 0; j < name_len; j++)
773 name[j] = tolower(info->cat[i].name[j]);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100774
Harald Welteb43bc042011-06-27 10:29:17 +0200775 name[name_len-1] = '\0';
776 ret = snprintf(str + offset, rem, "%s|", name+1);
777 if (ret < 0)
778 goto err;
779 OSMO_SNPRINTF_RET(ret, rem, offset, len);
780 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100781 }
782 offset--; /* to remove the trailing | */
783 rem++;
784
785 ret = snprintf(str + offset, rem, ") (");
786 if (ret < 0)
787 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200788 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100789
790 for (i = 0; i < LOGLEVEL_DEFS; i++) {
791 int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1;
792 char loglevel_str[loglevel_str_len];
793
794 for (j = 0; j < loglevel_str_len; j++)
795 loglevel_str[j] = tolower(loglevel_strs[i].str[j]);
796
797 loglevel_str[loglevel_str_len-1] = '\0';
798 ret = snprintf(str + offset, rem, "%s|", loglevel_str);
799 if (ret < 0)
800 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200801 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100802 }
803 offset--; /* to remove the trailing | */
804 rem++;
805
806 ret = snprintf(str + offset, rem, ")");
807 if (ret < 0)
808 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200809 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100810err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200811 str[size-1] = '\0';
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100812 return str;
813}
814
Harald Welte18fc4652011-08-17 14:14:17 +0200815/*! \brief Generates the logging command description for VTY
816 * \param[in] unused_info Deprecated parameter, no longer used!
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200817 * \returns logging command description for use by VTY command node
Harald Welte18fc4652011-08-17 14:14:17 +0200818 */
Harald Weltece9fec32011-06-27 14:19:16 +0200819const char *log_vty_command_description(const struct log_info *unused_info)
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100820{
Harald Weltece9fec32011-06-27 14:19:16 +0200821 struct log_info *info = osmo_log_info;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100822 char *str;
823 int i, ret, len = 0, offset = 0, rem;
824 unsigned int size =
825 strlen(LOGGING_STR
826 "Set the log level for a specified category\n") + 1;
827
Harald Welteb43bc042011-06-27 10:29:17 +0200828 for (i = 0; i < info->num_cat; i++) {
829 if (info->cat[i].name == NULL)
830 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100831 size += strlen(info->cat[i].description) + 1;
Harald Welteb43bc042011-06-27 10:29:17 +0200832 }
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100833
834 for (i = 0; i < LOGLEVEL_DEFS; i++)
835 size += strlen(loglevel_descriptions[i]) + 1;
836
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200837 size += strlen("Global setting for all subsystems") + 1;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100838 rem = size;
Pablo Neira Ayusof1fae4d2011-05-03 22:32:42 +0200839 str = talloc_zero_size(tall_log_ctx, size);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100840 if (!str)
841 return NULL;
842
843 ret = snprintf(str + offset, rem, LOGGING_STR
844 "Set the log level for a specified category\n");
845 if (ret < 0)
846 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200847 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100848
Pablo Neira Ayusod6b51952011-05-03 22:32:32 +0200849 ret = snprintf(str + offset, rem,
850 "Global setting for all subsystems\n");
851 if (ret < 0)
852 goto err;
853 OSMO_SNPRINTF_RET(ret, rem, offset, len);
854
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100855 for (i = 0; i < info->num_cat; i++) {
Harald Welteb43bc042011-06-27 10:29:17 +0200856 if (info->cat[i].name == NULL)
857 continue;
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100858 ret = snprintf(str + offset, rem, "%s\n",
859 info->cat[i].description);
860 if (ret < 0)
861 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200862 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100863 }
864 for (i = 0; i < LOGLEVEL_DEFS; i++) {
865 ret = snprintf(str + offset, rem, "%s\n",
866 loglevel_descriptions[i]);
867 if (ret < 0)
868 goto err;
Pablo Neira Ayuso3abad6a2011-03-28 19:24:22 +0200869 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100870 }
871err:
Pablo Neira Ayuso534ba812011-05-03 22:32:48 +0200872 str[size-1] = '\0';
Harald Welte7638af92010-05-11 16:39:22 +0200873 return str;
874}
875
Harald Welte18fc4652011-08-17 14:14:17 +0200876/*! \brief Initialize the Osmocom logging core
877 * \param[in] inf Information regarding logging categories
878 * \param[in] ctx \ref talloc context for logging allocations
879 * \returns 0 in case of success, negative in case of error
880 */
Harald Welteb43bc042011-06-27 10:29:17 +0200881int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800882{
Harald Welteb43bc042011-06-27 10:29:17 +0200883 int i;
884
885 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
886 if (!tall_log_ctx)
887 return -ENOMEM;
888
889 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
890 if (!osmo_log_info)
891 return -ENOMEM;
892
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100893 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200894 osmo_log_info->num_cat_user = inf->num_cat;
895 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200896 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200897
898 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
899 struct log_info_cat,
900 osmo_log_info->num_cat);
901 if (!osmo_log_info->cat) {
902 talloc_free(osmo_log_info);
903 osmo_log_info = NULL;
904 return -ENOMEM;
905 }
906
907 /* copy over the user part */
908 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200909 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
910 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200911 sizeof(struct log_info_cat));
912 }
913
914 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200915 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200916 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200917 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200918 &internal_cat[i], sizeof(struct log_info_cat));
919 }
920
921 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800922}
Harald Welte18fc4652011-08-17 14:14:17 +0200923
Harald Welte69e6c3c2016-04-20 10:41:27 +0200924/* \brief De-initialize the Osmocom logging core
925 * This function destroys all targets and releases associated memory */
926void log_fini(void)
927{
928 struct log_target *tar, *tar2;
929
930 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
931 log_target_destroy(tar);
932
933 talloc_free(osmo_log_info);
934 osmo_log_info = NULL;
935 talloc_free(tall_log_ctx);
936 tall_log_ctx = NULL;
937}
938
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100939/*! \brief Check whether a log entry will be generated.
940 * \returns != 0 if a log entry might get generated by at least one target */
941int log_check_level(int subsys, unsigned int level)
942{
943 struct log_target *tar;
944
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100945 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100946
947 /* TODO: The following could/should be cached (update on config) */
948
949 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100950 if (!check_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100951 continue;
952
953 /* This might get logged (ignoring filters) */
954 return 1;
955 }
956
957 /* We are sure, that this will not be logged. */
958 return 0;
959}
960
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200961/*! @} */