[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" }
|
---|
| 27 |
|
---|
[11] | 28 | _noGoScore = 10000
|
---|
| 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))
|
---|
| 39 |
|
---|
| 40 | def message(self, timestamp, msg):
|
---|
| 41 | """Put a simple textual message into the log with the given timestamp."""
|
---|
| 42 | timeStr = Logger._getTimeStr(timestamp)
|
---|
| 43 | print >> self._output, timeStr + ":", msg
|
---|
| 44 | print timeStr + ":", msg
|
---|
| 45 |
|
---|
[9] | 46 | def debug(self, msg):
|
---|
[8] | 47 | """Log a debug message."""
|
---|
[9] | 48 | print >> self._output, "[DEBUG] ", msg
|
---|
| 49 | print "[DEBUG]", msg
|
---|
[8] | 50 |
|
---|
| 51 | def stage(self, timestamp, stage):
|
---|
| 52 | """Report a change in the flight stage."""
|
---|
| 53 | s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
|
---|
| 54 | self.message(timestamp, "--- %s ---" % (s,))
|
---|
| 55 |
|
---|
[11] | 56 | def fault(self, faultID, timestamp, what, score):
|
---|
| 57 | """Report a fault.
|
---|
[8] | 58 |
|
---|
[11] | 59 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
| 60 | this ID has been reported earlier, it will be reported again only if
|
---|
| 61 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
| 62 | the report comes from."""
|
---|
| 63 | if faultID in self._faults:
|
---|
| 64 | if score<=self._faults[faultID]:
|
---|
| 65 | return
|
---|
| 66 | self._faults[faultID] = score
|
---|
| 67 | if score==Logger._noGoScore:
|
---|
| 68 | self.message(timestamp, "%s (NO GO)" % (what))
|
---|
| 69 | else:
|
---|
| 70 | self.message(timestamp, "%s (%f)" % (what, score))
|
---|
| 71 |
|
---|
| 72 | def noGo(self, faultID, timestamp, what, shortReason):
|
---|
[8] | 73 | """Report a No-Go fault."""
|
---|
[11] | 74 | self.fault(faultID, timestamp, what, Logger._noGoScore)
|
---|
[8] | 75 |
|
---|
| 76 | #--------------------------------------------------------------------------------------
|
---|