blob: 5ef214d86bf0f504aa1953494e5e829a9bff7f92 [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
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (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 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/core/logging.h>
22#include <osmocom/core/utils.h>
23
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010024#include <stdlib.h>
25
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020026enum {
27 DRLL,
28 DCC,
29 DMM,
30};
31
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010032static int filter_called = 0;
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010033static int select_output = 0;
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010034
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020035static const struct log_info_cat default_categories[] = {
36 [DRLL] = {
37 .name = "DRLL",
38 .description = "A-bis Radio Link Layer (RLL)",
39 .color = "\033[1;31m",
40 .enabled = 1, .loglevel = LOGL_NOTICE,
41 },
42 [DCC] = {
43 .name = "DCC",
44 .description = "Layer3 Call Control (CC)",
45 .color = "\033[1;32m",
46 .enabled = 1, .loglevel = LOGL_NOTICE,
47 },
48 [DMM] = {
Holger Hans Peter Freyther779d2f42012-09-27 16:31:29 +020049 .name = NULL,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020050 .description = "Layer3 Mobility Management (MM)",
51 .color = "\033[1;33m",
52 .enabled = 1, .loglevel = LOGL_NOTICE,
53 },
54};
55
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010056static int test_filter(const struct log_context *ctx, struct log_target *target)
57{
58 filter_called += 1;
59 /* omit everything */
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010060 return select_output;
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010061}
62
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020063const struct log_info log_info = {
64 .cat = default_categories,
65 .num_cat = ARRAY_SIZE(default_categories),
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +010066 .filter_fn = test_filter,
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020067};
68
Neels Hofmeyr85b42c42016-12-12 14:34:41 +010069extern struct log_info *osmo_log_info;
70
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020071int main(int argc, char **argv)
72{
73 struct log_target *stderr_target;
74
75 log_init(&log_info, NULL);
76 stderr_target = log_target_create_stderr();
77 log_add_target(stderr_target);
78 log_set_all_filter(stderr_target, 1);
79 log_set_print_filename(stderr_target, 0);
Neels Hofmeyr9adf32f2016-12-12 14:22:59 +010080 log_set_print_category(stderr_target, 1);
81 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020082
83 log_parse_category_mask(stderr_target, "DRLL:DCC");
84 log_parse_category_mask(stderr_target, "DRLL");
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010085
86 select_output = 0;
87
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020088 DEBUGP(DCC, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010089 if (log_check_level(DMM, LOGL_DEBUG) != 0)
90 fprintf(stderr, "log_check_level did not catch this case\n");
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020091
92 log_parse_category_mask(stderr_target, "DRLL:DCC");
93 DEBUGP(DRLL, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010094 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020095 DEBUGP(DCC, "You should see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010096 OSMO_ASSERT(log_check_level(DCC, LOGL_DEBUG) != 0);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +020097 DEBUGP(DMM, "You should not see this\n");
Jacob Erlbeck64e0eb52015-11-17 11:52:26 +010098
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +010099 OSMO_ASSERT(log_check_level(DMM, LOGL_DEBUG) == 0);
Holger Hans Peter Freytherb7d0f462013-12-29 19:38:01 +0100100 OSMO_ASSERT(filter_called == 0);
101
102 log_set_all_filter(stderr_target, 0);
103 DEBUGP(DRLL, "You should not see this and filter is called\n");
104 OSMO_ASSERT(filter_called == 1);
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100105 OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) == 0);
106 OSMO_ASSERT(filter_called == 2);
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200107
Holger Hans Peter Freyther79599ac2016-01-15 16:49:06 +0100108 DEBUGP(DRLL, "You should not see this\n");
109 OSMO_ASSERT(filter_called == 3);
110 select_output = 1;
111 DEBUGP(DRLL, "You should see this\n");
112 OSMO_ASSERT(filter_called == 5); /* called twice on output */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100113
114 /* Make sure out-of-bounds category maps to DLGLOBAL */
115 log_parse_category_mask(stderr_target, "DLGLOBAL,1");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100116 /* For IDs out of bounds of the overall osmo_log_info array */
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100117 DEBUGP(osmo_log_info->num_cat + 1, "You should see this on DLGLOBAL (a)\n");
118 DEBUGP(osmo_log_info->num_cat + 100, "You should see this on DLGLOBAL (b)\n");
Neels Hofmeyra280b822016-12-12 14:42:56 +0100119 DEBUGP(osmo_log_info->num_cat, "You should see this on DLGLOBAL (c)\n");
Neels Hofmeyrd1a145e2016-12-12 15:53:51 +0100120 /* For IDs out of bounds of the user categories part */
121 DEBUGP(log_info.num_cat + 1, "You should see this on DLGLOBAL (d)\n");
122 DEBUGP(log_info.num_cat, "You should see this on DLGLOBAL (e)\n");
Neels Hofmeyr85b42c42016-12-12 14:34:41 +0100123
Holger Hans Peter Freyther4b54cab2012-09-27 14:18:37 +0200124 return 0;
125}