-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloggingmodel.py
60 lines (50 loc) · 1.82 KB
/
loggingmodel.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
'''
This module defines the logging server system 'model'
code, sort of like the model of an MVC pattern of thinking
about the logging server system.
'''
# system modules
import datetime
class LoggingServerModel(object):
'''This class defines what data will be saved and available to
the logging system viewers and controllers.
'''
MAX_SIZE = 200
def __init__(self):
'''Constructor for the class, initializes the class level
variables.
'''
self._startTime = datetime.datetime.now()
self._logRecordsTotal = 0L
self._logrecords = []
def __iter__(self):
'''Provide a reverse iterator so logrecords are provided
in newest to oldest order.
'''
index = len(self._logrecords)
while index > 0:
index -= 1
yield self._logrecords[index]
def _getStartTime(self):
'''Get the time the logging server was started'''
return self._startTime.strftime("%Y-%m-%d %H:%M:%S")
starttime = property(_getStartTime)
def _getUpTime(self):
'''Get the current uptime of the logging server minus the
microseconds'''
diff = (datetime.datetime.now() - self._startTime).__str__()
return diff[:diff.find('.')]
uptime = property(_getUpTime)
def _getLogRecordsTotal(self):
return self._logRecordsTotal
logRecordsTotal = property(_getLogRecordsTotal)
def logRecordHandler(self, logrecord):
'''This method adds the logrecord to the sliding
window of logrecords coming into the logging server.
'''
logrecords = self._logrecords
logrecords.append(logrecord)
if len(logrecords) > LoggingServerModel.MAX_SIZE:
logrecords.pop(0)
self._logRecordsTotal += 1
model = LoggingServerModel()