Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 1 | /* 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 Freyther | 53aa0f5 | 2017-12-09 13:03:29 +0000 | [diff] [blame] | 7 | * 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 Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 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 | * |
Holger Hans Peter Freyther | 53aa0f5 | 2017-12-09 13:03:29 +0000 | [diff] [blame] | 16 | * 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 Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <osmocom/core/logging.h> |
| 23 | #include <osmocom/core/utils.h> |
| 24 | |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 27 | enum { |
| 28 | DRLL, |
| 29 | DCC, |
| 30 | DMM, |
| 31 | }; |
| 32 | |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 33 | static int filter_called = 0; |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 34 | static int select_output = 0; |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 36 | static 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 Freyther | 779d2f4 | 2012-09-27 16:31:29 +0200 | [diff] [blame] | 50 | .name = NULL, |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 51 | .description = "Layer3 Mobility Management (MM)", |
| 52 | .color = "\033[1;33m", |
| 53 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 54 | }, |
| 55 | }; |
| 56 | |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 57 | static int test_filter(const struct log_context *ctx, struct log_target *target) |
| 58 | { |
| 59 | filter_called += 1; |
| 60 | /* omit everything */ |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 61 | return select_output; |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 64 | const struct log_info log_info = { |
| 65 | .cat = default_categories, |
| 66 | .num_cat = ARRAY_SIZE(default_categories), |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 67 | .filter_fn = test_filter, |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
Neels Hofmeyr | 85b42c4 | 2016-12-12 14:34:41 +0100 | [diff] [blame] | 70 | extern struct log_info *osmo_log_info; |
| 71 | |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 72 | int 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 Hofmeyr | 9adf32f | 2016-12-12 14:22:59 +0100 | [diff] [blame] | 81 | log_set_print_category(stderr_target, 1); |
| 82 | log_set_use_color(stderr_target, 0); |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 83 | |
| 84 | log_parse_category_mask(stderr_target, "DRLL:DCC"); |
| 85 | log_parse_category_mask(stderr_target, "DRLL"); |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 86 | |
| 87 | select_output = 0; |
| 88 | |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 89 | DEBUGP(DCC, "You should not see this\n"); |
Jacob Erlbeck | 64e0eb5 | 2015-11-17 11:52:26 +0100 | [diff] [blame] | 90 | if (log_check_level(DMM, LOGL_DEBUG) != 0) |
| 91 | fprintf(stderr, "log_check_level did not catch this case\n"); |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 92 | |
| 93 | log_parse_category_mask(stderr_target, "DRLL:DCC"); |
| 94 | DEBUGP(DRLL, "You should see this\n"); |
Jacob Erlbeck | 64e0eb5 | 2015-11-17 11:52:26 +0100 | [diff] [blame] | 95 | OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) != 0); |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 96 | DEBUGP(DCC, "You should see this\n"); |
Jacob Erlbeck | 64e0eb5 | 2015-11-17 11:52:26 +0100 | [diff] [blame] | 97 | OSMO_ASSERT(log_check_level(DCC, LOGL_DEBUG) != 0); |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 98 | DEBUGP(DMM, "You should not see this\n"); |
Jacob Erlbeck | 64e0eb5 | 2015-11-17 11:52:26 +0100 | [diff] [blame] | 99 | |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 100 | OSMO_ASSERT(log_check_level(DMM, LOGL_DEBUG) == 0); |
Holger Hans Peter Freyther | b7d0f46 | 2013-12-29 19:38:01 +0100 | [diff] [blame] | 101 | 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 Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 106 | OSMO_ASSERT(log_check_level(DRLL, LOGL_DEBUG) == 0); |
| 107 | OSMO_ASSERT(filter_called == 2); |
Holger Hans Peter Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 108 | |
Holger Hans Peter Freyther | 79599ac | 2016-01-15 16:49:06 +0100 | [diff] [blame] | 109 | 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 Hofmeyr | 85b42c4 | 2016-12-12 14:34:41 +0100 | [diff] [blame] | 114 | |
| 115 | /* Make sure out-of-bounds category maps to DLGLOBAL */ |
| 116 | log_parse_category_mask(stderr_target, "DLGLOBAL,1"); |
Neels Hofmeyr | d1a145e | 2016-12-12 15:53:51 +0100 | [diff] [blame] | 117 | /* For IDs out of bounds of the overall osmo_log_info array */ |
Neels Hofmeyr | 85b42c4 | 2016-12-12 14:34:41 +0100 | [diff] [blame] | 118 | 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 Hofmeyr | a280b82 | 2016-12-12 14:42:56 +0100 | [diff] [blame] | 120 | DEBUGP(osmo_log_info->num_cat, "You should see this on DLGLOBAL (c)\n"); |
Neels Hofmeyr | d1a145e | 2016-12-12 15:53:51 +0100 | [diff] [blame] | 121 | /* 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 Hofmeyr | 85b42c4 | 2016-12-12 14:34:41 +0100 | [diff] [blame] | 124 | |
Neels Hofmeyr | a4842af | 2016-12-12 14:11:31 +0100 | [diff] [blame] | 125 | /* 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 Freyther | 4b54cab | 2012-09-27 14:18:37 +0200 | [diff] [blame] | 131 | return 0; |
| 132 | } |