Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # osmo_gsm_tester: logging tests |
| 4 | # |
| 5 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 6 | # |
| 7 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 8 | # |
| 9 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 10 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 11 | # published by the Free Software Foundation, either version 3 of the |
| 12 | # License, or (at your option) any later version. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # GNU General Public License for more details. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 18 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 19 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | import _prep |
| 23 | |
| 24 | import sys |
| 25 | import os |
| 26 | |
| 27 | from osmo_gsm_tester import log |
| 28 | |
| 29 | #log.targets[0].get_time_str = lambda: '01:02:03' |
| 30 | fake_time = '01:02:03' |
| 31 | log.style_change(time=True, time_fmt=fake_time) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 32 | log.set_all_levels(None) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 33 | |
| 34 | print('- Testing global log functions') |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 35 | log.log('from log.log()', _origin='<origin>', _category=log.C_TST) |
| 36 | log.dbg('from log.dbg(), not seen', _origin='<origin>', _category=log.C_TST) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 37 | log.set_level(log.C_TST, log.L_DBG) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 38 | log.dbg('from log.dbg()', _origin='<origin>', _category=log.C_TST) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 39 | log.set_level(log.C_TST, log.L_LOG) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 40 | log.err('from log.err()', _origin='<origin>', _category=log.C_TST) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 41 | |
| 42 | print('- Testing log.Origin functions') |
| 43 | class LogTest(log.Origin): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 44 | def __init__(self, *name_items, **detail_items): |
| 45 | super().__init__(log.C_TST, *name_items, **detail_items) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 46 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 47 | t = LogTest('some', 'name', some="detail") |
| 48 | |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 49 | |
| 50 | t.log("hello log") |
| 51 | t.err("hello err") |
| 52 | t.dbg("hello dbg not visible") |
| 53 | |
| 54 | t.log("message", int=3, tuple=('foo', 42), none=None, str='str\n') |
| 55 | |
| 56 | log.set_level(log.C_TST, log.L_DBG) |
| 57 | t.dbg("hello dbg") |
| 58 | |
| 59 | print('- Testing log.style()') |
| 60 | |
| 61 | log.style(time=True, category=False, level=False, origin=False, src=False, time_fmt=fake_time) |
| 62 | t.dbg("only time") |
| 63 | log.style(time=False, category=True, level=False, origin=False, src=False, time_fmt=fake_time) |
| 64 | t.dbg("only category") |
| 65 | log.style(time=False, category=False, level=True, origin=False, src=False, time_fmt=fake_time) |
| 66 | t.dbg("only level") |
| 67 | log.style(time=False, category=False, level=False, origin=True, src=False, time_fmt=fake_time) |
| 68 | t.dbg("only origin") |
| 69 | log.style(time=False, category=False, level=False, origin=False, src=True, time_fmt=fake_time) |
| 70 | t.dbg("only src") |
| 71 | |
| 72 | print('- Testing log.style_change()') |
| 73 | log.style(time=False, category=False, level=False, origin=False, src=False, time_fmt=fake_time) |
| 74 | t.dbg("no log format") |
| 75 | log.style_change(time=True) |
| 76 | t.dbg("add time") |
| 77 | log.style_change(time=True, time_fmt=0) |
| 78 | t.dbg("but no time format") |
| 79 | log.style_change(time=True, time_fmt=fake_time) |
| 80 | log.style_change(level=True) |
| 81 | t.dbg("add level") |
| 82 | log.style_change(category=True) |
| 83 | t.dbg("add category") |
| 84 | log.style_change(src=True) |
| 85 | t.dbg("add src") |
| 86 | log.style_change(origin=True) |
| 87 | t.dbg("add origin") |
| 88 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 89 | # some space to keep source line numbers identical to previous code |
| 90 | |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 91 | print('- Testing origin_width') |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 92 | t = LogTest('shortname') |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 93 | log.style(origin_width=23, time_fmt=fake_time) |
| 94 | t.log("origin str set to 23 chars") |
| 95 | t.set_name('very long name', some='details', and_some=(3, 'things', 'in a tuple')) |
| 96 | t.log("long origin str") |
| 97 | t.dbg("long origin str dbg") |
| 98 | t.err("long origin str err") |
| 99 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 100 | |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 101 | print('- Testing log.Origin with omitted info') |
| 102 | t = LogTest() |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 103 | t.log("hello log, name implicit from class name") |
| 104 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 105 | t = LogTest('explicit_name') |
| 106 | t._set_log_category(None) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 107 | t.log("hello log, no category set") |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 108 | t = LogTest() |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 109 | t._set_log_category(None) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 110 | t.log("hello log, no category nor name set") |
| 111 | t.dbg("hello log, no category nor name set, not seen") |
| 112 | log.set_level(log.C_DEFAULT, log.L_DBG) |
| 113 | t.dbg("debug message, no category nor name set") |
| 114 | |
| 115 | print('- Testing logging of Exceptions, tracing origins') |
Neels Hofmeyr | 532126a | 2017-05-05 19:51:40 +0200 | [diff] [blame] | 116 | log.style(time_fmt=fake_time, origin_width=0) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 117 | |
| 118 | class Thing(log.Origin): |
| 119 | def __init__(self, some_path): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 120 | super().__init__(log.C_TST, some_path) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 121 | |
| 122 | def say(self, msg): |
| 123 | print(msg) |
| 124 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 125 | def l1(self): |
| 126 | level2 = Thing('level2') |
| 127 | level2.l2() |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 128 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 129 | def l2(self): |
| 130 | level3 = Thing('level3') |
| 131 | level3.l3(self) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 132 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 133 | def l3(self, level2): |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 134 | print('nested print just prints') |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 135 | self.log('nested log()') |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 136 | level2.log('nested l2 log() from within l3 scope') |
| 137 | raise ValueError('bork') |
| 138 | |
| 139 | try: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 140 | level1 = Thing('level1') |
| 141 | level1.l1() |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 142 | except Exception: |
| 143 | log.log_exn() |
| 144 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 145 | print('- Disallow origin loops') |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 146 | try: |
| 147 | t = Thing('foo') |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame^] | 148 | t._set_parent(t) |
| 149 | raise RuntimeError('this should not be reached') |
| 150 | except log.OriginLoopError: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 151 | print('disallowed successfully') |
| 152 | pass |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 153 | |
| 154 | # vim: expandtab tabstop=4 shiftwidth=4 |