Debian Codegraph @

mlat/client/util.py

raw symbols SCIP
1 # -*- python -*-
2
3 # Part of mlat-client - an ADS-B multilateration client.
4 # Copyright 2015, Oliver Jowett <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
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 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys
20 import time
21 import traceback
22
23
24 __all__ = ('log', 'log_exc', 'monotonic_time')
25
26
27 suppress_log_timestamps = True
28
29 def log(msg, *args, **kwargs):
30 if suppress_log_timestamps:
31 print(msg.format(*args, **kwargs), file=sys.stderr)
32 else:
33 print(time.ctime(), msg.format(*args, **kwargs), file=sys.stderr)
34 sys.stderr.flush()
35
36
37 def log_exc(msg, *args, **kwargs):
38 if suppress_log_timestamps:
39 print(msg.format(*args, **kwargs), file=sys.stderr)
40 else:
41 print(time.ctime(), msg.format(*args, **kwargs), file=sys.stderr)
42 traceback.print_exc(file=sys.stderr)
43 sys.stderr.flush()
44
45
46 _adjust = 0
47 _last = 0
48
49
50 def monotonic_time():
51 """Emulates time.monotonic() if not available."""
52 global _adjust, _last
53
54 now = time.time()
55 if now < _last:
56 # system clock went backwards, add in a
57 # fudge factor so our monotonic clock
58 # does not.
59 _adjust = _adjust + (_last - now)
60
61 _last = now
62 return now + _adjust
63
64
65 try:
66 # try to use the 3.3+ version when available
67 from time import monotonic as monotonic_time # noqa
68 except ImportError:
69 pass