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