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