-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.py
58 lines (47 loc) · 1.23 KB
/
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
# Log class
#
# Copyright (C) Mark Gladding 2023.
#
# MIT License (see the accompanying license file)
#
# https://github.com/mark-gladding/weatherstation
#
import json
from io import StringIO
import os
import sys
import time
class Log:
"""
"""
def __init__(self):
self._last_error_filename = 'LastError.json'
def write_last_error(self, exception):
current_time = f'{time.time()}'
message = {
'Time': current_time,
'MeasureName': 'exception',
'MeasureValue': str(exception),
}
s = StringIO()
sys.print_exception(exception, s)
stack_trace = {
'Time': current_time,
'MeasureName': 'stack_trace',
'MeasureValue': s.getvalue(),
}
records = [message, stack_trace]
with open(self._last_error_filename, 'w') as f:
json.dump(records, f)
def read_last_error(self):
try:
with open(self._last_error_filename) as f:
return json.load(f)
except OSError:
pass
return None
def clear_last_error(self):
try:
os.remove(self._last_error_filename)
except:
pass