Repository navigation
Expand file tree
/
Copy pathDailyTextLogFile.py
More file actions
117 lines (87 loc) · 3.28 KB
/
Copy pathDailyTextLogFile.py
File metadata and controls
117 lines (87 loc) · 3.28 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
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
116
#######################################################
# tDailyTextLogFile - Rotating daily text log file helper
#
# Provides the shared .log file rotation behavior used by collector console
# logs and the master stdout/stderr tee.
#
import os
import sys
from datetime import datetime
from PySide6.QtCore import QFile
##########################################################################################
##########################################################################################
##########################################################################################
# tDailyTextLogFile
#
#
class tDailyTextLogFile:
###############################################
# Constructor
#
def __init__(self, DailyFolder, ShortLogFileName):
self.DailyFolder = DailyFolder
self.ShortLogFileName = ShortLogFileName
self.LogFileName = os.path.join(self.DailyFolder, self.ShortLogFileName)
self.StartNewFile()
###############################################
# StartNewFile - Rotate the existing file and create a fresh empty log
#
def StartNewFile(self):
os.makedirs(self.DailyFolder, exist_ok=True)
self.LogFileName = os.path.join(self.DailyFolder, self.ShortLogFileName)
if not self._MoveExistingLogFile():
return False
try:
with open(self.LogFileName, "w", encoding="utf-8", newline="") as file:
pass
return True
except OSError as e:
self._ReportWarning(f"tDailyTextLogFile: could not create {self.LogFileName}: {e}")
return False
###############################################
# WriteText - Append text to the current log file
#
def WriteText(self, Text):
try:
with open(self.LogFileName, "a", encoding="utf-8", newline="") as file:
file.write(Text)
return True
except OSError as e:
self._ReportWarning(f"tDailyTextLogFile: could not write {self.LogFileName}: {e}")
return False
###############################################
# _MoveExistingLogFile - Move an existing log to trash, with rename fallback
#
def _MoveExistingLogFile(self):
if not os.path.exists(self.LogFileName):
return True
if QFile.moveToTrash(self.LogFileName):
return True
BackupFileName = self._BackupFileName()
try:
os.replace(self.LogFileName, BackupFileName)
self._ReportWarning(f"tDailyTextLogFile: could not move {self.LogFileName} to recycle bin; renamed it to {BackupFileName}")
return True
except OSError as e:
self._ReportWarning(f"tDailyTextLogFile: could not rotate {self.LogFileName}: {e}")
return False
###############################################
# _BackupFileName - Build a unique timestamped backup name
#
def _BackupFileName(self):
Timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
BackupFileName = f"{self.LogFileName}.{Timestamp}.bak"
Index = 1
while os.path.exists(BackupFileName):
BackupFileName = f"{self.LogFileName}.{Timestamp}_{Index}.bak"
Index += 1
return BackupFileName
###############################################
# _ReportWarning - Write warnings without recursing through any active tee
#
def _ReportWarning(self, Message):
try:
sys.__stderr__.write(Message + "\n")
sys.__stderr__.flush()
except Exception:
pass