[8] | 1 | # Module for the logging.
|
---|
| 2 |
|
---|
| 3 | #--------------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
| 5 | import const
|
---|
[97] | 6 | import util
|
---|
[8] | 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."""
|
---|
[11] | 15 | # FIXME: shall we use const.stage2string() instead?
|
---|
[8] | 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" }
|
---|
[30] | 28 |
|
---|
| 29 | NO_GO_SCORE = 10000
|
---|
[11] | 30 |
|
---|
[96] | 31 | def __init__(self, output):
|
---|
[8] | 32 | """Construct the logger."""
|
---|
[96] | 33 | self._lines = []
|
---|
[11] | 34 | self._faults = {}
|
---|
[97] | 35 | self._faultLineIndexes = []
|
---|
[8] | 36 | self._output = output
|
---|
| 37 |
|
---|
[97] | 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
|
---|
[24] | 48 |
|
---|
| 49 | def reset(self):
|
---|
| 50 | """Reset the logger.
|
---|
| 51 |
|
---|
| 52 | The faults logged so far will be cleared."""
|
---|
[96] | 53 | self._lines = []
|
---|
[24] | 54 | self._faults.clear()
|
---|
[97] | 55 | self._faultLineIndexes = []
|
---|
[8] | 56 |
|
---|
| 57 | def message(self, timestamp, msg):
|
---|
| 58 | """Put a simple textual message into the log with the given timestamp."""
|
---|
[97] | 59 | timeStr = util.getTimestampString(timestamp)
|
---|
[96] | 60 | return self._logLine(msg, timeStr)
|
---|
[8] | 61 |
|
---|
[17] | 62 | def untimedMessage(self, msg):
|
---|
| 63 | """Put an untimed message into the log."""
|
---|
[96] | 64 | return self._logLine(msg)
|
---|
[17] | 65 |
|
---|
[9] | 66 | def debug(self, msg):
|
---|
[8] | 67 | """Log a debug message."""
|
---|
[9] | 68 | print "[DEBUG]", msg
|
---|
[8] | 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,))
|
---|
[24] | 74 | if stage==const.STAGE_END:
|
---|
[31] | 75 | self.untimedMessage("Rating: %.0f" % (self.getRating(),))
|
---|
[8] | 76 |
|
---|
[11] | 77 | def fault(self, faultID, timestamp, what, score):
|
---|
| 78 | """Report a fault.
|
---|
[8] | 79 |
|
---|
[11] | 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
|
---|
[30] | 88 | if score==Logger.NO_GO_SCORE:
|
---|
[97] | 89 | lineIndex = self.message(timestamp, "%s (NO GO)" % (what))
|
---|
[11] | 90 | else:
|
---|
[97] | 91 | lineIndex = self.message(timestamp, "%s (%.1f)" % (what, score))
|
---|
| 92 | self._faultLineIndexes.append(lineIndex)
|
---|
[11] | 93 |
|
---|
[30] | 94 | def noGo(self, faultID, timestamp, what):
|
---|
[8] | 95 | """Report a No-Go fault."""
|
---|
[30] | 96 | self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
|
---|
[8] | 97 |
|
---|
[31] | 98 | def getRating(self):
|
---|
| 99 | """Get the rating of the flight so far."""
|
---|
[30] | 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
|
---|
[96] | 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
|
---|
[30] | 120 |
|
---|
[8] | 121 | #--------------------------------------------------------------------------------------
|
---|