Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file logging.c |
| 2 | * Debugging/Logging support code. */ |
| 3 | /* |
| 4 | * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 5 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 10 | * 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 Welte | f01b238 | 2017-10-16 13:55:34 +0200 | [diff] [blame] | 26 | /*! \addtogroup logging |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 27 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 28 | * libosmocore Logging sub-system |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 29 | * |
| 30 | * \file logging.c */ |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 31 | |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 32 | #include "../config.h" |
| 33 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 34 | #include <stdarg.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 38 | |
| 39 | #ifdef HAVE_STRINGS_H |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 40 | #include <strings.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 41 | #endif |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 42 | #include <time.h> |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 43 | #include <sys/time.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 44 | #include <errno.h> |
| 45 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 46 | #include <osmocom/core/talloc.h> |
| 47 | #include <osmocom/core/utils.h> |
| 48 | #include <osmocom/core/logging.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 49 | #include <osmocom/core/timer.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 50 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 51 | #include <osmocom/vty/logging.h> /* for LOGGING_STR. */ |
| 52 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 53 | osmo_static_assert(_LOG_CTX_COUNT <= ARRAY_SIZE(((struct log_context*)NULL)->ctx), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 54 | enum_logging_ctx_items_fit_in_struct_log_context); |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 55 | osmo_static_assert(_LOG_FLT_COUNT <= ARRAY_SIZE(((struct log_target*)NULL)->filter_data), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 56 | enum_logging_filters_fit_in_log_target_filter_data); |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 57 | osmo_static_assert(_LOG_FLT_COUNT <= 8*sizeof(((struct log_target*)NULL)->filter_map), |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 58 | enum_logging_filters_fit_in_log_target_filter_map); |
| 59 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 60 | struct log_info *osmo_log_info; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 61 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 62 | static struct log_context log_context; |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 63 | void *tall_log_ctx = NULL; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 64 | LLIST_HEAD(osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 65 | |
Neels Hofmeyr | 098038a | 2018-09-11 23:49:13 +0200 | [diff] [blame] | 66 | const struct value_string loglevel_strs[] = { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 67 | { 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 Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 75 | #define INT2IDX(x) (-1*(x)-1) |
| 76 | static 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 | }, |
root | 8a996b4 | 2011-09-26 11:22:21 +0200 | [diff] [blame] | 83 | [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */ |
| 84 | .name = "DLLAPD", |
| 85 | .description = "LAPD in libosmogsm", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 86 | .loglevel = LOGL_NOTICE, |
| 87 | .enabled = 1, |
| 88 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 89 | [INT2IDX(DLINP)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 90 | .name = "DLINP", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 91 | .description = "A-bis Intput Subsystem", |
| 92 | .loglevel = LOGL_NOTICE, |
| 93 | .enabled = 1, |
| 94 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 95 | [INT2IDX(DLMUX)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 96 | .name = "DLMUX", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 97 | .description = "A-bis B-Subchannel TRAU Frame Multiplex", |
| 98 | .loglevel = LOGL_NOTICE, |
| 99 | .enabled = 1, |
| 100 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 101 | [INT2IDX(DLMI)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 102 | .name = "DLMI", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 103 | .description = "A-bis Input Driver for Signalling", |
| 104 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 105 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 106 | [INT2IDX(DLMIB)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 107 | .name = "DLMIB", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 108 | .description = "A-bis Input Driver for B-Channels (voice)", |
| 109 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 110 | }, |
Andreas Eversberg | c626da9 | 2011-10-28 03:53:50 +0200 | [diff] [blame] | 111 | [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 Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 117 | [INT2IDX(DLCTRL)] = { |
| 118 | .name = "DLCTRL", |
| 119 | .description = "Control Interface", |
| 120 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 121 | }, |
Holger Hans Peter Freyther | a5dc19d | 2014-12-04 14:35:21 +0100 | [diff] [blame] | 122 | [INT2IDX(DLGTP)] = { |
| 123 | .name = "DLGTP", |
| 124 | .description = "GPRS GTP library", |
| 125 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 126 | }, |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 127 | [INT2IDX(DLSTATS)] = { |
| 128 | .name = "DLSTATS", |
| 129 | .description = "Statistics messages and logging", |
| 130 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 131 | }, |
Neels Hofmeyr | 9795cf1 | 2016-12-10 17:01:06 +0100 | [diff] [blame] | 132 | [INT2IDX(DLGSUP)] = { |
| 133 | .name = "DLGSUP", |
| 134 | .description = "Generic Subscriber Update Protocol", |
| 135 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 136 | }, |
Harald Welte | c0f0007 | 2016-04-27 18:32:35 +0200 | [diff] [blame] | 137 | [INT2IDX(DLOAP)] = { |
| 138 | .name = "DLOAP", |
| 139 | .description = "Osmocom Authentication Protocol", |
| 140 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 141 | }, |
Harald Welte | 059c404 | 2017-04-03 22:20:49 +0200 | [diff] [blame] | 142 | [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 Hofmeyr | a7ccf61 | 2017-07-11 18:43:09 +0200 | [diff] [blame] | 162 | [INT2IDX(DLMGCP)] = { |
| 163 | .name = "DLMGCP", |
| 164 | .description = "libosmo-mgcp Media Gateway Control Protocol", |
| 165 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 166 | }, |
Pau Espin Pedrol | 8fd8557 | 2018-02-27 19:43:10 +0100 | [diff] [blame] | 167 | [INT2IDX(DLJIBUF)] = { |
| 168 | .name = "DLJIBUF", |
| 169 | .description = "libosmo-netif Jitter Buffer", |
| 170 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 171 | }, |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 174 | void assert_loginfo(const char *src) |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 175 | { |
| 176 | if (!osmo_log_info) { |
| 177 | fprintf(stderr, "ERROR: osmo_log_info == NULL! " |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 178 | "You must call log_init() before using logging in %s()!\n", src); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 179 | OSMO_ASSERT(osmo_log_info); |
| 180 | } |
| 181 | } |
| 182 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 183 | /* special magic for negative (library-internal) log subsystem numbers */ |
| 184 | static int subsys_lib2index(int subsys) |
| 185 | { |
| 186 | return (subsys * -1) + (osmo_log_info->num_cat_user-1); |
| 187 | } |
| 188 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 189 | /*! Parse a human-readable log level into a numeric value |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 190 | * \param lvl[in] zero-terminated string containing log level name |
| 191 | * \returns numeric log level |
| 192 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 193 | int log_parse_level(const char *lvl) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 194 | { |
| 195 | return get_string_value(loglevel_strs, lvl); |
| 196 | } |
| 197 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 198 | /*! convert a numeric log level into human-readable string |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 199 | * \param lvl[in] numeric log level |
| 200 | * \returns zero-terminated string (log level name) |
| 201 | */ |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 202 | const char *log_level_str(unsigned int lvl) |
| 203 | { |
| 204 | return get_value_string(loglevel_strs, lvl); |
| 205 | } |
| 206 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 207 | /*! parse a human-readable log category into numeric form |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 208 | * \param[in] category human-readable log category name |
| 209 | * \returns numeric category value, or -EINVAL otherwise |
| 210 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 211 | int log_parse_category(const char *category) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 212 | { |
| 213 | int i; |
| 214 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 215 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 216 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 217 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 218 | if (osmo_log_info->cat[i].name == NULL) |
| 219 | continue; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 220 | if (!strcasecmp(osmo_log_info->cat[i].name+1, category)) |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 221 | return i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 227 | /*! parse the log category mask |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 228 | * \param[in] target log target to be configured |
| 229 | * \param[in] _mask log category mask string |
| 230 | * |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 231 | * The format can be this: category1:category2:category3 |
| 232 | * or category1,2:category2,3:... |
| 233 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 234 | void log_parse_category_mask(struct log_target* target, const char *_mask) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 235 | { |
| 236 | int i = 0; |
| 237 | char *mask = strdup(_mask); |
| 238 | char *category_token = NULL; |
| 239 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 240 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 241 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 242 | /* Disable everything to enable it afterwards */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 243 | for (i = 0; i < osmo_log_info->num_cat; ++i) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 244 | target->categories[i].enabled = 0; |
| 245 | |
| 246 | category_token = strtok(mask, ":"); |
Neels Hofmeyr | da1b20c | 2016-04-14 15:12:16 +0200 | [diff] [blame] | 247 | OSMO_ASSERT(category_token); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 248 | do { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 249 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Nico Golde | 0262d3f | 2012-09-21 17:44:58 +0200 | [diff] [blame] | 250 | size_t length, cat_length; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 251 | char* colon = strstr(category_token, ","); |
Nico Golde | 0262d3f | 2012-09-21 17:44:58 +0200 | [diff] [blame] | 252 | |
| 253 | if (!osmo_log_info->cat[i].name) |
| 254 | continue; |
| 255 | |
| 256 | length = strlen(category_token); |
| 257 | cat_length = strlen(osmo_log_info->cat[i].name); |
Pablo Neira Ayuso | 300e78d | 2011-08-11 13:24:18 +0200 | [diff] [blame] | 258 | |
| 259 | /* Use longest length not to match subocurrences. */ |
| 260 | if (cat_length > length) |
| 261 | length = cat_length; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 262 | |
| 263 | if (colon) |
| 264 | length = colon - category_token; |
| 265 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 266 | if (strncasecmp(osmo_log_info->cat[i].name, |
| 267 | category_token, length) == 0) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 268 | int level = 0; |
| 269 | |
| 270 | if (colon) |
| 271 | level = atoi(colon+1); |
| 272 | |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 273 | target->categories[i].enabled = 1; |
| 274 | target->categories[i].loglevel = level; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } while ((category_token = strtok(NULL, ":"))); |
| 278 | |
| 279 | free(mask); |
| 280 | } |
| 281 | |
| 282 | static const char* color(int subsys) |
| 283 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 284 | if (subsys < osmo_log_info->num_cat) |
| 285 | return osmo_log_info->cat[subsys].color; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 286 | |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 287 | return NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 290 | static const struct value_string level_colors[] = { |
| 291 | { LOGL_DEBUG, "\033[1;34m" }, |
| 292 | { LOGL_INFO, "\033[1;32m" }, |
| 293 | { LOGL_NOTICE, "\033[1;33m" }, |
| 294 | { LOGL_ERROR, "\033[1;31m" }, |
| 295 | { LOGL_FATAL, "\033[1;31m" }, |
| 296 | { 0, NULL } |
| 297 | }; |
| 298 | |
| 299 | static const char *level_color(int level) |
| 300 | { |
| 301 | const char *c = get_value_string_or_null(level_colors, level); |
| 302 | if (!c) |
| 303 | return get_value_string(level_colors, LOGL_FATAL); |
| 304 | return c; |
| 305 | } |
| 306 | |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 307 | const char* log_category_name(int subsys) |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 308 | { |
| 309 | if (subsys < osmo_log_info->num_cat) |
| 310 | return osmo_log_info->cat[subsys].name; |
| 311 | |
| 312 | return NULL; |
| 313 | } |
| 314 | |
Neels Hofmeyr | 0e2a943 | 2018-01-16 02:49:48 +0100 | [diff] [blame] | 315 | static const char *const_basename(const char *path) |
| 316 | { |
| 317 | const char *bn = strrchr(path, '/'); |
| 318 | if (!bn || !bn[1]) |
| 319 | return path; |
| 320 | return bn + 1; |
| 321 | } |
| 322 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 323 | static void _output(struct log_target *target, unsigned int subsys, |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 324 | unsigned int level, const char *file, int line, int cont, |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 325 | const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 326 | { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 327 | char buf[4096]; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 328 | int ret, len = 0, offset = 0, rem = sizeof(buf); |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 329 | const char *c_subsys = NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 330 | |
| 331 | /* are we using color */ |
| 332 | if (target->use_color) { |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 333 | c_subsys = color(subsys); |
| 334 | if (c_subsys) { |
Neels Hofmeyr | 5e518b5 | 2018-01-17 13:20:02 +0100 | [diff] [blame] | 335 | ret = snprintf(buf + offset, rem, "%s", c_subsys); |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 336 | if (ret < 0) |
| 337 | goto err; |
| 338 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 339 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 340 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 341 | if (!cont) { |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 342 | if (target->print_ext_timestamp) { |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 343 | #ifdef HAVE_LOCALTIME_R |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 344 | struct tm tm; |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 345 | struct timeval tv; |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 346 | osmo_gettimeofday(&tv, NULL); |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 347 | localtime_r(&tv.tv_sec, &tm); |
| 348 | ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ", |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 349 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
Jacob Erlbeck | b61b2ca | 2015-03-17 10:21:15 +0100 | [diff] [blame] | 350 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 351 | (int)(tv.tv_usec / 1000)); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 352 | if (ret < 0) |
| 353 | goto err; |
| 354 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 355 | #endif |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 356 | } else if (target->print_timestamp) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 357 | char *timestr; |
| 358 | time_t tm; |
| 359 | tm = time(NULL); |
| 360 | timestr = ctime(&tm); |
| 361 | timestr[strlen(timestr)-1] = '\0'; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 362 | ret = snprintf(buf + offset, rem, "%s ", timestr); |
| 363 | if (ret < 0) |
| 364 | goto err; |
| 365 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 366 | } |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 367 | if (target->print_category) { |
Neels Hofmeyr | e653472 | 2018-01-16 03:02:06 +0100 | [diff] [blame] | 368 | ret = snprintf(buf + offset, rem, "%s%s%s%s ", |
| 369 | target->use_color ? level_color(level) : "", |
| 370 | log_category_name(subsys), |
| 371 | target->use_color ? "\033[0;m" : "", |
| 372 | c_subsys ? c_subsys : ""); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 373 | if (ret < 0) |
| 374 | goto err; |
| 375 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 376 | } |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 377 | if (target->print_level) { |
Neels Hofmeyr | f3fa369 | 2018-01-16 02:56:01 +0100 | [diff] [blame] | 378 | ret = snprintf(buf + offset, rem, "%s%s%s%s ", |
| 379 | target->use_color ? level_color(level) : "", |
| 380 | log_level_str(level), |
| 381 | target->use_color ? "\033[0;m" : "", |
| 382 | c_subsys ? c_subsys : ""); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 383 | if (ret < 0) |
| 384 | goto err; |
| 385 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 386 | } |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 387 | if (target->print_category_hex) { |
| 388 | ret = snprintf(buf + offset, rem, "<%4.4x> ", subsys); |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 389 | if (ret < 0) |
| 390 | goto err; |
| 391 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 392 | } |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 393 | |
| 394 | if (target->print_filename_pos == LOG_FILENAME_POS_HEADER_END) { |
| 395 | switch (target->print_filename2) { |
| 396 | case LOG_FILENAME_NONE: |
| 397 | break; |
| 398 | case LOG_FILENAME_PATH: |
| 399 | ret = snprintf(buf + offset, rem, "%s:%d ", file, line); |
| 400 | if (ret < 0) |
| 401 | goto err; |
| 402 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 403 | break; |
| 404 | case LOG_FILENAME_BASENAME: |
| 405 | ret = snprintf(buf + offset, rem, "%s:%d ", const_basename(file), line); |
| 406 | if (ret < 0) |
| 407 | goto err; |
| 408 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 409 | break; |
| 410 | } |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 411 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 412 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 413 | ret = vsnprintf(buf + offset, rem, format, ap); |
| 414 | if (ret < 0) |
| 415 | goto err; |
| 416 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 417 | |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 418 | /* For LOG_FILENAME_POS_LAST, print the source file info only when the caller ended the log |
| 419 | * message in '\n'. If so, nip the last '\n' away, insert the source file info and re-append an |
| 420 | * '\n'. All this to allow LOGP("start..."); LOGPC("...end\n") constructs. */ |
| 421 | if (target->print_filename_pos == LOG_FILENAME_POS_LINE_END |
| 422 | && offset > 0 && buf[offset-1] == '\n') { |
| 423 | switch (target->print_filename2) { |
| 424 | case LOG_FILENAME_NONE: |
| 425 | break; |
| 426 | case LOG_FILENAME_PATH: |
| 427 | offset --; |
| 428 | ret = snprintf(buf + offset, rem, " (%s:%d)\n", file, line); |
| 429 | if (ret < 0) |
| 430 | goto err; |
| 431 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 432 | break; |
| 433 | case LOG_FILENAME_BASENAME: |
| 434 | offset --; |
| 435 | ret = snprintf(buf + offset, rem, " (%s:%d)\n", const_basename(file), line); |
| 436 | if (ret < 0) |
| 437 | goto err; |
| 438 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | |
Neels Hofmeyr | c475988 | 2018-01-16 02:10:48 +0100 | [diff] [blame] | 443 | if (target->use_color) { |
| 444 | ret = snprintf(buf + offset, rem, "\033[0;m"); |
| 445 | if (ret < 0) |
| 446 | goto err; |
| 447 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 448 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 449 | err: |
| 450 | buf[sizeof(buf)-1] = '\0'; |
| 451 | target->output(target, level, buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 452 | } |
| 453 | |
Neels Hofmeyr | 42240de | 2016-12-12 15:13:56 +0100 | [diff] [blame] | 454 | /* Catch internal logging category indexes as well as out-of-bounds indexes. |
| 455 | * For internal categories, the ID is negative starting with -1; and internal |
| 456 | * logging categories are added behind the user categories. For out-of-bounds |
| 457 | * indexes, return the index of DLGLOBAL. The returned category index is |
| 458 | * guaranteed to exist in osmo_log_info, otherwise the program would abort, |
| 459 | * which should never happen unless even the DLGLOBAL category is missing. */ |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 460 | static inline int map_subsys(int subsys) |
| 461 | { |
Neels Hofmeyr | 7480226 | 2016-12-12 16:00:24 +0100 | [diff] [blame] | 462 | /* Note: comparing signed and unsigned integers */ |
| 463 | |
| 464 | if (subsys > 0 && ((unsigned int)subsys) >= osmo_log_info->num_cat_user) |
| 465 | subsys = DLGLOBAL; |
| 466 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 467 | if (subsys < 0) |
| 468 | subsys = subsys_lib2index(subsys); |
| 469 | |
Neels Hofmeyr | ca13574 | 2016-12-12 14:18:54 +0100 | [diff] [blame] | 470 | if (subsys < 0 || subsys >= osmo_log_info->num_cat) |
Neels Hofmeyr | 42240de | 2016-12-12 15:13:56 +0100 | [diff] [blame] | 471 | subsys = subsys_lib2index(DLGLOBAL); |
| 472 | |
Neels Hofmeyr | ca13574 | 2016-12-12 14:18:54 +0100 | [diff] [blame] | 473 | OSMO_ASSERT(!(subsys < 0 || subsys >= osmo_log_info->num_cat)); |
| 474 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 475 | return subsys; |
| 476 | } |
| 477 | |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 478 | static inline bool should_log_to_target(struct log_target *tar, int subsys, |
| 479 | int level) |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 480 | { |
| 481 | struct log_category *category; |
| 482 | |
| 483 | category = &tar->categories[subsys]; |
| 484 | |
| 485 | /* subsystem is not supposed to be logged */ |
| 486 | if (!category->enabled) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 487 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 488 | |
| 489 | /* Check the global log level */ |
| 490 | if (tar->loglevel != 0 && level < tar->loglevel) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 491 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 492 | |
| 493 | /* Check the category log level */ |
| 494 | if (tar->loglevel == 0 && category->loglevel != 0 && |
| 495 | level < category->loglevel) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 496 | return false; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 497 | |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 498 | /* Apply filters here... if that becomes messy we will |
| 499 | * need to put filters in a list and each filter will |
| 500 | * say stop, continue, output */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 501 | if ((tar->filter_map & (1 << LOG_FLT_ALL)) != 0) |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 502 | return true; |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 503 | |
| 504 | if (osmo_log_info->filter_fn) |
| 505 | return osmo_log_info->filter_fn(&log_context, tar); |
| 506 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 507 | /* TODO: Check the filter/selector too? */ |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 508 | return true; |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 511 | /*! vararg version of logging function |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 512 | * \param[in] subsys Logging sub-system |
| 513 | * \param[in] level Log level |
| 514 | * \param[in] file name of source code file |
| 515 | * \param[in] cont continuation (1) or new line (0) |
| 516 | * \param[in] format format string |
| 517 | * \param[in] ap vararg-list containing format string arguments |
| 518 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 519 | void osmo_vlogp(int subsys, int level, const char *file, int line, |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 520 | int cont, const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 521 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 522 | struct log_target *tar; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 523 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 524 | subsys = map_subsys(subsys); |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 525 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 526 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 527 | va_list bp; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 528 | |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 529 | if (!should_log_to_target(tar, subsys, level)) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 530 | continue; |
| 531 | |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 532 | /* According to the manpage, vsnprintf leaves the value of ap |
| 533 | * in undefined state. Since _output uses vsnprintf and it may |
| 534 | * be called several times, we have to pass a copy of ap. */ |
| 535 | va_copy(bp, ap); |
Harald Welte | d7c0a37 | 2016-12-02 13:52:59 +0100 | [diff] [blame] | 536 | if (tar->raw_output) |
| 537 | tar->raw_output(tar, subsys, level, file, line, cont, format, bp); |
| 538 | else |
| 539 | _output(tar, subsys, level, file, line, cont, format, bp); |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 540 | va_end(bp); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 544 | /*! logging function used by DEBUGP() macro |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 545 | * \param[in] subsys Logging sub-system |
| 546 | * \param[in] file name of source code file |
| 547 | * \param[in] cont continuation (1) or new line (0) |
| 548 | * \param[in] format format string |
| 549 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 550 | void logp(int subsys, const char *file, int line, int cont, |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 551 | const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 552 | { |
| 553 | va_list ap; |
| 554 | |
| 555 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 556 | osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 557 | va_end(ap); |
| 558 | } |
| 559 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 560 | /*! logging function used by LOGP() macro |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 561 | * \param[in] subsys Logging sub-system |
| 562 | * \param[in] level Log level |
| 563 | * \param[in] file name of source code file |
| 564 | * \param[in] cont continuation (1) or new line (0) |
| 565 | * \param[in] format format string |
| 566 | */ |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 567 | void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 568 | { |
| 569 | va_list ap; |
| 570 | |
| 571 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 572 | osmo_vlogp(subsys, level, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 573 | va_end(ap); |
| 574 | } |
| 575 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 576 | /*! Register a new log target with the logging core |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 577 | * \param[in] target Log target to be registered |
| 578 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 579 | void log_add_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 580 | { |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 581 | llist_add_tail(&target->entry, &osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 582 | } |
| 583 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 584 | /*! Unregister a log target from the logging core |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 585 | * \param[in] target Log target to be unregistered |
| 586 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 587 | void log_del_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 588 | { |
| 589 | llist_del(&target->entry); |
| 590 | } |
| 591 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 592 | /*! Reset (clear) the logging context */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 593 | void log_reset_context(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 594 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 595 | memset(&log_context, 0, sizeof(log_context)); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 596 | } |
| 597 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 598 | /*! Set the logging context |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 599 | * \param[in] ctx_nr logging context number |
| 600 | * \param[in] value value to which the context is to be set |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 601 | * \returns 0 in case of success; negative otherwise |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 602 | * |
| 603 | * A logging context is something like the subscriber identity to which |
| 604 | * the currently processed message relates, or the BTS through which it |
| 605 | * was received. As soon as this data is known, it can be set using |
| 606 | * this function. The main use of context information is for logging |
| 607 | * filters. |
| 608 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 609 | int log_set_context(uint8_t ctx_nr, void *value) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 610 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 611 | if (ctx_nr > LOG_MAX_CTX) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 612 | return -EINVAL; |
| 613 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 614 | log_context.ctx[ctx_nr] = value; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 619 | /*! Enable the \ref LOG_FLT_ALL log filter |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 620 | * \param[in] target Log target to be affected |
| 621 | * \param[in] all enable (1) or disable (0) the ALL filter |
| 622 | * |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 623 | * When the \ref LOG_FLT_ALL filter is enabled, all log messages will be |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 624 | * printed. It acts as a wildcard. Setting it to \a 1 means there is no |
| 625 | * filtering. |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 626 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 627 | void log_set_all_filter(struct log_target *target, int all) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 628 | { |
| 629 | if (all) |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 630 | target->filter_map |= (1 << LOG_FLT_ALL); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 631 | else |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 632 | target->filter_map &= ~(1 << LOG_FLT_ALL); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 633 | } |
| 634 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 635 | /*! Enable or disable the use of colored output |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 636 | * \param[in] target Log target to be affected |
| 637 | * \param[in] use_color Use color (1) or don't use color (0) |
| 638 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 639 | void log_set_use_color(struct log_target *target, int use_color) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 640 | { |
| 641 | target->use_color = use_color; |
| 642 | } |
| 643 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 644 | /*! Enable or disable printing of timestamps while logging |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 645 | * \param[in] target Log target to be affected |
| 646 | * \param[in] print_timestamp Enable (1) or disable (0) timestamps |
| 647 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 648 | void log_set_print_timestamp(struct log_target *target, int print_timestamp) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 649 | { |
| 650 | target->print_timestamp = print_timestamp; |
| 651 | } |
| 652 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 653 | /*! Enable or disable printing of extended timestamps while logging |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 654 | * \param[in] target Log target to be affected |
| 655 | * \param[in] print_timestamp Enable (1) or disable (0) timestamps |
| 656 | * |
| 657 | * When both timestamp and extended timestamp is enabled then only |
| 658 | * the extended timestamp will be used. The format of the timestamp |
| 659 | * is YYYYMMDDhhmmssnnn. |
| 660 | */ |
| 661 | void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp) |
| 662 | { |
| 663 | target->print_ext_timestamp = print_timestamp; |
| 664 | } |
| 665 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 666 | /*! Use log_set_print_filename2() instead. |
| 667 | * Call log_set_print_filename2() with LOG_FILENAME_PATH or LOG_FILENAME_NONE, *as well as* call |
| 668 | * log_set_print_category_hex() with the argument passed to this function. This is to mirror legacy |
| 669 | * behavior, which combined the category in hex with the filename. For example, if the category-hex |
| 670 | * output were no longer affected by log_set_print_filename(), many unit tests (in libosmocore as well as |
| 671 | * dependent projects) would fail since they expect the category to disappear along with the filename. |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 672 | * \param[in] target Log target to be affected |
| 673 | * \param[in] print_filename Enable (1) or disable (0) filenames |
| 674 | */ |
| 675 | void log_set_print_filename(struct log_target *target, int print_filename) |
| 676 | { |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 677 | log_set_print_filename2(target, print_filename ? LOG_FILENAME_PATH : LOG_FILENAME_NONE); |
| 678 | log_set_print_category_hex(target, print_filename); |
| 679 | } |
| 680 | |
| 681 | /*! Enable or disable printing of the filename while logging. |
| 682 | * \param[in] target Log target to be affected. |
| 683 | * \param[in] print_filename An LOG_FILENAME_* enum value. |
| 684 | * LOG_FILENAME_NONE omits the source file and line information from logs. |
| 685 | * LOG_FILENAME_PATH prints the entire source file path as passed to LOGP macros. |
| 686 | */ |
| 687 | void log_set_print_filename2(struct log_target *target, enum log_filename_type lft) |
| 688 | { |
| 689 | target->print_filename2 = lft; |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 692 | /*! Set the position where on a log line the source file info should be logged. |
| 693 | * \param[in] target Log target to be affected. |
| 694 | * \param[in] pos A LOG_FILENAME_POS_* enum value. |
| 695 | * LOG_FILENAME_POS_DEFAULT logs just before the caller supplied log message. |
| 696 | * LOG_FILENAME_POS_LAST logs only at the end of a log line, where the caller issued an '\n' to end the |
| 697 | */ |
| 698 | void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos) |
| 699 | { |
| 700 | target->print_filename_pos = pos; |
| 701 | } |
| 702 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 703 | /*! Enable or disable printing of the category name |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 704 | * \param[in] target Log target to be affected |
| 705 | * \param[in] print_catname Enable (1) or disable (0) filenames |
| 706 | * |
| 707 | * Print the category/subsys name in front of every log message. |
| 708 | */ |
| 709 | void log_set_print_category(struct log_target *target, int print_category) |
| 710 | { |
| 711 | target->print_category = print_category; |
| 712 | } |
| 713 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 714 | /*! Enable or disable printing of the category number in hex ('<000b>'). |
| 715 | * \param[in] target Log target to be affected. |
| 716 | * \param[in] print_category_hex Enable (1) or disable (0) hex category. |
| 717 | */ |
| 718 | void log_set_print_category_hex(struct log_target *target, int print_category_hex) |
| 719 | { |
| 720 | target->print_category_hex = print_category_hex; |
| 721 | } |
| 722 | |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 723 | /*! Enable or disable printing of the log level name. |
| 724 | * \param[in] target Log target to be affected |
| 725 | * \param[in] print_catname Enable (1) or disable (0) filenames |
| 726 | * |
| 727 | * Print the log level name in front of every log message. |
| 728 | */ |
| 729 | void log_set_print_level(struct log_target *target, int print_level) |
| 730 | { |
| 731 | target->print_level = (bool)print_level; |
| 732 | } |
| 733 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 734 | /*! Set the global log level for a given log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 735 | * \param[in] target Log target to be affected |
| 736 | * \param[in] log_level New global log level |
| 737 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 738 | void log_set_log_level(struct log_target *target, int log_level) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 739 | { |
| 740 | target->loglevel = log_level; |
| 741 | } |
| 742 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 743 | /*! Set a category filter on a given log target |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 744 | * \param[in] target Log target to be affected |
| 745 | * \param[in] category Log category to be affected |
| 746 | * \param[in] enable whether to enable or disable the filter |
| 747 | * \param[in] level Log level of the filter |
| 748 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 749 | void log_set_category_filter(struct log_target *target, int category, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 750 | int enable, int level) |
| 751 | { |
Neels Hofmeyr | 886d6fd | 2016-12-12 13:49:03 +0100 | [diff] [blame] | 752 | if (!target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 753 | return; |
Neels Hofmeyr | 886d6fd | 2016-12-12 13:49:03 +0100 | [diff] [blame] | 754 | category = map_subsys(category); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 755 | target->categories[category].enabled = !!enable; |
| 756 | target->categories[category].loglevel = level; |
| 757 | } |
| 758 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 759 | #if (!EMBEDDED) |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 760 | static void _file_output(struct log_target *target, unsigned int level, |
| 761 | const char *log) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 762 | { |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 763 | fprintf(target->tgt_file.out, "%s", log); |
| 764 | fflush(target->tgt_file.out); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 765 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 766 | #endif |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 767 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 768 | /*! Create a new log target skeleton |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 769 | * \returns dynamically-allocated log target |
| 770 | * This funcition allocates a \ref log_target and initializes it |
| 771 | * with some default values. The newly created target is not |
| 772 | * registered yet. |
| 773 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 774 | struct log_target *log_target_create(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 775 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 776 | struct log_target *target; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 777 | unsigned int i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 778 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 779 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 780 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 781 | target = talloc_zero(tall_log_ctx, struct log_target); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 782 | if (!target) |
| 783 | return NULL; |
| 784 | |
Pau Espin Pedrol | 9d4a36e | 2018-07-26 11:55:33 +0200 | [diff] [blame] | 785 | target->categories = talloc_zero_array(target, |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 786 | struct log_category, |
| 787 | osmo_log_info->num_cat); |
| 788 | if (!target->categories) { |
| 789 | talloc_free(target); |
| 790 | return NULL; |
| 791 | } |
| 792 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 793 | INIT_LLIST_HEAD(&target->entry); |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 794 | |
| 795 | /* initialize the per-category enabled/loglevel from defaults */ |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 796 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 797 | struct log_category *cat = &target->categories[i]; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 798 | cat->enabled = osmo_log_info->cat[i].enabled; |
| 799 | cat->loglevel = osmo_log_info->cat[i].loglevel; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* global settings */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 803 | target->use_color = 1; |
| 804 | target->print_timestamp = 0; |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 805 | target->print_filename2 = LOG_FILENAME_PATH; |
| 806 | target->print_category_hex = true; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 807 | |
| 808 | /* global log level */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 809 | target->loglevel = 0; |
| 810 | return target; |
| 811 | } |
| 812 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 813 | /*! Create the STDERR log target |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 814 | * \returns dynamically-allocated \ref log_target for STDERR */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 815 | struct log_target *log_target_create_stderr(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 816 | { |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 817 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
Harald Welte | b93ce5a | 2017-05-15 10:58:15 +0200 | [diff] [blame] | 818 | #if !EMBEDDED && defined(stderr) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 819 | struct log_target *target; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 820 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 821 | target = log_target_create(); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 822 | if (!target) |
| 823 | return NULL; |
| 824 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 825 | target->type = LOG_TGT_TYPE_STDERR; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 826 | target->tgt_file.out = stderr; |
| 827 | target->output = _file_output; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 828 | return target; |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 829 | #else |
| 830 | return NULL; |
| 831 | #endif /* stderr */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 832 | } |
| 833 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 834 | #if (!EMBEDDED) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 835 | /*! Create a new file-based log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 836 | * \param[in] fname File name of the new log file |
| 837 | * \returns Log target in case of success, NULL otherwise |
| 838 | */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 839 | struct log_target *log_target_create_file(const char *fname) |
| 840 | { |
| 841 | struct log_target *target; |
| 842 | |
| 843 | target = log_target_create(); |
| 844 | if (!target) |
| 845 | return NULL; |
| 846 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 847 | target->type = LOG_TGT_TYPE_FILE; |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 848 | target->tgt_file.out = fopen(fname, "a"); |
| 849 | if (!target->tgt_file.out) |
| 850 | return NULL; |
| 851 | |
| 852 | target->output = _file_output; |
| 853 | |
| 854 | target->tgt_file.fname = talloc_strdup(target, fname); |
| 855 | |
| 856 | return target; |
| 857 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 858 | #endif |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 859 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 860 | /*! Find a registered log target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 861 | * \param[in] type Log target type |
| 862 | * \param[in] fname File name |
| 863 | * \returns Log target (if found), NULL otherwise |
| 864 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 865 | struct log_target *log_target_find(int type, const char *fname) |
| 866 | { |
| 867 | struct log_target *tgt; |
| 868 | |
| 869 | llist_for_each_entry(tgt, &osmo_log_target_list, entry) { |
| 870 | if (tgt->type != type) |
| 871 | continue; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 872 | switch (tgt->type) { |
| 873 | case LOG_TGT_TYPE_FILE: |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 874 | if (!strcmp(fname, tgt->tgt_file.fname)) |
| 875 | return tgt; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 876 | break; |
| 877 | case LOG_TGT_TYPE_GSMTAP: |
| 878 | if (!strcmp(fname, tgt->tgt_gsmtap.hostname)) |
| 879 | return tgt; |
| 880 | break; |
| 881 | default: |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 882 | return tgt; |
Max | c90f40a | 2018-01-11 10:52:28 +0100 | [diff] [blame] | 883 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 884 | } |
| 885 | return NULL; |
| 886 | } |
| 887 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 888 | /*! Unregister, close and delete a log target |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 889 | * \param target[in] log target to unregister, close and delete */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 890 | void log_target_destroy(struct log_target *target) |
| 891 | { |
| 892 | |
| 893 | /* just in case, to make sure we don't have any references */ |
| 894 | log_del_target(target); |
| 895 | |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 896 | #if (!EMBEDDED) |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 897 | if (target->output == &_file_output) { |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 898 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 899 | #ifdef stderr |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 900 | /* don't close stderr */ |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 901 | if (target->tgt_file.out != stderr) |
| 902 | #endif |
| 903 | { |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 904 | fclose(target->tgt_file.out); |
| 905 | target->tgt_file.out = NULL; |
| 906 | } |
| 907 | } |
Harald Welte | 44c0f63 | 2017-01-15 17:58:29 +0100 | [diff] [blame] | 908 | #endif |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 909 | |
| 910 | talloc_free(target); |
| 911 | } |
| 912 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 913 | /*! close and re-open a log file (for log file rotation) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 914 | * \param[in] target log target to re-open |
| 915 | * \returns 0 in case of success; negative otherwise */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 916 | int log_target_file_reopen(struct log_target *target) |
| 917 | { |
| 918 | fclose(target->tgt_file.out); |
| 919 | |
| 920 | target->tgt_file.out = fopen(target->tgt_file.fname, "a"); |
| 921 | if (!target->tgt_file.out) |
| 922 | return -errno; |
| 923 | |
| 924 | /* we assume target->output already to be set */ |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 929 | /*! close and re-open all log files (for log file rotation) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 930 | * \returns 0 in case of success; negative otherwise */ |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 931 | int log_targets_reopen(void) |
| 932 | { |
| 933 | struct log_target *tar; |
| 934 | int rc = 0; |
| 935 | |
| 936 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
| 937 | switch (tar->type) { |
| 938 | case LOG_TGT_TYPE_FILE: |
| 939 | if (log_target_file_reopen(tar) < 0) |
| 940 | rc = -1; |
| 941 | break; |
| 942 | default: |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return rc; |
| 948 | } |
| 949 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 950 | /*! Initialize the Osmocom logging core |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 951 | * \param[in] inf Information regarding logging categories, could be NULL |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 952 | * \param[in] ctx \ref talloc context for logging allocations |
| 953 | * \returns 0 in case of success, negative in case of error |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 954 | * |
| 955 | * If inf is NULL then only library-internal categories are initialized. |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 956 | */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 957 | int log_init(const struct log_info *inf, void *ctx) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 958 | { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 959 | int i; |
| 960 | |
| 961 | tall_log_ctx = talloc_named_const(ctx, 1, "logging"); |
| 962 | if (!tall_log_ctx) |
| 963 | return -ENOMEM; |
| 964 | |
| 965 | osmo_log_info = talloc_zero(tall_log_ctx, struct log_info); |
| 966 | if (!osmo_log_info) |
| 967 | return -ENOMEM; |
| 968 | |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 969 | osmo_log_info->num_cat = ARRAY_SIZE(internal_cat); |
| 970 | |
| 971 | if (inf) { |
| 972 | osmo_log_info->filter_fn = inf->filter_fn; |
| 973 | osmo_log_info->num_cat_user = inf->num_cat; |
| 974 | osmo_log_info->num_cat += inf->num_cat; |
| 975 | } |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 976 | |
| 977 | osmo_log_info->cat = talloc_zero_array(osmo_log_info, |
| 978 | struct log_info_cat, |
| 979 | osmo_log_info->num_cat); |
| 980 | if (!osmo_log_info->cat) { |
| 981 | talloc_free(osmo_log_info); |
| 982 | osmo_log_info = NULL; |
| 983 | return -ENOMEM; |
| 984 | } |
| 985 | |
Max | 72dfd43 | 2018-12-04 11:24:18 +0100 | [diff] [blame] | 986 | if (inf) { /* copy over the user part */ |
| 987 | for (i = 0; i < inf->num_cat; i++) { |
| 988 | memcpy((struct log_info_cat *) &osmo_log_info->cat[i], |
| 989 | &inf->cat[i], sizeof(struct log_info_cat)); |
| 990 | } |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* copy over the library part */ |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 994 | for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 995 | unsigned int cn = osmo_log_info->num_cat_user + i; |
Holger Hans Peter Freyther | 06f6455 | 2012-09-11 10:31:29 +0200 | [diff] [blame] | 996 | memcpy((struct log_info_cat *) &osmo_log_info->cat[cn], |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 997 | &internal_cat[i], sizeof(struct log_info_cat)); |
| 998 | } |
| 999 | |
| 1000 | return 0; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 1001 | } |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 1002 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1003 | /* De-initialize the Osmocom logging core |
Harald Welte | 69e6c3c | 2016-04-20 10:41:27 +0200 | [diff] [blame] | 1004 | * This function destroys all targets and releases associated memory */ |
| 1005 | void log_fini(void) |
| 1006 | { |
| 1007 | struct log_target *tar, *tar2; |
| 1008 | |
| 1009 | llist_for_each_entry_safe(tar, tar2, &osmo_log_target_list, entry) |
| 1010 | log_target_destroy(tar); |
| 1011 | |
| 1012 | talloc_free(osmo_log_info); |
| 1013 | osmo_log_info = NULL; |
| 1014 | talloc_free(tall_log_ctx); |
| 1015 | tall_log_ctx = NULL; |
| 1016 | } |
| 1017 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1018 | /*! Check whether a log entry will be generated. |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1019 | * \returns != 0 if a log entry might get generated by at least one target */ |
| 1020 | int log_check_level(int subsys, unsigned int level) |
| 1021 | { |
| 1022 | struct log_target *tar; |
| 1023 | |
Max | 68bf16a | 2018-01-10 17:00:43 +0100 | [diff] [blame] | 1024 | assert_loginfo(__func__); |
Harald Welte | 18a7d81 | 2017-03-16 23:54:55 +0100 | [diff] [blame] | 1025 | |
Holger Hans Peter Freyther | e0dc6a1 | 2015-12-21 14:45:16 +0100 | [diff] [blame] | 1026 | subsys = map_subsys(subsys); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1027 | |
| 1028 | /* TODO: The following could/should be cached (update on config) */ |
| 1029 | |
| 1030 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 1031 | if (!should_log_to_target(tar, subsys, level)) |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 1032 | continue; |
| 1033 | |
| 1034 | /* This might get logged (ignoring filters) */ |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
| 1038 | /* We are sure, that this will not be logged. */ |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 1042 | /*! @} */ |