forked from fablab-wue/piTelex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txDevLog.py
116 lines (87 loc) · 2.67 KB
/
txDevLog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python3
"""
Telex Device - Writing a log-file
"""
__author__ = "Jochen Krapf"
__email__ = "[email protected]"
__copyright__ = "Copyright 2018, JK"
__license__ = "GPL3"
__version__ = "0.0.1"
import time
import logging
l = logging.getLogger("piTelex." + __name__)
import txBase
#######
def find_rev() -> str:
"""
Try finding out the git commit id and return it.
"""
import subprocess
result = subprocess.run(["git", "log", "--oneline", "-1"], stdout=subprocess.PIPE, check=True)
return result.stdout.decode("utf-8", errors="replace").strip()
class TelexLog(txBase.TelexBase):
def __init__(self, **params):
super().__init__()
self.id = 'Log'
self.params = params
self._filename = params.get('filename', 'log.txt')
self._last_time = self._last_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self._last_source = ' '
self._line = '===== piTelex rev '
try:
self._line += find_rev()
# cut line to head, commit hash and max. 50 characters description
# (as widely recommended)
self._line = self._line[:77]
self._line += ' '
except:
pass
self._line += (80 - len(self._line)) * '='
def __del__(self):
self.exit()
super().__del__()
def exit(self):
pass
# =====
def read(self) -> str:
return ''
def write(self, a:str, source:str):
out = False
add = ''
if a == '\r':
a = '\\'
if a == '\n':
a = '_'
if a == '\t':
a = '\\t'
if len(a) > 1:
# Print all commands, except WELCOME (internal use)
if a[1:] == "WELCOME":
return
a = '{' + a[1:] + '}'
if source == self._last_source and a == '_':
out = True
add = '_'
a = ''
elif source == self._last_source and len(self._line) >= 80:
out = True
add = ' |'
if source != self._last_source:
out = True
if out:
line = self._last_time
line += ' ,'
line += self._last_source
line += ', '
line += self._line
line += add
with open(self._filename, 'a', encoding='UTF-8') as fp:
line += '\n'
fp.write(line)
self._line = ''
self._last_time = ''
self._last_source = source
self._line += a
if not self._last_time:
self._last_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#######