forked from is210-fall-2106/is210-week-12-warmup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_03.py
44 lines (33 loc) · 1.09 KB
/
task_03.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Task 03 module"""
import time
class CustomLogger(object):
def __init__(self, logfilename): #Creates customlogger
self.logfilename = logfilename
self.msgs = []
def log(self, msg, timestamp=None): # logs changes
if timestamp is None:
timestamp = time.time()
self.msgs.append((timestamp, msg))
def flush(self): #Looks for errors and handles them
handled = []
try:
fhandler = open(self.logfilename, 'a')
except IOError:
self.log('Unable to open logfile.')
raise IOError
for index, entry in enumerate(self.msgs):
try:
fhandler.write(str(entry) + '\n')
handled.append(index)
except IOError:
self.log('Unable to write logfile')
handled.append('Error')
break
fhandler.close()
for index in handled[::-1]:
if handled[::-1] is 'Error':
pass
else:
del self.msgs[index]