[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 |
|
---|
[8] | 30 | def __init__(self, output = sys.stdout):
|
---|
| 31 | """Construct the logger."""
|
---|
[11] | 32 | self._faults = {}
|
---|
[8] | 33 | self._output = output
|
---|
| 34 |
|
---|
| 35 | @staticmethod
|
---|
| 36 | def _getTimeStr(timestamp):
|
---|
| 37 | """Get the string representation of the given timestamp."""
|
---|
| 38 | return time.strftime("%H:%M:%S", time.gmtime(timestamp))
|
---|
[24] | 39 |
|
---|
| 40 | def reset(self):
|
---|
| 41 | """Reset the logger.
|
---|
| 42 |
|
---|
| 43 | The faults logged so far will be cleared."""
|
---|
| 44 | self._faults.clear()
|
---|
[8] | 45 |
|
---|
| 46 | def message(self, timestamp, msg):
|
---|
| 47 | """Put a simple textual message into the log with the given timestamp."""
|
---|
| 48 | timeStr = Logger._getTimeStr(timestamp)
|
---|
| 49 | print >> self._output, timeStr + ":", msg
|
---|
| 50 |
|
---|
[17] | 51 | def untimedMessage(self, msg):
|
---|
| 52 | """Put an untimed message into the log."""
|
---|
| 53 | print >> self._output, msg
|
---|
| 54 |
|
---|
[9] | 55 | def debug(self, msg):
|
---|
[8] | 56 | """Log a debug message."""
|
---|
[9] | 57 | print "[DEBUG]", msg
|
---|
[8] | 58 |
|
---|
| 59 | def stage(self, timestamp, stage):
|
---|
| 60 | """Report a change in the flight stage."""
|
---|
| 61 | s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
|
---|
| 62 | self.message(timestamp, "--- %s ---" % (s,))
|
---|
[24] | 63 | if stage==const.STAGE_END:
|
---|
[31] | 64 | self.untimedMessage("Rating: %.0f" % (self.getRating(),))
|
---|
[8] | 65 |
|
---|
[11] | 66 | def fault(self, faultID, timestamp, what, score):
|
---|
| 67 | """Report a fault.
|
---|
[8] | 68 |
|
---|
[11] | 69 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
| 70 | this ID has been reported earlier, it will be reported again only if
|
---|
| 71 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
| 72 | the report comes from."""
|
---|
| 73 | if faultID in self._faults:
|
---|
| 74 | if score<=self._faults[faultID]:
|
---|
| 75 | return
|
---|
| 76 | self._faults[faultID] = score
|
---|
[30] | 77 | if score==Logger.NO_GO_SCORE:
|
---|
[11] | 78 | self.message(timestamp, "%s (NO GO)" % (what))
|
---|
| 79 | else:
|
---|
[21] | 80 | self.message(timestamp, "%s (%.1f)" % (what, score))
|
---|
[11] | 81 |
|
---|
[30] | 82 | def noGo(self, faultID, timestamp, what):
|
---|
[8] | 83 | """Report a No-Go fault."""
|
---|
[30] | 84 | self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
|
---|
[8] | 85 |
|
---|
[31] | 86 | def getRating(self):
|
---|
| 87 | """Get the rating of the flight so far."""
|
---|
[30] | 88 | totalScore = 100
|
---|
| 89 | for (id, score) in self._faults.iteritems():
|
---|
| 90 | if score==Logger.NO_GO_SCORE:
|
---|
| 91 | return -score
|
---|
| 92 | else:
|
---|
| 93 | totalScore -= score
|
---|
| 94 | return totalScore
|
---|
| 95 |
|
---|
[8] | 96 | #--------------------------------------------------------------------------------------
|
---|