blob: 54a0fb692869a8ba3a3bbd98e0f047a65ed92a28 [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)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020032log.set_all_levels(None)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020033
34print('- Testing global log functions')
35log.log('<origin>', log.C_TST, 'from log.log()')
36log.dbg('<origin>', log.C_TST, 'from log.dbg(), not seen')
37log.set_level(log.C_TST, log.L_DBG)
38log.dbg('<origin>', log.C_TST, 'from log.dbg()')
39log.set_level(log.C_TST, log.L_LOG)
40log.err('<origin>', log.C_TST, 'from log.err()')
41
42print('- Testing log.Origin functions')
43class LogTest(log.Origin):
44 pass
45
46t = LogTest()
47t.set_log_category(log.C_TST)
48t.set_name('some', 'name', some="detail")
49
50t.log("hello log")
51t.err("hello err")
52t.dbg("hello dbg not visible")
53
54t.log("message", int=3, tuple=('foo', 42), none=None, str='str\n')
55
56log.set_level(log.C_TST, log.L_DBG)
57t.dbg("hello dbg")
58
59print('- Testing log.style()')
60
61log.style(time=True, category=False, level=False, origin=False, src=False, time_fmt=fake_time)
62t.dbg("only time")
63log.style(time=False, category=True, level=False, origin=False, src=False, time_fmt=fake_time)
64t.dbg("only category")
65log.style(time=False, category=False, level=True, origin=False, src=False, time_fmt=fake_time)
66t.dbg("only level")
67log.style(time=False, category=False, level=False, origin=True, src=False, time_fmt=fake_time)
68t.dbg("only origin")
69log.style(time=False, category=False, level=False, origin=False, src=True, time_fmt=fake_time)
70t.dbg("only src")
71
72print('- Testing log.style_change()')
73log.style(time=False, category=False, level=False, origin=False, src=False, time_fmt=fake_time)
74t.dbg("no log format")
75log.style_change(time=True)
76t.dbg("add time")
77log.style_change(time=True, time_fmt=0)
78t.dbg("but no time format")
79log.style_change(time=True, time_fmt=fake_time)
80log.style_change(level=True)
81t.dbg("add level")
82log.style_change(category=True)
83t.dbg("add category")
84log.style_change(src=True)
85t.dbg("add src")
86log.style_change(origin=True)
87t.dbg("add origin")
88
89print('- Testing origin_width')
90t = LogTest()
91t.set_log_category(log.C_TST)
92t.set_name('shortname')
93log.style(origin_width=23, time_fmt=fake_time)
94t.log("origin str set to 23 chars")
95t.set_name('very long name', some='details', and_some=(3, 'things', 'in a tuple'))
96t.log("long origin str")
97t.dbg("long origin str dbg")
98t.err("long origin str err")
99
100print('- Testing log.Origin with omitted info')
101t = LogTest()
102t.set_log_category(log.C_TST)
103t.log("hello log, name implicit from class name")
104
105t = LogTest()
106t.set_name('explicit_name')
107t.log("hello log, no category set")
108
109t = LogTest()
110t.log("hello log, no category nor name set")
111t.dbg("hello log, no category nor name set, not seen")
112log.set_level(log.C_DEFAULT, log.L_DBG)
113t.dbg("debug message, no category nor name set")
114
115print('- Testing logging of Exceptions, tracing origins')
Neels Hofmeyr532126a2017-05-05 19:51:40 +0200116log.style(time_fmt=fake_time, origin_width=0)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200117
118class 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
128with Thing('print_redirected'):
129 print("Not throwing an exception in 'with:' works.")
130
131def l1():
132 level1 = Thing('level1')
133 with level1:
134 l2()
135
136def l2():
137 level2 = Thing('level2')
138 with level2:
139 l3(level2)
140
141def 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
149try:
150 l1()
151except Exception:
152 log.log_exn()
153
154print('- Enter the same Origin context twice')
155with 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