1 |
|
---|
2 | from fs import sendMessage
|
---|
3 | import const
|
---|
4 | import util
|
---|
5 |
|
---|
6 | import sys
|
---|
7 | import 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 |
|
---|
29 | class 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 | messageType = \
|
---|
94 | const.MESSAGETYPE_INFLIGHT if stage in \
|
---|
95 | [const.STAGE_CLIMB, const.STAGE_CRUISE, \
|
---|
96 | const.STAGE_DESCENT, const.STAGE_LANDING] \
|
---|
97 | else const.MESSAGETYPE_INFORMATION
|
---|
98 | sendMessage(messageType, "Flight stage: " + s, 3)
|
---|
99 |
|
---|
100 | def fault(self, faultID, timestamp, what, score):
|
---|
101 | """Report a fault.
|
---|
102 |
|
---|
103 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
104 | this ID has been reported earlier, it will be reported again only if
|
---|
105 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
106 | the report comes from."""
|
---|
107 | if faultID in self._faults:
|
---|
108 | if score<=self._faults[faultID]:
|
---|
109 | return
|
---|
110 | self._faults[faultID] = score
|
---|
111 | text = "%s (NO GO)" % (what) if score==Logger.NO_GO_SCORE \
|
---|
112 | else "%s (%.1f)" % (what, score)
|
---|
113 | lineIndex = self.message(timestamp, text, isFault = True)
|
---|
114 | self._faultLineIndexes.append(lineIndex)
|
---|
115 | (messageType, duration) = (const.MESSAGETYPE_NOGO, 10) \
|
---|
116 | if score==Logger.NO_GO_SCORE \
|
---|
117 | else (const.MESSAGETYPE_FAULT, 5)
|
---|
118 | sendMessage(messageType, text, duration)
|
---|
119 |
|
---|
120 | def noGo(self, faultID, timestamp, what):
|
---|
121 | """Report a No-Go fault."""
|
---|
122 | self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
|
---|
123 |
|
---|
124 | def getRating(self):
|
---|
125 | """Get the rating of the flight so far."""
|
---|
126 | totalScore = 100
|
---|
127 | for (id, score) in self._faults.iteritems():
|
---|
128 | if score==Logger.NO_GO_SCORE:
|
---|
129 | return -score
|
---|
130 | else:
|
---|
131 | totalScore -= score
|
---|
132 | return totalScore
|
---|
133 |
|
---|
134 | def updateLine(self, index, line):
|
---|
135 | """Update the line at the given index with the given string."""
|
---|
136 | (timeStr, _line) = self._lines[index]
|
---|
137 | self._lines[index] = (timeStr, line)
|
---|
138 | self._output.updateFlightLogLine(index, timeStr, line)
|
---|
139 |
|
---|
140 | def _logLine(self, line, timeStr = None, isFault = False):
|
---|
141 | """Log the given line."""
|
---|
142 | index = len(self._lines)
|
---|
143 | self._lines.append((timeStr, line))
|
---|
144 | self._output.addFlightLogLine(timeStr, line, isFault)
|
---|
145 | return index
|
---|
146 |
|
---|
147 | #--------------------------------------------------------------------------------------
|
---|