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."""
|
---|
14 | _stages = { const.STAGE_BOARDING : "Boarding",
|
---|
15 | const.STAGE_PUSHANDTAXI : "Pushback and Taxi",
|
---|
16 | const.STAGE_TAKEOFF : "Takeoff",
|
---|
17 | const.STAGE_RTO : "RTO",
|
---|
18 | const.STAGE_CLIMB : "Climb",
|
---|
19 | const.STAGE_CRUISE : "Cruise",
|
---|
20 | const.STAGE_DESCENT : "Descent",
|
---|
21 | const.STAGE_LANDING : "Landing",
|
---|
22 | const.STAGE_TAXIAFTERLAND : "Taxi",
|
---|
23 | const.STAGE_PARKING : "Parking",
|
---|
24 | const.STAGE_GOAROUND : "Go-Around",
|
---|
25 | const.STAGE_END : "End" }
|
---|
26 |
|
---|
27 | def __init__(self, output = sys.stdout):
|
---|
28 | """Construct the logger."""
|
---|
29 | self._score = 100.0
|
---|
30 | self._output = output
|
---|
31 |
|
---|
32 | @staticmethod
|
---|
33 | def _getTimeStr(timestamp):
|
---|
34 | """Get the string representation of the given timestamp."""
|
---|
35 | return time.strftime("%H:%M:%S", time.gmtime(timestamp))
|
---|
36 |
|
---|
37 | def message(self, timestamp, msg):
|
---|
38 | """Put a simple textual message into the log with the given timestamp."""
|
---|
39 | timeStr = Logger._getTimeStr(timestamp)
|
---|
40 | print >> self._output, timeStr + ":", msg
|
---|
41 | print timeStr + ":", msg
|
---|
42 |
|
---|
43 | def debug(self, timestamp, msg):
|
---|
44 | """Log a debug message."""
|
---|
45 | timeStr = Logger._getTimeStr(timestamp)
|
---|
46 | print >> self._output, timeStr + ": [DEBUG] ", msg
|
---|
47 | print timeStr + ": [DEBUG]", msg
|
---|
48 |
|
---|
49 | def stage(self, timestamp, stage):
|
---|
50 | """Report a change in the flight stage."""
|
---|
51 | s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
|
---|
52 | self.message(timestamp, "--- %s ---" % (s,))
|
---|
53 |
|
---|
54 | def fault(self, timestamp, what, score):
|
---|
55 | """Report a fault."""
|
---|
56 | self._score -= score
|
---|
57 | self.message(timestamp, "%s (%f)" % (what, score))
|
---|
58 |
|
---|
59 | def noGo(self, timestamp, what):
|
---|
60 | """Report a No-Go fault."""
|
---|
61 | self._score = -1
|
---|
62 | self.message(timestamp, "%s (NO GO)" % (what,))
|
---|
63 |
|
---|
64 | #--------------------------------------------------------------------------------------
|
---|