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 | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 23 | #include "../config.h" |
| 24 | |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 25 | #include <stdarg.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 29 | #include <ctype.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_STRINGS_H |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 32 | #include <strings.h> |
Harald Welte | 01fd5cb | 2010-03-26 23:51:31 +0800 | [diff] [blame] | 33 | #endif |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <errno.h> |
| 36 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/utils.h> |
| 39 | #include <osmocom/core/logging.h> |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 40 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 41 | #include <osmocom/vty/logging.h> /* for LOGGING_STR. */ |
| 42 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 43 | const struct log_info *osmo_log_info; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 44 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 45 | static struct log_context log_context; |
| 46 | static void *tall_log_ctx = NULL; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 47 | LLIST_HEAD(osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 48 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 49 | #define LOGLEVEL_DEFS 6 /* Number of loglevels.*/ |
| 50 | |
| 51 | static const struct value_string loglevel_strs[LOGLEVEL_DEFS+1] = { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 52 | { 0, "EVERYTHING" }, |
| 53 | { LOGL_DEBUG, "DEBUG" }, |
| 54 | { LOGL_INFO, "INFO" }, |
| 55 | { LOGL_NOTICE, "NOTICE" }, |
| 56 | { LOGL_ERROR, "ERROR" }, |
| 57 | { LOGL_FATAL, "FATAL" }, |
| 58 | { 0, NULL }, |
| 59 | }; |
| 60 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 61 | /* You have to keep this in sync with the structure loglevel_strs. */ |
| 62 | const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = { |
| 63 | "Log simply everything", |
| 64 | "Log debug messages and higher levels", |
| 65 | "Log informational messages and higher levels", |
| 66 | "Log noticable messages and higher levels", |
| 67 | "Log error messages and higher levels", |
| 68 | "Log only fatal messages", |
| 69 | NULL, |
| 70 | }; |
| 71 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 72 | int log_parse_level(const char *lvl) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 73 | { |
| 74 | return get_string_value(loglevel_strs, lvl); |
| 75 | } |
| 76 | |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 77 | const char *log_level_str(unsigned int lvl) |
| 78 | { |
| 79 | return get_value_string(loglevel_strs, lvl); |
| 80 | } |
| 81 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 82 | int log_parse_category(const char *category) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 86 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
| 87 | if (!strcasecmp(osmo_log_info->cat[i].name+1, category)) |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 88 | return i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Parse the category mask. |
| 96 | * The format can be this: category1:category2:category3 |
| 97 | * or category1,2:category2,3:... |
| 98 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 99 | void log_parse_category_mask(struct log_target* target, const char *_mask) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 100 | { |
| 101 | int i = 0; |
| 102 | char *mask = strdup(_mask); |
| 103 | char *category_token = NULL; |
| 104 | |
| 105 | /* Disable everything to enable it afterwards */ |
| 106 | for (i = 0; i < ARRAY_SIZE(target->categories); ++i) |
| 107 | target->categories[i].enabled = 0; |
| 108 | |
| 109 | category_token = strtok(mask, ":"); |
| 110 | do { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 111 | for (i = 0; i < osmo_log_info->num_cat; ++i) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 112 | char* colon = strstr(category_token, ","); |
| 113 | int length = strlen(category_token); |
| 114 | |
| 115 | if (colon) |
| 116 | length = colon - category_token; |
| 117 | |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 118 | if (strncasecmp(osmo_log_info->cat[i].name, |
| 119 | category_token, length) == 0) { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 120 | int level = 0; |
| 121 | |
| 122 | if (colon) |
| 123 | level = atoi(colon+1); |
| 124 | |
Harald Welte | faadfe2 | 2010-03-26 21:05:43 +0800 | [diff] [blame] | 125 | target->categories[i].enabled = 1; |
| 126 | target->categories[i].loglevel = level; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } while ((category_token = strtok(NULL, ":"))); |
| 130 | |
| 131 | free(mask); |
| 132 | } |
| 133 | |
| 134 | static const char* color(int subsys) |
| 135 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 136 | if (subsys < osmo_log_info->num_cat) |
| 137 | return osmo_log_info->cat[subsys].color; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 138 | |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 139 | return NULL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 142 | static void _output(struct log_target *target, unsigned int subsys, |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 143 | unsigned int level, char *file, int line, int cont, |
| 144 | const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 145 | { |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 146 | char buf[4096]; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 147 | int ret, len = 0, offset = 0, rem = sizeof(buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 148 | |
| 149 | /* are we using color */ |
| 150 | if (target->use_color) { |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 151 | const char *c = color(subsys); |
| 152 | if (c) { |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 153 | ret = snprintf(buf + offset, rem, "%s", color(subsys)); |
| 154 | if (ret < 0) |
| 155 | goto err; |
| 156 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | d788f66 | 2010-03-26 09:45:03 +0800 | [diff] [blame] | 157 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 158 | } |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 159 | if (!cont) { |
| 160 | if (target->print_timestamp) { |
| 161 | char *timestr; |
| 162 | time_t tm; |
| 163 | tm = time(NULL); |
| 164 | timestr = ctime(&tm); |
| 165 | timestr[strlen(timestr)-1] = '\0'; |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 166 | ret = snprintf(buf + offset, rem, "%s ", timestr); |
| 167 | if (ret < 0) |
| 168 | goto err; |
| 169 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 170 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 171 | ret = snprintf(buf + offset, rem, "<%4.4x> %s:%d ", |
| 172 | subsys, file, line); |
| 173 | if (ret < 0) |
| 174 | goto err; |
| 175 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 176 | } |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 177 | ret = vsnprintf(buf + offset, rem, format, ap); |
| 178 | if (ret < 0) |
| 179 | goto err; |
| 180 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 181 | |
Pablo Neira Ayuso | 7503fb8 | 2011-05-03 22:32:43 +0200 | [diff] [blame] | 182 | ret = snprintf(buf + offset, rem, "%s", |
| 183 | target->use_color ? "\033[0;m" : ""); |
| 184 | if (ret < 0) |
| 185 | goto err; |
| 186 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 187 | err: |
| 188 | buf[sizeof(buf)-1] = '\0'; |
| 189 | target->output(target, level, buf); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 193 | static void _logp(unsigned int subsys, int level, char *file, int line, |
| 194 | int cont, const char *format, va_list ap) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 195 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 196 | struct log_target *tar; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 197 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 198 | llist_for_each_entry(tar, &osmo_log_target_list, entry) { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 199 | struct log_category *category; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 200 | int output = 0; |
| 201 | |
| 202 | category = &tar->categories[subsys]; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 203 | /* subsystem is not supposed to be logged */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 204 | if (!category->enabled) |
| 205 | continue; |
| 206 | |
| 207 | /* Check the global log level */ |
| 208 | if (tar->loglevel != 0 && level < tar->loglevel) |
| 209 | continue; |
| 210 | |
| 211 | /* Check the category log level */ |
| 212 | if (tar->loglevel == 0 && category->loglevel != 0 && |
| 213 | level < category->loglevel) |
| 214 | continue; |
| 215 | |
| 216 | /* Apply filters here... if that becomes messy we will |
| 217 | * need to put filters in a list and each filter will |
| 218 | * say stop, continue, output */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 219 | if ((tar->filter_map & LOG_FILTER_ALL) != 0) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 220 | output = 1; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 221 | else if (osmo_log_info->filter_fn) |
| 222 | output = osmo_log_info->filter_fn(&log_context, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 223 | tar); |
Pablo Neira Ayuso | 81e9636 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 224 | if (!output) |
| 225 | continue; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 226 | |
Pablo Neira Ayuso | 81e9636 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 227 | _output(tar, subsys, level, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 231 | void logp(unsigned int subsys, char *file, int line, int cont, |
| 232 | const char *format, ...) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 233 | { |
| 234 | va_list ap; |
| 235 | |
| 236 | va_start(ap, format); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 237 | _logp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 238 | va_end(ap); |
| 239 | } |
| 240 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 241 | void logp2(unsigned 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] | 242 | { |
| 243 | va_list ap; |
| 244 | |
| 245 | va_start(ap, format); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 246 | _logp(subsys, level, file, line, cont, format, ap); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 247 | va_end(ap); |
| 248 | } |
| 249 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 250 | void log_add_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 251 | { |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 252 | llist_add_tail(&target->entry, &osmo_log_target_list); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 255 | void log_del_target(struct log_target *target) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 256 | { |
| 257 | llist_del(&target->entry); |
| 258 | } |
| 259 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 260 | void log_reset_context(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 261 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 262 | memset(&log_context, 0, sizeof(log_context)); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 263 | } |
| 264 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 265 | int log_set_context(uint8_t ctx_nr, void *value) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 266 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 267 | if (ctx_nr > LOG_MAX_CTX) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 270 | log_context.ctx[ctx_nr] = value; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 275 | void log_set_all_filter(struct log_target *target, int all) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 276 | { |
| 277 | if (all) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 278 | target->filter_map |= LOG_FILTER_ALL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 279 | else |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 280 | target->filter_map &= ~LOG_FILTER_ALL; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 283 | void log_set_use_color(struct log_target *target, int use_color) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 284 | { |
| 285 | target->use_color = use_color; |
| 286 | } |
| 287 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 288 | void log_set_print_timestamp(struct log_target *target, int print_timestamp) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 289 | { |
| 290 | target->print_timestamp = print_timestamp; |
| 291 | } |
| 292 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 293 | void log_set_log_level(struct log_target *target, int log_level) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 294 | { |
| 295 | target->loglevel = log_level; |
| 296 | } |
| 297 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 298 | void log_set_category_filter(struct log_target *target, int category, |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 299 | int enable, int level) |
| 300 | { |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 301 | if (category >= osmo_log_info->num_cat) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 302 | return; |
| 303 | target->categories[category].enabled = !!enable; |
| 304 | target->categories[category].loglevel = level; |
| 305 | } |
| 306 | |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 307 | static void _file_output(struct log_target *target, unsigned int level, |
| 308 | const char *log) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 309 | { |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 310 | fprintf(target->tgt_file.out, "%s", log); |
| 311 | fflush(target->tgt_file.out); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 312 | } |
| 313 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 314 | struct log_target *log_target_create(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 315 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 316 | struct log_target *target; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 317 | unsigned int i; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 318 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 319 | target = talloc_zero(tall_log_ctx, struct log_target); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 320 | if (!target) |
| 321 | return NULL; |
| 322 | |
| 323 | INIT_LLIST_HEAD(&target->entry); |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 324 | |
| 325 | /* initialize the per-category enabled/loglevel from defaults */ |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 326 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 327 | struct log_category *cat = &target->categories[i]; |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 328 | cat->enabled = osmo_log_info->cat[i].enabled; |
| 329 | cat->loglevel = osmo_log_info->cat[i].loglevel; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* global settings */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 333 | target->use_color = 1; |
| 334 | target->print_timestamp = 0; |
Harald Welte | cc6313c | 2010-03-26 22:04:03 +0800 | [diff] [blame] | 335 | |
| 336 | /* global log level */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 337 | target->loglevel = 0; |
| 338 | return target; |
| 339 | } |
| 340 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 341 | struct log_target *log_target_create_stderr(void) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 342 | { |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 343 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 344 | #ifdef stderr |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 345 | struct log_target *target; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 346 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 347 | target = log_target_create(); |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 348 | if (!target) |
| 349 | return NULL; |
| 350 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 351 | target->type = LOG_TGT_TYPE_STDERR; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 352 | target->tgt_file.out = stderr; |
| 353 | target->output = _file_output; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 354 | return target; |
Harald Welte | a3b844c | 2010-03-27 00:04:40 +0800 | [diff] [blame] | 355 | #else |
| 356 | return NULL; |
| 357 | #endif /* stderr */ |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 358 | } |
| 359 | |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 360 | struct log_target *log_target_create_file(const char *fname) |
| 361 | { |
| 362 | struct log_target *target; |
| 363 | |
| 364 | target = log_target_create(); |
| 365 | if (!target) |
| 366 | return NULL; |
| 367 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 368 | target->type = LOG_TGT_TYPE_FILE; |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 369 | target->tgt_file.out = fopen(fname, "a"); |
| 370 | if (!target->tgt_file.out) |
| 371 | return NULL; |
| 372 | |
| 373 | target->output = _file_output; |
| 374 | |
| 375 | target->tgt_file.fname = talloc_strdup(target, fname); |
| 376 | |
| 377 | return target; |
| 378 | } |
| 379 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 380 | struct log_target *log_target_find(int type, const char *fname) |
| 381 | { |
| 382 | struct log_target *tgt; |
| 383 | |
| 384 | llist_for_each_entry(tgt, &osmo_log_target_list, entry) { |
| 385 | if (tgt->type != type) |
| 386 | continue; |
| 387 | if (tgt->type == LOG_TGT_TYPE_FILE) { |
| 388 | if (!strcmp(fname, tgt->tgt_file.fname)) |
| 389 | return tgt; |
| 390 | } else |
| 391 | return tgt; |
| 392 | } |
| 393 | return NULL; |
| 394 | } |
| 395 | |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 396 | void log_target_destroy(struct log_target *target) |
| 397 | { |
| 398 | |
| 399 | /* just in case, to make sure we don't have any references */ |
| 400 | log_del_target(target); |
| 401 | |
| 402 | if (target->output == &_file_output) { |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 403 | /* since C89/C99 says stderr is a macro, we can safely do this! */ |
| 404 | #ifdef stderr |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 405 | /* don't close stderr */ |
Sylvain Munaut | af5ee34 | 2010-09-17 14:38:17 +0200 | [diff] [blame] | 406 | if (target->tgt_file.out != stderr) |
| 407 | #endif |
| 408 | { |
Harald Welte | 3086c39 | 2010-08-25 19:10:50 +0200 | [diff] [blame] | 409 | fclose(target->tgt_file.out); |
| 410 | target->tgt_file.out = NULL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | talloc_free(target); |
| 415 | } |
| 416 | |
| 417 | /* close and re-open a log file (for log file rotation) */ |
| 418 | int log_target_file_reopen(struct log_target *target) |
| 419 | { |
| 420 | fclose(target->tgt_file.out); |
| 421 | |
| 422 | target->tgt_file.out = fopen(target->tgt_file.fname, "a"); |
| 423 | if (!target->tgt_file.out) |
| 424 | return -errno; |
| 425 | |
| 426 | /* we assume target->output already to be set */ |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 431 | /* This generates the logging command string for VTY. */ |
| 432 | const char *log_vty_command_string(const struct log_info *info) |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 433 | { |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 434 | int len = 0, offset = 0, ret, i, rem; |
| 435 | int size = strlen("logging level () ()") + 1; |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 436 | char *str; |
| 437 | |
| 438 | for (i = 0; i < info->num_cat; i++) |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 439 | size += strlen(info->cat[i].name) + 1; |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 440 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 441 | for (i = 0; i < LOGLEVEL_DEFS; i++) |
| 442 | size += strlen(loglevel_strs[i].str) + 1; |
| 443 | |
| 444 | rem = size; |
Pablo Neira Ayuso | f1fae4d | 2011-05-03 22:32:42 +0200 | [diff] [blame] | 445 | str = talloc_zero_size(tall_log_ctx, size); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 446 | if (!str) |
| 447 | return NULL; |
| 448 | |
Holger Hans Peter Freyther | 952a18e | 2011-03-29 17:03:56 +0200 | [diff] [blame] | 449 | ret = snprintf(str + offset, rem, "logging level (all|"); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 450 | if (ret < 0) |
| 451 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 452 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 453 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 454 | for (i = 0; i < info->num_cat; i++) { |
| 455 | int j, name_len = strlen(info->cat[i].name)+1; |
| 456 | char name[name_len]; |
| 457 | |
| 458 | for (j = 0; j < name_len; j++) |
| 459 | name[j] = tolower(info->cat[i].name[j]); |
| 460 | |
| 461 | name[name_len-1] = '\0'; |
| 462 | ret = snprintf(str + offset, rem, "%s|", name+1); |
| 463 | if (ret < 0) |
| 464 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 465 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 466 | } |
| 467 | offset--; /* to remove the trailing | */ |
| 468 | rem++; |
| 469 | |
| 470 | ret = snprintf(str + offset, rem, ") ("); |
| 471 | if (ret < 0) |
| 472 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 473 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 474 | |
| 475 | for (i = 0; i < LOGLEVEL_DEFS; i++) { |
| 476 | int j, loglevel_str_len = strlen(loglevel_strs[i].str)+1; |
| 477 | char loglevel_str[loglevel_str_len]; |
| 478 | |
| 479 | for (j = 0; j < loglevel_str_len; j++) |
| 480 | loglevel_str[j] = tolower(loglevel_strs[i].str[j]); |
| 481 | |
| 482 | loglevel_str[loglevel_str_len-1] = '\0'; |
| 483 | ret = snprintf(str + offset, rem, "%s|", loglevel_str); |
| 484 | if (ret < 0) |
| 485 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 486 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 487 | } |
| 488 | offset--; /* to remove the trailing | */ |
| 489 | rem++; |
| 490 | |
| 491 | ret = snprintf(str + offset, rem, ")"); |
| 492 | if (ret < 0) |
| 493 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 494 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 495 | err: |
Pablo Neira Ayuso | 534ba81 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 496 | str[size-1] = '\0'; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 497 | return str; |
| 498 | } |
| 499 | |
| 500 | /* This generates the logging command description for VTY. */ |
| 501 | const char *log_vty_command_description(const struct log_info *info) |
| 502 | { |
| 503 | char *str; |
| 504 | int i, ret, len = 0, offset = 0, rem; |
| 505 | unsigned int size = |
| 506 | strlen(LOGGING_STR |
| 507 | "Set the log level for a specified category\n") + 1; |
| 508 | |
| 509 | for (i = 0; i < info->num_cat; i++) |
| 510 | size += strlen(info->cat[i].description) + 1; |
| 511 | |
| 512 | for (i = 0; i < LOGLEVEL_DEFS; i++) |
| 513 | size += strlen(loglevel_descriptions[i]) + 1; |
| 514 | |
Pablo Neira Ayuso | d6b5195 | 2011-05-03 22:32:32 +0200 | [diff] [blame] | 515 | size += strlen("Global setting for all subsystems") + 1; |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 516 | rem = size; |
Pablo Neira Ayuso | f1fae4d | 2011-05-03 22:32:42 +0200 | [diff] [blame] | 517 | str = talloc_zero_size(tall_log_ctx, size); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 518 | if (!str) |
| 519 | return NULL; |
| 520 | |
| 521 | ret = snprintf(str + offset, rem, LOGGING_STR |
| 522 | "Set the log level for a specified category\n"); |
| 523 | if (ret < 0) |
| 524 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 525 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 526 | |
Pablo Neira Ayuso | d6b5195 | 2011-05-03 22:32:32 +0200 | [diff] [blame] | 527 | ret = snprintf(str + offset, rem, |
| 528 | "Global setting for all subsystems\n"); |
| 529 | if (ret < 0) |
| 530 | goto err; |
| 531 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 532 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 533 | for (i = 0; i < info->num_cat; i++) { |
| 534 | ret = snprintf(str + offset, rem, "%s\n", |
| 535 | info->cat[i].description); |
| 536 | if (ret < 0) |
| 537 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 538 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 539 | } |
| 540 | for (i = 0; i < LOGLEVEL_DEFS; i++) { |
| 541 | ret = snprintf(str + offset, rem, "%s\n", |
| 542 | loglevel_descriptions[i]); |
| 543 | if (ret < 0) |
| 544 | goto err; |
Pablo Neira Ayuso | 3abad6a | 2011-03-28 19:24:22 +0200 | [diff] [blame] | 545 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 546 | } |
| 547 | err: |
Pablo Neira Ayuso | 534ba81 | 2011-05-03 22:32:48 +0200 | [diff] [blame] | 548 | str[size-1] = '\0'; |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 549 | return str; |
| 550 | } |
| 551 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 552 | void log_init(const struct log_info *cat) |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 553 | { |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 554 | tall_log_ctx = talloc_named_const(NULL, 1, "logging"); |
Harald Welte | 4ebdf74 | 2010-05-19 19:54:00 +0200 | [diff] [blame] | 555 | osmo_log_info = cat; |
Harald Welte | 4a2bb9e | 2010-03-26 09:33:40 +0800 | [diff] [blame] | 556 | } |