-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_log.py
74 lines (56 loc) · 2.3 KB
/
serial_log.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
import time
import ujson
from gc import mem_free, collect
class SerialLog(object):
enabled = True
logHistoryData = []
@staticmethod
def enable():
SerialLog.enabled = True
@staticmethod
def disable():
SerialLog.enabled = False
@staticmethod
def logHistory():
return ujson.dumps(SerialLog.logHistoryData)
@staticmethod
def log(message = "", message2 = None, message3 = None, message4 = None, message5 = None):
# Check free memory, if we have less than 10k, then disable logging
if (mem_free() < 10000):
SerialLog.logHistoryData = ["Log history purged due to low memory"]
collect()
# Ignore blank messages
if (message == "" or message == None):
return
if (len(SerialLog.logHistoryData) > 100):
SerialLog.logHistoryData.pop(0)
if (message2 == None):
SerialLog.logHistoryData.append(str(message))
elif (message3 == None):
SerialLog.logHistoryData.append("%s %s" % (message, message2))
elif (message4 == None):
SerialLog.logHistoryData.append("%s %s %s" % (message, message2, message3))
elif (message5 == None):
SerialLog.logHistoryData.append("%s %s %s %s" % (message, message2, message3, message4))
else:
SerialLog.logHistoryData.append("%s %s %s %s %s" % (message, message2, message3, message4, message5))
if SerialLog.enabled:
startedAt = time.ticks_ms()
if (message2 == None):
print(message)
else:
if (message3 == None):
print(message, message2)
else:
if (message4 == None):
print(message, message2, message3)
else:
if (message5 == None):
print(message, message2, message3, message4)
else:
print(message, message2, message3, message4, message5)
endedAt = time.ticks_ms()
# Logging is taking more than 1000ms PER LINE, stopping now
diff = time.ticks_diff(endedAt, startedAt)
if (diff > 1000):
SerialLog.disable()