blob: 4c864739674364257c5f6f5c2f71a25afa254591 [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 char *timestr;
363 time_t tm;
Pau Espin Pedrol3aef2382019-06-12 18:50:29 +0200364 if ((tm = time(NULL)) == (time_t) -1)
365 goto err;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800366 timestr = ctime(&tm);
367 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200368 ret = snprintf(buf + offset, rem, "%s ", timestr);
369 if (ret < 0)
370 goto err;
371 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800372 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100373 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100374 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
375 target->use_color ? level_color(level) : "",
376 log_category_name(subsys),
377 target->use_color ? "\033[0;m" : "",
378 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100379 if (ret < 0)
380 goto err;
381 OSMO_SNPRINTF_RET(ret, rem, offset, len);
382 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100383 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100384 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
385 target->use_color ? level_color(level) : "",
386 log_level_str(level),
387 target->use_color ? "\033[0;m" : "",
388 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100389 if (ret < 0)
390 goto err;
391 OSMO_SNPRINTF_RET(ret, rem, offset, len);
392 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100393 if (target->print_category_hex) {
394 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200395 if (ret < 0)
396 goto err;
397 OSMO_SNPRINTF_RET(ret, rem, offset, len);
398 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200399
400 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
401 switch (target->print_filename2) {
402 case LOG_FILENAME_NONE:
403 break;
404 case LOG_FILENAME_PATH:
405 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
406 if (ret < 0)
407 goto err;
408 OSMO_SNPRINTF_RET(ret, rem, offset, len);
409 break;
410 case LOG_FILENAME_BASENAME:
411 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
412 if (ret < 0)
413 goto err;
414 OSMO_SNPRINTF_RET(ret, rem, offset, len);
415 break;
416 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100417 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800418 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200419 ret = vsnprintf(buf + offset, rem, format, ap);
420 if (ret < 0)
421 goto err;
422 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800423
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200424 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
425 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
426 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
427 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
428 && offset > 0 && buf[offset-1] == '\n') {
429 switch (target->print_filename2) {
430 case LOG_FILENAME_NONE:
431 break;
432 case LOG_FILENAME_PATH:
433 offset --;
434 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
435 if (ret < 0)
436 goto err;
437 OSMO_SNPRINTF_RET(ret, rem, offset, len);
438 break;
439 case LOG_FILENAME_BASENAME:
440 offset --;
441 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
442 if (ret < 0)
443 goto err;
444 OSMO_SNPRINTF_RET(ret, rem, offset, len);
445 break;
446 }
447 }
448
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100449 if (target->use_color) {
450 ret = snprintf(buf + offset, rem, "\033[0;m");
451 if (ret < 0)
452 goto err;
453 OSMO_SNPRINTF_RET(ret, rem, offset, len);
454 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200455err:
456 buf[sizeof(buf)-1] = '\0';
457 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800458}
459
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100460/* Catch internal logging category indexes as well as out-of-bounds indexes.
461 * For internal categories, the ID is negative starting with -1; and internal
462 * logging categories are added behind the user categories. For out-of-bounds
463 * indexes, return the index of DLGLOBAL. The returned category index is
464 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
465 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100466static inline int map_subsys(int subsys)
467{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100468 /* Note: comparing signed and unsigned integers */
469
470 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
471 subsys = DLGLOBAL;
472
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100473 if (subsys < 0)
474 subsys = subsys_lib2index(subsys);
475
Neels Hofmeyrca135742016-12-12 14:18:54 +0100476 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100477 subsys = subsys_lib2index(DLGLOBAL);
478
Neels Hofmeyrca135742016-12-12 14:18:54 +0100479 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
480
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100481 return subsys;
482}
483
Maxc65c5b42017-03-15 13:20:23 +0100484static inline bool should_log_to_target(struct log_target *tar, int subsys,
485 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100486{
487 struct log_category *category;
488
489 category = &tar->categories[subsys];
490
491 /* subsystem is not supposed to be logged */
492 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100493 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100494
495 /* Check the global log level */
496 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100497 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100498
499 /* Check the category log level */
500 if (tar->loglevel == 0 && category->loglevel != 0 &&
501 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100502 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100503
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100504 /* Apply filters here... if that becomes messy we will
505 * need to put filters in a list and each filter will
506 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100507 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100508 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100509
510 if (osmo_log_info->filter_fn)
511 return osmo_log_info->filter_fn(&log_context, tar);
512
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100513 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100514 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100515}
516
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200517/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200518 * \param[in] subsys Logging sub-system
519 * \param[in] level Log level
520 * \param[in] file name of source code file
521 * \param[in] cont continuation (1) or new line (0)
522 * \param[in] format format string
523 * \param[in] ap vararg-list containing format string arguments
524 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200525void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200526 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800527{
Harald Welte3ae27582010-03-26 21:24:24 +0800528 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800529
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100530 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200531
Harald Welte28222962011-02-18 20:37:04 +0100532 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200533 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800534
Maxc65c5b42017-03-15 13:20:23 +0100535 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800536 continue;
537
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200538 /* According to the manpage, vsnprintf leaves the value of ap
539 * in undefined state. Since _output uses vsnprintf and it may
540 * be called several times, we have to pass a copy of ap. */
541 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100542 if (tar->raw_output)
543 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
544 else
545 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200546 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800547 }
548}
549
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200550/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200551 * \param[in] subsys Logging sub-system
552 * \param[in] file name of source code file
553 * \param[in] cont continuation (1) or new line (0)
554 * \param[in] format format string
555 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200556void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800557 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800558{
559 va_list ap;
560
561 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200562 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800563 va_end(ap);
564}
565
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200566/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200567 * \param[in] subsys Logging sub-system
568 * \param[in] level Log level
569 * \param[in] file name of source code file
570 * \param[in] cont continuation (1) or new line (0)
571 * \param[in] format format string
572 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200573void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800574{
575 va_list ap;
576
577 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200578 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800579 va_end(ap);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200583 * \param[in] target Log target to be registered
584 */
Harald Welte3ae27582010-03-26 21:24:24 +0800585void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800586{
Harald Welte28222962011-02-18 20:37:04 +0100587 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800588}
589
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200590/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200591 * \param[in] target Log target to be unregistered
592 */
Harald Welte3ae27582010-03-26 21:24:24 +0800593void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800594{
595 llist_del(&target->entry);
596}
597
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200598/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800599void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800600{
Harald Welte3ae27582010-03-26 21:24:24 +0800601 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800602}
603
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200604/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200605 * \param[in] ctx_nr logging context number
606 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200607 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200608 *
609 * A logging context is something like the subscriber identity to which
610 * the currently processed message relates, or the BTS through which it
611 * was received. As soon as this data is known, it can be set using
612 * this function. The main use of context information is for logging
613 * filters.
614 */
Harald Welte3ae27582010-03-26 21:24:24 +0800615int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800616{
Harald Welte3ae27582010-03-26 21:24:24 +0800617 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800618 return -EINVAL;
619
Harald Welte3ae27582010-03-26 21:24:24 +0800620 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800621
622 return 0;
623}
624
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200625/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200626 * \param[in] target Log target to be affected
627 * \param[in] all enable (1) or disable (0) the ALL filter
628 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100629 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100630 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
631 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200632 */
Harald Welte3ae27582010-03-26 21:24:24 +0800633void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800634{
635 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100636 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800637 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100638 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800639}
640
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200641/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200642 * \param[in] target Log target to be affected
643 * \param[in] use_color Use color (1) or don't use color (0)
644 */
Harald Welte3ae27582010-03-26 21:24:24 +0800645void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800646{
647 target->use_color = use_color;
648}
649
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200650/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200651 * \param[in] target Log target to be affected
652 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
653 */
Harald Welte3ae27582010-03-26 21:24:24 +0800654void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800655{
656 target->print_timestamp = print_timestamp;
657}
658
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200659/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100660 * \param[in] target Log target to be affected
661 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
662 *
663 * When both timestamp and extended timestamp is enabled then only
664 * the extended timestamp will be used. The format of the timestamp
665 * is YYYYMMDDhhmmssnnn.
666 */
667void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
668{
669 target->print_ext_timestamp = print_timestamp;
670}
671
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100672/*! Use log_set_print_filename2() instead.
673 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
674 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
675 * behavior, which combined the category in hex with the filename. For example, if the category-hex
676 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
677 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200678 * \param[in] target Log target to be affected
679 * \param[in] print_filename Enable (1) or disable (0) filenames
680 */
681void log_set_print_filename(struct log_target *target, int print_filename)
682{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100683 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
684 log_set_print_category_hex(target, print_filename);
685}
686
687/*! Enable or disable printing of the filename while logging.
688 * \param[in] target Log target to be affected.
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700689 * \param[in] lft An LOG_FILENAME_* enum value.
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100690 * LOG_FILENAME_NONE omits the source file and line information from logs.
691 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
692 */
693void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
694{
695 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200696}
697
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200698/*! Set the position where on a log line the source file info should be logged.
699 * \param[in] target Log target to be affected.
700 * \param[in] pos A LOG_FILENAME_POS_* enum value.
701 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
702 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
703 */
704void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
705{
706 target->print_filename_pos = pos;
707}
708
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200709/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100710 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700711 * \param[in] print_category Enable (1) or disable (0) filenames
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100712 *
713 * Print the category/subsys name in front of every log message.
714 */
715void log_set_print_category(struct log_target *target, int print_category)
716{
717 target->print_category = print_category;
718}
719
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100720/*! Enable or disable printing of the category number in hex ('<000b>').
721 * \param[in] target Log target to be affected.
722 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
723 */
724void log_set_print_category_hex(struct log_target *target, int print_category_hex)
725{
726 target->print_category_hex = print_category_hex;
727}
728
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100729/*! Enable or disable printing of the log level name.
730 * \param[in] target Log target to be affected
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700731 * \param[in] print_level Enable (1) or disable (0) log level name
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100732 *
733 * Print the log level name in front of every log message.
734 */
735void log_set_print_level(struct log_target *target, int print_level)
736{
737 target->print_level = (bool)print_level;
738}
739
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200740/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200741 * \param[in] target Log target to be affected
742 * \param[in] log_level New global log level
743 */
Harald Welte3ae27582010-03-26 21:24:24 +0800744void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800745{
746 target->loglevel = log_level;
747}
748
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200749/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100750 * \param[in] target Log target to be affected
751 * \param[in] category Log category to be affected
752 * \param[in] enable whether to enable or disable the filter
753 * \param[in] level Log level of the filter
754 */
Harald Welte3ae27582010-03-26 21:24:24 +0800755void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800756 int enable, int level)
757{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100758 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800759 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100760 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800761 target->categories[category].enabled = !!enable;
762 target->categories[category].loglevel = level;
763}
764
Harald Welte44c0f632017-01-15 17:58:29 +0100765#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100766static void _file_output(struct log_target *target, unsigned int level,
767 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800768{
Harald Welte0083cd32010-08-25 14:55:44 +0200769 fprintf(target->tgt_file.out, "%s", log);
770 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800771}
Harald Welte44c0f632017-01-15 17:58:29 +0100772#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800773
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200774/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200775 * \returns dynamically-allocated log target
776 * This funcition allocates a \ref log_target and initializes it
777 * with some default values. The newly created target is not
778 * registered yet.
779 */
Harald Welte3ae27582010-03-26 21:24:24 +0800780struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800781{
Harald Welte3ae27582010-03-26 21:24:24 +0800782 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800783 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800784
Max68bf16a2018-01-10 17:00:43 +0100785 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100786
Harald Welte3ae27582010-03-26 21:24:24 +0800787 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800788 if (!target)
789 return NULL;
790
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200791 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200792 struct log_category,
793 osmo_log_info->num_cat);
794 if (!target->categories) {
795 talloc_free(target);
796 return NULL;
797 }
798
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800799 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800800
801 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200802 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800803 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200804 cat->enabled = osmo_log_info->cat[i].enabled;
805 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800806 }
807
808 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800809 target->use_color = 1;
810 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100811 target->print_filename2 = LOG_FILENAME_PATH;
812 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800813
814 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800815 target->loglevel = 0;
816 return target;
817}
818
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200819/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200820 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800821struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800822{
Harald Weltea3b844c2010-03-27 00:04:40 +0800823/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200824#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800825 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800826
Harald Welte3ae27582010-03-26 21:24:24 +0800827 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800828 if (!target)
829 return NULL;
830
Harald Welte28222962011-02-18 20:37:04 +0100831 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200832 target->tgt_file.out = stderr;
833 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800834 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800835#else
836 return NULL;
837#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800838}
839
Harald Welte44c0f632017-01-15 17:58:29 +0100840#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200841/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200842 * \param[in] fname File name of the new log file
843 * \returns Log target in case of success, NULL otherwise
844 */
Harald Welte3086c392010-08-25 19:10:50 +0200845struct log_target *log_target_create_file(const char *fname)
846{
847 struct log_target *target;
848
849 target = log_target_create();
850 if (!target)
851 return NULL;
852
Harald Welte28222962011-02-18 20:37:04 +0100853 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200854 target->tgt_file.out = fopen(fname, "a");
855 if (!target->tgt_file.out)
856 return NULL;
857
858 target->output = _file_output;
859
860 target->tgt_file.fname = talloc_strdup(target, fname);
861
862 return target;
863}
Harald Welte44c0f632017-01-15 17:58:29 +0100864#endif
Harald Welte3086c392010-08-25 19:10:50 +0200865
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200866/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200867 * \param[in] type Log target type
868 * \param[in] fname File name
869 * \returns Log target (if found), NULL otherwise
870 */
Harald Welte28222962011-02-18 20:37:04 +0100871struct log_target *log_target_find(int type, const char *fname)
872{
873 struct log_target *tgt;
874
875 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
876 if (tgt->type != type)
877 continue;
Maxc90f40a2018-01-11 10:52:28 +0100878 switch (tgt->type) {
879 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100880 if (!strcmp(fname, tgt->tgt_file.fname))
881 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100882 break;
883 case LOG_TGT_TYPE_GSMTAP:
884 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
885 return tgt;
886 break;
887 default:
Harald Welte28222962011-02-18 20:37:04 +0100888 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100889 }
Harald Welte28222962011-02-18 20:37:04 +0100890 }
891 return NULL;
892}
893
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200894/*! Unregister, close and delete a log target
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700895 * \param[in] target log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200896void log_target_destroy(struct log_target *target)
897{
898
899 /* just in case, to make sure we don't have any references */
900 log_del_target(target);
901
Harald Welte44c0f632017-01-15 17:58:29 +0100902#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200903 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200904/* since C89/C99 says stderr is a macro, we can safely do this! */
905#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200906 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200907 if (target->tgt_file.out != stderr)
908#endif
909 {
Harald Welte3086c392010-08-25 19:10:50 +0200910 fclose(target->tgt_file.out);
911 target->tgt_file.out = NULL;
912 }
913 }
Harald Welte44c0f632017-01-15 17:58:29 +0100914#endif
Harald Welte3086c392010-08-25 19:10:50 +0200915
916 talloc_free(target);
917}
918
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200919/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200920 * \param[in] target log target to re-open
921 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200922int log_target_file_reopen(struct log_target *target)
923{
924 fclose(target->tgt_file.out);
925
926 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
927 if (!target->tgt_file.out)
928 return -errno;
929
930 /* we assume target->output already to be set */
931
932 return 0;
933}
934
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200935/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200936 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100937int log_targets_reopen(void)
938{
939 struct log_target *tar;
940 int rc = 0;
941
942 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
943 switch (tar->type) {
944 case LOG_TGT_TYPE_FILE:
945 if (log_target_file_reopen(tar) < 0)
946 rc = -1;
947 break;
948 default:
949 break;
950 }
951 }
952
953 return rc;
954}
955
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200956/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +0100957 * \param[in] inf Information regarding logging categories, could be NULL
Vadim Yanitskiy73e66b32019-03-25 21:24:20 +0700958 * \param[in] ctx talloc context for logging allocations
Harald Welte18fc4652011-08-17 14:14:17 +0200959 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +0100960 *
961 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +0200962 */
Harald Welteb43bc042011-06-27 10:29:17 +0200963int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800964{
Harald Welteb43bc042011-06-27 10:29:17 +0200965 int i;
966
967 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
968 if (!tall_log_ctx)
969 return -ENOMEM;
970
971 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
972 if (!osmo_log_info)
973 return -ENOMEM;
974
Max72dfd432018-12-04 11:24:18 +0100975 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
976
977 if (inf) {
978 osmo_log_info->filter_fn = inf->filter_fn;
979 osmo_log_info->num_cat_user = inf->num_cat;
980 osmo_log_info->num_cat += inf->num_cat;
981 }
Harald Welteb43bc042011-06-27 10:29:17 +0200982
983 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
984 struct log_info_cat,
985 osmo_log_info->num_cat);
986 if (!osmo_log_info->cat) {
987 talloc_free(osmo_log_info);
988 osmo_log_info = NULL;
989 return -ENOMEM;
990 }
991
Max72dfd432018-12-04 11:24:18 +0100992 if (inf) { /* copy over the user part */
993 for (i = 0; i < inf->num_cat; i++) {
994 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
995 &inf->cat[i], sizeof(struct log_info_cat));
996 }
Harald Welteb43bc042011-06-27 10:29:17 +0200997 }
998
999 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +02001000 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001001 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001002 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001003 &internal_cat[i], sizeof(struct log_info_cat));
1004 }
1005
1006 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001007}
Harald Welte18fc4652011-08-17 14:14:17 +02001008
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001009/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001010 * This function destroys all targets and releases associated memory */
1011void log_fini(void)
1012{
1013 struct log_target *tar, *tar2;
1014
1015 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1016 log_target_destroy(tar);
1017
1018 talloc_free(osmo_log_info);
1019 osmo_log_info = NULL;
1020 talloc_free(tall_log_ctx);
1021 tall_log_ctx = NULL;
1022}
1023
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001024/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001025 * \returns != 0 if a log entry might get generated by at least one target */
1026int log_check_level(int subsys, unsigned int level)
1027{
1028 struct log_target *tar;
1029
Max68bf16a2018-01-10 17:00:43 +01001030 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001031
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001032 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001033
1034 /* TODO: The following could/should be cached (update on config) */
1035
1036 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001037 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001038 continue;
1039
1040 /* This might get logged (ignoring filters) */
1041 return 1;
1042 }
1043
1044 /* We are sure, that this will not be logged. */
1045 return 0;
1046}
1047
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001048/*! @} */