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