source: src/mlx/logger.py@ 96:aa6a0b79c073

Last change on this file since 96:aa6a0b79c073 was 96:aa6a0b79c073, checked in by István Váradi <ivaradi@…>, 12 years ago

The contents of log lines can be modified after they are written, and we are using it for Vref

File size: 3.9 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):
31 """Construct the logger."""
32 self._lines = []
33 self._faults = {}
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))
40
41 def reset(self):
42 """Reset the logger.
43
44 The faults logged so far will be cleared."""
45 self._lines = []
46 self._faults.clear()
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)
51 return self._logLine(msg, timeStr)
52
53 def untimedMessage(self, msg):
54 """Put an untimed message into the log."""
55 return self._logLine(msg)
56
57 def debug(self, msg):
58 """Log a debug message."""
59 print "[DEBUG]", msg
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,))
65 if stage==const.STAGE_END:
66 self.untimedMessage("Rating: %.0f" % (self.getRating(),))
67
68 def fault(self, faultID, timestamp, what, score):
69 """Report a fault.
70
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
79 if score==Logger.NO_GO_SCORE:
80 self.message(timestamp, "%s (NO GO)" % (what))
81 else:
82 self.message(timestamp, "%s (%.1f)" % (what, score))
83
84 def noGo(self, faultID, timestamp, what):
85 """Report a No-Go fault."""
86 self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
87
88 def getRating(self):
89 """Get the rating of the flight so far."""
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
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
110
111#--------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.