source: src/mlx/logger.py@ 66:d288a492befe

Last change on this file since 66:d288a492befe was 31:c9ed7a60bf3b, checked in by István Váradi <ivaradi@…>, 13 years ago

Various information about the flight is displayed in the status icon's tooltip or menu

File size: 3.5 KB
Line 
1# Module for the logging.
2
3#--------------------------------------------------------------------------------------
4
5import const
6
7import sys
8import time
9
10#--------------------------------------------------------------------------------------
11
12class Logger(object):
13 """The class with the interface to log the various events."""
14 # FIXME: shall we use const.stage2string() instead?
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
28 NO_GO_SCORE = 10000
29
30 def __init__(self, output = sys.stdout):
31 """Construct the logger."""
32 self._faults = {}
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 reset(self):
41 """Reset the logger.
42
43 The faults logged so far will be cleared."""
44 self._faults.clear()
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 print timeStr + ":", msg
51
52 def untimedMessage(self, msg):
53 """Put an untimed message into the log."""
54 print >> self._output, msg
55 print msg
56
57 def debug(self, msg):
58 """Log a debug message."""
59 print >> self._output, "[DEBUG]", msg
60 print "[DEBUG]", msg
61
62 def stage(self, timestamp, stage):
63 """Report a change in the flight stage."""
64 s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
65 self.message(timestamp, "--- %s ---" % (s,))
66 if stage==const.STAGE_END:
67 self.untimedMessage("Rating: %.0f" % (self.getRating(),))
68
69 def fault(self, faultID, timestamp, what, score):
70 """Report a fault.
71
72 faultID as a unique ID for the given kind of fault. If another fault of
73 this ID has been reported earlier, it will be reported again only if
74 the score is greater than last time. This ID can be, e.g. the checker
75 the report comes from."""
76 if faultID in self._faults:
77 if score<=self._faults[faultID]:
78 return
79 self._faults[faultID] = score
80 if score==Logger.NO_GO_SCORE:
81 self.message(timestamp, "%s (NO GO)" % (what))
82 else:
83 self.message(timestamp, "%s (%.1f)" % (what, score))
84
85 def noGo(self, faultID, timestamp, what):
86 """Report a No-Go fault."""
87 self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
88
89 def getRating(self):
90 """Get the rating of the flight so far."""
91 totalScore = 100
92 for (id, score) in self._faults.iteritems():
93 if score==Logger.NO_GO_SCORE:
94 return -score
95 else:
96 totalScore -= score
97 return totalScore
98
99#--------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.