Neels Hofmeyr | 3a9ff11 | 2018-09-10 17:18:28 +0200 | [diff] [blame] | 1 | /* test program with a vty interface to test logging behavior */ |
| 2 | /* |
| 3 | * (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 7 | * |
| 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 | * |
Neels Hofmeyr | 3a9ff11 | 2018-09-10 17:18:28 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <getopt.h> |
| 22 | |
| 23 | #include <signal.h> |
| 24 | |
| 25 | #include <osmocom/core/logging.h> |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/application.h> |
| 28 | |
| 29 | #include <osmocom/vty/command.h> |
| 30 | #include <osmocom/vty/logging.h> |
| 31 | #include <osmocom/vty/misc.h> |
| 32 | #include <osmocom/vty/telnet_interface.h> |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include "config.h" |
| 37 | |
| 38 | void *root_ctx = NULL; |
| 39 | |
| 40 | enum { |
| 41 | DAA, |
| 42 | DBB, |
| 43 | DCCC, |
| 44 | DDDDD, |
| 45 | DEEE, |
| 46 | }; |
| 47 | |
| 48 | DEFUN(log_sweep, log_sweep_cmd, |
| 49 | "log-sweep [CATEGORY]", |
| 50 | "Log once for all categories on all levels\n") |
| 51 | { |
| 52 | int only_category = argc ? log_parse_category(argv[0]) : -1; |
| 53 | |
| 54 | if (argc && only_category < 0) { |
| 55 | vty_out(vty, "%% Error: unknown category: %s%s", argv[0], VTY_NEWLINE); |
| 56 | return CMD_WARNING; |
| 57 | } |
| 58 | |
| 59 | #define LOG_LEVEL(CAT, LEVEL) \ |
| 60 | if (only_category < 0 || only_category == CAT) \ |
| 61 | LOGP(CAT, LEVEL, "Log message for " #CAT " on level " #LEVEL "\n") |
| 62 | |
| 63 | #define LOG_ALL_LEVELS(CAT) \ |
| 64 | LOG_LEVEL(CAT, LOGL_DEBUG); \ |
| 65 | LOG_LEVEL(CAT, LOGL_INFO); \ |
| 66 | LOG_LEVEL(CAT, LOGL_NOTICE); \ |
| 67 | LOG_LEVEL(CAT, LOGL_ERROR); \ |
| 68 | LOG_LEVEL(CAT, LOGL_FATAL) |
| 69 | |
| 70 | LOG_ALL_LEVELS(DAA); |
| 71 | LOG_ALL_LEVELS(DBB); |
| 72 | LOG_ALL_LEVELS(DCCC); |
| 73 | LOG_ALL_LEVELS(DDDDD); |
| 74 | LOG_ALL_LEVELS(DEEE); |
| 75 | |
| 76 | vty_out(vty, "%s", VTY_NEWLINE); |
| 77 | |
| 78 | return CMD_SUCCESS; |
| 79 | } |
| 80 | |
| 81 | static void vty_commands_init() |
| 82 | { |
| 83 | install_element_ve(&log_sweep_cmd); |
| 84 | } |
| 85 | |
| 86 | static const struct log_info_cat default_categories[] = { |
| 87 | [DAA] = { |
| 88 | .name = "DAA", |
| 89 | .description = "Antropomorphic Armadillos (AA)", |
| 90 | .color = "\033[1;31m", |
| 91 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 92 | }, |
| 93 | [DBB] = { |
| 94 | .name = "DBB", |
| 95 | .description = "Bidirectional Breadspread (BB)", |
| 96 | .color = "\033[1;32m", |
| 97 | .enabled = 1, .loglevel = LOGL_INFO, |
| 98 | }, |
| 99 | [DCCC] = { |
| 100 | .name = "DCCC", |
| 101 | .description = "Chaos Communication Congress (CCC)", |
| 102 | .color = "\033[1;33m", |
| 103 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 104 | }, |
| 105 | [DDDDD] = { |
| 106 | .name = "DDDDD", |
| 107 | .description = "Dehydrated Dribbling Duck Dunkers (DDDD)", |
| 108 | .color = "\033[1;34m", |
| 109 | .enabled = 1, .loglevel = LOGL_ERROR, |
| 110 | }, |
| 111 | [DEEE] = { |
| 112 | .name = "DEEE", |
| 113 | .description = "Exhaustive Entropy Extraction (EEE)", |
| 114 | .color = "\033[1;35m", |
| 115 | .enabled = 1, .loglevel = LOGL_FATAL, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | const struct log_info log_info = { |
| 120 | .cat = default_categories, |
| 121 | .num_cat = ARRAY_SIZE(default_categories), |
| 122 | }; |
| 123 | |
| 124 | static void print_help() |
| 125 | { |
| 126 | printf( "options:\n" |
| 127 | " -h --help this text\n" |
| 128 | " -d --debug MASK Enable debugging (e.g. -d DRSL:DOML:DLAPDM)\n" |
| 129 | " -D --daemonize For the process into a background daemon\n" |
| 130 | " -c --config-file Specify the filename of the config file\n" |
| 131 | " -s --disable-color Don't use colors in stderr log output\n" |
| 132 | " -T --timestamp Prefix every log line with a timestamp\n" |
| 133 | " -V --version Print version information and exit\n" |
| 134 | " -e --log-level Set a global log-level\n" |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | static struct { |
| 139 | const char *config_file; |
| 140 | int daemonize; |
| 141 | } cmdline_config = {}; |
| 142 | |
| 143 | static void handle_options(int argc, char **argv) |
| 144 | { |
| 145 | while (1) { |
| 146 | int option_idx = 0, c; |
| 147 | static const struct option long_options[] = { |
| 148 | { "help", 0, 0, 'h' }, |
| 149 | { "debug", 1, 0, 'd' }, |
| 150 | { "daemonize", 0, 0, 'D' }, |
| 151 | { "config-file", 1, 0, 'c' }, |
| 152 | { "disable-color", 0, 0, 's' }, |
| 153 | { "timestamp", 0, 0, 'T' }, |
| 154 | { "version", 0, 0, 'V' }, |
| 155 | { "log-level", 1, 0, 'e' }, |
| 156 | {} |
| 157 | }; |
| 158 | |
| 159 | c = getopt_long(argc, argv, "hc:d:Dc:sTVe:", |
| 160 | long_options, &option_idx); |
| 161 | if (c == -1) |
| 162 | break; |
| 163 | |
| 164 | switch (c) { |
| 165 | case 'h': |
| 166 | print_help(); |
| 167 | exit(0); |
| 168 | case 's': |
| 169 | log_set_use_color(osmo_stderr_target, 0); |
| 170 | break; |
| 171 | case 'd': |
| 172 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 173 | break; |
| 174 | case 'D': |
| 175 | cmdline_config.daemonize = 1; |
| 176 | break; |
| 177 | case 'c': |
| 178 | cmdline_config.config_file = optarg; |
| 179 | break; |
| 180 | case 'T': |
| 181 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 182 | break; |
| 183 | case 'e': |
| 184 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 185 | break; |
| 186 | case 'V': |
| 187 | print_version(1); |
| 188 | exit(0); |
| 189 | break; |
| 190 | default: |
| 191 | /* catch unknown options *as well as* missing arguments. */ |
| 192 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 193 | exit(-1); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static int quit = 0; |
| 199 | |
| 200 | static void signal_handler(int signal) |
| 201 | { |
| 202 | fprintf(stdout, "signal %u received\n", signal); |
| 203 | |
| 204 | switch (signal) { |
| 205 | case SIGINT: |
| 206 | case SIGTERM: |
| 207 | quit++; |
| 208 | break; |
| 209 | case SIGABRT: |
| 210 | osmo_generate_backtrace(); |
| 211 | /* in case of abort, we want to obtain a talloc report |
| 212 | * and then return to the caller, who will abort the process */ |
| 213 | case SIGUSR1: |
| 214 | talloc_report(tall_vty_ctx, stderr); |
| 215 | talloc_report_full(root_ctx, stderr); |
| 216 | break; |
| 217 | case SIGUSR2: |
| 218 | talloc_report_full(tall_vty_ctx, stderr); |
| 219 | break; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static struct vty_app_info vty_info = { |
| 226 | .name = "logging_vty_test", |
| 227 | .version = PACKAGE_VERSION, |
| 228 | }; |
| 229 | |
| 230 | int main(int argc, char **argv) |
| 231 | { |
| 232 | int rc; |
| 233 | |
| 234 | root_ctx = talloc_named_const(NULL, 0, "logging_vty_test"); |
| 235 | |
| 236 | vty_info.tall_ctx = root_ctx; |
| 237 | vty_init(&vty_info); |
| 238 | |
| 239 | osmo_init_logging2(root_ctx, &log_info); |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 240 | log_enable_multithread(); |
Neels Hofmeyr | 3a9ff11 | 2018-09-10 17:18:28 +0200 | [diff] [blame] | 241 | |
| 242 | vty_commands_init(); |
| 243 | |
| 244 | handle_options(argc, argv); |
| 245 | |
Pau Espin Pedrol | f65278f | 2019-08-05 14:10:43 +0200 | [diff] [blame] | 246 | logging_vty_add_cmds(); |
Neels Hofmeyr | 3a9ff11 | 2018-09-10 17:18:28 +0200 | [diff] [blame] | 247 | osmo_talloc_vty_add_cmds(); |
| 248 | |
| 249 | log_set_print_category(osmo_stderr_target, 1); |
| 250 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 251 | log_set_print_level(osmo_stderr_target, 1); |
| 252 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Harald Welte | b72867f | 2020-09-26 21:45:16 +0200 | [diff] [blame] | 253 | log_target_file_switch_to_wqueue(osmo_stderr_target); |
Neels Hofmeyr | 3a9ff11 | 2018-09-10 17:18:28 +0200 | [diff] [blame] | 254 | |
| 255 | if (cmdline_config.config_file) { |
| 256 | rc = vty_read_config_file(cmdline_config.config_file, NULL); |
| 257 | if (rc < 0) { |
| 258 | LOGP(DLGLOBAL, LOGL_FATAL, "Failed to parse the config file: '%s'\n", |
| 259 | cmdline_config.config_file); |
| 260 | return 1; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | rc = telnet_init_dynif(root_ctx, NULL, vty_get_bind_addr(), 42042); |
| 265 | if (rc < 0) |
| 266 | return 2; |
| 267 | |
| 268 | signal(SIGINT, &signal_handler); |
| 269 | signal(SIGTERM, &signal_handler); |
| 270 | signal(SIGABRT, &signal_handler); |
| 271 | signal(SIGUSR1, &signal_handler); |
| 272 | signal(SIGUSR2, &signal_handler); |
| 273 | osmo_init_ignore_signals(); |
| 274 | |
| 275 | if (cmdline_config.daemonize) { |
| 276 | rc = osmo_daemonize(); |
| 277 | if (rc < 0) { |
| 278 | perror("Error during daemonize"); |
| 279 | return 6; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | while (!quit) { |
| 284 | log_reset_context(); |
| 285 | osmo_select_main(0); |
| 286 | } |
| 287 | |
| 288 | log_fini(); |
| 289 | |
| 290 | talloc_free(root_ctx); |
| 291 | talloc_free(tall_vty_ctx); |
| 292 | |
| 293 | return 0; |
| 294 | } |