blob: 8fd71d0c601c34149faf102a57591fb373926872 [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)",
40 .color = "\033[1;31m",
41 .enabled = 1, .loglevel = LOGL_NOTICE,
42 },
43 [DCC] = {
44 .name = "DCC",
45 .description = "Layer3 Call Control (CC)",
46 .color = "\033[1;32m",
47 .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)",
52 .color = "\033[1;33m",
53 .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);
80 log_set_print_filename(stderr_target, 0);
Neels Hofmeyr9adf32f2016-12-12 14:22:59 +010081 log_set_print_category(stderr_target, 1);
82 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020083
84 log_parse_category_mask(stderr_target, "DRLL:DCC");
85 log_parse_category_mask(stderr_target, "DRLL");
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010086
87 select_output = 0;
88
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020089 DEBUGP(DCC, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010090 if (log_check_level(DMM, LOGL_DEBUG) != 0)
91 fprintf(stderr, "log_check_level did not catch this case\n");
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020092
93 log_parse_category_mask(stderr_target, "DRLL:DCC");
94 DEBUGP(DRLL, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010095 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020096 DEBUGP(DCC, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010097 OSMO_ASSERT(log_check_level(DCC, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020098 DEBUGP(DMM, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010099
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100100 OSMO_ASSERT(log_check_level(DMM, LOGL_DEBUG) == 0);
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100101 OSMO_ASSERT(filter_called == 0);
102
103 log_set_all_filter(stderr_target, 0);
104 DEBUGP(DRLL, "You should not see this and filter is called\n");
105 OSMO_ASSERT(filter_called == 1);
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100106 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) == 0);
107 OSMO_ASSERT(filter_called == 2);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200108
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100109 DEBUGP(DRLL, "You should not see this\n");
110 OSMO_ASSERT(filter_called == 3);
111 select_output = 1;
112 DEBUGP(DRLL, "You should see this\n");
113 OSMO_ASSERT(filter_called == 5); /* called twice on output */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100114
115 /* Make sure out-of-bounds category maps to DLGLOBAL */
116 log_parse_category_mask(stderr_target, "DLGLOBAL,1");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100117 /* For IDs out of bounds of the overall osmo_log_info array */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100118 DEBUGP(osmo_log_info->num_cat + 1, "You should see this on DLGLOBAL (a)\n");
119 DEBUGP(osmo_log_info->num_cat + 100, "You should see this on DLGLOBAL (b)\n");
Neels Hofmeyra280b822016-12-12 14:42:56 +0100120 DEBUGP(osmo_log_info->num_cat, "You should see this on DLGLOBAL (c)\n");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100121 /* For IDs out of bounds of the user categories part */
122 DEBUGP(log_info.num_cat + 1, "You should see this on DLGLOBAL (d)\n");
123 DEBUGP(log_info.num_cat, "You should see this on DLGLOBAL (e)\n");
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100124
Neels Hofmeyra4842af2016-12-12 14:11:31 +0100125 /* Check log_set_category_filter() with internal categories */
126 log_parse_category_mask(stderr_target, "DLGLOBAL,3");
127 DEBUGP(DLGLOBAL, "You should not see this (DLGLOBAL not on DEBUG)\n");
128 log_set_category_filter(stderr_target, DLGLOBAL, 1, LOGL_DEBUG);
129 DEBUGP(DLGLOBAL, "You should see this (DLGLOBAL on DEBUG)\n");
130
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200131 return 0;
132}