This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmirrorkan_log.py
112 lines (82 loc) · 2.84 KB
/
mirrorkan_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
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
#!/usr/bin/env python
import os, sys
import json
class Log:
def __init__(self, path):
self.path = path
if os.path.exists(path):
return
self.clear()
def clear(self):
with open(self.path, 'w') as db_file:
db_file.write('{ "warnings": [], "errors": [], "info": [], "events": [] }')
def logInfo(self, message):
db = None
with open(self.path, 'r') as db_file:
db = json.load(db_file)
db['info'] += [message]
if db is not None:
with open(self.path, 'w') as db_file:
json.dump(db, db_file)
def logWarning(self, message):
db = None
with open(self.path, 'r') as db_file:
db = json.load(db_file)
db['warnings'] += [message]
if db is not None:
with open(self.path, 'w') as db_file:
json.dump(db, db_file)
def logError(self, message):
db = None
with open(self.path, 'r') as db_file:
db = json.load(db_file)
db['errors'] += [message]
if db is not None:
with open(self.path, 'w') as db_file:
json.dump(db, db_file)
def logEvent(self, event, message):
db = None
with open(self.path, 'r') as db_file:
db = json.load(db_file)
db['events'] += [{'event': event, 'message': message}]
if db is not None:
with open(self.path, 'w') as db_file:
json.dump(db, db_file)
def getWarnings(self):
with open(self.path, 'r') as db_file:
db = json.load(db_file)
return db['warnings']
return None
def getErrors(self):
with open(self.path, 'r') as db_file:
db = json.load(db_file)
return db['errors']
return None
def getInfo(self):
with open(self.path, 'r') as db_file:
db = json.load(db_file)
return db['info']
return None
def getEvents(self):
with open(self.path, 'r') as db_file:
db = json.load(db_file)
return db['events']
return None
def main():
if len(sys.argv) < 4:
print 'Usage:'
print sys.argv[0] + ' <log_db> <warning/error/info> <message>'
sys.exit(0)
log = Log(sys.argv[1])
if sys.argv[2] == 'warning':
log.logWarning(sys.argv[3])
elif sys.argv[2] == 'error':
log.logError(sys.argv[3])
elif sys.argv[2] == 'info':
log.logInfo(sys.argv[3])
else:
print 'Invalid argument "%s", expected "warning", "error" or "info"' % sys.argv[2]
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()