Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 3 | * (C) 2009-2014 by Holger Hans Peter Freyther |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 27 | #include "../../config.h" |
| 28 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/logging.h> |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 31 | #include <osmocom/core/logging_internal.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 33 | #include <osmocom/core/strrb.h> |
| 34 | #include <osmocom/core/loggingrb.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 35 | #include <osmocom/core/gsmtap.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 36 | |
| 37 | #include <osmocom/vty/command.h> |
| 38 | #include <osmocom/vty/buffer.h> |
| 39 | #include <osmocom/vty/vty.h> |
| 40 | #include <osmocom/vty/telnet_interface.h> |
| 41 | #include <osmocom/vty/logging.h> |
| 42 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 43 | #define LOG_STR "Configure logging sub-system\n" |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 44 | #define LEVEL_STR "Set the log level for a specified category\n" |
| 45 | |
| 46 | #define CATEGORY_ALL_STR "Global setting for all subsystems\n" |
| 47 | |
| 48 | #define LOG_LEVEL_ARGS "debug|info|notice|error|fatal" |
| 49 | #define LOG_LEVEL_STRS \ |
| 50 | "Log debug messages and higher levels\n" \ |
| 51 | "Log informational messages and higher levels\n" \ |
| 52 | "Log noticeable messages and higher levels\n" \ |
| 53 | "Log error messages and higher levels\n" \ |
| 54 | "Log only fatal messages\n" |
| 55 | |
| 56 | #define EVERYTHING_STR "Don't use. It doesn't log anything\n" |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 57 | |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 58 | /*! \file logging_vty.c |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 59 | * Configuration of logging from VTY |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 60 | * |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 61 | * This module implements |
| 62 | * - functions that permit configuration of the libosmocore logging |
| 63 | * framework from VTY commands in the configure -> logging node. |
| 64 | * |
| 65 | * - functions that permit logging *to* a VTY session. Basically each |
| 66 | * VTY session gets its own log target, with configurable |
| 67 | * per-subsystem log levels. This is performed internally via the |
| 68 | * \ref log_target_create_vty function. |
| 69 | * |
| 70 | * You have to call \ref logging_vty_add_cmds from your application |
| 71 | * once to enable both of the above. |
| 72 | * |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 73 | */ |
| 74 | |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 75 | static void _vty_output(struct log_target *tgt, |
| 76 | unsigned int level, const char *line) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 77 | { |
| 78 | struct vty *vty = tgt->tgt_vty.vty; |
| 79 | vty_out(vty, "%s", line); |
| 80 | /* This is an ugly hack, but there is no easy way... */ |
| 81 | if (strchr(line, '\n')) |
| 82 | vty_out(vty, "\r"); |
| 83 | } |
| 84 | |
| 85 | struct log_target *log_target_create_vty(struct vty *vty) |
| 86 | { |
| 87 | struct log_target *target; |
| 88 | |
| 89 | target = log_target_create(); |
| 90 | if (!target) |
| 91 | return NULL; |
| 92 | |
| 93 | target->tgt_vty.vty = vty; |
| 94 | target->output = _vty_output; |
| 95 | return target; |
| 96 | } |
| 97 | |
| 98 | DEFUN(enable_logging, |
| 99 | enable_logging_cmd, |
| 100 | "logging enable", |
| 101 | LOGGING_STR |
| 102 | "Enables logging to this vty\n") |
| 103 | { |
| 104 | struct telnet_connection *conn; |
| 105 | |
| 106 | conn = (struct telnet_connection *) vty->priv; |
| 107 | if (conn->dbg) { |
| 108 | vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE); |
| 109 | return CMD_WARNING; |
| 110 | } |
| 111 | |
| 112 | conn->dbg = log_target_create_vty(vty); |
| 113 | if (!conn->dbg) |
| 114 | return CMD_WARNING; |
| 115 | |
| 116 | log_add_target(conn->dbg); |
| 117 | return CMD_SUCCESS; |
| 118 | } |
| 119 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 120 | struct log_target *osmo_log_vty2tgt(struct vty *vty) |
| 121 | { |
| 122 | struct telnet_connection *conn; |
| 123 | |
| 124 | if (vty->node == CFG_LOG_NODE) |
| 125 | return vty->index; |
| 126 | |
| 127 | |
| 128 | conn = (struct telnet_connection *) vty->priv; |
| 129 | if (!conn->dbg) |
| 130 | vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE); |
| 131 | |
| 132 | return conn->dbg; |
| 133 | } |
| 134 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 135 | DEFUN(logging_fltr_all, |
| 136 | logging_fltr_all_cmd, |
| 137 | "logging filter all (0|1)", |
| 138 | LOGGING_STR FILTER_STR |
| 139 | "Do you want to log all messages?\n" |
| 140 | "Only print messages matched by other filters\n" |
| 141 | "Bypass filter and print all messages\n") |
| 142 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 143 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 144 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 145 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 146 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 147 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 148 | log_set_all_filter(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 149 | return CMD_SUCCESS; |
| 150 | } |
| 151 | |
| 152 | DEFUN(logging_use_clr, |
| 153 | logging_use_clr_cmd, |
| 154 | "logging color (0|1)", |
| 155 | LOGGING_STR "Configure color-printing for log messages\n" |
| 156 | "Don't use color for printing messages\n" |
| 157 | "Use color for printing messages\n") |
| 158 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 159 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 160 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 161 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 162 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 163 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 164 | log_set_use_color(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 165 | return CMD_SUCCESS; |
| 166 | } |
| 167 | |
| 168 | DEFUN(logging_prnt_timestamp, |
| 169 | logging_prnt_timestamp_cmd, |
| 170 | "logging timestamp (0|1)", |
| 171 | LOGGING_STR "Configure log message timestamping\n" |
| 172 | "Don't prefix each log message\n" |
| 173 | "Prefix each log message with current timestamp\n") |
| 174 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 175 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 176 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 177 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 178 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 179 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 180 | log_set_print_timestamp(tgt, atoi(argv[0])); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 181 | return CMD_SUCCESS; |
| 182 | } |
| 183 | |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 184 | DEFUN(logging_prnt_ext_timestamp, |
| 185 | logging_prnt_ext_timestamp_cmd, |
| 186 | "logging print extended-timestamp (0|1)", |
| 187 | LOGGING_STR "Log output settings\n" |
| 188 | "Configure log message timestamping\n" |
| 189 | "Don't prefix each log message\n" |
| 190 | "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n") |
| 191 | { |
| 192 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 193 | |
| 194 | if (!tgt) |
| 195 | return CMD_WARNING; |
| 196 | |
| 197 | log_set_print_extended_timestamp(tgt, atoi(argv[0])); |
| 198 | return CMD_SUCCESS; |
| 199 | } |
| 200 | |
| 201 | DEFUN(logging_prnt_cat, |
| 202 | logging_prnt_cat_cmd, |
| 203 | "logging print category (0|1)", |
| 204 | LOGGING_STR "Log output settings\n" |
| 205 | "Configure log message\n" |
| 206 | "Don't prefix each log message\n" |
| 207 | "Prefix each log message with category/subsystem name\n") |
| 208 | { |
| 209 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 210 | |
| 211 | if (!tgt) |
| 212 | return CMD_WARNING; |
| 213 | |
| 214 | log_set_print_category(tgt, atoi(argv[0])); |
| 215 | return CMD_SUCCESS; |
| 216 | } |
| 217 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 218 | DEFUN(logging_prnt_cat_hex, |
| 219 | logging_prnt_cat_hex_cmd, |
| 220 | "logging print category-hex (0|1)", |
| 221 | LOGGING_STR "Log output settings\n" |
| 222 | "Configure log message\n" |
| 223 | "Don't prefix each log message\n" |
| 224 | "Prefix each log message with category/subsystem nr in hex ('<000b>')\n") |
| 225 | { |
| 226 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 227 | |
| 228 | if (!tgt) |
| 229 | return CMD_WARNING; |
| 230 | |
| 231 | log_set_print_category_hex(tgt, atoi(argv[0])); |
| 232 | return CMD_SUCCESS; |
| 233 | } |
| 234 | |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 235 | DEFUN(logging_prnt_level, |
| 236 | logging_prnt_level_cmd, |
| 237 | "logging print level (0|1)", |
| 238 | LOGGING_STR "Log output settings\n" |
| 239 | "Configure log message\n" |
| 240 | "Don't prefix each log message\n" |
| 241 | "Prefix each log message with the log level name\n") |
| 242 | { |
| 243 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 244 | |
| 245 | if (!tgt) |
| 246 | return CMD_WARNING; |
| 247 | |
| 248 | log_set_print_level(tgt, atoi(argv[0])); |
| 249 | return CMD_SUCCESS; |
| 250 | } |
| 251 | |
Neels Hofmeyr | 22772cc | 2018-02-06 00:52:08 +0100 | [diff] [blame] | 252 | static const struct value_string logging_print_file_args[] = { |
| 253 | { LOG_FILENAME_NONE, "0" }, |
| 254 | { LOG_FILENAME_PATH, "1" }, |
| 255 | { LOG_FILENAME_BASENAME, "basename" }, |
| 256 | { 0, NULL } |
| 257 | }; |
| 258 | |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 259 | DEFUN(logging_prnt_file, |
| 260 | logging_prnt_file_cmd, |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 261 | "logging print file (0|1|basename) [last]", |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 262 | LOGGING_STR "Log output settings\n" |
| 263 | "Configure log message\n" |
| 264 | "Don't prefix each log message\n" |
| 265 | "Prefix each log message with the source file and line\n" |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 266 | "Prefix each log message with the source file's basename (strip leading paths) and line\n" |
| 267 | "Log source file info at the end of a log line. If omitted, log source file info just" |
| 268 | " before the log text.\n") |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 269 | { |
| 270 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 271 | |
| 272 | if (!tgt) |
| 273 | return CMD_WARNING; |
| 274 | |
Neels Hofmeyr | 22772cc | 2018-02-06 00:52:08 +0100 | [diff] [blame] | 275 | log_set_print_filename2(tgt, get_string_value(logging_print_file_args, argv[0])); |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 276 | if (argc > 1) |
| 277 | log_set_print_filename_pos(tgt, LOG_FILENAME_POS_LINE_END); |
| 278 | else |
| 279 | log_set_print_filename_pos(tgt, LOG_FILENAME_POS_HEADER_END); |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 280 | return CMD_SUCCESS; |
| 281 | } |
| 282 | |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 283 | static void add_category_strings(char **cmd_str_p, char **doc_str_p, |
| 284 | const struct log_info *categories) |
| 285 | { |
| 286 | int i; |
| 287 | for (i = 0; i < categories->num_cat; i++) { |
| 288 | if (categories->cat[i].name == NULL) |
| 289 | continue; |
| 290 | /* skip the leading 'D' in each category name, hence '+ 1' */ |
| 291 | osmo_talloc_asprintf(tall_log_ctx, *cmd_str_p, "%s%s", |
| 292 | i ? "|" : "", |
| 293 | osmo_str_tolower(categories->cat[i].name + 1)); |
| 294 | osmo_talloc_asprintf(tall_log_ctx, *doc_str_p, "%s\n", |
| 295 | categories->cat[i].description); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void gen_logging_level_cmd_strs(struct cmd_element *cmd, |
| 300 | const char *level_args, const char *level_strs) |
| 301 | { |
| 302 | char *cmd_str = NULL; |
| 303 | char *doc_str = NULL; |
| 304 | |
| 305 | assert_loginfo(__func__); |
| 306 | |
| 307 | OSMO_ASSERT(cmd->string == NULL); |
| 308 | OSMO_ASSERT(cmd->doc == NULL); |
| 309 | |
| 310 | osmo_talloc_asprintf(tall_log_ctx, cmd_str, "logging level (all|"); |
| 311 | osmo_talloc_asprintf(tall_log_ctx, doc_str, |
| 312 | LOGGING_STR |
| 313 | LEVEL_STR |
| 314 | CATEGORY_ALL_STR); |
| 315 | add_category_strings(&cmd_str, &doc_str, osmo_log_info); |
| 316 | osmo_talloc_asprintf(tall_log_ctx, cmd_str, ") %s", level_args); |
| 317 | osmo_talloc_asprintf(tall_log_ctx, doc_str, "%s", level_strs); |
| 318 | |
| 319 | cmd->string = cmd_str; |
| 320 | cmd->doc = doc_str; |
| 321 | } |
| 322 | |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 323 | /* logging level (all|<categories>) (debug|...|fatal) */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 324 | DEFUN(logging_level, |
| 325 | logging_level_cmd, |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 326 | NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */ |
| 327 | NULL) /* same thing for helpstr. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 328 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 329 | int category = log_parse_category(argv[0]); |
| 330 | int level = log_parse_level(argv[1]); |
| 331 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 332 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 333 | if (!tgt) |
| 334 | return CMD_WARNING; |
| 335 | |
| 336 | if (level < 0) { |
| 337 | vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 338 | return CMD_WARNING; |
| 339 | } |
| 340 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 341 | /* Check for special case where we want to set global log level */ |
| 342 | if (!strcmp(argv[0], "all")) { |
| 343 | log_set_log_level(tgt, level); |
| 344 | return CMD_SUCCESS; |
| 345 | } |
| 346 | |
| 347 | if (category < 0) { |
| 348 | vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE); |
| 349 | return CMD_WARNING; |
| 350 | } |
| 351 | |
| 352 | tgt->categories[category].enabled = 1; |
| 353 | tgt->categories[category].loglevel = level; |
| 354 | |
| 355 | return CMD_SUCCESS; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 358 | /* logging level (all|<categories>) everything */ |
| 359 | DEFUN_DEPRECATED(deprecated_logging_level_everything, deprecated_logging_level_everything_cmd, |
| 360 | NULL, /* cmdstr is dynamically set in logging_vty_add_cmds(). */ |
| 361 | NULL) /* same thing for helpstr. */ |
| 362 | { |
| 363 | vty_out(vty, "%% Ignoring deprecated logging level 'everything' keyword%s", VTY_NEWLINE); |
| 364 | return CMD_SUCCESS; |
| 365 | } |
| 366 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 367 | DEFUN(logging_set_category_mask, |
| 368 | logging_set_category_mask_cmd, |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 369 | "logging set-log-mask MASK", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 370 | LOGGING_STR |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 371 | "Set the logmask of this logging target\n" |
Neels Hofmeyr | 84ea2e0 | 2017-12-09 05:53:18 +0100 | [diff] [blame] | 372 | "List of logging categories to log, e.g. 'abc:mno:xyz'. Available log categories depend on the specific" |
| 373 | " application, refer to the 'logging level' command. Optionally add individual log levels like" |
| 374 | " 'abc,1:mno,3:xyz,5', where the level numbers are" |
| 375 | " " OSMO_STRINGIFY(LOGL_DEBUG) "=" OSMO_STRINGIFY_VAL(LOGL_DEBUG) |
| 376 | " " OSMO_STRINGIFY(LOGL_INFO) "=" OSMO_STRINGIFY_VAL(LOGL_INFO) |
| 377 | " " OSMO_STRINGIFY(LOGL_NOTICE) "=" OSMO_STRINGIFY_VAL(LOGL_NOTICE) |
| 378 | " " OSMO_STRINGIFY(LOGL_ERROR) "=" OSMO_STRINGIFY_VAL(LOGL_ERROR) |
| 379 | " " OSMO_STRINGIFY(LOGL_FATAL) "=" OSMO_STRINGIFY_VAL(LOGL_FATAL) |
| 380 | "\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 381 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 382 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 383 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 384 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 385 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 386 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 387 | log_parse_category_mask(tgt, argv[0]); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 388 | return CMD_SUCCESS; |
| 389 | } |
| 390 | |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 391 | ALIAS_DEPRECATED(logging_set_category_mask, |
| 392 | logging_set_category_mask_old_cmd, |
| 393 | "logging set log mask MASK", |
| 394 | LOGGING_STR |
| 395 | "Decide which categories to output.\n" |
Neels Hofmeyr | 84ea2e0 | 2017-12-09 05:53:18 +0100 | [diff] [blame] | 396 | "Log commands\n" "Mask commands\n" |
| 397 | "'set log mask' is deprecated, please refer to the docs of 'set-log-mask' instead\n"); |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 398 | |
| 399 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 400 | DEFUN(diable_logging, |
| 401 | disable_logging_cmd, |
| 402 | "logging disable", |
| 403 | LOGGING_STR |
| 404 | "Disables logging to this vty\n") |
| 405 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 406 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
| 407 | struct telnet_connection *conn = (struct telnet_connection *) vty->priv; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 408 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 409 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 410 | return CMD_WARNING; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 411 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 412 | log_del_target(tgt); |
| 413 | talloc_free(tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 414 | conn->dbg = NULL; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 415 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 416 | return CMD_SUCCESS; |
| 417 | } |
| 418 | |
| 419 | static void vty_print_logtarget(struct vty *vty, const struct log_info *info, |
| 420 | const struct log_target *tgt) |
| 421 | { |
| 422 | unsigned int i; |
| 423 | |
| 424 | vty_out(vty, " Global Loglevel: %s%s", |
| 425 | log_level_str(tgt->loglevel), VTY_NEWLINE); |
| 426 | vty_out(vty, " Use color: %s, Print Timestamp: %s%s", |
| 427 | tgt->use_color ? "On" : "Off", |
| 428 | tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE); |
| 429 | |
| 430 | vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE); |
| 431 | |
| 432 | for (i = 0; i < info->num_cat; i++) { |
| 433 | const struct log_category *cat = &tgt->categories[i]; |
Daniel Willmann | 55363a9 | 2016-11-15 10:05:51 +0100 | [diff] [blame] | 434 | /* Skip categories that were not initialized */ |
| 435 | if (!info->cat[i].name) |
| 436 | continue; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 437 | vty_out(vty, " %-10s %-10s %-8s %s%s", |
| 438 | info->cat[i].name+1, log_level_str(cat->loglevel), |
| 439 | cat->enabled ? "Enabled" : "Disabled", |
| 440 | info->cat[i].description, |
| 441 | VTY_NEWLINE); |
| 442 | } |
Harald Welte | 6d2d4d6 | 2013-03-10 09:53:24 +0000 | [diff] [blame] | 443 | |
| 444 | vty_out(vty, " Log Filter 'ALL': %s%s", |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 445 | tgt->filter_map & (1 << LOG_FLT_ALL) ? "Enabled" : "Disabled", |
Harald Welte | 6d2d4d6 | 2013-03-10 09:53:24 +0000 | [diff] [blame] | 446 | VTY_NEWLINE); |
| 447 | |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 448 | /* print application specific filters */ |
| 449 | if (info->print_fn) |
| 450 | info->print_fn(vty, info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | #define SHOW_LOG_STR "Show current logging configuration\n" |
| 454 | |
| 455 | DEFUN(show_logging_vty, |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 456 | show_logging_vty_cmd, |
| 457 | "show logging vty", |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 458 | SHOW_STR SHOW_LOG_STR |
| 459 | "Show current logging configuration for this vty\n") |
| 460 | { |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 461 | struct log_target *tgt = osmo_log_vty2tgt(vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 462 | |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 463 | if (!tgt) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 464 | return CMD_WARNING; |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 465 | |
| 466 | vty_print_logtarget(vty, osmo_log_info, tgt); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 467 | |
| 468 | return CMD_SUCCESS; |
| 469 | } |
| 470 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 471 | DEFUN(show_alarms, |
| 472 | show_alarms_cmd, |
| 473 | "show alarms", |
| 474 | SHOW_STR SHOW_LOG_STR |
| 475 | "Show the contents of the logging ringbuffer\n") |
| 476 | { |
| 477 | int i, num_alarms; |
| 478 | struct osmo_strrb *rb; |
| 479 | struct log_target *tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 480 | if (!tgt) { |
| 481 | vty_out(vty, "%% No alarms, run 'log alarms <2-32700>'%s", |
| 482 | VTY_NEWLINE); |
| 483 | return CMD_WARNING; |
| 484 | } |
| 485 | |
| 486 | rb = tgt->tgt_rb.rb; |
| 487 | num_alarms = osmo_strrb_elements(rb); |
| 488 | |
| 489 | vty_out(vty, "%% Showing %i alarms%s", num_alarms, VTY_NEWLINE); |
| 490 | |
| 491 | for (i = 0; i < num_alarms; i++) |
| 492 | vty_out(vty, "%% %s%s", osmo_strrb_get_nth(rb, i), |
| 493 | VTY_NEWLINE); |
| 494 | |
| 495 | return CMD_SUCCESS; |
| 496 | } |
| 497 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 498 | gDEFUN(cfg_description, cfg_description_cmd, |
| 499 | "description .TEXT", |
Thorsten Alteholz | a81055d | 2017-03-02 22:13:48 +0100 | [diff] [blame] | 500 | "Save human-readable description of the object\n" |
Holger Hans Peter Freyther | c9b3e06 | 2012-07-25 13:02:49 +0200 | [diff] [blame] | 501 | "Text until the end of the line\n") |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 502 | { |
| 503 | char **dptr = vty->index_sub; |
| 504 | |
| 505 | if (!dptr) { |
| 506 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 507 | return CMD_WARNING; |
| 508 | } |
| 509 | |
Holger Hans Peter Freyther | ff0670e | 2011-02-24 14:20:41 +0100 | [diff] [blame] | 510 | if (*dptr) |
| 511 | talloc_free(*dptr); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 512 | *dptr = argv_concat(argv, argc, 0); |
Holger Hans Peter Freyther | 6a75d16 | 2013-07-14 09:07:11 +0200 | [diff] [blame] | 513 | if (!*dptr) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 514 | return CMD_WARNING; |
| 515 | |
| 516 | return CMD_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | gDEFUN(cfg_no_description, cfg_no_description_cmd, |
| 520 | "no description", |
| 521 | NO_STR |
| 522 | "Remove description of the object\n") |
| 523 | { |
| 524 | char **dptr = vty->index_sub; |
| 525 | |
| 526 | if (!dptr) { |
| 527 | vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE); |
| 528 | return CMD_WARNING; |
| 529 | } |
| 530 | |
| 531 | if (*dptr) { |
| 532 | talloc_free(*dptr); |
| 533 | *dptr = NULL; |
| 534 | } |
| 535 | |
| 536 | return CMD_SUCCESS; |
| 537 | } |
| 538 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 539 | /* Support for configuration of log targets != the current vty */ |
| 540 | |
| 541 | struct cmd_node cfg_log_node = { |
| 542 | CFG_LOG_NODE, |
| 543 | "%s(config-log)# ", |
| 544 | 1 |
| 545 | }; |
| 546 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 547 | #ifdef HAVE_SYSLOG_H |
| 548 | |
| 549 | #include <syslog.h> |
| 550 | |
| 551 | static const int local_sysl_map[] = { |
| 552 | [0] = LOG_LOCAL0, |
| 553 | [1] = LOG_LOCAL1, |
| 554 | [2] = LOG_LOCAL2, |
| 555 | [3] = LOG_LOCAL3, |
| 556 | [4] = LOG_LOCAL4, |
| 557 | [5] = LOG_LOCAL5, |
| 558 | [6] = LOG_LOCAL6, |
| 559 | [7] = LOG_LOCAL7 |
| 560 | }; |
| 561 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 562 | /* From VTY core code */ |
| 563 | extern struct host host; |
| 564 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 565 | static int _cfg_log_syslog(struct vty *vty, int facility) |
| 566 | { |
| 567 | struct log_target *tgt; |
| 568 | |
| 569 | /* First delete the old syslog target, if any */ |
| 570 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 571 | if (tgt) |
| 572 | log_target_destroy(tgt); |
| 573 | |
Harald Welte | de79cee | 2011-02-24 23:47:57 +0100 | [diff] [blame] | 574 | tgt = log_target_create_syslog(host.app_info->name, 0, facility); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 575 | if (!tgt) { |
| 576 | vty_out(vty, "%% Unable to open syslog%s", VTY_NEWLINE); |
| 577 | return CMD_WARNING; |
| 578 | } |
| 579 | log_add_target(tgt); |
| 580 | |
| 581 | vty->index = tgt; |
| 582 | vty->node = CFG_LOG_NODE; |
| 583 | |
| 584 | return CMD_SUCCESS; |
| 585 | } |
| 586 | |
| 587 | DEFUN(cfg_log_syslog_local, cfg_log_syslog_local_cmd, |
| 588 | "log syslog local <0-7>", |
| 589 | LOG_STR "Logging via syslog\n" "Syslog LOCAL facility\n" |
| 590 | "Local facility number\n") |
| 591 | { |
| 592 | int local = atoi(argv[0]); |
| 593 | int facility = local_sysl_map[local]; |
| 594 | |
| 595 | return _cfg_log_syslog(vty, facility); |
| 596 | } |
| 597 | |
| 598 | static struct value_string sysl_level_names[] = { |
| 599 | { LOG_AUTHPRIV, "authpriv" }, |
| 600 | { LOG_CRON, "cron" }, |
| 601 | { LOG_DAEMON, "daemon" }, |
| 602 | { LOG_FTP, "ftp" }, |
| 603 | { LOG_LPR, "lpr" }, |
| 604 | { LOG_MAIL, "mail" }, |
| 605 | { LOG_NEWS, "news" }, |
| 606 | { LOG_USER, "user" }, |
| 607 | { LOG_UUCP, "uucp" }, |
| 608 | /* only for value -> string conversion */ |
| 609 | { LOG_LOCAL0, "local 0" }, |
| 610 | { LOG_LOCAL1, "local 1" }, |
| 611 | { LOG_LOCAL2, "local 2" }, |
| 612 | { LOG_LOCAL3, "local 3" }, |
| 613 | { LOG_LOCAL4, "local 4" }, |
| 614 | { LOG_LOCAL5, "local 5" }, |
| 615 | { LOG_LOCAL6, "local 6" }, |
| 616 | { LOG_LOCAL7, "local 7" }, |
| 617 | { 0, NULL } |
| 618 | }; |
| 619 | |
| 620 | DEFUN(cfg_log_syslog, cfg_log_syslog_cmd, |
| 621 | "log syslog (authpriv|cron|daemon|ftp|lpr|mail|news|user|uucp)", |
Holger Hans Peter Freyther | a4463fd | 2011-10-03 23:17:36 +0200 | [diff] [blame] | 622 | LOG_STR "Logging via syslog\n" |
| 623 | "Security/authorization messages facility\n" |
| 624 | "Clock daemon (cron/at) facility\n" |
| 625 | "General system daemon facility\n" |
| 626 | "Ftp daemon facility\n" |
| 627 | "Line printer facility\n" |
| 628 | "Mail facility\n" |
| 629 | "News facility\n" |
| 630 | "Generic facility\n" |
| 631 | "UUCP facility\n") |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 632 | { |
| 633 | int facility = get_string_value(sysl_level_names, argv[0]); |
| 634 | |
| 635 | return _cfg_log_syslog(vty, facility); |
| 636 | } |
| 637 | |
| 638 | DEFUN(cfg_no_log_syslog, cfg_no_log_syslog_cmd, |
| 639 | "no log syslog", |
| 640 | NO_STR LOG_STR "Logging via syslog\n") |
| 641 | { |
| 642 | struct log_target *tgt; |
| 643 | |
| 644 | tgt = log_target_find(LOG_TGT_TYPE_SYSLOG, NULL); |
| 645 | if (!tgt) { |
| 646 | vty_out(vty, "%% No syslog target found%s", |
| 647 | VTY_NEWLINE); |
| 648 | return CMD_WARNING; |
| 649 | } |
| 650 | |
| 651 | log_target_destroy(tgt); |
| 652 | |
| 653 | return CMD_SUCCESS; |
| 654 | } |
| 655 | #endif /* HAVE_SYSLOG_H */ |
| 656 | |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 657 | DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd, |
| 658 | "log gsmtap [HOSTNAME]", |
Neels Hofmeyr | fd9ec3b | 2016-12-11 01:48:26 +0100 | [diff] [blame] | 659 | LOG_STR "Logging via GSMTAP\n" |
| 660 | "Host name to send the GSMTAP logging to (UDP port 4729)\n") |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 661 | { |
Max | 2f153b5 | 2018-01-04 12:25:57 +0100 | [diff] [blame] | 662 | const char *hostname = argc ? argv[0] : "127.0.0.1"; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 663 | struct log_target *tgt; |
| 664 | |
| 665 | tgt = log_target_find(LOG_TGT_TYPE_GSMTAP, hostname); |
| 666 | if (!tgt) { |
| 667 | tgt = log_target_create_gsmtap(hostname, GSMTAP_UDP_PORT, |
| 668 | host.app_info->name, false, |
| 669 | true); |
| 670 | if (!tgt) { |
Max | 2f153b5 | 2018-01-04 12:25:57 +0100 | [diff] [blame] | 671 | vty_out(vty, "%% Unable to create GSMTAP log for %s%s", |
| 672 | hostname, VTY_NEWLINE); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 673 | return CMD_WARNING; |
| 674 | } |
| 675 | log_add_target(tgt); |
| 676 | } |
| 677 | |
| 678 | vty->index = tgt; |
| 679 | vty->node = CFG_LOG_NODE; |
| 680 | |
| 681 | return CMD_SUCCESS; |
| 682 | } |
| 683 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 684 | DEFUN(cfg_log_stderr, cfg_log_stderr_cmd, |
| 685 | "log stderr", |
| 686 | LOG_STR "Logging via STDERR of the process\n") |
| 687 | { |
| 688 | struct log_target *tgt; |
| 689 | |
| 690 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 691 | if (!tgt) { |
| 692 | tgt = log_target_create_stderr(); |
| 693 | if (!tgt) { |
| 694 | vty_out(vty, "%% Unable to create stderr log%s", |
| 695 | VTY_NEWLINE); |
| 696 | return CMD_WARNING; |
| 697 | } |
| 698 | log_add_target(tgt); |
| 699 | } |
| 700 | |
| 701 | vty->index = tgt; |
| 702 | vty->node = CFG_LOG_NODE; |
| 703 | |
| 704 | return CMD_SUCCESS; |
| 705 | } |
| 706 | |
| 707 | DEFUN(cfg_no_log_stderr, cfg_no_log_stderr_cmd, |
| 708 | "no log stderr", |
| 709 | NO_STR LOG_STR "Logging via STDERR of the process\n") |
| 710 | { |
| 711 | struct log_target *tgt; |
| 712 | |
| 713 | tgt = log_target_find(LOG_TGT_TYPE_STDERR, NULL); |
| 714 | if (!tgt) { |
| 715 | vty_out(vty, "%% No stderr logging active%s", VTY_NEWLINE); |
| 716 | return CMD_WARNING; |
| 717 | } |
| 718 | |
| 719 | log_target_destroy(tgt); |
| 720 | |
| 721 | return CMD_SUCCESS; |
| 722 | } |
| 723 | |
| 724 | DEFUN(cfg_log_file, cfg_log_file_cmd, |
| 725 | "log file .FILENAME", |
| 726 | LOG_STR "Logging to text file\n" "Filename\n") |
| 727 | { |
| 728 | const char *fname = argv[0]; |
| 729 | struct log_target *tgt; |
| 730 | |
| 731 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 732 | if (!tgt) { |
| 733 | tgt = log_target_create_file(fname); |
| 734 | if (!tgt) { |
| 735 | vty_out(vty, "%% Unable to create file `%s'%s", |
| 736 | fname, VTY_NEWLINE); |
| 737 | return CMD_WARNING; |
| 738 | } |
| 739 | log_add_target(tgt); |
| 740 | } |
| 741 | |
| 742 | vty->index = tgt; |
| 743 | vty->node = CFG_LOG_NODE; |
| 744 | |
| 745 | return CMD_SUCCESS; |
| 746 | } |
| 747 | |
| 748 | |
| 749 | DEFUN(cfg_no_log_file, cfg_no_log_file_cmd, |
| 750 | "no log file .FILENAME", |
| 751 | NO_STR LOG_STR "Logging to text file\n" "Filename\n") |
| 752 | { |
| 753 | const char *fname = argv[0]; |
| 754 | struct log_target *tgt; |
| 755 | |
| 756 | tgt = log_target_find(LOG_TGT_TYPE_FILE, fname); |
| 757 | if (!tgt) { |
| 758 | vty_out(vty, "%% No such log file `%s'%s", |
| 759 | fname, VTY_NEWLINE); |
| 760 | return CMD_WARNING; |
| 761 | } |
| 762 | |
| 763 | log_target_destroy(tgt); |
| 764 | |
| 765 | return CMD_SUCCESS; |
| 766 | } |
| 767 | |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 768 | DEFUN(cfg_log_alarms, cfg_log_alarms_cmd, |
| 769 | "log alarms <2-32700>", |
| 770 | LOG_STR "Logging alarms to osmo_strrb\n" |
| 771 | "Maximum number of messages to log\n") |
| 772 | { |
| 773 | struct log_target *tgt; |
| 774 | unsigned int rbsize = atoi(argv[0]); |
| 775 | |
| 776 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 777 | if (tgt) |
| 778 | log_target_destroy(tgt); |
| 779 | |
| 780 | tgt = log_target_create_rb(rbsize); |
| 781 | if (!tgt) { |
| 782 | vty_out(vty, "%% Unable to create osmo_strrb (size %u)%s", |
| 783 | rbsize, VTY_NEWLINE); |
| 784 | return CMD_WARNING; |
| 785 | } |
| 786 | log_add_target(tgt); |
| 787 | |
| 788 | vty->index = tgt; |
| 789 | vty->node = CFG_LOG_NODE; |
| 790 | |
| 791 | return CMD_SUCCESS; |
| 792 | } |
| 793 | |
| 794 | DEFUN(cfg_no_log_alarms, cfg_no_log_alarms_cmd, |
| 795 | "no log alarms", |
| 796 | NO_STR LOG_STR "Logging alarms to osmo_strrb\n") |
| 797 | { |
| 798 | struct log_target *tgt; |
| 799 | |
| 800 | tgt = log_target_find(LOG_TGT_TYPE_STRRB, NULL); |
| 801 | if (!tgt) { |
| 802 | vty_out(vty, "%% No osmo_strrb target found%s", VTY_NEWLINE); |
| 803 | return CMD_WARNING; |
| 804 | } |
| 805 | |
| 806 | log_target_destroy(tgt); |
| 807 | |
| 808 | return CMD_SUCCESS; |
| 809 | } |
| 810 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 811 | static int config_write_log_single(struct vty *vty, struct log_target *tgt) |
| 812 | { |
| 813 | int i; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 814 | |
| 815 | switch (tgt->type) { |
| 816 | case LOG_TGT_TYPE_VTY: |
| 817 | return 1; |
| 818 | break; |
| 819 | case LOG_TGT_TYPE_STDERR: |
| 820 | vty_out(vty, "log stderr%s", VTY_NEWLINE); |
| 821 | break; |
| 822 | case LOG_TGT_TYPE_SYSLOG: |
| 823 | #ifdef HAVE_SYSLOG_H |
| 824 | vty_out(vty, "log syslog %s%s", |
| 825 | get_value_string(sysl_level_names, |
| 826 | tgt->tgt_syslog.facility), |
| 827 | VTY_NEWLINE); |
| 828 | #endif |
| 829 | break; |
| 830 | case LOG_TGT_TYPE_FILE: |
| 831 | vty_out(vty, "log file %s%s", tgt->tgt_file.fname, VTY_NEWLINE); |
| 832 | break; |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 833 | case LOG_TGT_TYPE_STRRB: |
| 834 | vty_out(vty, "log alarms %zu%s", |
| 835 | log_target_rb_avail_size(tgt), VTY_NEWLINE); |
| 836 | break; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 837 | case LOG_TGT_TYPE_GSMTAP: |
| 838 | vty_out(vty, "log gsmtap %s%s", |
| 839 | tgt->tgt_gsmtap.hostname, VTY_NEWLINE); |
| 840 | break; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Harald Welte | 2da47f1 | 2012-10-22 19:31:54 +0200 | [diff] [blame] | 843 | vty_out(vty, " logging filter all %u%s", |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 844 | tgt->filter_map & (1 << LOG_FLT_ALL) ? 1 : 0, VTY_NEWLINE); |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 845 | /* save filters outside of libosmocore, i.e. in app code */ |
| 846 | if (osmo_log_info->save_fn) |
| 847 | osmo_log_info->save_fn(vty, osmo_log_info, tgt); |
Harald Welte | 2da47f1 | 2012-10-22 19:31:54 +0200 | [diff] [blame] | 848 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 849 | vty_out(vty, " logging color %u%s", tgt->use_color ? 1 : 0, |
| 850 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 879acef | 2015-01-27 11:06:51 +0100 | [diff] [blame] | 851 | vty_out(vty, " logging print category %d%s", |
Michael McTernan | 7893346 | 2015-03-20 15:29:25 +0100 | [diff] [blame] | 852 | tgt->print_category ? 1 : 0, VTY_NEWLINE); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 853 | if (tgt->print_ext_timestamp) |
| 854 | vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE); |
| 855 | else |
| 856 | vty_out(vty, " logging timestamp %u%s", |
| 857 | tgt->print_timestamp ? 1 : 0, VTY_NEWLINE); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 858 | if (tgt->print_level) |
| 859 | vty_out(vty, " logging print level 1%s", VTY_NEWLINE); |
Neels Hofmeyr | 22772cc | 2018-02-06 00:52:08 +0100 | [diff] [blame] | 860 | vty_out(vty, " logging print file %s%s", |
| 861 | get_value_string(logging_print_file_args, tgt->print_filename2), |
| 862 | VTY_NEWLINE); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 863 | |
Neels Hofmeyr | 098038a | 2018-09-11 23:49:13 +0200 | [diff] [blame^] | 864 | if (tgt->loglevel) { |
| 865 | const char *level_str = get_value_string_or_null(loglevel_strs, tgt->loglevel); |
| 866 | level_str = osmo_str_tolower(level_str); |
| 867 | if (!level_str) |
| 868 | vty_out(vty, "%% Invalid log level %u for 'all'%s", tgt->loglevel, VTY_NEWLINE); |
| 869 | else |
| 870 | vty_out(vty, " logging level all %s%s", level_str, VTY_NEWLINE); |
| 871 | } |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 872 | |
| 873 | for (i = 0; i < osmo_log_info->num_cat; i++) { |
| 874 | const struct log_category *cat = &tgt->categories[i]; |
Neels Hofmeyr | 098038a | 2018-09-11 23:49:13 +0200 | [diff] [blame^] | 875 | const char *cat_name; |
| 876 | const char *level_str; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 877 | |
Harald Welte | 1a02cfc | 2013-03-19 10:37:39 +0100 | [diff] [blame] | 878 | /* skip empty entries in the array */ |
| 879 | if (!osmo_log_info->cat[i].name) |
| 880 | continue; |
| 881 | |
Neels Hofmeyr | 098038a | 2018-09-11 23:49:13 +0200 | [diff] [blame^] | 882 | /* Note: cat_name references the static buffer returned by osmo_str_tolower(), will |
| 883 | * become invalid after next osmo_str_tolower() invocation. */ |
| 884 | cat_name = osmo_str_tolower(osmo_log_info->cat[i].name+1); |
| 885 | |
| 886 | level_str = get_value_string_or_null(loglevel_strs, cat->loglevel); |
| 887 | if (!level_str) { |
| 888 | vty_out(vty, "%% Invalid log level %u for %s%s", cat->loglevel, cat_name, |
| 889 | VTY_NEWLINE); |
| 890 | continue; |
| 891 | } |
| 892 | |
| 893 | vty_out(vty, " logging level %s", cat_name); |
| 894 | vty_out(vty, " %s%s", osmo_str_tolower(level_str), VTY_NEWLINE); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | static int config_write_log(struct vty *vty) |
| 901 | { |
| 902 | struct log_target *dbg = vty->index; |
| 903 | |
| 904 | llist_for_each_entry(dbg, &osmo_log_target_list, entry) |
| 905 | config_write_log_single(vty, dbg); |
| 906 | |
| 907 | return 1; |
| 908 | } |
| 909 | |
Harald Welte | 11eb4b5 | 2018-06-09 17:41:31 +0200 | [diff] [blame] | 910 | static int log_deprecated_func(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[]) |
| 911 | { |
| 912 | vty_out(vty, "%% Ignoring deprecated '%s'%s", cmd->string, VTY_NEWLINE); |
| 913 | return CMD_WARNING; |
| 914 | } |
| 915 | |
| 916 | void logging_vty_add_deprecated_subsys(void *ctx, const char *name) |
| 917 | { |
| 918 | struct cmd_element *cmd = talloc_zero(ctx, struct cmd_element); |
| 919 | OSMO_ASSERT(cmd); |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 920 | cmd->string = talloc_asprintf(cmd, "logging level %s (debug|info|notice|error|fatal)", |
Harald Welte | 11eb4b5 | 2018-06-09 17:41:31 +0200 | [diff] [blame] | 921 | name); |
| 922 | printf("%s\n", cmd->string); |
| 923 | cmd->func = log_deprecated_func; |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 924 | cmd->doc = LEVEL_STR |
Harald Welte | 11eb4b5 | 2018-06-09 17:41:31 +0200 | [diff] [blame] | 925 | "Deprecated Category\n"; |
| 926 | cmd->attr = CMD_ATTR_DEPRECATED; |
| 927 | |
| 928 | install_element(CFG_LOG_NODE, cmd); |
| 929 | } |
| 930 | |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 931 | /*! Register logging related commands to the VTY. Call this once from |
| 932 | * your application if you want to support those commands. */ |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 933 | void logging_vty_add_cmds() |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 934 | { |
| 935 | install_element_ve(&enable_logging_cmd); |
| 936 | install_element_ve(&disable_logging_cmd); |
| 937 | install_element_ve(&logging_fltr_all_cmd); |
| 938 | install_element_ve(&logging_use_clr_cmd); |
| 939 | install_element_ve(&logging_prnt_timestamp_cmd); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 940 | install_element_ve(&logging_prnt_ext_timestamp_cmd); |
| 941 | install_element_ve(&logging_prnt_cat_cmd); |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 942 | install_element_ve(&logging_prnt_cat_hex_cmd); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 943 | install_element_ve(&logging_prnt_level_cmd); |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 944 | install_element_ve(&logging_prnt_file_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 945 | install_element_ve(&logging_set_category_mask_cmd); |
Holger Hans Peter Freyther | 146d1d3 | 2011-10-03 23:15:41 +0200 | [diff] [blame] | 946 | install_element_ve(&logging_set_category_mask_old_cmd); |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 947 | |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 948 | /* logging level (all|<categories>) (debug|...|fatal) */ |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 949 | gen_logging_level_cmd_strs(&logging_level_cmd, |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 950 | "(" LOG_LEVEL_ARGS ")", |
| 951 | LOG_LEVEL_STRS); |
| 952 | /* logging level (all|<categories>) everything */ |
| 953 | gen_logging_level_cmd_strs(&deprecated_logging_level_everything_cmd, |
| 954 | "everything", EVERYTHING_STR); |
Neels Hofmeyr | ba0762d | 2018-09-10 13:56:03 +0200 | [diff] [blame] | 955 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 956 | install_element_ve(&logging_level_cmd); |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 957 | install_element_ve(&deprecated_logging_level_everything_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 958 | install_element_ve(&show_logging_vty_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 959 | install_element_ve(&show_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 960 | |
| 961 | install_node(&cfg_log_node, config_write_log); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 962 | install_element(CFG_LOG_NODE, &logging_fltr_all_cmd); |
| 963 | install_element(CFG_LOG_NODE, &logging_use_clr_cmd); |
| 964 | install_element(CFG_LOG_NODE, &logging_prnt_timestamp_cmd); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 965 | install_element(CFG_LOG_NODE, &logging_prnt_ext_timestamp_cmd); |
| 966 | install_element(CFG_LOG_NODE, &logging_prnt_cat_cmd); |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 967 | install_element(CFG_LOG_NODE, &logging_prnt_cat_hex_cmd); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 968 | install_element(CFG_LOG_NODE, &logging_prnt_level_cmd); |
Neels Hofmeyr | c6fd245 | 2018-01-16 01:57:38 +0100 | [diff] [blame] | 969 | install_element(CFG_LOG_NODE, &logging_prnt_file_cmd); |
Harald Welte | a62648b | 2011-02-18 21:03:27 +0100 | [diff] [blame] | 970 | install_element(CFG_LOG_NODE, &logging_level_cmd); |
Neels Hofmeyr | 7e0686c | 2018-09-10 20:58:52 +0200 | [diff] [blame] | 971 | install_element(CFG_LOG_NODE, &deprecated_logging_level_everything_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 972 | |
| 973 | install_element(CONFIG_NODE, &cfg_log_stderr_cmd); |
| 974 | install_element(CONFIG_NODE, &cfg_no_log_stderr_cmd); |
| 975 | install_element(CONFIG_NODE, &cfg_log_file_cmd); |
| 976 | install_element(CONFIG_NODE, &cfg_no_log_file_cmd); |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 977 | install_element(CONFIG_NODE, &cfg_log_alarms_cmd); |
| 978 | install_element(CONFIG_NODE, &cfg_no_log_alarms_cmd); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 979 | #ifdef HAVE_SYSLOG_H |
| 980 | install_element(CONFIG_NODE, &cfg_log_syslog_cmd); |
| 981 | install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd); |
| 982 | install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd); |
| 983 | #endif |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 984 | install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 985 | } |