source: src/mlx/logger.py@ 308:ce19a15a3152

Last change on this file since 308:ce19a15a3152 was 298:24c67ec5cdca, checked in by István Váradi <ivaradi@…>, 12 years ago

Documented the non-GUI modules

File size: 5.2 KB
Line 
1
2from fs import sendMessage
3import const
4import util
5
6import sys
7import time
8
9#--------------------------------------------------------------------------------------
10
11## @package mlx.logger
12#
13# The module for the logger.
14#
15# While the program itself is "logger", it contains an internal logger, which
16# maintains the textual log containing information on the various events and is
17# the reason why the program is called "logger".
18#
19# The log is made up of lines containing an optional timestamp and the text of
20# the message. A line can be updated after having been put into the log by
21# referring to its index.
22#
23# The logger object also maintains a separate set of faults and ensures that
24# one fault type has only one score, even if that fault has been reported
25# multiple times.
26
27#--------------------------------------------------------------------------------------
28
29class Logger(object):
30 """The class with the interface to log the various events."""
31 # FIXME: shall we use const.stage2string() instead?
32 _stages = { const.STAGE_BOARDING : "Boarding",
33 const.STAGE_PUSHANDTAXI : "Pushback and Taxi",
34 const.STAGE_TAKEOFF : "Takeoff",
35 const.STAGE_RTO : "RTO",
36 const.STAGE_CLIMB : "Climb",
37 const.STAGE_CRUISE : "Cruise",
38 const.STAGE_DESCENT : "Descent",
39 const.STAGE_LANDING : "Landing",
40 const.STAGE_TAXIAFTERLAND : "Taxi",
41 const.STAGE_PARKING : "Parking",
42 const.STAGE_GOAROUND : "Go-Around",
43 const.STAGE_END : "End" }
44
45 NO_GO_SCORE = 10000
46
47 def __init__(self, output):
48 """Construct the logger."""
49 self._lines = []
50 self._faults = {}
51 self._faultLineIndexes = []
52 self._output = output
53
54 @property
55 def lines(self):
56 """Get the lines of the log."""
57 return self._lines
58
59 @property
60 def faultLineIndexes(self):
61 """Get the array of the indexes of the log line that contains a
62 fault."""
63 return self._faultLineIndexes
64
65 def reset(self):
66 """Reset the logger.
67
68 The faults logged so far will be cleared."""
69 self._lines = []
70 self._faults.clear()
71 self._faultLineIndexes = []
72
73 def message(self, timestamp, msg, isFault = False):
74 """Put a simple textual message into the log with the given timestamp."""
75 timeStr = util.getTimestampString(timestamp)
76 return self._logLine(msg, timeStr, isFault = isFault)
77
78 def untimedMessage(self, msg, isFault = False):
79 """Put an untimed message into the log."""
80 return self._logLine(msg, isFault = isFault)
81
82 def debug(self, msg):
83 """Log a debug message."""
84 print "[DEBUG]", msg
85
86 def stage(self, timestamp, stage):
87 """Report a change in the flight stage."""
88 s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
89 self.message(timestamp, "--- %s ---" % (s,))
90 if stage==const.STAGE_END:
91 self.untimedMessage("Rating: %.0f" % (self.getRating(),))
92 else:
93 sendMessage(const.MESSAGETYPE_INFORMATION, "Flight stage: " + s, 3)
94
95 def fault(self, faultID, timestamp, what, score):
96 """Report a fault.
97
98 faultID as a unique ID for the given kind of fault. If another fault of
99 this ID has been reported earlier, it will be reported again only if
100 the score is greater than last time. This ID can be, e.g. the checker
101 the report comes from."""
102 if faultID in self._faults:
103 if score<=self._faults[faultID]:
104 return
105 self._faults[faultID] = score
106 text = "%s (NO GO)" % (what) if score==Logger.NO_GO_SCORE \
107 else "%s (%.1f)" % (what, score)
108 lineIndex = self.message(timestamp, text, isFault = True)
109 self._faultLineIndexes.append(lineIndex)
110 (messageType, duration) = (const.MESSAGETYPE_NOGO, 10) \
111 if score==Logger.NO_GO_SCORE \
112 else (const.MESSAGETYPE_FAULT, 5)
113 sendMessage(messageType, text, duration)
114
115 def noGo(self, faultID, timestamp, what):
116 """Report a No-Go fault."""
117 self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
118
119 def getRating(self):
120 """Get the rating of the flight so far."""
121 totalScore = 100
122 for (id, score) in self._faults.iteritems():
123 if score==Logger.NO_GO_SCORE:
124 return -score
125 else:
126 totalScore -= score
127 return totalScore
128
129 def updateLine(self, index, line):
130 """Update the line at the given index with the given string."""
131 (timeStr, _line) = self._lines[index]
132 self._lines[index] = (timeStr, line)
133 self._output.updateFlightLogLine(index, timeStr, line)
134
135 def _logLine(self, line, timeStr = None, isFault = False):
136 """Log the given line."""
137 index = len(self._lines)
138 self._lines.append((timeStr, line))
139 self._output.addFlightLogLine(timeStr, line, isFault)
140 return index
141
142#--------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.