blob: f873958743571f6cf93990367f1d36961863a2c6 [file] [log] [blame]
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +02001/* simple test for the debug interface */
2/*
3 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Holger Hans Peter Freyther53aa0f52017-12-09 13:03:29 +00007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Holger Hans Peter Freyther53aa0f52017-12-09 13:03:29 +000016 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020019 *
20 */
21
22#include <osmocom/core/logging.h>
23#include <osmocom/core/utils.h>
24
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010025#include <stdlib.h>
26
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020027enum {
28 DRLL,
29 DCC,
30 DMM,
31};
32
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010033static int filter_called = 0;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010034static int select_output = 0;
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010035
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020036static const struct log_info_cat default_categories[] = {
37 [DRLL] = {
38 .name = "DRLL",
39 .description = "A-bis Radio Link Layer (RLL)",
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +010040 .color = OSMO_LOGCOLOR_RED,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020041 .enabled = 1, .loglevel = LOGL_NOTICE,
42 },
43 [DCC] = {
44 .name = "DCC",
45 .description = "Layer3 Call Control (CC)",
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +010046 .color = OSMO_LOGCOLOR_GREEN,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020047 .enabled = 1, .loglevel = LOGL_NOTICE,
48 },
49 [DMM] = {
Holger Hans Peter Freyther779d2f42012-09-27 16:31:29 +020050 .name = NULL,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020051 .description = "Layer3 Mobility Management (MM)",
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +010052 .color = OSMO_LOGCOLOR_YELLOW,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020053 .enabled = 1, .loglevel = LOGL_NOTICE,
54 },
55};
56
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010057static int test_filter(const struct log_context *ctx, struct log_target *target)
58{
59 filter_called += 1;
60 /* omit everything */
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010061 return select_output;
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010062}
63
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020064const struct log_info log_info = {
65 .cat = default_categories,
66 .num_cat = ARRAY_SIZE(default_categories),
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010067 .filter_fn = test_filter,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020068};
69
Neels Hofmeyr85b42c42016-12-12 14:34:41 +010070extern struct log_info *osmo_log_info;
71
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020072int main(int argc, char **argv)
73{
74 struct log_target *stderr_target;
75
76 log_init(&log_info, NULL);
77 stderr_target = log_target_create_stderr();
78 log_add_target(stderr_target);
79 log_set_all_filter(stderr_target, 1);
Pau Espin Pedrol01e0d3e2021-02-18 19:25:44 +010080 log_set_print_filename2(stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrol690b6612021-02-18 19:10:28 +010081 log_set_print_category_hex(stderr_target, 0);
Neels Hofmeyr9adf32f2016-12-12 14:22:59 +010082 log_set_print_category(stderr_target, 1);
83 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020084
85 log_parse_category_mask(stderr_target, "DRLL:DCC");
86 log_parse_category_mask(stderr_target, "DRLL");
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010087
88 select_output = 0;
89
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020090 DEBUGP(DCC, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010091 if (log_check_level(DMM, LOGL_DEBUG) != 0)
92 fprintf(stderr, "log_check_level did not catch this case\n");
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020093
94 log_parse_category_mask(stderr_target, "DRLL:DCC");
95 DEBUGP(DRLL, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010096 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020097 DEBUGP(DCC, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010098 OSMO_ASSERT(log_check_level(DCC, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020099 DEBUGP(DMM, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +0100100
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100101 OSMO_ASSERT(log_check_level(DMM, LOGL_DEBUG) == 0);
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100102 OSMO_ASSERT(filter_called == 0);
103
104 log_set_all_filter(stderr_target, 0);
105 DEBUGP(DRLL, "You should not see this and filter is called\n");
106 OSMO_ASSERT(filter_called == 1);
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100107 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) == 0);
108 OSMO_ASSERT(filter_called == 2);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200109
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100110 DEBUGP(DRLL, "You should not see this\n");
111 OSMO_ASSERT(filter_called == 3);
112 select_output = 1;
113 DEBUGP(DRLL, "You should see this\n");
114 OSMO_ASSERT(filter_called == 5); /* called twice on output */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100115
116 /* Make sure out-of-bounds category maps to DLGLOBAL */
117 log_parse_category_mask(stderr_target, "DLGLOBAL,1");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100118 /* For IDs out of bounds of the overall osmo_log_info array */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100119 DEBUGP(osmo_log_info->num_cat + 1, "You should see this on DLGLOBAL (a)\n");
120 DEBUGP(osmo_log_info->num_cat + 100, "You should see this on DLGLOBAL (b)\n");
Neels Hofmeyra280b822016-12-12 14:42:56 +0100121 DEBUGP(osmo_log_info->num_cat, "You should see this on DLGLOBAL (c)\n");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100122 /* For IDs out of bounds of the user categories part */
123 DEBUGP(log_info.num_cat + 1, "You should see this on DLGLOBAL (d)\n");
124 DEBUGP(log_info.num_cat, "You should see this on DLGLOBAL (e)\n");
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100125
Neels Hofmeyra4842af2016-12-12 14:11:31 +0100126 /* Check log_set_category_filter() with internal categories */
127 log_parse_category_mask(stderr_target, "DLGLOBAL,3");
128 DEBUGP(DLGLOBAL, "You should not see this (DLGLOBAL not on DEBUG)\n");
129 log_set_category_filter(stderr_target, DLGLOBAL, 1, LOGL_DEBUG);
130 DEBUGP(DLGLOBAL, "You should see this (DLGLOBAL on DEBUG)\n");
131
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200132 return 0;
133}