Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | |
| 3 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 23 | /* \addtogroup logging |
| 24 | * @{ |
| 25 | */ |
| 26 | |
| 27 | /* \file logging.c */ |
| 28 | |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 29 | #include "../config.h" |
| 30 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 31 | #include <stdarg.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 35 | #include <ctype.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 36 | |
| 37 | #ifdef HAVE_STRINGS_H |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 38 | #include <strings.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 39 | #endif |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 40 | #include <time.h> |
| 41 | #include <errno.h> |
| 42 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 43 | #include <osmocom/core/talloc.h> |
| 44 | #include <osmocom/core/utils.h> |
| 45 | #include <osmocom/core/logging.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 46 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 47 | #include <osmocom/vty/logging.h> /* for LOGGING_STR. */ |
| 48 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 49 | struct log_info *osmo_log_info; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 50 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 51 | static struct log_context log_context; |
| 52 | static void *tall_log_ctx = NULL; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 53 | LLIST_HEAD(osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 54 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 55 | #define LOGLEVEL_DEFS 6 /* Number of loglevels.*/ |
| 56 | |
| 57 | static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 58 | { 0, "EVERYTHING" }, |
| 59 | { LOGL_DEBUG, "DEBUG" }, |
| 60 | { LOGL_INFO, "INFO" }, |
| 61 | { LOGL_NOTICE, "NOTICE" }, |
| 62 | { LOGL_ERROR, "ERROR" }, |
| 63 | { LOGL_FATAL, "FATAL" }, |
| 64 | { 0, NULL }, |
| 65 | }; |
| 66 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 67 | #define INT2IDX(x) (-1*(x)-1) |
| 68 | static const struct log_info_cat internal_cat[OSMO_NUM_DLIB] = { |
| 69 | [INT2IDX(DLGLOBAL)] = { /* -1 becomes 0 */ |
| 70 | .name = "DLGLOBAL", |
| 71 | .description = "Library-internal global log family", |
| 72 | .loglevel = LOGL_NOTICE, |
| 73 | .enabled = 1, |
| 74 | }, |
root | 8a996b4 | 2011-09-26 11:22:21 +0200 | [diff] [blame] | 75 | [INT2IDX(DLLAPD)] = { /* -2 becomes 1 */ |
| 76 | .name = "DLLAPD", |
| 77 | .description = "LAPD in libosmogsm", |
Harald Welte | 1f0b8c2 | 2011-06-27 10:51:37 +0200 | [diff] [blame] | 78 | .loglevel = LOGL_NOTICE, |
| 79 | .enabled = 1, |
| 80 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 81 | [INT2IDX(DLINP)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 82 | .name = "DLINP", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 83 | .description = "A-bis Intput Subsystem", |
| 84 | .loglevel = LOGL_NOTICE, |
| 85 | .enabled = 1, |
| 86 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 87 | [INT2IDX(DLMUX)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 88 | .name = "DLMUX", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 89 | .description = "A-bis B-Subchannel TRAU Frame Multiplex", |
| 90 | .loglevel = LOGL_NOTICE, |
| 91 | .enabled = 1, |
| 92 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 93 | [INT2IDX(DLMI)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 94 | .name = "DLMI", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 95 | .description = "A-bis Input Driver for Signalling", |
| 96 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 97 | }, |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 98 | [INT2IDX(DLMIB)] = { |
Harald Welte | 087e113 | 2011-07-29 11:43:39 +0200 | [diff] [blame] | 99 | .name = "DLMIB", |
Pablo Neira Ayuso | 199f377 | 2011-07-07 19:46:38 +0200 | [diff] [blame] | 100 | .description = "A-bis Input Driver for B-Channels (voice)", |
| 101 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 102 | }, |
Andreas Eversberg | c626da9 | 2011-10-28 03:53:50 +0200 | [diff] [blame] | 103 | [INT2IDX(DLSMS)] = { |
| 104 | .name = "DLSMS", |
| 105 | .description = "Layer3 Short Message Service (SMS)", |
| 106 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 107 | .color = "\033[1;38m", |
| 108 | }, |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 111 | /* You have to keep this in sync with the structure loglevel_strs. */ |
| 112 | const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = { |
| 113 | "Log simply everything", |
| 114 | "Log debug messages and higher levels", |
| 115 | "Log informational messages and higher levels", |
| 116 | "Log noticable messages and higher levels", |
| 117 | "Log error messages and higher levels", |
| 118 | "Log only fatal messages", |
| 119 | NULL, |
| 120 | }; |
| 121 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 122 | /* special magic for negative (library-internal) log subsystem numbers */ |
| 123 | static int subsys_lib2index(int subsys) |
| 124 | { |
| 125 | return (subsys * -1) + (osmo_log_info->num_cat_user-1); |
| 126 | } |
| 127 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 128 | /*! \brief Parse a human-readable log level into a numeric value */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 129 | int log_parse_level(const char *lvl) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 130 | { |
| 131 | return get_string_value(loglevel_strs, lvl); |
| 132 | } |
| 133 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 134 | /*! \brief convert a numeric log level into human-readable string */ |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 135 | const char *log_level_str(unsigned int lvl) |
| 136 | { |
| 137 | return get_value_string(loglevel_strs, lvl); |
| 138 | } |
| 139 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 140 | /*! \brief parse a human-readable log category into numeric form |
| 141 | * \param[in] category human-readable log category name |
| 142 | * \returns numeric category value, or -EINVAL otherwise |
| 143 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 144 | int log_parse_category(const char *category) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 145 | { |
| 146 | int i; |
| 147 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 148 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 149 | if (osmo_log_info->cat[i].name == NULL) |
| 150 | continue; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 151 | if (!strcasecmp(osmo_log_info->cat[i].name+1, category)) |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 152 | return i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 158 | /*! \brief parse the log category mask |
| 159 | * \param[in] target log target to be configured |
| 160 | * \param[in] _mask log category mask string |
| 161 | * |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 162 | * The format can be this: category1:category2:category3 |
| 163 | * or category1,2:category2,3:... |
| 164 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 165 | void log_parse_category_mask(struct log_target* target, const char *_mask) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 166 | { |
| 167 | int i = 0; |
| 168 | char *mask = strdup(_mask); |
| 169 | char *category_token = NULL; |
| 170 | |
| 171 | /* Disable everything to enable it afterwards */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 172 | for (i = 0; i < osmo_log_info->num_cat; ++i) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 173 | target->categories[i].enabled = 0; |
| 174 | |
| 175 | category_token = strtok(mask, ":"); |
| 176 | do { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 177 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 178 | char* colon = strstr(category_token, ","); |
| 179 | int length = strlen(category_token); |
Pablo Neira Ayuso | 300e78d | 2011-08-11 13:24:18 +0200 | [diff] [blame] | 180 | int cat_length = strlen(osmo_log_info->cat[i].name); |
| 181 | |
| 182 | /* Use longest length not to match subocurrences. */ |
| 183 | if (cat_length > length) |
| 184 | length = cat_length; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 185 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 186 | if (!osmo_log_info->cat[i].name) |
| 187 | continue; |
| 188 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 189 | if (colon) |
| 190 | length = colon - category_token; |
| 191 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 192 | if (strncasecmp(osmo_log_info->cat[i].name, |
| 193 | category_token, length) == 0) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 194 | int level = 0; |
| 195 | |
| 196 | if (colon) |
| 197 | level = atoi(colon+1); |
| 198 | |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 199 | target->categories[i].enabled = 1; |
| 200 | target->categories[i].loglevel = level; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } while ((category_token = strtok(NULL, ":"))); |
| 204 | |
| 205 | free(mask); |
| 206 | } |
| 207 | |
| 208 | static const char* color(int subsys) |
| 209 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 210 | if (subsys < osmo_log_info->num_cat) |
| 211 | return osmo_log_info->cat[subsys].color; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 212 | |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 213 | return NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 216 | static void _output(struct log_target *target, unsigned int subsys, |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 217 | unsigned int level, char *file, int line, int cont, |
| 218 | const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 219 | { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 220 | char buf[4096]; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 221 | int ret, len = 0, offset = 0, rem = sizeof(buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 222 | |
| 223 | /* are we using color */ |
| 224 | if (target->use_color) { |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 225 | const char *c = color(subsys); |
| 226 | if (c) { |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 227 | ret = snprintf(buf + offset, rem, "%s", color(subsys)); |
| 228 | if (ret < 0) |
| 229 | goto err; |
| 230 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 231 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 232 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 233 | if (!cont) { |
| 234 | if (target->print_timestamp) { |
| 235 | char *timestr; |
| 236 | time_t tm; |
| 237 | tm = time(NULL); |
| 238 | timestr = ctime(&tm); |
| 239 | timestr[strlen(timestr)-1] = '\0'; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 240 | ret = snprintf(buf + offset, rem, "%s ", timestr); |
| 241 | if (ret < 0) |
| 242 | goto err; |
| 243 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 244 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 245 | ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ", |
| 246 | subsys, file, line); |
| 247 | if (ret < 0) |
| 248 | goto err; |
| 249 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 250 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 251 | ret = vsnprintf(buf + offset, rem, format, ap); |
| 252 | if (ret < 0) |
| 253 | goto err; |
| 254 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 255 | |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 256 | ret = snprintf(buf + offset, rem, "%s", |
| 257 | target->use_color ? "\033[0;m" : ""); |
| 258 | if (ret < 0) |
| 259 | goto err; |
| 260 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 261 | err: |
| 262 | buf[sizeof(buf)-1] = '\0'; |
| 263 | target->output(target, level, buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 266 | /*! \brief vararg version of logging function */ |
| 267 | void osmo_vlogp(int subsys, int level, char *file, int line, |
| 268 | int cont, const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 269 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 270 | struct log_target *tar; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 271 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 272 | if (subsys < 0) |
| 273 | subsys = subsys_lib2index(subsys); |
| 274 | |
| 275 | if (subsys > osmo_log_info->num_cat) |
| 276 | subsys = DLGLOBAL; |
| 277 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 278 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 279 | struct log_category *category; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 280 | int output = 0; |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 281 | va_list bp; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 282 | |
| 283 | category = &tar->categories[subsys]; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 284 | /* subsystem is not supposed to be logged */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 285 | if (!category->enabled) |
| 286 | continue; |
| 287 | |
| 288 | /* Check the global log level */ |
| 289 | if (tar->loglevel != 0 && level < tar->loglevel) |
| 290 | continue; |
| 291 | |
| 292 | /* Check the category log level */ |
| 293 | if (tar->loglevel == 0 && category->loglevel != 0 && |
| 294 | level < category->loglevel) |
| 295 | continue; |
| 296 | |
| 297 | /* Apply filters here... if that becomes messy we will |
| 298 | * need to put filters in a list and each filter will |
| 299 | * say stop, continue, output */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 300 | if ((tar->filter_map & LOG_FILTER_ALL) != 0) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 301 | output = 1; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 302 | else if (osmo_log_info->filter_fn) |
| 303 | output = osmo_log_info->filter_fn(&log_context, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 304 | tar); |
Pablo Neira Ayuso | 81e9636 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 305 | if (!output) |
| 306 | continue; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 307 | |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 308 | /* According to the manpage, vsnprintf leaves the value of ap |
| 309 | * in undefined state. Since _output uses vsnprintf and it may |
| 310 | * be called several times, we have to pass a copy of ap. */ |
| 311 | va_copy(bp, ap); |
Harald Welte | da127cb | 2011-07-02 21:51:32 +0200 | [diff] [blame] | 312 | _output(tar, subsys, level, file, line, cont, format, bp); |
Pablo Neira Ayuso | dd93bf4 | 2011-05-19 01:40:43 +0200 | [diff] [blame] | 313 | va_end(bp); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 317 | void logp(int subsys, char *file, int line, int cont, |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 318 | const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 319 | { |
| 320 | va_list ap; |
| 321 | |
| 322 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 323 | osmo_vlogp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 324 | va_end(ap); |
| 325 | } |
| 326 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 327 | void logp2(int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 328 | { |
| 329 | va_list ap; |
| 330 | |
| 331 | va_start(ap, format); |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 332 | osmo_vlogp(subsys, level, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 333 | va_end(ap); |
| 334 | } |
| 335 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 336 | /*! \brief Register a new log target with the logging core |
| 337 | * \param[in] target Log target to be registered |
| 338 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 339 | void log_add_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 340 | { |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 341 | llist_add_tail(&target->entry, &osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 342 | } |
| 343 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 344 | /*! \brief Unregister a log target from the logging core |
| 345 | * \param[in] target Log target to be unregistered |
| 346 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 347 | void log_del_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 348 | { |
| 349 | llist_del(&target->entry); |
| 350 | } |
| 351 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 352 | /*! \brief Reset (clear) the logging context */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 353 | void log_reset_context(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 354 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 355 | memset(&log_context, 0, sizeof(log_context)); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 356 | } |
| 357 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 358 | /*! \brief Set the logging context |
| 359 | * \param[in] ctx_nr logging context number |
| 360 | * \param[in] value value to which the context is to be set |
| 361 | * |
| 362 | * A logging context is something like the subscriber identity to which |
| 363 | * the currently processed message relates, or the BTS through which it |
| 364 | * was received. As soon as this data is known, it can be set using |
| 365 | * this function. The main use of context information is for logging |
| 366 | * filters. |
| 367 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 368 | int log_set_context(uint8_t ctx_nr, void *value) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 369 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 370 | if (ctx_nr > LOG_MAX_CTX) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 371 | return -EINVAL; |
| 372 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 373 | log_context.ctx[ctx_nr] = value; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 378 | /*! \brief Enable the \ref LOG_FILTER_ALL log filter |
| 379 | * \param[in] target Log target to be affected |
| 380 | * \param[in] all enable (1) or disable (0) the ALL filter |
| 381 | * |
| 382 | * When the \ref LOG_FILTER_ALL filter is enabled, all log messages will |
| 383 | * be printed. It acts as a wildcard. Setting it to \a 1 means there |
| 384 | * is no filtering. |
| 385 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 386 | void log_set_all_filter(struct log_target *target, int all) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 387 | { |
| 388 | if (all) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 389 | target->filter_map |= LOG_FILTER_ALL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 390 | else |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 391 | target->filter_map &= ~LOG_FILTER_ALL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 394 | /*! \brief Enable or disable the use of colored output |
| 395 | * \param[in] target Log target to be affected |
| 396 | * \param[in] use_color Use color (1) or don't use color (0) |
| 397 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 398 | void log_set_use_color(struct log_target *target, int use_color) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 399 | { |
| 400 | target->use_color = use_color; |
| 401 | } |
| 402 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 403 | /*! \brief Enable or disable printing of timestamps while logging |
| 404 | * \param[in] target Log target to be affected |
| 405 | * \param[in] print_timestamp Enable (1) or disable (0) timestamps |
| 406 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 407 | void log_set_print_timestamp(struct log_target *target, int print_timestamp) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 408 | { |
| 409 | target->print_timestamp = print_timestamp; |
| 410 | } |
| 411 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 412 | /*! \brief Set the global log level for a given log target |
| 413 | * \param[in] target Log target to be affected |
| 414 | * \param[in] log_level New global log level |
| 415 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 416 | void log_set_log_level(struct log_target *target, int log_level) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 417 | { |
| 418 | target->loglevel = log_level; |
| 419 | } |
| 420 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 421 | void log_set_category_filter(struct log_target *target, int category, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 422 | int enable, int level) |
| 423 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 424 | if (category >= osmo_log_info->num_cat) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 425 | return; |
| 426 | target->categories[category].enabled = !!enable; |
| 427 | target->categories[category].loglevel = level; |
| 428 | } |
| 429 | |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 430 | static void _file_output(struct log_target *target, unsigned int level, |
| 431 | const char *log) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 432 | { |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 433 | fprintf(target->tgt_file.out, "%s", log); |
| 434 | fflush(target->tgt_file.out); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 435 | } |
| 436 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 437 | /*! \brief Create a new log target skeleton */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 438 | struct log_target *log_target_create(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 439 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 440 | struct log_target *target; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 441 | unsigned int i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 442 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 443 | target = talloc_zero(tall_log_ctx, struct log_target); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 444 | if (!target) |
| 445 | return NULL; |
| 446 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 447 | target->categories = talloc_zero_array(target, |
| 448 | struct log_category, |
| 449 | osmo_log_info->num_cat); |
| 450 | if (!target->categories) { |
| 451 | talloc_free(target); |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 455 | INIT_LLIST_HEAD(&target->entry); |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 456 | |
| 457 | /* initialize the per-category enabled/loglevel from defaults */ |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 458 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 459 | struct log_category *cat = &target->categories[i]; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 460 | cat->enabled = osmo_log_info->cat[i].enabled; |
| 461 | cat->loglevel = osmo_log_info->cat[i].loglevel; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* global settings */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 465 | target->use_color = 1; |
| 466 | target->print_timestamp = 0; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 467 | |
| 468 | /* global log level */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 469 | target->loglevel = 0; |
| 470 | return target; |
| 471 | } |
| 472 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 473 | /*! \brief Create the STDERR log target */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 474 | struct log_target *log_target_create_stderr(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 475 | { |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 476 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 477 | #ifdef stderr |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 478 | struct log_target *target; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 479 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 480 | target = log_target_create(); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 481 | if (!target) |
| 482 | return NULL; |
| 483 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 484 | target->type = LOG_TGT_TYPE_STDERR; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 485 | target->tgt_file.out = stderr; |
| 486 | target->output = _file_output; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 487 | return target; |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 488 | #else |
| 489 | return NULL; |
| 490 | #endif /* stderr */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 491 | } |
| 492 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 493 | /*! \brief Create a new file-based log target |
| 494 | * \param[in] fname File name of the new log file |
| 495 | * \returns Log target in case of success, NULL otherwise |
| 496 | */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 497 | struct log_target *log_target_create_file(const char *fname) |
| 498 | { |
| 499 | struct log_target *target; |
| 500 | |
| 501 | target = log_target_create(); |
| 502 | if (!target) |
| 503 | return NULL; |
| 504 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 505 | target->type = LOG_TGT_TYPE_FILE; |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 506 | target->tgt_file.out = fopen(fname, "a"); |
| 507 | if (!target->tgt_file.out) |
| 508 | return NULL; |
| 509 | |
| 510 | target->output = _file_output; |
| 511 | |
| 512 | target->tgt_file.fname = talloc_strdup(target, fname); |
| 513 | |
| 514 | return target; |
| 515 | } |
| 516 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 517 | /*! \brief Find a registered log target |
| 518 | * \param[in] type Log target type |
| 519 | * \param[in] fname File name |
| 520 | * \returns Log target (if found), NULL otherwise |
| 521 | */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 522 | struct log_target *log_target_find(int type, const char *fname) |
| 523 | { |
| 524 | struct log_target *tgt; |
| 525 | |
| 526 | llist_for_each_entry(tgt, &osmo_log_target_list, entry) { |
| 527 | if (tgt->type != type) |
| 528 | continue; |
| 529 | if (tgt->type == LOG_TGT_TYPE_FILE) { |
| 530 | if (!strcmp(fname, tgt->tgt_file.fname)) |
| 531 | return tgt; |
| 532 | } else |
| 533 | return tgt; |
| 534 | } |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 538 | /*! \brief Unregister, close and delete a log target */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 539 | void log_target_destroy(struct log_target *target) |
| 540 | { |
| 541 | |
| 542 | /* just in case, to make sure we don't have any references */ |
| 543 | log_del_target(target); |
| 544 | |
| 545 | if (target->output == &_file_output) { |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 546 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 547 | #ifdef stderr |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 548 | /* don't close stderr */ |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 549 | if (target->tgt_file.out != stderr) |
| 550 | #endif |
| 551 | { |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 552 | fclose(target->tgt_file.out); |
| 553 | target->tgt_file.out = NULL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | talloc_free(target); |
| 558 | } |
| 559 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 560 | /*! \brief close and re-open a log file (for log file rotation) */ |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 561 | int log_target_file_reopen(struct log_target *target) |
| 562 | { |
| 563 | fclose(target->tgt_file.out); |
| 564 | |
| 565 | target->tgt_file.out = fopen(target->tgt_file.fname, "a"); |
| 566 | if (!target->tgt_file.out) |
| 567 | return -errno; |
| 568 | |
| 569 | /* we assume target->output already to be set */ |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 574 | /*! \brief Generates the logging command string for VTY |
| 575 | * \param[in] unused_info Deprecated parameter, no longer used! |
| 576 | */ |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 577 | const char *log_vty_command_string(const struct log_info *unused_info) |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 578 | { |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 579 | struct log_info *info = osmo_log_info; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 580 | int len = 0, offset = 0, ret, i, rem; |
| 581 | int size = strlen("logging level () ()") + 1; |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 582 | char *str; |
| 583 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 584 | for (i = 0; i < info->num_cat; i++) { |
| 585 | if (info->cat[i].name == NULL) |
| 586 | continue; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 587 | size += strlen(info->cat[i].name) + 1; |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 588 | } |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 589 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 590 | for (i = 0; i < LOGLEVEL_DEFS; i++) |
| 591 | size += strlen(loglevel_strs[i].str) + 1; |
| 592 | |
| 593 | rem = size; |
Pablo Neira Ayuso | f1fae4d | 2011-05-03 22:32:42 +0200 | [diff] [blame] | 594 | str = talloc_zero_size(tall_log_ctx, size); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 595 | if (!str) |
| 596 | return NULL; |
| 597 | |
Holger Hans Peter Freyther | 952a18e | 2011-03-29 17:03:56 +0200 | [diff] [blame] | 598 | ret = snprintf(str + offset, rem, "logging level (all|"); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 599 | if (ret < 0) |
| 600 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 601 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 602 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 603 | for (i = 0; i < info->num_cat; i++) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 604 | if (info->cat[i].name) { |
| 605 | int j, name_len = strlen(info->cat[i].name)+1; |
| 606 | char name[name_len]; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 607 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 608 | for (j = 0; j < name_len; j++) |
| 609 | name[j] = tolower(info->cat[i].name[j]); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 610 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 611 | name[name_len-1] = '\0'; |
| 612 | ret = snprintf(str + offset, rem, "%s|", name+1); |
| 613 | if (ret < 0) |
| 614 | goto err; |
| 615 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 616 | } |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 617 | } |
| 618 | offset--; /* to remove the trailing | */ |
| 619 | rem++; |
| 620 | |
| 621 | ret = snprintf(str + offset, rem, ") ("); |
| 622 | if (ret < 0) |
| 623 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 624 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 625 | |
| 626 | for (i = 0; i < LOGLEVEL_DEFS; i++) { |
| 627 | int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1; |
| 628 | char loglevel_str[loglevel_str_len]; |
| 629 | |
| 630 | for (j = 0; j < loglevel_str_len; j++) |
| 631 | loglevel_str[j] = tolower(loglevel_strs[i].str[j]); |
| 632 | |
| 633 | loglevel_str[loglevel_str_len-1] = '\0'; |
| 634 | ret = snprintf(str + offset, rem, "%s|", loglevel_str); |
| 635 | if (ret < 0) |
| 636 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 637 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 638 | } |
| 639 | offset--; /* to remove the trailing | */ |
| 640 | rem++; |
| 641 | |
| 642 | ret = snprintf(str + offset, rem, ")"); |
| 643 | if (ret < 0) |
| 644 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 645 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 646 | err: |
Pablo Neira Ayuso | 534ba81 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 647 | str[size-1] = '\0'; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 648 | return str; |
| 649 | } |
| 650 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 651 | /*! \brief Generates the logging command description for VTY |
| 652 | * \param[in] unused_info Deprecated parameter, no longer used! |
| 653 | */ |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 654 | const char *log_vty_command_description(const struct log_info *unused_info) |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 655 | { |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 656 | struct log_info *info = osmo_log_info; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 657 | char *str; |
| 658 | int i, ret, len = 0, offset = 0, rem; |
| 659 | unsigned int size = |
| 660 | strlen(LOGGING_STR |
| 661 | "Set the log level for a specified category\n") + 1; |
| 662 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 663 | for (i = 0; i < info->num_cat; i++) { |
| 664 | if (info->cat[i].name == NULL) |
| 665 | continue; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 666 | size += strlen(info->cat[i].description) + 1; |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 667 | } |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 668 | |
| 669 | for (i = 0; i < LOGLEVEL_DEFS; i++) |
| 670 | size += strlen(loglevel_descriptions[i]) + 1; |
| 671 | |
Pablo Neira Ayuso | d6b5195 | 2011-05-03 22:32:32 +0200 | [diff] [blame] | 672 | size += strlen("Global setting for all subsystems") + 1; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 673 | rem = size; |
Pablo Neira Ayuso | f1fae4d | 2011-05-03 22:32:42 +0200 | [diff] [blame] | 674 | str = talloc_zero_size(tall_log_ctx, size); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 675 | if (!str) |
| 676 | return NULL; |
| 677 | |
| 678 | ret = snprintf(str + offset, rem, LOGGING_STR |
| 679 | "Set the log level for a specified category\n"); |
| 680 | if (ret < 0) |
| 681 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 682 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 683 | |
Pablo Neira Ayuso | d6b5195 | 2011-05-03 22:32:32 +0200 | [diff] [blame] | 684 | ret = snprintf(str + offset, rem, |
| 685 | "Global setting for all subsystems\n"); |
| 686 | if (ret < 0) |
| 687 | goto err; |
| 688 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 689 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 690 | for (i = 0; i < info->num_cat; i++) { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 691 | if (info->cat[i].name == NULL) |
| 692 | continue; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 693 | ret = snprintf(str + offset, rem, "%s\n", |
| 694 | info->cat[i].description); |
| 695 | if (ret < 0) |
| 696 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 697 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 698 | } |
| 699 | for (i = 0; i < LOGLEVEL_DEFS; i++) { |
| 700 | ret = snprintf(str + offset, rem, "%s\n", |
| 701 | loglevel_descriptions[i]); |
| 702 | if (ret < 0) |
| 703 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 704 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 705 | } |
| 706 | err: |
Pablo Neira Ayuso | 534ba81 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 707 | str[size-1] = '\0'; |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 708 | return str; |
| 709 | } |
| 710 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 711 | /*! \brief Initialize the Osmocom logging core |
| 712 | * \param[in] inf Information regarding logging categories |
| 713 | * \param[in] ctx \ref talloc context for logging allocations |
| 714 | * \returns 0 in case of success, negative in case of error |
| 715 | */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 716 | int log_init(const struct log_info *inf, void *ctx) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 717 | { |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 718 | int i; |
| 719 | |
| 720 | tall_log_ctx = talloc_named_const(ctx, 1, "logging"); |
| 721 | if (!tall_log_ctx) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | osmo_log_info = talloc_zero(tall_log_ctx, struct log_info); |
| 725 | if (!osmo_log_info) |
| 726 | return -ENOMEM; |
| 727 | |
| 728 | osmo_log_info->num_cat_user = inf->num_cat; |
| 729 | /* total number = number of user cat + library cat */ |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 730 | osmo_log_info->num_cat = inf->num_cat + ARRAY_SIZE(internal_cat); |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 731 | |
| 732 | osmo_log_info->cat = talloc_zero_array(osmo_log_info, |
| 733 | struct log_info_cat, |
| 734 | osmo_log_info->num_cat); |
| 735 | if (!osmo_log_info->cat) { |
| 736 | talloc_free(osmo_log_info); |
| 737 | osmo_log_info = NULL; |
| 738 | return -ENOMEM; |
| 739 | } |
| 740 | |
| 741 | /* copy over the user part */ |
| 742 | for (i = 0; i < inf->num_cat; i++) { |
| 743 | memcpy(&osmo_log_info->cat[i], &inf->cat[i], |
| 744 | sizeof(struct log_info_cat)); |
| 745 | } |
| 746 | |
| 747 | /* copy over the library part */ |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 748 | for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { |
Harald Welte | ce9fec3 | 2011-06-27 14:19:16 +0200 | [diff] [blame] | 749 | unsigned int cn = osmo_log_info->num_cat_user + i; |
| 750 | memcpy(&osmo_log_info->cat[cn], |
Harald Welte | 9fe1652 | 2011-06-27 14:00:03 +0200 | [diff] [blame] | 751 | &internal_cat[i], sizeof(struct log_info_cat)); |
| 752 | } |
| 753 | |
| 754 | return 0; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 755 | } |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 756 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 757 | /*! @} */ |