blob: 1c3544fae2dd1d7a5f950d905990203bc3f7e8ff [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 Hofmeyr098038a2018-09-11 23:49:13 +020066const struct value_string loglevel_strs[] = {
Harald Welte4a2bb9e2010-03-26 09:33:40 +080067 { LOGL_DEBUG, "DEBUG" },
68 { LOGL_INFO, "INFO" },
69 { LOGL_NOTICE, "NOTICE" },
70 { LOGL_ERROR, "ERROR" },
71 { LOGL_FATAL, "FATAL" },
72 { 0, NULL },
73};
74
Harald Welteb43bc042011-06-27 10:29:17 +020075#define INT2IDX(x) (-1*(x)-1)
76static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = {
77 [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */
78 .name = "DLGLOBAL",
79 .description = "Library-internal global log family",
80 .loglevel = LOGL_NOTICE,
81 .enabled = 1,
82 },
root8a996b42011-09-26 11:22:21 +020083 [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */
84 .name = "DLLAPD",
85 .description = "LAPD in libosmogsm",
Harald Welte1f0b8c22011-06-27 10:51:37 +020086 .loglevel = LOGL_NOTICE,
87 .enabled = 1,
88 },
Harald Welte892e6212011-07-19 14:31:44 +020089 [INT2IDX(DLINP)] = {
Harald Welte087e1132011-07-29 11:43:39 +020090 .name = "DLINP",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020091 .description = "A-bis Intput Subsystem",
92 .loglevel = LOGL_NOTICE,
93 .enabled = 1,
94 },
Harald Welte892e6212011-07-19 14:31:44 +020095 [INT2IDX(DLMUX)] = {
Harald Welte087e1132011-07-29 11:43:39 +020096 .name = "DLMUX",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020097 .description = "A-bis B-Subchannel TRAU Frame Multiplex",
98 .loglevel = LOGL_NOTICE,
99 .enabled = 1,
100 },
Harald Welte892e6212011-07-19 14:31:44 +0200101 [INT2IDX(DLMI)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200102 .name = "DLMI",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200103 .description = "A-bis Input Driver for Signalling",
104 .enabled = 0, .loglevel = LOGL_NOTICE,
105 },
Harald Welte892e6212011-07-19 14:31:44 +0200106 [INT2IDX(DLMIB)] = {
Harald Welte087e1132011-07-29 11:43:39 +0200107 .name = "DLMIB",
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +0200108 .description = "A-bis Input Driver for B-Channels (voice)",
109 .enabled = 0, .loglevel = LOGL_NOTICE,
110 },
Andreas Eversbergc626da92011-10-28 03:53:50 +0200111 [INT2IDX(DLSMS)] = {
112 .name = "DLSMS",
113 .description = "Layer3 Short Message Service (SMS)",
114 .enabled = 1, .loglevel = LOGL_NOTICE,
115 .color = "\033[1;38m",
116 },
Harald Welte7fd0c832014-08-20 19:58:13 +0200117 [INT2IDX(DLCTRL)] = {
118 .name = "DLCTRL",
119 .description = "Control Interface",
120 .enabled = 1, .loglevel = LOGL_NOTICE,
121 },
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +0100122 [INT2IDX(DLGTP)] = {
123 .name = "DLGTP",
124 .description = "GPRS GTP library",
125 .enabled = 1, .loglevel = LOGL_NOTICE,
126 },
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100127 [INT2IDX(DLSTATS)] = {
128 .name = "DLSTATS",
129 .description = "Statistics messages and logging",
130 .enabled = 1, .loglevel = LOGL_NOTICE,
131 },
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100132 [INT2IDX(DLGSUP)] = {
133 .name = "DLGSUP",
134 .description = "Generic Subscriber Update Protocol",
135 .enabled = 1, .loglevel = LOGL_NOTICE,
136 },
Harald Weltec0f00072016-04-27 18:32:35 +0200137 [INT2IDX(DLOAP)] = {
138 .name = "DLOAP",
139 .description = "Osmocom Authentication Protocol",
140 .enabled = 1, .loglevel = LOGL_NOTICE,
141 },
Harald Welte059c4042017-04-03 22:20:49 +0200142 [INT2IDX(DLSS7)] = {
143 .name = "DLSS7",
144 .description = "libosmo-sigtran Signalling System 7",
145 .enabled = 1, .loglevel = LOGL_NOTICE,
146 },
147 [INT2IDX(DLSCCP)] = {
148 .name = "DLSCCP",
149 .description = "libosmo-sigtran SCCP Implementation",
150 .enabled = 1, .loglevel = LOGL_NOTICE,
151 },
152 [INT2IDX(DLSUA)] = {
153 .name = "DLSUA",
154 .description = "libosmo-sigtran SCCP User Adaptation",
155 .enabled = 1, .loglevel = LOGL_NOTICE,
156 },
157 [INT2IDX(DLM3UA)] = {
158 .name = "DLM3UA",
159 .description = "libosmo-sigtran MTP3 User Adaptation",
160 .enabled = 1, .loglevel = LOGL_NOTICE,
161 },
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200162 [INT2IDX(DLMGCP)] = {
163 .name = "DLMGCP",
164 .description = "libosmo-mgcp Media Gateway Control Protocol",
165 .enabled = 1, .loglevel = LOGL_NOTICE,
166 },
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100167 [INT2IDX(DLJIBUF)] = {
168 .name = "DLJIBUF",
169 .description = "libosmo-netif Jitter Buffer",
170 .enabled = 1, .loglevel = LOGL_NOTICE,
171 },
Max450f5ac2019-02-14 19:12:03 +0100172 [INT2IDX(DLRSPRO)] = {
173 .name = "DLRSPRO",
174 .description = "Remote SIM protocol",
175 .enabled = 1, .loglevel = LOGL_NOTICE,
176 },
Harald Welteb43bc042011-06-27 10:29:17 +0200177};
178
Neels Hofmeyrba0762d2018-09-10 13:56:03 +0200179void assert_loginfo(const char *src)
Harald Welte18a7d812017-03-16 23:54:55 +0100180{
181 if (!osmo_log_info) {
182 fprintf(stderr, "ERROR: osmo_log_info == NULL! "
Max68bf16a2018-01-10 17:00:43 +0100183 "You must call log_init() before using logging in %s()!\n", src);
Harald Welte18a7d812017-03-16 23:54:55 +0100184 OSMO_ASSERT(osmo_log_info);
185 }
186}
187
Harald Welteb43bc042011-06-27 10:29:17 +0200188/* special magic for negative (library-internal) log subsystem numbers */
189static int subsys_lib2index(int subsys)
190{
191 return (subsys * -1) + (osmo_log_info->num_cat_user-1);
192}
193
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200194/*! Parse a human-readable log level into a numeric value
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700195 * \param[in] lvl zero-terminated string containing log level name
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200196 * \returns numeric log level
197 */
Harald Welte3ae27582010-03-26 21:24:24 +0800198int log_parse_level(const char *lvl)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800199{
200 return get_string_value(loglevel_strs, lvl);
201}
202
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200203/*! convert a numeric log level into human-readable string
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700204 * \param[in] lvl numeric log level
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200205 * \returns zero-terminated string (log level name)
206 */
Harald Welte9ac22252010-05-11 11:19:40 +0200207const char *log_level_str(unsigned int lvl)
208{
209 return get_value_string(loglevel_strs, lvl);
210}
211
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200212/*! parse a human-readable log category into numeric form
Harald Welte18fc4652011-08-17 14:14:17 +0200213 * \param[in] category human-readable log category name
214 * \returns numeric category value, or -EINVAL otherwise
215 */
Harald Welte3ae27582010-03-26 21:24:24 +0800216int log_parse_category(const char *category)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800217{
218 int i;
219
Max68bf16a2018-01-10 17:00:43 +0100220 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100221
Harald Welte4ebdf742010-05-19 19:54:00 +0200222 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Harald Welteb43bc042011-06-27 10:29:17 +0200223 if (osmo_log_info->cat[i].name == NULL)
224 continue;
Harald Welte4ebdf742010-05-19 19:54:00 +0200225 if (!strcasecmp(osmo_log_info->cat[i].name+1, category))
Harald Weltefaadfe22010-03-26 21:05:43 +0800226 return i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800227 }
228
229 return -EINVAL;
230}
231
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200232/*! parse the log category mask
Harald Welte18fc4652011-08-17 14:14:17 +0200233 * \param[in] target log target to be configured
234 * \param[in] _mask log category mask string
235 *
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800236 * The format can be this: category1:category2:category3
237 * or category1,2:category2,3:...
238 */
Harald Welte3ae27582010-03-26 21:24:24 +0800239void log_parse_category_mask(struct log_target* target, const char *_mask)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800240{
241 int i = 0;
242 char *mask = strdup(_mask);
243 char *category_token = NULL;
244
Max68bf16a2018-01-10 17:00:43 +0100245 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100246
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800247 /* Disable everything to enable it afterwards */
Harald Welteb43bc042011-06-27 10:29:17 +0200248 for (i = 0; i < osmo_log_info->num_cat; ++i)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800249 target->categories[i].enabled = 0;
250
251 category_token = strtok(mask, ":");
Neels Hofmeyrda1b20c2016-04-14 15:12:16 +0200252 OSMO_ASSERT(category_token);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800253 do {
Harald Welte4ebdf742010-05-19 19:54:00 +0200254 for (i = 0; i < osmo_log_info->num_cat; ++i) {
Nico Golde0262d3f2012-09-21 17:44:58 +0200255 size_t length, cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800256 char* colon = strstr(category_token, ",");
Nico Golde0262d3f2012-09-21 17:44:58 +0200257
258 if (!osmo_log_info->cat[i].name)
259 continue;
260
261 length = strlen(category_token);
262 cat_length = strlen(osmo_log_info->cat[i].name);
Pablo Neira Ayuso300e78d2011-08-11 13:24:18 +0200263
264 /* Use longest length not to match subocurrences. */
265 if (cat_length > length)
266 length = cat_length;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800267
268 if (colon)
269 length = colon - category_token;
270
Harald Welte4ebdf742010-05-19 19:54:00 +0200271 if (strncasecmp(osmo_log_info->cat[i].name,
272 category_token, length) == 0) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800273 int level = 0;
274
275 if (colon)
276 level = atoi(colon+1);
277
Harald Weltefaadfe22010-03-26 21:05:43 +0800278 target->categories[i].enabled = 1;
279 target->categories[i].loglevel = level;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800280 }
281 }
282 } while ((category_token = strtok(NULL, ":")));
283
284 free(mask);
285}
286
287static const char* color(int subsys)
288{
Harald Welte4ebdf742010-05-19 19:54:00 +0200289 if (subsys < osmo_log_info->num_cat)
290 return osmo_log_info->cat[subsys].color;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800291
Harald Welted788f662010-03-26 09:45:03 +0800292 return NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800293}
294
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100295static const struct value_string level_colors[] = {
296 { LOGL_DEBUG, "\033[1;34m" },
297 { LOGL_INFO, "\033[1;32m" },
298 { LOGL_NOTICE, "\033[1;33m" },
299 { LOGL_ERROR, "\033[1;31m" },
300 { LOGL_FATAL, "\033[1;31m" },
301 { 0, NULL }
302};
303
304static const char *level_color(int level)
305{
306 const char *c = get_value_string_or_null(level_colors, level);
307 if (!c)
308 return get_value_string(level_colors, LOGL_FATAL);
309 return c;
310}
311
Harald Welteaa00f992016-12-02 15:30:02 +0100312const char* log_category_name(int subsys)
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100313{
314 if (subsys < osmo_log_info->num_cat)
315 return osmo_log_info->cat[subsys].name;
316
317 return NULL;
318}
319
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100320static const char *const_basename(const char *path)
321{
322 const char *bn = strrchr(path, '/');
323 if (!bn || !bn[1])
324 return path;
325 return bn + 1;
326}
327
Harald Welte3ae27582010-03-26 21:24:24 +0800328static void _output(struct log_target *target, unsigned int subsys,
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200329 unsigned int level, const char *file, int line, int cont,
Harald Welte76e72ab2011-02-17 15:52:39 +0100330 const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800331{
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800332 char buf[4096];
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200333 int ret, len = 0, offset = 0, rem = sizeof(buf);
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100334 const char *c_subsys = NULL;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800335
336 /* are we using color */
337 if (target->use_color) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100338 c_subsys = color(subsys);
339 if (c_subsys) {
Neels Hofmeyr5e518b52018-01-17 13:20:02 +0100340 ret = snprintf(buf + offset, rem, "%s", c_subsys);
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200341 if (ret < 0)
342 goto err;
343 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welted788f662010-03-26 09:45:03 +0800344 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800345 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800346 if (!cont) {
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100347 if (target->print_ext_timestamp) {
Harald Welte14c4c492018-06-28 08:28:52 +0200348#ifdef HAVE_LOCALTIME_R
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100349 struct tm tm;
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100350 struct timeval tv;
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200351 osmo_gettimeofday(&tv, NULL);
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100352 localtime_r(&tv.tv_sec, &tm);
353 ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ",
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100354 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
Jacob Erlbeckb61b2ca2015-03-17 10:21:15 +0100355 tm.tm_hour, tm.tm_min, tm.tm_sec,
356 (int)(tv.tv_usec / 1000));
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100357 if (ret < 0)
358 goto err;
359 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte14c4c492018-06-28 08:28:52 +0200360#endif
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100361 } else if (target->print_timestamp) {
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800362 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200363 if ((tm = time(NULL)) == (time_t) -1)
364 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200365 /* Get human-readable representation of time.
366 man ctime: we need at least 26 bytes in buf */
367 if (rem < 26 || !ctime_r(&tm, buf + offset))
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200368 goto err;
Pau Espin Pedrolcc794e92019-06-12 16:22:53 +0200369 ret = strlen(buf + offset);
370 if (ret <= 0)
371 goto err;
372 /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
373 buf[offset + ret - 1] = ' ';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200374 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800375 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100376 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100377 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
378 target->use_color ? level_color(level) : "",
379 log_category_name(subsys),
380 target->use_color ? "\033[0;m" : "",
381 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100382 if (ret < 0)
383 goto err;
384 OSMO_SNPRINTF_RET(ret, rem, offset, len);
385 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100386 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100387 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
388 target->use_color ? level_color(level) : "",
389 log_level_str(level),
390 target->use_color ? "\033[0;m" : "",
391 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100392 if (ret < 0)
393 goto err;
394 OSMO_SNPRINTF_RET(ret, rem, offset, len);
395 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100396 if (target->print_category_hex) {
397 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200398 if (ret < 0)
399 goto err;
400 OSMO_SNPRINTF_RET(ret, rem, offset, len);
401 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200402
403 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
404 switch (target->print_filename2) {
405 case LOG_FILENAME_NONE:
406 break;
407 case LOG_FILENAME_PATH:
408 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
409 if (ret < 0)
410 goto err;
411 OSMO_SNPRINTF_RET(ret, rem, offset, len);
412 break;
413 case LOG_FILENAME_BASENAME:
414 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
415 if (ret < 0)
416 goto err;
417 OSMO_SNPRINTF_RET(ret, rem, offset, len);
418 break;
419 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100420 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800421 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200422 ret = vsnprintf(buf + offset, rem, format, ap);
423 if (ret < 0)
424 goto err;
425 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800426
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200427 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
428 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
429 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
430 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
431 && offset > 0 && buf[offset-1] == '\n') {
432 switch (target->print_filename2) {
433 case LOG_FILENAME_NONE:
434 break;
435 case LOG_FILENAME_PATH:
436 offset --;
437 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
438 if (ret < 0)
439 goto err;
440 OSMO_SNPRINTF_RET(ret, rem, offset, len);
441 break;
442 case LOG_FILENAME_BASENAME:
443 offset --;
444 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
445 if (ret < 0)
446 goto err;
447 OSMO_SNPRINTF_RET(ret, rem, offset, len);
448 break;
449 }
450 }
451
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100452 if (target->use_color) {
453 ret = snprintf(buf + offset, rem, "\033[0;m");
454 if (ret < 0)
455 goto err;
456 OSMO_SNPRINTF_RET(ret, rem, offset, len);
457 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200458err:
459 buf[sizeof(buf)-1] = '\0';
460 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800461}
462
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100463/* Catch internal logging category indexes as well as out-of-bounds indexes.
464 * For internal categories, the ID is negative starting with -1; and internal
465 * logging categories are added behind the user categories. For out-of-bounds
466 * indexes, return the index of DLGLOBAL. The returned category index is
467 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
468 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100469static inline int map_subsys(int subsys)
470{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100471 /* Note: comparing signed and unsigned integers */
472
473 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
474 subsys = DLGLOBAL;
475
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100476 if (subsys < 0)
477 subsys = subsys_lib2index(subsys);
478
Neels Hofmeyrca135742016-12-12 14:18:54 +0100479 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100480 subsys = subsys_lib2index(DLGLOBAL);
481
Neels Hofmeyrca135742016-12-12 14:18:54 +0100482 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
483
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100484 return subsys;
485}
486
Maxc65c5b42017-03-15 13:20:23 +0100487static inline bool should_log_to_target(struct log_target *tar, int subsys,
488 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100489{
490 struct log_category *category;
491
492 category = &tar->categories[subsys];
493
494 /* subsystem is not supposed to be logged */
495 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100496 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100497
498 /* Check the global log level */
499 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100500 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100501
502 /* Check the category log level */
503 if (tar->loglevel == 0 && category->loglevel != 0 &&
504 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100505 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100506
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100507 /* Apply filters here... if that becomes messy we will
508 * need to put filters in a list and each filter will
509 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100510 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100511 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100512
513 if (osmo_log_info->filter_fn)
514 return osmo_log_info->filter_fn(&log_context, tar);
515
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100516 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100517 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100518}
519
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200520/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200521 * \param[in] subsys Logging sub-system
522 * \param[in] level Log level
523 * \param[in] file name of source code file
524 * \param[in] cont continuation (1) or new line (0)
525 * \param[in] format format string
526 * \param[in] ap vararg-list containing format string arguments
527 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200528void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200529 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800530{
Harald Welte3ae27582010-03-26 21:24:24 +0800531 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800532
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100533 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200534
Harald Welte28222962011-02-18 20:37:04 +0100535 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200536 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800537
Maxc65c5b42017-03-15 13:20:23 +0100538 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800539 continue;
540
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200541 /* According to the manpage, vsnprintf leaves the value of ap
542 * in undefined state. Since _output uses vsnprintf and it may
543 * be called several times, we have to pass a copy of ap. */
544 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100545 if (tar->raw_output)
546 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
547 else
548 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200549 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800550 }
551}
552
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200553/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200554 * \param[in] subsys Logging sub-system
555 * \param[in] file name of source code file
556 * \param[in] cont continuation (1) or new line (0)
557 * \param[in] format format string
558 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200559void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800560 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800561{
562 va_list ap;
563
564 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200565 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800566 va_end(ap);
567}
568
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200569/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200570 * \param[in] subsys Logging sub-system
571 * \param[in] level Log level
572 * \param[in] file name of source code file
573 * \param[in] cont continuation (1) or new line (0)
574 * \param[in] format format string
575 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200576void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800577{
578 va_list ap;
579
580 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200581 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800582 va_end(ap);
583}
584
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200585/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200586 * \param[in] target Log target to be registered
587 */
Harald Welte3ae27582010-03-26 21:24:24 +0800588void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800589{
Harald Welte28222962011-02-18 20:37:04 +0100590 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800591}
592
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200593/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200594 * \param[in] target Log target to be unregistered
595 */
Harald Welte3ae27582010-03-26 21:24:24 +0800596void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800597{
598 llist_del(&target->entry);
599}
600
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200601/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800602void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800603{
Harald Welte3ae27582010-03-26 21:24:24 +0800604 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800605}
606
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200607/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200608 * \param[in] ctx_nr logging context number
609 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200610 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200611 *
612 * A logging context is something like the subscriber identity to which
613 * the currently processed message relates, or the BTS through which it
614 * was received. As soon as this data is known, it can be set using
615 * this function. The main use of context information is for logging
616 * filters.
617 */
Harald Welte3ae27582010-03-26 21:24:24 +0800618int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800619{
Harald Welte3ae27582010-03-26 21:24:24 +0800620 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621 return -EINVAL;
622
Harald Welte3ae27582010-03-26 21:24:24 +0800623 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800624
625 return 0;
626}
627
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200628/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200629 * \param[in] target Log target to be affected
630 * \param[in] all enable (1) or disable (0) the ALL filter
631 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100632 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100633 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
634 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200635 */
Harald Welte3ae27582010-03-26 21:24:24 +0800636void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637{
638 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100639 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800640 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100641 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800642}
643
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200644/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200645 * \param[in] target Log target to be affected
646 * \param[in] use_color Use color (1) or don't use color (0)
647 */
Harald Welte3ae27582010-03-26 21:24:24 +0800648void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800649{
650 target->use_color = use_color;
651}
652
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200653/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200654 * \param[in] target Log target to be affected
655 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
656 */
Harald Welte3ae27582010-03-26 21:24:24 +0800657void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800658{
659 target->print_timestamp = print_timestamp;
660}
661
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200662/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100663 * \param[in] target Log target to be affected
664 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
665 *
666 * When both timestamp and extended timestamp is enabled then only
667 * the extended timestamp will be used. The format of the timestamp
668 * is YYYYMMDDhhmmssnnn.
669 */
670void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
671{
672 target->print_ext_timestamp = print_timestamp;
673}
674
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100675/*! Use log_set_print_filename2() instead.
676 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
677 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
678 * behavior, which combined the category in hex with the filename. For example, if the category-hex
679 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
680 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200681 * \param[in] target Log target to be affected
682 * \param[in] print_filename Enable (1) or disable (0) filenames
683 */
684void log_set_print_filename(struct log_target *target, int print_filename)
685{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100686 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
687 log_set_print_category_hex(target, print_filename);
688}
689
690/*! Enable or disable printing of the filename while logging.
691 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700692 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100693 * LOG_FILENAME_NONE omits the source file and line information from logs.
694 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
695 */
696void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
697{
698 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200699}
700
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200701/*! Set the position where on a log line the source file info should be logged.
702 * \param[in] target Log target to be affected.
703 * \param[in] pos A LOG_FILENAME_POS_* enum value.
704 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
705 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
706 */
707void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
708{
709 target->print_filename_pos = pos;
710}
711
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200712/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100713 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700714 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100715 *
716 * Print the category/subsys name in front of every log message.
717 */
718void log_set_print_category(struct log_target *target, int print_category)
719{
720 target->print_category = print_category;
721}
722
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100723/*! Enable or disable printing of the category number in hex ('<000b>').
724 * \param[in] target Log target to be affected.
725 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
726 */
727void log_set_print_category_hex(struct log_target *target, int print_category_hex)
728{
729 target->print_category_hex = print_category_hex;
730}
731
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100732/*! Enable or disable printing of the log level name.
733 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700734 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100735 *
736 * Print the log level name in front of every log message.
737 */
738void log_set_print_level(struct log_target *target, int print_level)
739{
740 target->print_level = (bool)print_level;
741}
742
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200743/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200744 * \param[in] target Log target to be affected
745 * \param[in] log_level New global log level
746 */
Harald Welte3ae27582010-03-26 21:24:24 +0800747void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800748{
749 target->loglevel = log_level;
750}
751
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200752/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100753 * \param[in] target Log target to be affected
754 * \param[in] category Log category to be affected
755 * \param[in] enable whether to enable or disable the filter
756 * \param[in] level Log level of the filter
757 */
Harald Welte3ae27582010-03-26 21:24:24 +0800758void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800759 int enable, int level)
760{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100761 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800762 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100763 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800764 target->categories[category].enabled = !!enable;
765 target->categories[category].loglevel = level;
766}
767
Harald Welte44c0f632017-01-15 17:58:29 +0100768#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100769static void _file_output(struct log_target *target, unsigned int level,
770 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800771{
Harald Welte0083cd32010-08-25 14:55:44 +0200772 fprintf(target->tgt_file.out, "%s", log);
773 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800774}
Harald Welte44c0f632017-01-15 17:58:29 +0100775#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800776
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200777/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200778 * \returns dynamically-allocated log target
779 * This funcition allocates a \ref log_target and initializes it
780 * with some default values. The newly created target is not
781 * registered yet.
782 */
Harald Welte3ae27582010-03-26 21:24:24 +0800783struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800784{
Harald Welte3ae27582010-03-26 21:24:24 +0800785 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800786 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800787
Max68bf16a2018-01-10 17:00:43 +0100788 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100789
Harald Welte3ae27582010-03-26 21:24:24 +0800790 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800791 if (!target)
792 return NULL;
793
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200794 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200795 struct log_category,
796 osmo_log_info->num_cat);
797 if (!target->categories) {
798 talloc_free(target);
799 return NULL;
800 }
801
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800802 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800803
804 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200805 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800806 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200807 cat->enabled = osmo_log_info->cat[i].enabled;
808 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800809 }
810
811 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800812 target->use_color = 1;
813 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100814 target->print_filename2 = LOG_FILENAME_PATH;
815 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800816
817 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800818 target->loglevel = 0;
819 return target;
820}
821
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200822/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200823 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800824struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800825{
Harald Weltea3b844c2010-03-27 00:04:40 +0800826/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200827#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800828 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800829
Harald Welte3ae27582010-03-26 21:24:24 +0800830 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800831 if (!target)
832 return NULL;
833
Harald Welte28222962011-02-18 20:37:04 +0100834 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200835 target->tgt_file.out = stderr;
836 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800837 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800838#else
839 return NULL;
840#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800841}
842
Harald Welte44c0f632017-01-15 17:58:29 +0100843#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200844/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200845 * \param[in] fname File name of the new log file
846 * \returns Log target in case of success, NULL otherwise
847 */
Harald Welte3086c392010-08-25 19:10:50 +0200848struct log_target *log_target_create_file(const char *fname)
849{
850 struct log_target *target;
851
852 target = log_target_create();
853 if (!target)
854 return NULL;
855
Harald Welte28222962011-02-18 20:37:04 +0100856 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200857 target->tgt_file.out = fopen(fname, "a");
858 if (!target->tgt_file.out)
859 return NULL;
860
861 target->output = _file_output;
862
863 target->tgt_file.fname = talloc_strdup(target, fname);
864
865 return target;
866}
Harald Welte44c0f632017-01-15 17:58:29 +0100867#endif
Harald Welte3086c392010-08-25 19:10:50 +0200868
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200869/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200870 * \param[in] type Log target type
871 * \param[in] fname File name
872 * \returns Log target (if found), NULL otherwise
873 */
Harald Welte28222962011-02-18 20:37:04 +0100874struct log_target *log_target_find(int type, const char *fname)
875{
876 struct log_target *tgt;
877
878 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
879 if (tgt->type != type)
880 continue;
Maxc90f40a2018-01-11 10:52:28 +0100881 switch (tgt->type) {
882 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100883 if (!strcmp(fname, tgt->tgt_file.fname))
884 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100885 break;
886 case LOG_TGT_TYPE_GSMTAP:
887 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
888 return tgt;
889 break;
890 default:
Harald Welte28222962011-02-18 20:37:04 +0100891 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100892 }
Harald Welte28222962011-02-18 20:37:04 +0100893 }
894 return NULL;
895}
896
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200897/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700898 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200899void log_target_destroy(struct log_target *target)
900{
901
902 /* just in case, to make sure we don't have any references */
903 log_del_target(target);
904
Harald Welte44c0f632017-01-15 17:58:29 +0100905#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200906 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200907/* since C89/C99 says stderr is a macro, we can safely do this! */
908#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200909 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200910 if (target->tgt_file.out != stderr)
911#endif
912 {
Harald Welte3086c392010-08-25 19:10:50 +0200913 fclose(target->tgt_file.out);
914 target->tgt_file.out = NULL;
915 }
916 }
Harald Welte44c0f632017-01-15 17:58:29 +0100917#endif
Harald Welte3086c392010-08-25 19:10:50 +0200918
919 talloc_free(target);
920}
921
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200922/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200923 * \param[in] target log target to re-open
924 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200925int log_target_file_reopen(struct log_target *target)
926{
927 fclose(target->tgt_file.out);
928
929 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
930 if (!target->tgt_file.out)
931 return -errno;
932
933 /* we assume target->output already to be set */
934
935 return 0;
936}
937
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200938/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200939 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100940int log_targets_reopen(void)
941{
942 struct log_target *tar;
943 int rc = 0;
944
945 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
946 switch (tar->type) {
947 case LOG_TGT_TYPE_FILE:
948 if (log_target_file_reopen(tar) < 0)
949 rc = -1;
950 break;
951 default:
952 break;
953 }
954 }
955
956 return rc;
957}
958
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200959/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +0100960 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700961 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +0200962 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +0100963 *
964 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +0200965 */
Harald Welteb43bc042011-06-27 10:29:17 +0200966int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800967{
Harald Welteb43bc042011-06-27 10:29:17 +0200968 int i;
969
970 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
971 if (!tall_log_ctx)
972 return -ENOMEM;
973
974 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
975 if (!osmo_log_info)
976 return -ENOMEM;
977
Max72dfd432018-12-04 11:24:18 +0100978 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
979
980 if (inf) {
981 osmo_log_info->filter_fn = inf->filter_fn;
982 osmo_log_info->num_cat_user = inf->num_cat;
983 osmo_log_info->num_cat += inf->num_cat;
984 }
Harald Welteb43bc042011-06-27 10:29:17 +0200985
986 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
987 struct log_info_cat,
988 osmo_log_info->num_cat);
989 if (!osmo_log_info->cat) {
990 talloc_free(osmo_log_info);
991 osmo_log_info = NULL;
992 return -ENOMEM;
993 }
994
Max72dfd432018-12-04 11:24:18 +0100995 if (inf) { /* copy over the user part */
996 for (i = 0; i < inf->num_cat; i++) {
997 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
998 &inf->cat[i], sizeof(struct log_info_cat));
999 }
Harald Welteb43bc042011-06-27 10:29:17 +02001000 }
1001
1002 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001003 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001004 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001005 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001006 &internal_cat[i], sizeof(struct log_info_cat));
1007 }
1008
1009 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001010}
Harald Welte18fc4652011-08-17 14:14:17 +02001011
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001012/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001013 * This function destroys all targets and releases associated memory */
1014void log_fini(void)
1015{
1016 struct log_target *tar, *tar2;
1017
1018 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1019 log_target_destroy(tar);
1020
1021 talloc_free(osmo_log_info);
1022 osmo_log_info = NULL;
1023 talloc_free(tall_log_ctx);
1024 tall_log_ctx = NULL;
1025}
1026
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001027/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001028 * \returns != 0 if a log entry might get generated by at least one target */
1029int log_check_level(int subsys, unsigned int level)
1030{
1031 struct log_target *tar;
1032
Max68bf16a2018-01-10 17:00:43 +01001033 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001034
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001035 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001036
1037 /* TODO: The following could/should be cached (update on config) */
1038
1039 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001040 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001041 continue;
1042
1043 /* This might get logged (ignoring filters) */
1044 return 1;
1045 }
1046
1047 /* We are sure, that this will not be logged. */
1048 return 0;
1049}
1050
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001051/*! @} */