blob: 09021e422f1498933d1da86f92fcf544f2a82773 [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
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200195 * \param lvl[in] zero-terminated string containing log level name
196 * \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
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200204 * \param lvl[in] numeric log level
205 * \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;
364 tm = time(NULL);
365 timestr = ctime(&tm);
366 timestr[strlen(timestr)-1] = '\0';
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200367 ret = snprintf(buf + offset, rem, "%s ", timestr);
368 if (ret < 0)
369 goto err;
370 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800371 }
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100372 if (target->print_category) {
Neels Hofmeyre6534722018-01-16 03:02:06 +0100373 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
374 target->use_color ? level_color(level) : "",
375 log_category_name(subsys),
376 target->use_color ? "\033[0;m" : "",
377 c_subsys ? c_subsys : "");
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100378 if (ret < 0)
379 goto err;
380 OSMO_SNPRINTF_RET(ret, rem, offset, len);
381 }
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100382 if (target->print_level) {
Neels Hofmeyrf3fa3692018-01-16 02:56:01 +0100383 ret = snprintf(buf + offset, rem, "%s%s%s%s ",
384 target->use_color ? level_color(level) : "",
385 log_level_str(level),
386 target->use_color ? "\033[0;m" : "",
387 c_subsys ? c_subsys : "");
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100388 if (ret < 0)
389 goto err;
390 OSMO_SNPRINTF_RET(ret, rem, offset, len);
391 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100392 if (target->print_category_hex) {
393 ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200394 if (ret < 0)
395 goto err;
396 OSMO_SNPRINTF_RET(ret, rem, offset, len);
397 }
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200398
399 if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) {
400 switch (target->print_filename2) {
401 case LOG_FILENAME_NONE:
402 break;
403 case LOG_FILENAME_PATH:
404 ret = snprintf(buf + offset, rem, "%s:%d ", file, line);
405 if (ret < 0)
406 goto err;
407 OSMO_SNPRINTF_RET(ret, rem, offset, len);
408 break;
409 case LOG_FILENAME_BASENAME:
410 ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line);
411 if (ret < 0)
412 goto err;
413 OSMO_SNPRINTF_RET(ret, rem, offset, len);
414 break;
415 }
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100416 }
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800417 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200418 ret = vsnprintf(buf + offset, rem, format, ap);
419 if (ret < 0)
420 goto err;
421 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800422
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200423 /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log
424 * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an
425 * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */
426 if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END
427 && offset > 0 && buf[offset-1] == '\n') {
428 switch (target->print_filename2) {
429 case LOG_FILENAME_NONE:
430 break;
431 case LOG_FILENAME_PATH:
432 offset --;
433 ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line);
434 if (ret < 0)
435 goto err;
436 OSMO_SNPRINTF_RET(ret, rem, offset, len);
437 break;
438 case LOG_FILENAME_BASENAME:
439 offset --;
440 ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line);
441 if (ret < 0)
442 goto err;
443 OSMO_SNPRINTF_RET(ret, rem, offset, len);
444 break;
445 }
446 }
447
Neels Hofmeyrc4759882018-01-16 02:10:48 +0100448 if (target->use_color) {
449 ret = snprintf(buf + offset, rem, "\033[0;m");
450 if (ret < 0)
451 goto err;
452 OSMO_SNPRINTF_RET(ret, rem, offset, len);
453 }
Pablo Neira Ayuso7503fb82011-05-03 22:32:43 +0200454err:
455 buf[sizeof(buf)-1] = '\0';
456 target->output(target, level, buf);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800457}
458
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100459/* Catch internal logging category indexes as well as out-of-bounds indexes.
460 * For internal categories, the ID is negative starting with -1; and internal
461 * logging categories are added behind the user categories. For out-of-bounds
462 * indexes, return the index of DLGLOBAL. The returned category index is
463 * guaranteed to exist in osmo_log_info, otherwise the program would abort,
464 * which should never happen unless even the DLGLOBAL category is missing. */
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100465static inline int map_subsys(int subsys)
466{
Neels Hofmeyr74802262016-12-12 16:00:24 +0100467 /* Note: comparing signed and unsigned integers */
468
469 if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user)
470 subsys = DLGLOBAL;
471
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100472 if (subsys < 0)
473 subsys = subsys_lib2index(subsys);
474
Neels Hofmeyrca135742016-12-12 14:18:54 +0100475 if (subsys < 0 || subsys >= osmo_log_info->num_cat)
Neels Hofmeyr42240de2016-12-12 15:13:56 +0100476 subsys = subsys_lib2index(DLGLOBAL);
477
Neels Hofmeyrca135742016-12-12 14:18:54 +0100478 OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat));
479
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100480 return subsys;
481}
482
Maxc65c5b42017-03-15 13:20:23 +0100483static inline bool should_log_to_target(struct log_target *tar, int subsys,
484 int level)
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100485{
486 struct log_category *category;
487
488 category = &tar->categories[subsys];
489
490 /* subsystem is not supposed to be logged */
491 if (!category->enabled)
Maxc65c5b42017-03-15 13:20:23 +0100492 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100493
494 /* Check the global log level */
495 if (tar->loglevel != 0 && level < tar->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100496 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100497
498 /* Check the category log level */
499 if (tar->loglevel == 0 && category->loglevel != 0 &&
500 level < category->loglevel)
Maxc65c5b42017-03-15 13:20:23 +0100501 return false;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100502
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100503 /* Apply filters here... if that becomes messy we will
504 * need to put filters in a list and each filter will
505 * say stop, continue, output */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100506 if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0)
Maxc65c5b42017-03-15 13:20:23 +0100507 return true;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100508
509 if (osmo_log_info->filter_fn)
510 return osmo_log_info->filter_fn(&log_context, tar);
511
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100512 /* TODO: Check the filter/selector too? */
Maxc65c5b42017-03-15 13:20:23 +0100513 return true;
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100514}
515
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200516/*! vararg version of logging function
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200517 * \param[in] subsys Logging sub-system
518 * \param[in] level Log level
519 * \param[in] file name of source code file
520 * \param[in] cont continuation (1) or new line (0)
521 * \param[in] format format string
522 * \param[in] ap vararg-list containing format string arguments
523 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200524void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +0200525 int cont, const char *format, va_list ap)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800526{
Harald Welte3ae27582010-03-26 21:24:24 +0800527 struct log_target *tar;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800528
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +0100529 subsys = map_subsys(subsys);
Harald Welteb43bc042011-06-27 10:29:17 +0200530
Harald Welte28222962011-02-18 20:37:04 +0100531 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200532 va_list bp;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800533
Maxc65c5b42017-03-15 13:20:23 +0100534 if (!should_log_to_target(tar, subsys, level))
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800535 continue;
536
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200537 /* According to the manpage, vsnprintf leaves the value of ap
538 * in undefined state. Since _output uses vsnprintf and it may
539 * be called several times, we have to pass a copy of ap. */
540 va_copy(bp, ap);
Harald Welted7c0a372016-12-02 13:52:59 +0100541 if (tar->raw_output)
542 tar->raw_output(tar, subsys, level, file, line, cont, format, bp);
543 else
544 _output(tar, subsys, level, file, line, cont, format, bp);
Pablo Neira Ayusodd93bf42011-05-19 01:40:43 +0200545 va_end(bp);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800546 }
547}
548
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200549/*! logging function used by DEBUGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200550 * \param[in] subsys Logging sub-system
551 * \param[in] file name of source code file
552 * \param[in] cont continuation (1) or new line (0)
553 * \param[in] format format string
554 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200555void logp(int subsys, const char *file, int line, int cont,
Harald Welte3ae27582010-03-26 21:24:24 +0800556 const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800557{
558 va_list ap;
559
560 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200561 osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800562 va_end(ap);
563}
564
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200565/*! logging function used by LOGP() macro
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200566 * \param[in] subsys Logging sub-system
567 * \param[in] level Log level
568 * \param[in] file name of source code file
569 * \param[in] cont continuation (1) or new line (0)
570 * \param[in] format format string
571 */
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +0200572void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800573{
574 va_list ap;
575
576 va_start(ap, format);
Harald Welte36c5a3e2011-08-27 14:33:19 +0200577 osmo_vlogp(subsys, level, file, line, cont, format, ap);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800578 va_end(ap);
579}
580
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200581/*! Register a new log target with the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200582 * \param[in] target Log target to be registered
583 */
Harald Welte3ae27582010-03-26 21:24:24 +0800584void log_add_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800585{
Harald Welte28222962011-02-18 20:37:04 +0100586 llist_add_tail(&target->entry, &osmo_log_target_list);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800587}
588
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200589/*! Unregister a log target from the logging core
Harald Welte18fc4652011-08-17 14:14:17 +0200590 * \param[in] target Log target to be unregistered
591 */
Harald Welte3ae27582010-03-26 21:24:24 +0800592void log_del_target(struct log_target *target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800593{
594 llist_del(&target->entry);
595}
596
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200597/*! Reset (clear) the logging context */
Harald Welte3ae27582010-03-26 21:24:24 +0800598void log_reset_context(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800599{
Harald Welte3ae27582010-03-26 21:24:24 +0800600 memset(&log_context, 0, sizeof(log_context));
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800601}
602
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200603/*! Set the logging context
Harald Welte18fc4652011-08-17 14:14:17 +0200604 * \param[in] ctx_nr logging context number
605 * \param[in] value value to which the context is to be set
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200606 * \returns 0 in case of success; negative otherwise
Harald Welte18fc4652011-08-17 14:14:17 +0200607 *
608 * A logging context is something like the subscriber identity to which
609 * the currently processed message relates, or the BTS through which it
610 * was received. As soon as this data is known, it can be set using
611 * this function. The main use of context information is for logging
612 * filters.
613 */
Harald Welte3ae27582010-03-26 21:24:24 +0800614int log_set_context(uint8_t ctx_nr, void *value)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800615{
Harald Welte3ae27582010-03-26 21:24:24 +0800616 if (ctx_nr > LOG_MAX_CTX)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800617 return -EINVAL;
618
Harald Welte3ae27582010-03-26 21:24:24 +0800619 log_context.ctx[ctx_nr] = value;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800620
621 return 0;
622}
623
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200624/*! Enable the \ref LOG_FLT_ALL log filter
Harald Welte18fc4652011-08-17 14:14:17 +0200625 * \param[in] target Log target to be affected
626 * \param[in] all enable (1) or disable (0) the ALL filter
627 *
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100628 * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100629 * printed. It acts as a wildcard. Setting it to \a 1 means there is no
630 * filtering.
Harald Welte18fc4652011-08-17 14:14:17 +0200631 */
Harald Welte3ae27582010-03-26 21:24:24 +0800632void log_set_all_filter(struct log_target *target, int all)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800633{
634 if (all)
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100635 target->filter_map |= (1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800636 else
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100637 target->filter_map &= ~(1 << LOG_FLT_ALL);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800638}
639
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200640/*! Enable or disable the use of colored output
Harald Welte18fc4652011-08-17 14:14:17 +0200641 * \param[in] target Log target to be affected
642 * \param[in] use_color Use color (1) or don't use color (0)
643 */
Harald Welte3ae27582010-03-26 21:24:24 +0800644void log_set_use_color(struct log_target *target, int use_color)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800645{
646 target->use_color = use_color;
647}
648
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200649/*! Enable or disable printing of timestamps while logging
Harald Welte18fc4652011-08-17 14:14:17 +0200650 * \param[in] target Log target to be affected
651 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
652 */
Harald Welte3ae27582010-03-26 21:24:24 +0800653void log_set_print_timestamp(struct log_target *target, int print_timestamp)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800654{
655 target->print_timestamp = print_timestamp;
656}
657
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200658/*! Enable or disable printing of extended timestamps while logging
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100659 * \param[in] target Log target to be affected
660 * \param[in] print_timestamp Enable (1) or disable (0) timestamps
661 *
662 * When both timestamp and extended timestamp is enabled then only
663 * the extended timestamp will be used. The format of the timestamp
664 * is YYYYMMDDhhmmssnnn.
665 */
666void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp)
667{
668 target->print_ext_timestamp = print_timestamp;
669}
670
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100671/*! Use log_set_print_filename2() instead.
672 * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call
673 * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy
674 * behavior, which combined the category in hex with the filename. For example, if the category-hex
675 * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as
676 * dependent projects) would fail since they expect the category to disappear along with the filename.
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200677 * \param[in] target Log target to be affected
678 * \param[in] print_filename Enable (1) or disable (0) filenames
679 */
680void log_set_print_filename(struct log_target *target, int print_filename)
681{
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100682 log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE);
683 log_set_print_category_hex(target, print_filename);
684}
685
686/*! Enable or disable printing of the filename while logging.
687 * \param[in] target Log target to be affected.
688 * \param[in] print_filename An LOG_FILENAME_* enum value.
689 * LOG_FILENAME_NONE omits the source file and line information from logs.
690 * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros.
691 */
692void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
693{
694 target->print_filename2 = lft;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200695}
696
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200697/*! Set the position where on a log line the source file info should be logged.
698 * \param[in] target Log target to be affected.
699 * \param[in] pos A LOG_FILENAME_POS_* enum value.
700 * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message.
701 * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the
702 */
703void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
704{
705 target->print_filename_pos = pos;
706}
707
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200708/*! Enable or disable printing of the category name
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100709 * \param[in] target Log target to be affected
710 * \param[in] print_catname Enable (1) or disable (0) filenames
711 *
712 * Print the category/subsys name in front of every log message.
713 */
714void log_set_print_category(struct log_target *target, int print_category)
715{
716 target->print_category = print_category;
717}
718
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100719/*! Enable or disable printing of the category number in hex ('<000b>').
720 * \param[in] target Log target to be affected.
721 * \param[in] print_category_hex Enable (1) or disable (0) hex category.
722 */
723void log_set_print_category_hex(struct log_target *target, int print_category_hex)
724{
725 target->print_category_hex = print_category_hex;
726}
727
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100728/*! Enable or disable printing of the log level name.
729 * \param[in] target Log target to be affected
730 * \param[in] print_catname Enable (1) or disable (0) filenames
731 *
732 * Print the log level name in front of every log message.
733 */
734void log_set_print_level(struct log_target *target, int print_level)
735{
736 target->print_level = (bool)print_level;
737}
738
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200739/*! Set the global log level for a given log target
Harald Welte18fc4652011-08-17 14:14:17 +0200740 * \param[in] target Log target to be affected
741 * \param[in] log_level New global log level
742 */
Harald Welte3ae27582010-03-26 21:24:24 +0800743void log_set_log_level(struct log_target *target, int log_level)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800744{
745 target->loglevel = log_level;
746}
747
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200748/*! Set a category filter on a given log target
Harald Weltede6e4982012-12-06 21:25:27 +0100749 * \param[in] target Log target to be affected
750 * \param[in] category Log category to be affected
751 * \param[in] enable whether to enable or disable the filter
752 * \param[in] level Log level of the filter
753 */
Harald Welte3ae27582010-03-26 21:24:24 +0800754void log_set_category_filter(struct log_target *target, int category,
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800755 int enable, int level)
756{
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100757 if (!target)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800758 return;
Neels Hofmeyr886d6fd2016-12-12 13:49:03 +0100759 category = map_subsys(category);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800760 target->categories[category].enabled = !!enable;
761 target->categories[category].loglevel = level;
762}
763
Harald Welte44c0f632017-01-15 17:58:29 +0100764#if (!EMBEDDED)
Harald Welte76e72ab2011-02-17 15:52:39 +0100765static void _file_output(struct log_target *target, unsigned int level,
766 const char *log)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800767{
Harald Welte0083cd32010-08-25 14:55:44 +0200768 fprintf(target->tgt_file.out, "%s", log);
769 fflush(target->tgt_file.out);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800770}
Harald Welte44c0f632017-01-15 17:58:29 +0100771#endif
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800772
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200773/*! Create a new log target skeleton
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200774 * \returns dynamically-allocated log target
775 * This funcition allocates a \ref log_target and initializes it
776 * with some default values. The newly created target is not
777 * registered yet.
778 */
Harald Welte3ae27582010-03-26 21:24:24 +0800779struct log_target *log_target_create(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800780{
Harald Welte3ae27582010-03-26 21:24:24 +0800781 struct log_target *target;
Harald Weltecc6313c2010-03-26 22:04:03 +0800782 unsigned int i;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800783
Max68bf16a2018-01-10 17:00:43 +0100784 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +0100785
Harald Welte3ae27582010-03-26 21:24:24 +0800786 target = talloc_zero(tall_log_ctx, struct log_target);
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800787 if (!target)
788 return NULL;
789
Pau Espin Pedrol9d4a36e2018-07-26 11:55:33 +0200790 target->categories = talloc_zero_array(target,
Harald Welteb43bc042011-06-27 10:29:17 +0200791 struct log_category,
792 osmo_log_info->num_cat);
793 if (!target->categories) {
794 talloc_free(target);
795 return NULL;
796 }
797
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800798 INIT_LLIST_HEAD(&target->entry);
Harald Weltecc6313c2010-03-26 22:04:03 +0800799
800 /* initialize the per-category enabled/loglevel from defaults */
Harald Welte4ebdf742010-05-19 19:54:00 +0200801 for (i = 0; i < osmo_log_info->num_cat; i++) {
Harald Weltecc6313c2010-03-26 22:04:03 +0800802 struct log_category *cat = &target->categories[i];
Harald Welte4ebdf742010-05-19 19:54:00 +0200803 cat->enabled = osmo_log_info->cat[i].enabled;
804 cat->loglevel = osmo_log_info->cat[i].loglevel;
Harald Weltecc6313c2010-03-26 22:04:03 +0800805 }
806
807 /* global settings */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800808 target->use_color = 1;
809 target->print_timestamp = 0;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100810 target->print_filename2 = LOG_FILENAME_PATH;
811 target->print_category_hex = true;
Harald Weltecc6313c2010-03-26 22:04:03 +0800812
813 /* global log level */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800814 target->loglevel = 0;
815 return target;
816}
817
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200818/*! Create the STDERR log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200819 * \returns dynamically-allocated \ref log_target for STDERR */
Harald Welte3ae27582010-03-26 21:24:24 +0800820struct log_target *log_target_create_stderr(void)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800821{
Harald Weltea3b844c2010-03-27 00:04:40 +0800822/* since C89/C99 says stderr is a macro, we can safely do this! */
Harald Welteb93ce5a2017-05-15 10:58:15 +0200823#if !EMBEDDED && defined(stderr)
Harald Welte3ae27582010-03-26 21:24:24 +0800824 struct log_target *target;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800825
Harald Welte3ae27582010-03-26 21:24:24 +0800826 target = log_target_create();
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800827 if (!target)
828 return NULL;
829
Harald Welte28222962011-02-18 20:37:04 +0100830 target->type = LOG_TGT_TYPE_STDERR;
Harald Welte0083cd32010-08-25 14:55:44 +0200831 target->tgt_file.out = stderr;
832 target->output = _file_output;
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800833 return target;
Harald Weltea3b844c2010-03-27 00:04:40 +0800834#else
835 return NULL;
836#endif /* stderr */
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800837}
838
Harald Welte44c0f632017-01-15 17:58:29 +0100839#if (!EMBEDDED)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200840/*! Create a new file-based log target
Harald Welte18fc4652011-08-17 14:14:17 +0200841 * \param[in] fname File name of the new log file
842 * \returns Log target in case of success, NULL otherwise
843 */
Harald Welte3086c392010-08-25 19:10:50 +0200844struct log_target *log_target_create_file(const char *fname)
845{
846 struct log_target *target;
847
848 target = log_target_create();
849 if (!target)
850 return NULL;
851
Harald Welte28222962011-02-18 20:37:04 +0100852 target->type = LOG_TGT_TYPE_FILE;
Harald Welte3086c392010-08-25 19:10:50 +0200853 target->tgt_file.out = fopen(fname, "a");
854 if (!target->tgt_file.out)
855 return NULL;
856
857 target->output = _file_output;
858
859 target->tgt_file.fname = talloc_strdup(target, fname);
860
861 return target;
862}
Harald Welte44c0f632017-01-15 17:58:29 +0100863#endif
Harald Welte3086c392010-08-25 19:10:50 +0200864
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200865/*! Find a registered log target
Harald Welte18fc4652011-08-17 14:14:17 +0200866 * \param[in] type Log target type
867 * \param[in] fname File name
868 * \returns Log target (if found), NULL otherwise
869 */
Harald Welte28222962011-02-18 20:37:04 +0100870struct log_target *log_target_find(int type, const char *fname)
871{
872 struct log_target *tgt;
873
874 llist_for_each_entry(tgt, &osmo_log_target_list, entry) {
875 if (tgt->type != type)
876 continue;
Maxc90f40a2018-01-11 10:52:28 +0100877 switch (tgt->type) {
878 case LOG_TGT_TYPE_FILE:
Harald Welte28222962011-02-18 20:37:04 +0100879 if (!strcmp(fname, tgt->tgt_file.fname))
880 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100881 break;
882 case LOG_TGT_TYPE_GSMTAP:
883 if (!strcmp(fname, tgt->tgt_gsmtap.hostname))
884 return tgt;
885 break;
886 default:
Harald Welte28222962011-02-18 20:37:04 +0100887 return tgt;
Maxc90f40a2018-01-11 10:52:28 +0100888 }
Harald Welte28222962011-02-18 20:37:04 +0100889 }
890 return NULL;
891}
892
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200893/*! Unregister, close and delete a log target
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200894 * \param target[in] log target to unregister, close and delete */
Harald Welte3086c392010-08-25 19:10:50 +0200895void log_target_destroy(struct log_target *target)
896{
897
898 /* just in case, to make sure we don't have any references */
899 log_del_target(target);
900
Harald Welte44c0f632017-01-15 17:58:29 +0100901#if (!EMBEDDED)
Harald Welte3086c392010-08-25 19:10:50 +0200902 if (target->output == &_file_output) {
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200903/* since C89/C99 says stderr is a macro, we can safely do this! */
904#ifdef stderr
Harald Welte3086c392010-08-25 19:10:50 +0200905 /* don't close stderr */
Sylvain Munautaf5ee342010-09-17 14:38:17 +0200906 if (target->tgt_file.out != stderr)
907#endif
908 {
Harald Welte3086c392010-08-25 19:10:50 +0200909 fclose(target->tgt_file.out);
910 target->tgt_file.out = NULL;
911 }
912 }
Harald Welte44c0f632017-01-15 17:58:29 +0100913#endif
Harald Welte3086c392010-08-25 19:10:50 +0200914
915 talloc_free(target);
916}
917
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200918/*! close and re-open a log file (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200919 * \param[in] target log target to re-open
920 * \returns 0 in case of success; negative otherwise */
Harald Welte3086c392010-08-25 19:10:50 +0200921int log_target_file_reopen(struct log_target *target)
922{
923 fclose(target->tgt_file.out);
924
925 target->tgt_file.out = fopen(target->tgt_file.fname, "a");
926 if (!target->tgt_file.out)
927 return -errno;
928
929 /* we assume target->output already to be set */
930
931 return 0;
932}
933
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200934/*! close and re-open all log files (for log file rotation)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200935 * \returns 0 in case of success; negative otherwise */
Harald Welte4de854d2013-03-18 19:01:40 +0100936int log_targets_reopen(void)
937{
938 struct log_target *tar;
939 int rc = 0;
940
941 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
942 switch (tar->type) {
943 case LOG_TGT_TYPE_FILE:
944 if (log_target_file_reopen(tar) < 0)
945 rc = -1;
946 break;
947 default:
948 break;
949 }
950 }
951
952 return rc;
953}
954
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200955/*! Initialize the Osmocom logging core
Max72dfd432018-12-04 11:24:18 +0100956 * \param[in] inf Information regarding logging categories, could be NULL
Harald Welte18fc4652011-08-17 14:14:17 +0200957 * \param[in] ctx \ref talloc context for logging allocations
958 * \returns 0 in case of success, negative in case of error
Max72dfd432018-12-04 11:24:18 +0100959 *
960 * If inf is NULL then only library-internal categories are initialized.
Harald Welte18fc4652011-08-17 14:14:17 +0200961 */
Harald Welteb43bc042011-06-27 10:29:17 +0200962int log_init(const struct log_info *inf, void *ctx)
Harald Welte4a2bb9e2010-03-26 09:33:40 +0800963{
Harald Welteb43bc042011-06-27 10:29:17 +0200964 int i;
965
966 tall_log_ctx = talloc_named_const(ctx, 1, "logging");
967 if (!tall_log_ctx)
968 return -ENOMEM;
969
970 osmo_log_info = talloc_zero(tall_log_ctx, struct log_info);
971 if (!osmo_log_info)
972 return -ENOMEM;
973
Max72dfd432018-12-04 11:24:18 +0100974 osmo_log_info->num_cat = ARRAY_SIZE(internal_cat);
975
976 if (inf) {
977 osmo_log_info->filter_fn = inf->filter_fn;
978 osmo_log_info->num_cat_user = inf->num_cat;
979 osmo_log_info->num_cat += inf->num_cat;
980 }
Harald Welteb43bc042011-06-27 10:29:17 +0200981
982 osmo_log_info->cat = talloc_zero_array(osmo_log_info,
983 struct log_info_cat,
984 osmo_log_info->num_cat);
985 if (!osmo_log_info->cat) {
986 talloc_free(osmo_log_info);
987 osmo_log_info = NULL;
988 return -ENOMEM;
989 }
990
Max72dfd432018-12-04 11:24:18 +0100991 if (inf) { /* copy over the user part */
992 for (i = 0; i < inf->num_cat; i++) {
993 memcpy((struct log_info_cat *) &osmo_log_info->cat[i],
994 &inf->cat[i], sizeof(struct log_info_cat));
995 }
Harald Welteb43bc042011-06-27 10:29:17 +0200996 }
997
998 /* copy over the library part */
Harald Welte9fe16522011-06-27 14:00:03 +0200999 for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
Harald Weltece9fec32011-06-27 14:19:16 +02001000 unsigned int cn = osmo_log_info->num_cat_user + i;
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +02001001 memcpy((struct log_info_cat *) &osmo_log_info->cat[cn],
Harald Welte9fe16522011-06-27 14:00:03 +02001002 &internal_cat[i], sizeof(struct log_info_cat));
1003 }
1004
1005 return 0;
Harald Welte4a2bb9e2010-03-26 09:33:40 +08001006}
Harald Welte18fc4652011-08-17 14:14:17 +02001007
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001008/* De-initialize the Osmocom logging core
Harald Welte69e6c3c2016-04-20 10:41:27 +02001009 * This function destroys all targets and releases associated memory */
1010void log_fini(void)
1011{
1012 struct log_target *tar, *tar2;
1013
1014 llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry)
1015 log_target_destroy(tar);
1016
1017 talloc_free(osmo_log_info);
1018 osmo_log_info = NULL;
1019 talloc_free(tall_log_ctx);
1020 tall_log_ctx = NULL;
1021}
1022
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001023/*! Check whether a log entry will be generated.
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001024 * \returns != 0 if a log entry might get generated by at least one target */
1025int log_check_level(int subsys, unsigned int level)
1026{
1027 struct log_target *tar;
1028
Max68bf16a2018-01-10 17:00:43 +01001029 assert_loginfo(__func__);
Harald Welte18a7d812017-03-16 23:54:55 +01001030
Holger Hans Peter Freythere0dc6a12015-12-21 14:45:16 +01001031 subsys = map_subsys(subsys);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001032
1033 /* TODO: The following could/should be cached (update on config) */
1034
1035 llist_for_each_entry(tar, &osmo_log_target_list, entry) {
Maxc65c5b42017-03-15 13:20:23 +01001036 if (!should_log_to_target(tar, subsys, level))
Jacob Erlbeckde6dd722015-11-17 11:52:24 +01001037 continue;
1038
1039 /* This might get logged (ignoring filters) */
1040 return 1;
1041 }
1042
1043 /* We are sure, that this will not be logged. */
1044 return 0;
1045}
1046
Sylvain Munautdca7d2c2012-04-18 21:53:23 +02001047/*! @} */