blob: 6eca6aaa99dc1e1722f194e51d26eddbf5813424 [file] [log] [blame]
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02001#!/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
22import _prep
23
24import sys
25import os
26
27from osmo_gsm_tester import log
28
29#log.targets[0].get_time_str = lambda: '01:02:03'
30fake_time = '01:02:03'
31log.style_change(time=True, time_fmt=fake_time)
32
33print('- Testing global log functions')
34log.log('<origin>', log.C_TST, 'from log.log()')
35log.dbg('<origin>', log.C_TST, 'from log.dbg(), not seen')
36log.set_level(log.C_TST, log.L_DBG)
37log.dbg('<origin>', log.C_TST, 'from log.dbg()')
38log.set_level(log.C_TST, log.L_LOG)
39log.err('<origin>', log.C_TST, 'from log.err()')
40
41print('- Testing log.Origin functions')
42class LogTest(log.Origin):
43 pass
44
45t = LogTest()
46t.set_log_category(log.C_TST)
47t.set_name('some', 'name', some="detail")
48
49t.log("hello log")
50t.err("hello err")
51t.dbg("hello dbg not visible")
52
53t.log("message", int=3, tuple=('foo', 42), none=None, str='str\n')
54
55log.set_level(log.C_TST, log.L_DBG)
56t.dbg("hello dbg")
57
58print('- Testing log.style()')
59
60log.style(time=True, category=False, level=False, origin=False, src=False, time_fmt=fake_time)
61t.dbg("only time")
62log.style(time=False, category=True, level=False, origin=False, src=False, time_fmt=fake_time)
63t.dbg("only category")
64log.style(time=False, category=False, level=True, origin=False, src=False, time_fmt=fake_time)
65t.dbg("only level")
66log.style(time=False, category=False, level=False, origin=True, src=False, time_fmt=fake_time)
67t.dbg("only origin")
68log.style(time=False, category=False, level=False, origin=False, src=True, time_fmt=fake_time)
69t.dbg("only src")
70
71print('- Testing log.style_change()')
72log.style(time=False, category=False, level=False, origin=False, src=False, time_fmt=fake_time)
73t.dbg("no log format")
74log.style_change(time=True)
75t.dbg("add time")
76log.style_change(time=True, time_fmt=0)
77t.dbg("but no time format")
78log.style_change(time=True, time_fmt=fake_time)
79log.style_change(level=True)
80t.dbg("add level")
81log.style_change(category=True)
82t.dbg("add category")
83log.style_change(src=True)
84t.dbg("add src")
85log.style_change(origin=True)
86t.dbg("add origin")
87
88print('- Testing origin_width')
89t = LogTest()
90t.set_log_category(log.C_TST)
91t.set_name('shortname')
92log.style(origin_width=23, time_fmt=fake_time)
93t.log("origin str set to 23 chars")
94t.set_name('very long name', some='details', and_some=(3, 'things', 'in a tuple'))
95t.log("long origin str")
96t.dbg("long origin str dbg")
97t.err("long origin str err")
98
99print('- Testing log.Origin with omitted info')
100t = LogTest()
101t.set_log_category(log.C_TST)
102t.log("hello log, name implicit from class name")
103
104t = LogTest()
105t.set_name('explicit_name')
106t.log("hello log, no category set")
107
108t = LogTest()
109t.log("hello log, no category nor name set")
110t.dbg("hello log, no category nor name set, not seen")
111log.set_level(log.C_DEFAULT, log.L_DBG)
112t.dbg("debug message, no category nor name set")
113
114print('- Testing logging of Exceptions, tracing origins')
115log.style(time_fmt=fake_time)
116
117class Thing(log.Origin):
118 def __init__(self, some_path):
119 self.set_log_category(log.C_TST)
120 self.set_name(some_path)
121
122 def say(self, msg):
123 print(msg)
124
125#log.style_change(trace=True)
126
127with Thing('print_redirected'):
128 print("Not throwing an exception in 'with:' works.")
129
130def l1():
131 level1 = Thing('level1')
132 with level1:
133 l2()
134
135def l2():
136 level2 = Thing('level2')
137 with level2:
138 l3(level2)
139
140def l3(level2):
141 level3 = Thing('level3')
142 with level3:
143 print('nested print just prints')
144 level3.log('nested log()')
145 level2.log('nested l2 log() from within l3 scope')
146 raise ValueError('bork')
147
148try:
149 l1()
150except Exception:
151 log.log_exn()
152
153print('- Enter the same Origin context twice')
154with Thing('level1'):
155 l2 = Thing('level2')
156 with l2:
157 with l2:
158 l2.log('nested log')
159
160# vim: expandtab tabstop=4 shiftwidth=4