-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.py
More file actions
104 lines (86 loc) · 3.12 KB
/
Log.py
File metadata and controls
104 lines (86 loc) · 3.12 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
# Author: Quinn James (qj@quinnjam.es)
#
# A log object for storing and displaying status of the program.
#
# More details about this project can be found in the README file or at:
# https://github.com/qcjames53/AJM-RouteSummaries
from enum import Enum
from inspect import getframeinfo, stack, Traceback
from pathlib import Path
from datetime import datetime
# Constants
LOG_SHEET_TITLE = "Log"
LOG_PRINT_TIMESTAMP = False
LOG_PRINT_SEVERITY = True
LOG_PRINT_MESSAGE = True
LOG_PRINT_LOCATION = False
# Log severity enum
class Severity(Enum):
GENERAL = "G"
WARNING = "W"
ERROR = "E"
FAILURE = "F"
class Log:
def __init__(self, log_method) -> None:
self.messages = []
self.creation_time = datetime.now()
self.log_method = log_method
def __str__(self) -> str:
output = ""
for message in self.messages:
output += str(message) + '\n'
return output
def __repr__(self) -> str:
return self.__str__()
def logMessage(self, severity: Severity, message: str, \
# Create the message
location: Traceback= None) -> None:
temp_message = LogMessage(self.log_method, severity, message, location)
self.messages.append(temp_message)
# Output the message
temp_message.output()
def logGeneral(self, message: str):
location = getframeinfo(stack()[1][0])
self.logMessage(Severity.GENERAL, message, location)
def logWarning(self, message: str):
location = getframeinfo(stack()[1][0])
self.logMessage(Severity.WARNING, message, location)
def logError(self, message: str):
location = getframeinfo(stack()[1][0])
self.logMessage(Severity.ERROR, message, location)
def logFailure(self, message: str):
location = getframeinfo(stack()[1][0])
self.logMessage(Severity.FAILURE, message, location)
class LogMessage:
def __init__(self, log_method, severity: Severity, message: str, \
location: Traceback) -> None:
self.log_method = log_method
self.creation_time = datetime.now()
self.severity = severity
self.message = message
self.location = location
def __str__(self) -> str:
output = ""
if LOG_PRINT_TIMESTAMP:
output = str(self.creation_time) + " "
if LOG_PRINT_SEVERITY:
if self.severity == Severity.GENERAL:
output += "[General] "
elif self.severity == Severity.WARNING:
output += "[Warning] "
elif self.severity == Severity.ERROR:
output += "[Error] "
else:
output += "[Failure] "
if LOG_PRINT_MESSAGE:
output += self.message + " "
if LOG_PRINT_LOCATION:
output += "[" + self.getLocationShortFormatted() + "]"
return output
def __repr__(self) -> str:
return self.__str__()
def getLocationShortFormatted(self) -> str:
file_name = Path(self.location.filename).stem
return file_name + ":" + str(self.location.lineno)
def output(self) -> None:
self.log_method(self.__str__())