-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoplog.py
More file actions
40 lines (31 loc) · 1.11 KB
/
Copy pathoplog.py
File metadata and controls
40 lines (31 loc) · 1.11 KB
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
"""Local operation logging.
Writes maintenance-operation records to a local log file next to the application.
Privacy-by-default: records the printer MODEL, transport, counter values, backup
path, user-confirmation flags and errors -- but NOT printer serial numbers or any
personal identifiers. Nothing is transmitted off the device.
"""
import os
import time
import eeprom_io
def logs_dir():
d = os.path.join(eeprom_io.app_dir(), "logs")
os.makedirs(d, exist_ok=True)
return d
def log_path():
return os.path.join(logs_dir(), "operations.log")
def log_event(event, **fields):
"""Append one structured, human-readable line to the local operation log.
Do not pass serial numbers or personal identifiers as fields.
"""
ts = time.strftime("%Y-%m-%d %H:%M:%S")
parts = [f"{ts}", f"event={event}"]
for k, v in fields.items():
parts.append(f"{k}={v}")
line = " | ".join(parts)
try:
with open(log_path(), "a", encoding="utf-8") as f:
f.write(line + "\n")
except Exception:
# logging must never crash the application
pass
return line