blob: 7c2d61ffde78e3499144adf3bb048340c4ef6a4f [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file logging.c
2 * Debugging/Logging support code. */
3/*
4 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte4a2bb9e2010-03-26 09:33:40 +08005 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +080010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Harald Weltef01b2382017-10-16 13:55:34 +020026/*! \addtogroup logging
Harald Welte18fc4652011-08-17 14:14:17 +020027 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 * libosmocore Logging sub-system
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020029 *
30 * \file logging.c */
Harald Welte18fc4652011-08-17 14:14:17 +020031
Harald Welte01fd5cb2010-03-26 23:51:31 +080032#include "../config.h"
33
Harald Welte4a2bb9e2010-03-26 09:33:40 +080034#include <stdarg.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080038
39#ifdef HAVE_STRINGS_H
Harald Welte4a2bb9e2010-03-26 09:33:40 +080040#include <strings.h>
Harald Welte01fd5cb2010-03-26 23:51:31 +080041#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +080042#include <time.h>
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +010043#include <sys/time.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080044#include <errno.h>
45
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010046#include <osmocom/core/talloc.h>
47#include <osmocom/core/utils.h>
48#include <osmocom/core/logging.h>
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +020049#include <osmocom/core/timer.h>
Harald Welte4a2bb9e2010-03-26 09:33:40 +080050
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +010051#include <osmocom/vty/logging.h> /* for LOGGING_STR. */
52
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010053osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010054 enum_logging_ctx_items_fit_in_struct_log_context);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010055osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010056 enum_logging_filters_fit_in_log_target_filter_data);
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +010057osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map),
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +010058 enum_logging_filters_fit_in_log_target_filter_map);
59
Harald Welteb43bc042011-06-27 10:29:17 +020060struct log_info *osmo_log_info;
Harald Welte4a2bb9e2010-03-26 09:33:40 +080061
Harald Welte3ae27582010-03-26 21:24:24 +080062static struct log_context log_context;
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020063void *tall_log_ctx = NULL;
Harald Welte28222962011-02-18 20:37:04 +010064LLIST_HEAD(osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +080065
Neels Hofmeyrba0762d2018-09-10 13:56:03 +020066static const struct value_string loglevel_strs[] = {
Harald Welte9b186702013-03-19 09:55:28 +010067 { 0, "EVERYTHING" },
Harald Welte4a2bb9e2010-03-26 09:33:40 +080068 { LOGL_DEBUG, "DEBUG" },
69 { LOGL_INFO, "INFO" },
70 { LOGL_NOTICE, "NOTICE" },
71 { LOGL_ERROR, "ERROR" },
72 { LOGL_FATAL, "FATAL" },
73 { 0, NULL },
74};
75
Harald Welteb43bc042011-06-27 10:29:17 +020076#define INT2IDX(x) (-1*(x)-1)
77static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
78 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
79 .name = "DLGLOBAL",
80 .description = "Library-internal global log family",
81 .loglevel = LOGL_NOTICE,
82 .enabled = 1,
83 },
root8a996b42011-09-26 11:22:21 +020084 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
85 .name = "DLLAPD",
86 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020087 .loglevel = LOGL_NOTICE,
88 .enabled = 1,
89 },
Harald Welte892e6212011-07-19 14:31:44 +020090 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020091 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020092 .description = "A-bis Intput Subsystem",
93 .loglevel = LOGL_NOTICE,
94 .enabled = 1,
95 },
Harald Welte892e6212011-07-19 14:31:44 +020096 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +020097 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020098 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
99 .loglevel = LOGL_NOTICE,
100 .enabled = 1,
101 },
Harald Welte892e6212011-07-19 14:31:44 +0200102 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200103 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200104 .description = "A-bis Input Driver for Signalling",
105 .enabled = 0, .loglevel = LOGL_NOTICE,
106 },
Harald Welte892e6212011-07-19 14:31:44 +0200107 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200108 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200109 .description = "A-bis Input Driver for B-Channels (voice)",
110 .enabled = 0, .loglevel = LOGL_NOTICE,
111 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200112 [INT2IDX(DLSMS)] = {
113 .name = "DLSMS",
114 .description = "Layer3 Short Message Service (SMS)",
115 .enabled = 1, .loglevel = LOGL_NOTICE,
116 .color = "\033[1;38m",
117 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200118 [INT2IDX(DLCTRL)] = {
119 .name = "DLCTRL",
120 .description = "Control Interface",
121 .enabled = 1, .loglevel = LOGL_NOTICE,
122 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100123 [INT2IDX(DLGTP)] = {
124 .name = "DLGTP",
125 .description = "GPRS GTP library",
126 .enabled = 1, .loglevel = LOGL_NOTICE,
127 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100128 [INT2IDX(DLSTATS)] = {
129 .name = "DLSTATS",
130 .description = "Statistics messages and logging",
131 .enabled = 1, .loglevel = LOGL_NOTICE,
132 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100133 [INT2IDX(DLGSUP)] = {
134 .name = "DLGSUP",
135 .description = "Generic Subscriber Update Protocol",
136 .enabled = 1, .loglevel = LOGL_NOTICE,
137 },
Harald Weltec0f00072016-04-27 18:32:35 +0200138 [INT2IDX(DLOAP)] = {
139 .name = "DLOAP",
140 .description = "Osmocom Authentication Protocol",
141 .enabled = 1, .loglevel = LOGL_NOTICE,
142 },
Harald Welte059c4042017-04-03 22:20:49 +0200143 [INT2IDX(DLSS7)] = {
144 .name = "DLSS7",
145 .description = "libosmo-sigtran Signalling System 7",
146 .enabled = 1, .loglevel = LOGL_NOTICE,
147 },
148 [INT2IDX(DLSCCP)] = {
149 .name = "DLSCCP",
150 .description = "libosmo-sigtran SCCP Implementation",
151 .enabled = 1, .loglevel = LOGL_NOTICE,
152 },
153 [INT2IDX(DLSUA)] = {
154 .name = "DLSUA",
155 .description = "libosmo-sigtran SCCP User Adaptation",
156 .enabled = 1, .loglevel = LOGL_NOTICE,
157 },
158 [INT2IDX(DLM3UA)] = {
159 .name = "DLM3UA",
160 .description = "libosmo-sigtran MTP3 User Adaptation",
161 .enabled = 1, .loglevel = LOGL_NOTICE,
162 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200163 [INT2IDX(DLMGCP)] = {
164 .name = "DLMGCP",
165 .description = "libosmo-mgcp Media Gateway Control Protocol",
166 .enabled = 1, .loglevel = LOGL_NOTICE,
167 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100168 [INT2IDX(DLJIBUF)] = {
169 .name = "DLJIBUF",
170 .description = "libosmo-netif Jitter Buffer",
171 .enabled = 1, .loglevel = LOGL_NOTICE,
172 },
Harald Welteb43bc042011-06-27 10:29:17 +0200173};
174
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200175void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100176{
177 if (!osmo_log_info) {
178 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100179 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100180 OSMO_ASSERT(osmo_log_info);
181 }
182}
183
Harald Welteb43bc042011-06-27 10:29:17 +0200184/* special magic for negative (library-internal) log subsystem numbers */
185static int subsys_lib2index(int subsys)
186{
187 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
188}
189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190/*! Parse a human-readable log level into a numeric value
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200191 * \param lvl[in] zero-terminated string containing log level name
192 * \returns numeric log level
193 */
Harald Welte3ae27582010-03-26 21:24:24 +0800194int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800195{
196 return get_string_value(loglevel_strs, lvl);
197}
198
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200199/*! convert a numeric log level into human-readable string
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200200 * \param lvl[in] numeric log level
201 * \returns zero-terminated string (log level name)
202 */
Harald Welte9ac22252010-05-11 11:19:40 +0200203const char *log_level_str(unsigned int lvl)
204{
205 return get_value_string(loglevel_strs, lvl);
206}
207
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200209 * \param[in] category human-readable log category name
210 * \returns numeric category value, or -EINVAL otherwise
211 */
Harald Welte3ae27582010-03-26 21:24:24 +0800212int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800213{
214 int i;
215
Max68bf16a2018-01-10 17:00:43 +0100216 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100217
Harald Welte4ebdf742010-05-19 19:54:00 +0200218 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200219 if (osmo_log_info->cat[i].name == NULL)
220 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200221 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800222 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800223 }
224
225 return -EINVAL;
226}
227
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200228/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200229 * \param[in] target log target to be configured
230 * \param[in] _mask log category mask string
231 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800232 * The format can be this: category1:category2:category3
233 * or category1,2:category2,3:...
234 */
Harald Welte3ae27582010-03-26 21:24:24 +0800235void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800236{
237 int i = 0;
238 char *mask = strdup(_mask);
239 char *category_token = NULL;
240
Max68bf16a2018-01-10 17:00:43 +0100241 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100242
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800243 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200244 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800245 target->categories[i].enabled = 0;
246
247 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200248 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800249 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200250 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200251 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800252 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200253
254 if (!osmo_log_info->cat[i].name)
255 continue;
256
257 length = strlen(category_token);
258 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200259
260 /* Use longest length not to match subocurrences. */
261 if (cat_length > length)
262 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800263
264 if (colon)
265 length = colon - category_token;
266
Harald Welte4ebdf742010-05-19 19:54:00 +0200267 if (strncasecmp(osmo_log_info->cat[i].name,
268 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800269 int level = 0;
270
271 if (colon)
272 level = atoi(colon+1);
273
Harald Weltefaadfe22010-03-26 21:05:43 +0800274 target->categories[i].enabled = 1;
275 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800276 }
277 }
278 } while ((category_token = strtok(NULL, ":")));
279
280 free(mask);
281}
282
283static const char* color(int subsys)
284{
Harald Welte4ebdf742010-05-19 19:54:00 +0200285 if (subsys < osmo_log_info->num_cat)
286 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800287
Harald Welted788f662010-03-26 09:45:03 +0800288 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800289}
290
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100291static const struct value_string level_colors[] = {
292 { LOGL_DEBUG, "\033[1;34m" },
293 { LOGL_INFO, "\033[1;32m" },
294 { LOGL_NOTICE, "\033[1;33m" },
295 { LOGL_ERROR, "\033[1;31m" },
296 { LOGL_FATAL, "\033[1;31m" },
297 { 0, NULL }
298};
299
300static const char *level_color(int level)
301{
302 const char *c = get_value_string_or_null(level_colors, level);
303 if (!c)
304 return get_value_string(level_colors, LOGL_FATAL);
305 return c;
306}
307
Harald Welteaa00f992016-12-02 15:30:02 +0100308const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100309{
310 if (subsys < osmo_log_info->num_cat)
311 return osmo_log_info->cat[subsys].name;
312
313 return NULL;
314}
315
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100316static const char *const_basename(const char *path)
317{
318 const char *bn = strrchr(path, '/');
319 if (!bn || !bn[1])
320 return path;
321 return bn + 1;
322}
323
Harald Welte3ae27582010-03-26 21:24:24 +0800324static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200325 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100326 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800327{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800328 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200329 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100330 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800331
332 /* are we using color */
333 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100334 c_subsys = color(subsys);
335 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100336 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200337 if (ret < 0)
338 goto err;
339 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800340 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800341 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800342 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100343 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200344#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100345 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100346 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200347 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100348 localtime_r(&tv.tv_sec, &tm);
349 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100350 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100351 tm.tm_hour, tm.tm_min, tm.tm_sec,
352 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100353 if (ret < 0)
354 goto err;
355 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200356#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100357 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800358 char *timestr;
359 time_t tm;
360 tm = time(NULL);
361 timestr = ctime(&tm);
362 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200363 ret = snprintf(buf + offset, rem, "%s ", timestr);
364 if (ret < 0)
365 goto err;
366 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800367 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100368 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100369 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
370 target->use_color ? level_color(level) : "",
371 log_category_name(subsys),
372 target->use_color ? "\033[0;m" : "",
373 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100374 if (ret < 0)
375 goto err;
376 OSMO_SNPRINTF_RET(ret, rem, offset, len);
377 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100378 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100379 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
380 target->use_color ? level_color(level) : "",
381 log_level_str(level),
382 target->use_color ? "\033[0;m" : "",
383 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100384 if (ret < 0)
385 goto err;
386 OSMO_SNPRINTF_RET(ret, rem, offset, len);
387 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100388 if (target->print_category_hex) {
389 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200390 if (ret < 0)
391 goto err;
392 OSMO_SNPRINTF_RET(ret, rem, offset, len);
393 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200394
395 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
396 switch (target->print_filename2) {
397 case LOG_FILENAME_NONE:
398 break;
399 case LOG_FILENAME_PATH:
400 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
401 if (ret < 0)
402 goto err;
403 OSMO_SNPRINTF_RET(ret, rem, offset, len);
404 break;
405 case LOG_FILENAME_BASENAME:
406 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
407 if (ret < 0)
408 goto err;
409 OSMO_SNPRINTF_RET(ret, rem, offset, len);
410 break;
411 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100412 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800413 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200414 ret = vsnprintf(buf + offset, rem, format, ap);
415 if (ret < 0)
416 goto err;
417 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800418
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200419 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
420 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
421 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
422 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
423 && offset > 0 && buf[offset-1] == '\n') {
424 switch (target->print_filename2) {
425 case LOG_FILENAME_NONE:
426 break;
427 case LOG_FILENAME_PATH:
428 offset --;
429 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
430 if (ret < 0)
431 goto err;
432 OSMO_SNPRINTF_RET(ret, rem, offset, len);
433 break;
434 case LOG_FILENAME_BASENAME:
435 offset --;
436 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
437 if (ret < 0)
438 goto err;
439 OSMO_SNPRINTF_RET(ret, rem, offset, len);
440 break;
441 }
442 }
443
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100444 if (target->use_color) {
445 ret = snprintf(buf + offset, rem, "\033[0;m");
446 if (ret < 0)
447 goto err;
448 OSMO_SNPRINTF_RET(ret, rem, offset, len);
449 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200450err:
451 buf[sizeof(buf)-1] = '\0';
452 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800453}
454
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100455/* Catch internal logging category indexes as well as out-of-bounds indexes.
456 * For internal categories, the ID is negative starting with -1; and internal
457 * logging categories are added behind the user categories. For out-of-bounds
458 * indexes, return the index of DLGLOBAL. The returned category index is
459 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
460 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100461static inline int map_subsys(int subsys)
462{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100463 /* Note: comparing signed and unsigned integers */
464
465 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
466 subsys = DLGLOBAL;
467
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100468 if (subsys < 0)
469 subsys = subsys_lib2index(subsys);
470
Neels Hofmeyrca135742016-12-12 14:18:54 +0100471 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100472 subsys = subsys_lib2index(DLGLOBAL);
473
Neels Hofmeyrca135742016-12-12 14:18:54 +0100474 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
475
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100476 return subsys;
477}
478
Maxc65c5b42017-03-15 13:20:23 +0100479static inline bool should_log_to_target(struct log_target *tar, int subsys,
480 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100481{
482 struct log_category *category;
483
484 category = &tar->categories[subsys];
485
486 /* subsystem is not supposed to be logged */
487 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100488 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100489
490 /* Check the global log level */
491 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100492 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100493
494 /* Check the category log level */
495 if (tar->loglevel == 0 && category->loglevel != 0 &&
496 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100497 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100498
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100499 /* Apply filters here... if that becomes messy we will
500 * need to put filters in a list and each filter will
501 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100502 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100503 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100504
505 if (osmo_log_info->filter_fn)
506 return osmo_log_info->filter_fn(&log_context, tar);
507
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100508 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100509 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100510}
511
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200512/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200513 * \param[in] subsys Logging sub-system
514 * \param[in] level Log level
515 * \param[in] file name of source code file
516 * \param[in] cont continuation (1) or new line (0)
517 * \param[in] format format string
518 * \param[in] ap vararg-list containing format string arguments
519 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200520void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200521 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800522{
Harald Welte3ae27582010-03-26 21:24:24 +0800523 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800524
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100525 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200526
Harald Welte28222962011-02-18 20:37:04 +0100527 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200528 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800529
Maxc65c5b42017-03-15 13:20:23 +0100530 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800531 continue;
532
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200533 /* According to the manpage, vsnprintf leaves the value of ap
534 * in undefined state. Since _output uses vsnprintf and it may
535 * be called several times, we have to pass a copy of ap. */
536 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100537 if (tar->raw_output)
538 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
539 else
540 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200541 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800542 }
543}
544
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200545/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200546 * \param[in] subsys Logging sub-system
547 * \param[in] file name of source code file
548 * \param[in] cont continuation (1) or new line (0)
549 * \param[in] format format string
550 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200551void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800552 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800553{
554 va_list ap;
555
556 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200557 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800558 va_end(ap);
559}
560
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200561/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200562 * \param[in] subsys Logging sub-system
563 * \param[in] level Log level
564 * \param[in] file name of source code file
565 * \param[in] cont continuation (1) or new line (0)
566 * \param[in] format format string
567 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200568void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800569{
570 va_list ap;
571
572 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200573 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800574 va_end(ap);
575}
576
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200577/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200578 * \param[in] target Log target to be registered
579 */
Harald Welte3ae27582010-03-26 21:24:24 +0800580void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800581{
Harald Welte28222962011-02-18 20:37:04 +0100582 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800583}
584
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200585/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200586 * \param[in] target Log target to be unregistered
587 */
Harald Welte3ae27582010-03-26 21:24:24 +0800588void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800589{
590 llist_del(&target->entry);
591}
592
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200593/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800594void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800595{
Harald Welte3ae27582010-03-26 21:24:24 +0800596 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800597}
598
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200599/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200600 * \param[in] ctx_nr logging context number
601 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200602 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200603 *
604 * A logging context is something like the subscriber identity to which
605 * the currently processed message relates, or the BTS through which it
606 * was received. As soon as this data is known, it can be set using
607 * this function. The main use of context information is for logging
608 * filters.
609 */
Harald Welte3ae27582010-03-26 21:24:24 +0800610int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800611{
Harald Welte3ae27582010-03-26 21:24:24 +0800612 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800613 return -EINVAL;
614
Harald Welte3ae27582010-03-26 21:24:24 +0800615 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800616
617 return 0;
618}
619
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200620/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200621 * \param[in] target Log target to be affected
622 * \param[in] all enable (1) or disable (0) the ALL filter
623 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100624 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100625 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
626 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200627 */
Harald Welte3ae27582010-03-26 21:24:24 +0800628void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800629{
630 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100631 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800632 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100633 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800634}
635
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200636/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200637 * \param[in] target Log target to be affected
638 * \param[in] use_color Use color (1) or don't use color (0)
639 */
Harald Welte3ae27582010-03-26 21:24:24 +0800640void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800641{
642 target->use_color = use_color;
643}
644
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200645/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200646 * \param[in] target Log target to be affected
647 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
648 */
Harald Welte3ae27582010-03-26 21:24:24 +0800649void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800650{
651 target->print_timestamp = print_timestamp;
652}
653
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200654/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100655 * \param[in] target Log target to be affected
656 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
657 *
658 * When both timestamp and extended timestamp is enabled then only
659 * the extended timestamp will be used. The format of the timestamp
660 * is YYYYMMDDhhmmssnnn.
661 */
662void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
663{
664 target->print_ext_timestamp = print_timestamp;
665}
666
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100667/*! Use log_set_print_filename2() instead.
668 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
669 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
670 * behavior, which combined the category in hex with the filename. For example, if the category-hex
671 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
672 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200673 * \param[in] target Log target to be affected
674 * \param[in] print_filename Enable (1) or disable (0) filenames
675 */
676void log_set_print_filename(struct log_target *target, int print_filename)
677{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100678 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
679 log_set_print_category_hex(target, print_filename);
680}
681
682/*! Enable or disable printing of the filename while logging.
683 * \param[in] target Log target to be affected.
684 * \param[in] print_filename An LOG_FILENAME_* enum value.
685 * LOG_FILENAME_NONE omits the source file and line information from logs.
686 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
687 */
688void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
689{
690 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200691}
692
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200693/*! Set the position where on a log line the source file info should be logged.
694 * \param[in] target Log target to be affected.
695 * \param[in] pos A LOG_FILENAME_POS_* enum value.
696 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
697 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
698 */
699void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
700{
701 target->print_filename_pos = pos;
702}
703
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200704/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100705 * \param[in] target Log target to be affected
706 * \param[in] print_catname Enable (1) or disable (0) filenames
707 *
708 * Print the category/subsys name in front of every log message.
709 */
710void log_set_print_category(struct log_target *target, int print_category)
711{
712 target->print_category = print_category;
713}
714
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100715/*! Enable or disable printing of the category number in hex ('<000b>').
716 * \param[in] target Log target to be affected.
717 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
718 */
719void log_set_print_category_hex(struct log_target *target, int print_category_hex)
720{
721 target->print_category_hex = print_category_hex;
722}
723
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100724/*! Enable or disable printing of the log level name.
725 * \param[in] target Log target to be affected
726 * \param[in] print_catname Enable (1) or disable (0) filenames
727 *
728 * Print the log level name in front of every log message.
729 */
730void log_set_print_level(struct log_target *target, int print_level)
731{
732 target->print_level = (bool)print_level;
733}
734
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200735/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200736 * \param[in] target Log target to be affected
737 * \param[in] log_level New global log level
738 */
Harald Welte3ae27582010-03-26 21:24:24 +0800739void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800740{
741 target->loglevel = log_level;
742}
743
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200744/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100745 * \param[in] target Log target to be affected
746 * \param[in] category Log category to be affected
747 * \param[in] enable whether to enable or disable the filter
748 * \param[in] level Log level of the filter
749 */
Harald Welte3ae27582010-03-26 21:24:24 +0800750void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800751 int enable, int level)
752{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100753 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800754 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100755 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800756 target->categories[category].enabled = !!enable;
757 target->categories[category].loglevel = level;
758}
759
Harald Welte44c0f632017-01-15 17:58:29 +0100760#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100761static void _file_output(struct log_target *target, unsigned int level,
762 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800763{
Harald Welte0083cd32010-08-25 14:55:44 +0200764 fprintf(target->tgt_file.out, "%s", log);
765 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800766}
Harald Welte44c0f632017-01-15 17:58:29 +0100767#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800768
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200769/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200770 * \returns dynamically-allocated log target
771 * This funcition allocates a \ref log_target and initializes it
772 * with some default values. The newly created target is not
773 * registered yet.
774 */
Harald Welte3ae27582010-03-26 21:24:24 +0800775struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800776{
Harald Welte3ae27582010-03-26 21:24:24 +0800777 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800778 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800779
Max68bf16a2018-01-10 17:00:43 +0100780 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100781
Harald Welte3ae27582010-03-26 21:24:24 +0800782 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800783 if (!target)
784 return NULL;
785
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200786 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200787 struct log_category,
788 osmo_log_info->num_cat);
789 if (!target->categories) {
790 talloc_free(target);
791 return NULL;
792 }
793
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800794 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800795
796 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200797 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800798 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200799 cat->enabled = osmo_log_info->cat[i].enabled;
800 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800801 }
802
803 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800804 target->use_color = 1;
805 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100806 target->print_filename2 = LOG_FILENAME_PATH;
807 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800808
809 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800810 target->loglevel = 0;
811 return target;
812}
813
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200814/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200815 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800816struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800817{
Harald Weltea3b844c2010-03-27 00:04:40 +0800818/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200819#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800820 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800821
Harald Welte3ae27582010-03-26 21:24:24 +0800822 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800823 if (!target)
824 return NULL;
825
Harald Welte28222962011-02-18 20:37:04 +0100826 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200827 target->tgt_file.out = stderr;
828 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800829 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800830#else
831 return NULL;
832#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800833}
834
Harald Welte44c0f632017-01-15 17:58:29 +0100835#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200836/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200837 * \param[in] fname File name of the new log file
838 * \returns Log target in case of success, NULL otherwise
839 */
Harald Welte3086c392010-08-25 19:10:50 +0200840struct log_target *log_target_create_file(const char *fname)
841{
842 struct log_target *target;
843
844 target = log_target_create();
845 if (!target)
846 return NULL;
847
Harald Welte28222962011-02-18 20:37:04 +0100848 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200849 target->tgt_file.out = fopen(fname, "a");
850 if (!target->tgt_file.out)
851 return NULL;
852
853 target->output = _file_output;
854
855 target->tgt_file.fname = talloc_strdup(target, fname);
856
857 return target;
858}
Harald Welte44c0f632017-01-15 17:58:29 +0100859#endif
Harald Welte3086c392010-08-25 19:10:50 +0200860
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200861/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200862 * \param[in] type Log target type
863 * \param[in] fname File name
864 * \returns Log target (if found), NULL otherwise
865 */
Harald Welte28222962011-02-18 20:37:04 +0100866struct log_target *log_target_find(int type, const char *fname)
867{
868 struct log_target *tgt;
869
870 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
871 if (tgt->type != type)
872 continue;
Maxc90f40a2018-01-11 10:52:28 +0100873 switch (tgt->type) {
874 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100875 if (!strcmp(fname, tgt->tgt_file.fname))
876 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100877 break;
878 case LOG_TGT_TYPE_GSMTAP:
879 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
880 return tgt;
881 break;
882 default:
Harald Welte28222962011-02-18 20:37:04 +0100883 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100884 }
Harald Welte28222962011-02-18 20:37:04 +0100885 }
886 return NULL;
887}
888
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200889/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200890 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200891void log_target_destroy(struct log_target *target)
892{
893
894 /* just in case, to make sure we don't have any references */
895 log_del_target(target);
896
Harald Welte44c0f632017-01-15 17:58:29 +0100897#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200898 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200899/* since C89/C99 says stderr is a macro, we can safely do this! */
900#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200901 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200902 if (target->tgt_file.out != stderr)
903#endif
904 {
Harald Welte3086c392010-08-25 19:10:50 +0200905 fclose(target->tgt_file.out);
906 target->tgt_file.out = NULL;
907 }
908 }
Harald Welte44c0f632017-01-15 17:58:29 +0100909#endif
Harald Welte3086c392010-08-25 19:10:50 +0200910
911 talloc_free(target);
912}
913
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200914/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200915 * \param[in] target log target to re-open
916 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200917int log_target_file_reopen(struct log_target *target)
918{
919 fclose(target->tgt_file.out);
920
921 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
922 if (!target->tgt_file.out)
923 return -errno;
924
925 /* we assume target->output already to be set */
926
927 return 0;
928}
929
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200930/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200931 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100932int log_targets_reopen(void)
933{
934 struct log_target *tar;
935 int rc = 0;
936
937 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
938 switch (tar->type) {
939 case LOG_TGT_TYPE_FILE:
940 if (log_target_file_reopen(tar) < 0)
941 rc = -1;
942 break;
943 default:
944 break;
945 }
946 }
947
948 return rc;
949}
950
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200951/*! Initialize the Osmocom logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200952 * \param[in] inf Information regarding logging categories
953 * \param[in] ctx \ref talloc context for logging allocations
954 * \returns 0 in case of success, negative in case of error
955 */
Harald Welteb43bc042011-06-27 10:29:17 +0200956int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800957{
Harald Welteb43bc042011-06-27 10:29:17 +0200958 int i;
959
960 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
961 if (!tall_log_ctx)
962 return -ENOMEM;
963
964 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
965 if (!osmo_log_info)
966 return -ENOMEM;
967
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100968 osmo_log_info->filter_fn = inf->filter_fn;
Harald Welteb43bc042011-06-27 10:29:17 +0200969 osmo_log_info->num_cat_user = inf->num_cat;
970 /* total number = number of user cat + library cat */
Harald Weltece9fec32011-06-27 14:19:16 +0200971 osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat);
Harald Welteb43bc042011-06-27 10:29:17 +0200972
973 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
974 struct log_info_cat,
975 osmo_log_info->num_cat);
976 if (!osmo_log_info->cat) {
977 talloc_free(osmo_log_info);
978 osmo_log_info = NULL;
979 return -ENOMEM;
980 }
981
982 /* copy over the user part */
983 for (i = 0; i < inf->num_cat; i++) {
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200984 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
985 &inf->cat[i],
Harald Welteb43bc042011-06-27 10:29:17 +0200986 sizeof(struct log_info_cat));
987 }
988
989 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200990 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +0200991 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200992 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +0200993 &internal_cat[i], sizeof(struct log_info_cat));
994 }
995
996 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800997}
Harald Welte18fc4652011-08-17 14:14:17 +0200998
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200999/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001000 * This function destroys all targets and releases associated memory */
1001void log_fini(void)
1002{
1003 struct log_target *tar, *tar2;
1004
1005 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1006 log_target_destroy(tar);
1007
1008 talloc_free(osmo_log_info);
1009 osmo_log_info = NULL;
1010 talloc_free(tall_log_ctx);
1011 tall_log_ctx = NULL;
1012}
1013
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001014/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001015 * \returns != 0 if a log entry might get generated by at least one target */
1016int log_check_level(int subsys, unsigned int level)
1017{
1018 struct log_target *tar;
1019
Max68bf16a2018-01-10 17:00:43 +01001020 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001021
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001022 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001023
1024 /* TODO: The following could/should be cached (update on config) */
1025
1026 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001027 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001028 continue;
1029
1030 /* This might get logged (ignoring filters) */
1031 return 1;
1032 }
1033
1034 /* We are sure, that this will not be logged. */
1035 return 0;
1036}
1037
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001038/*! @} */