1 | # Module for the logging.
|
---|
2 |
|
---|
3 | #--------------------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | import const
|
---|
6 | import util
|
---|
7 |
|
---|
8 | import sys
|
---|
9 | import time
|
---|
10 |
|
---|
11 | #--------------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | class Logger(object):
|
---|
14 | """The class with the interface to log the various events."""
|
---|
15 | # FIXME: shall we use const.stage2string() instead?
|
---|
16 | _stages = { const.STAGE_BOARDING : "Boarding",
|
---|
17 | const.STAGE_PUSHANDTAXI : "Pushback and Taxi",
|
---|
18 | const.STAGE_TAKEOFF : "Takeoff",
|
---|
19 | const.STAGE_RTO : "RTO",
|
---|
20 | const.STAGE_CLIMB : "Climb",
|
---|
21 | const.STAGE_CRUISE : "Cruise",
|
---|
22 | const.STAGE_DESCENT : "Descent",
|
---|
23 | const.STAGE_LANDING : "Landing",
|
---|
24 | const.STAGE_TAXIAFTERLAND : "Taxi",
|
---|
25 | const.STAGE_PARKING : "Parking",
|
---|
26 | const.STAGE_GOAROUND : "Go-Around",
|
---|
27 | const.STAGE_END : "End" }
|
---|
28 |
|
---|
29 | NO_GO_SCORE = 10000
|
---|
30 |
|
---|
31 | def __init__(self, output):
|
---|
32 | """Construct the logger."""
|
---|
33 | self._lines = []
|
---|
34 | self._faults = {}
|
---|
35 | self._faultLineIndexes = []
|
---|
36 | self._output = output
|
---|
37 |
|
---|
38 | @property
|
---|
39 | def lines(self):
|
---|
40 | """Get the lines of the log."""
|
---|
41 | return self._lines
|
---|
42 |
|
---|
43 | @property
|
---|
44 | def faultLineIndexes(self):
|
---|
45 | """Get the array of the indexes of the log line that contains a
|
---|
46 | fault."""
|
---|
47 | return self._faultLineIndexes
|
---|
48 |
|
---|
49 | def reset(self):
|
---|
50 | """Reset the logger.
|
---|
51 |
|
---|
52 | The faults logged so far will be cleared."""
|
---|
53 | self._lines = []
|
---|
54 | self._faults.clear()
|
---|
55 | self._faultLineIndexes = []
|
---|
56 |
|
---|
57 | def message(self, timestamp, msg):
|
---|
58 | """Put a simple textual message into the log with the given timestamp."""
|
---|
59 | timeStr = util.getTimestampString(timestamp)
|
---|
60 | return self._logLine(msg, timeStr)
|
---|
61 |
|
---|
62 | def untimedMessage(self, msg):
|
---|
63 | """Put an untimed message into the log."""
|
---|
64 | return self._logLine(msg)
|
---|
65 |
|
---|
66 | def debug(self, msg):
|
---|
67 | """Log a debug message."""
|
---|
68 | print "[DEBUG]", msg
|
---|
69 |
|
---|
70 | def stage(self, timestamp, stage):
|
---|
71 | """Report a change in the flight stage."""
|
---|
72 | s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
|
---|
73 | self.message(timestamp, "--- %s ---" % (s,))
|
---|
74 | if stage==const.STAGE_END:
|
---|
75 | self.untimedMessage("Rating: %.0f" % (self.getRating(),))
|
---|
76 |
|
---|
77 | def fault(self, faultID, timestamp, what, score):
|
---|
78 | """Report a fault.
|
---|
79 |
|
---|
80 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
81 | this ID has been reported earlier, it will be reported again only if
|
---|
82 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
83 | the report comes from."""
|
---|
84 | if faultID in self._faults:
|
---|
85 | if score<=self._faults[faultID]:
|
---|
86 | return
|
---|
87 | self._faults[faultID] = score
|
---|
88 | if score==Logger.NO_GO_SCORE:
|
---|
89 | lineIndex = self.message(timestamp, "%s (NO GO)" % (what))
|
---|
90 | else:
|
---|
91 | lineIndex = self.message(timestamp, "%s (%.1f)" % (what, score))
|
---|
92 | self._faultLineIndexes.append(lineIndex)
|
---|
93 |
|
---|
94 | def noGo(self, faultID, timestamp, what):
|
---|
95 | """Report a No-Go fault."""
|
---|
96 | self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
|
---|
97 |
|
---|
98 | def getRating(self):
|
---|
99 | """Get the rating of the flight so far."""
|
---|
100 | totalScore = 100
|
---|
101 | for (id, score) in self._faults.iteritems():
|
---|
102 | if score==Logger.NO_GO_SCORE:
|
---|
103 | return -score
|
---|
104 | else:
|
---|
105 | totalScore -= score
|
---|
106 | return totalScore
|
---|
107 |
|
---|
108 | def updateLine(self, index, line):
|
---|
109 | """Update the line at the given index with the given string."""
|
---|
110 | (timeStr, _line) = self._lines[index]
|
---|
111 | self._lines[index] = (timeStr, line)
|
---|
112 | self._output.updateFlightLogLine(index, timeStr, line)
|
---|
113 |
|
---|
114 | def _logLine(self, line, timeStr = None):
|
---|
115 | """Log the given line."""
|
---|
116 | index = len(self._lines)
|
---|
117 | self._lines.append((timeStr, line))
|
---|
118 | self._output.addFlightLogLine(timeStr, line)
|
---|
119 | return index
|
---|
120 |
|
---|
121 | #--------------------------------------------------------------------------------------
|
---|