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