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 |
| 10 | # it under the terms of the GNU Affero General Public License as |
| 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 |
| 17 | # GNU Affero General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU Affero General Public License |
| 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') |
| 35 | log.log('<origin>', log.C_TST, 'from log.log()') |
| 36 | log.dbg('<origin>', log.C_TST, 'from log.dbg(), not seen') |
| 37 | log.set_level(log.C_TST, log.L_DBG) |
| 38 | log.dbg('<origin>', log.C_TST, 'from log.dbg()') |
| 39 | log.set_level(log.C_TST, log.L_LOG) |
| 40 | log.err('<origin>', log.C_TST, 'from log.err()') |
| 41 | |
| 42 | print('- Testing log.Origin functions') |
| 43 | class LogTest(log.Origin): |
| 44 | pass |
| 45 | |
| 46 | t = LogTest() |
| 47 | t.set_log_category(log.C_TST) |
| 48 | t.set_name('some', 'name', some="detail") |
| 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 | |
| 89 | print('- Testing origin_width') |
| 90 | t = LogTest() |
| 91 | t.set_log_category(log.C_TST) |
| 92 | t.set_name('shortname') |
| 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 | |
| 100 | print('- Testing log.Origin with omitted info') |
| 101 | t = LogTest() |
| 102 | t.set_log_category(log.C_TST) |
| 103 | t.log("hello log, name implicit from class name") |
| 104 | |
| 105 | t = LogTest() |
| 106 | t.set_name('explicit_name') |
| 107 | t.log("hello log, no category set") |
| 108 | |
| 109 | t = LogTest() |
| 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): |
| 120 | self.set_log_category(log.C_TST) |
| 121 | self.set_name(some_path) |
| 122 | |
| 123 | def say(self, msg): |
| 124 | print(msg) |
| 125 | |
| 126 | #log.style_change(trace=True) |
| 127 | |
| 128 | with Thing('print_redirected'): |
| 129 | print("Not throwing an exception in 'with:' works.") |
| 130 | |
| 131 | def l1(): |
| 132 | level1 = Thing('level1') |
| 133 | with level1: |
| 134 | l2() |
| 135 | |
| 136 | def l2(): |
| 137 | level2 = Thing('level2') |
| 138 | with level2: |
| 139 | l3(level2) |
| 140 | |
| 141 | def l3(level2): |
| 142 | level3 = Thing('level3') |
| 143 | with level3: |
| 144 | print('nested print just prints') |
| 145 | level3.log('nested log()') |
| 146 | level2.log('nested l2 log() from within l3 scope') |
| 147 | raise ValueError('bork') |
| 148 | |
| 149 | try: |
| 150 | l1() |
| 151 | except Exception: |
| 152 | log.log_exn() |
| 153 | |
| 154 | print('- Enter the same Origin context twice') |
| 155 | with Thing('level1'): |
| 156 | l2 = Thing('level2') |
| 157 | with l2: |
| 158 | with l2: |
| 159 | l2.log('nested log') |
| 160 | |
| 161 | # vim: expandtab tabstop=4 shiftwidth=4 |