-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmylogs.py
159 lines (117 loc) · 4.65 KB
/
mylogs.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/user/bin/env python3
"""mylogs.py
Author: Kimon Froussios
Last revised: 27/10/2017
Library for custom logging.
XXXstring() functions return formatted strings for custom use by the caller.
log_XXX() functions format and record messages to log-files.
NOTE: XXXstring() return values are not intended to be passed as arguments to
log_XXX(), as this will cause duplication and will mess up the formatting. Use
XXXstring() to print info to the screen or within your output files. Use
log_XXX() to record to log-files.
"""
import sys, datetime, os
# Do everything in ONE write operation, to prevent race conditions when accessing the logfile.
def tstamp():
"""Readable current date-time.
Returns:
(str)
"""
return str(datetime.datetime.now())
def escapise(comnd):
"""Escape special characters in command.
Any special characters would have arrived here by having been escaped in the command line.
To log the command in a way it can be reused as-is, escapes must be re-added.
Args:
comnd[str]: List of strings to sanitise.
Returns:
[str]
"""
for i,c in enumerate(comnd):
# Sometimes an empty string is an argument. That needs to be quoted for the log.
if c == '':
comnd[i] = '\'\''
else:
comnd[i] = c.translate(str.maketrans({"\\": r"\\",
"\"": r"\"",
"\'": r"\'",
"$": r"\$",
"\t": r"$'\t'",
">": r"\>",
"<": r"\<",
"|": r"\|",
"*": r"\*",
"#": r"\#",
"%": r"\%"}))
return comnd
def paramstring(message=""):
"""Execution parameters log-string.
I normally use this at the beginning of my output.
Args:
message(str): Optional message to add.
Returns:
(str)
"""
message.rstrip().replace("\n", " ")
return tstamp() + "\t## CWD ## " + os.getcwd() + "\n\t\t\t\t## CMND ## " + sys.executable + " " + " ".join(sys.argv) + ("\n\t\t\t\t## MSSG ## " if message else "") + message + "\n"
def donestring(message=""):
"""Done log-string.
I normally use this at the end of tasks.
Args:
message(str): A custom message to add.
Returns:
(str)
"""
message.rstrip().replace("\n", " ")
return tstamp() + "\t## DONE ## " + message + ".\n"
def infostring(message=""):
"""Info log-string.
I normally use this at the end of tasks.
Args:
message(str): A custom message to add.
Returns:
(str)
"""
message.rstrip().replace("\n", " ")
return tstamp() + "\t## INFO ## " + message + "\n"
def warnstring(message=""):
"""Warning log-string.
Args:
message(str): The message to add.
Returns:
(str)
"""
message.rstrip().replace("\n", " ")
return tstamp() + "\t##!WARN!## " + os.path.basename(sys.argv[0]) + " - " + message + "\n"
def errstring(message=""):
"""Error log-string.
Args:
message(str): The message to add.
Returns:
(str)
"""
message.rstrip().replace("\n", " ")
return tstamp() + "\t##!ERROR!# " + os.path.basename(sys.argv[0]) + " - " + message + "\n"
def log_command(message="", logfile="./commands.log"):
"""Record timestamp, command-line call and optional message.
This function obtains the command from sys.argv[].
Args:
message(str): Message to record in the log. (Default empty)
logfile(str): File to write to (./commands.log).
"""
cmnd = " ".join(sys.argv[:2]) + ' ' + " ".join(escapise(sys.argv[2:]))
with open(logfile, 'a') as comlog:
if message == "":
comlog.write(tstamp() + "\t" + str(sys.executable) + " " + cmnd + "\n")
else:
comlog.write(tstamp() + "\t" + str(sys.executable) + " " + cmnd + "\n" + " \t" + message.rstrip().replace("\n", "\n ") + "\n")
def log_message(message="", logfile="./messages.log"):
"""Record timestamp and message.
Records timestamp and message to specified log-file.
Args:
message(str): Message to record in the log. (Default empty)
logfile(str): File to write to. (./messages.log)
"""
with open(logfile, 'a') as comlog:
comlog.write(tstamp() + "\t" + message.rstrip().replace("\n", "\n ") + "\n")
# EOF