-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsibislogger.py
231 lines (181 loc) · 7.21 KB
/
sibislogger.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
##
## Copyright 2016 SRI International
## See COPYING file distributed along with the package for the copyright and license terms
##
from builtins import str
from builtins import object
from past.utils import old_div
import sys
import json
import traceback
# import logging
import collections
import time
import os
from . import post_issues_to_github as pig
# set logger of python packages to warning so that we avoid info messages being printed out
#logging.getLogger("urllib3").setLevel(logging.WARNING)
#logging.getLogger("requests").setLevel(logging.WARNING)
class sibisJSONEncoder(json.JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, Exception):
exc_info = sys.exc_info()
if (None, None, None) != exc_info:
import traceback
return traceback.format_exception(*exc_info)
return json.JSONEncoder.default(self, obj)
except TypeError as e:
if hasattr(obj, '__call__') and hasattr(obj, '__name__'):
return obj.__name__
raise e
class sibisLogging(object):
"""
SIBIS Logging Module
"""
def __init__(self,config_file=None):
# self.logging = logging
# all call that are info or above will be printed out
# self.logging.basicConfig(level=logging.INFO, format='%(message)s')
self.log = collections.OrderedDict()
# Configure file to be used when positing things to github
self.postGithubConfigFile = config_file
self.postGithubRepo = None
self.postGithubTitle = None
self.postGithubLabel = None
self.startTime1=None
self.startTime2=None
self.fileTime=None
self.verbose = False
def create_log(self,uid, message, **kwargs):
# Turn message into a ordered dictionary
self.log.update(experiment_site_id=uid,
error=message)
self.log.update(kwargs)
jlog = json.dumps(self.log, cls=sibisJSONEncoder)
self.log.clear()
return str(jlog)
def info(self, uid, message, **kwargs):
"""
Replaces logging.info
if postGithubRepo is defined then posts it to github instead of logger
"""
jlog = self.create_log(uid, message, **kwargs)
if self.postGithubRepo :
if self.verbose:
print("Posting", uid, str(message))
return pig.create_issues_from_list(self.postGithubRepo, self.postGithubTitle, self.postGithubLabel, [jlog],self.verbose)
# Post output to logger
# return self.logging.info(log)
print(jlog)
return []
def post_to_github(self,general_title,git_label):
if self.verbose:
print("================================")
print("== Setting up posting to GitHub ")
# Create Connection
if not self.postGithubRepo :
self.postGithubRepo = pig.connect_to_github(config_file=self.postGithubConfigFile,verbose=self.verbose)
# Make sure it is a valid title
if not self.postGithubRepo:
return False
self.postGithubLabel=pig.get_github_label(self.postGithubRepo, git_label, self.verbose)
if not self.postGithubLabel :
print("Warning: Github label does not exist so not creating issues on github!")
self.postGithubRepo = None
return False
self.postGithubTitle=general_title + " (" + git_label + ")"
if self.verbose:
print("== Posting to GitHub is ready ")
print("================================")
return True
def initiateTimer(self,timerFile):
timerDir = os.path.dirname(timerFile)
if not os.path.exists(timerDir):
self.info("sibislogger.py","Error: Directory " + timerDir + " does not exist - timer disabled")
else :
self.fileTime = timerFile
def startTimer1(self):
if self.fileTime :
self.startTime1 = time.time()
def startTimer2(self):
if self.fileTime :
self.startTime2 = time.time()
def _stopTimerGeneral(self,timerID,label=None,info=None):
if not self.fileTime :
return
if timerID == 1:
startTimer= self.startTime1
else :
startTimer= self.startTime2
if not startTimer:
return
endTimer=time.time()
time_date_format = '%Y-%m-%d %H:%M:%S'
time_diff = int(1000*(endTimer - startTimer))
time_diff_sec = int(old_div(time_diff, 1000))
timeLine= ','.join([time.strftime(time_date_format,time.localtime(startTimer)),time.strftime(time_date_format,time.localtime(endTimer)),str(old_div(time_diff_sec,60)) + ":" + str(time_diff_sec%60).zfill(2) + ":" + str(time_diff%1000).zfill(3)])
timeLine += ','
if label :
timeLine += str(label)
timeLine += ','
if info :
timeLine += '"' + str(info) + '"'
try :
if os.path.isfile(self.fileTime) :
fd = open(self.fileTime,'a')
else :
fd = open(self.fileTime,'w')
fd.write("start-time,end-time,difference-min:sec:msec,label,extra-info\n")
fd.write(timeLine +'\n')
fd.close()
except Exception as err_msg:
self.info('sibislogger','Error: Failed to write to time stamp to file',
fileTime=self.fileTime,
err_msg=str(err_msg))
def takeTimer1(self,label=None,info=None):
self._stopTimerGeneral(1,label,info)
def takeTimer2(self,label=None,info=None):
self._stopTimerGeneral(2,label,info)
#
# Specifically to raise an error that then can be easily posted to slgo.info !
#
class sibisExecutionError(Exception):
def __init__(self, uid, msg, **kwargs):
self.uid = uid
self.msg = msg
self.info =kwargs
def add(self, **kwargs):
self.info.update(kwargs)
def slog_post(self):
if self.info :
log.info(self.uid, self.msg, **self.info)
else :
log.info(self.uid, self.msg)
def __str__(self):
if self.info :
return str(log.create_log(self.uid, self.msg, **self.info))
else :
return str(log.create_log(self.uid, self.msg))
def init_log(verbose=False,post_to_github=False,github_issue_title="",github_issue_label="",timerDir=None):
global log
log = sibisLogging()
log.verbose = verbose
if post_to_github:
log.post_to_github(github_issue_title, github_issue_label)
if timerDir :
log.initiateTimer(os.path.join(timerDir,github_issue_label + "-time_log.csv"))
# if this fails bc it cannot find log, please make sure init_log is called first
def info(uid, message, **kwargs):
return log.info(uid,message,**kwargs)
def startTimer1():
log.startTimer1()
def startTimer2():
log.startTimer2()
def takeTimer1(label=None,info=None):
log.takeTimer1(label,info)
def takeTimer2(label=None,info=None):
log.takeTimer2(label,info)