source: src/mlx/logger.py@ 133:dcbe33497899

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

The general message sending works and the most important messages are sent

File size: 4.5 KB
Line 
1# Module for the logging.
2
3#--------------------------------------------------------------------------------------
4
5from fs import sendMessage
6import const
7import util
8
9import sys
10import time
11
12#--------------------------------------------------------------------------------------
13
14class Logger(object):
15 """The class with the interface to log the various events."""
16 # FIXME: shall we use const.stage2string() instead?
17 _stages = { const.STAGE_BOARDING : "Boarding",
18 const.STAGE_PUSHANDTAXI : "Pushback and Taxi",
19 const.STAGE_TAKEOFF : "Takeoff",
20 const.STAGE_RTO : "RTO",
21 const.STAGE_CLIMB : "Climb",
22 const.STAGE_CRUISE : "Cruise",
23 const.STAGE_DESCENT : "Descent",
24 const.STAGE_LANDING : "Landing",
25 const.STAGE_TAXIAFTERLAND : "Taxi",
26 const.STAGE_PARKING : "Parking",
27 const.STAGE_GOAROUND : "Go-Around",
28 const.STAGE_END : "End" }
29
30 NO_GO_SCORE = 10000
31
32 def __init__(self, output):
33 """Construct the logger."""
34 self._lines = []
35 self._faults = {}
36 self._faultLineIndexes = []
37 self._output = output
38
39 @property
40 def lines(self):
41 """Get the lines of the log."""
42 return self._lines
43
44 @property
45 def faultLineIndexes(self):
46 """Get the array of the indexes of the log line that contains a
47 fault."""
48 return self._faultLineIndexes
49
50 def reset(self):
51 """Reset the logger.
52
53 The faults logged so far will be cleared."""
54 self._lines = []
55 self._faults.clear()
56 self._faultLineIndexes = []
57
58 def message(self, timestamp, msg):
59 """Put a simple textual message into the log with the given timestamp."""
60 timeStr = util.getTimestampString(timestamp)
61 return self._logLine(msg, timeStr)
62
63 def untimedMessage(self, msg):
64 """Put an untimed message into the log."""
65 return self._logLine(msg)
66
67 def debug(self, msg):
68 """Log a debug message."""
69 print "[DEBUG]", msg
70
71 def stage(self, timestamp, stage):
72 """Report a change in the flight stage."""
73 s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
74 self.message(timestamp, "--- %s ---" % (s,))
75 if stage==const.STAGE_END:
76 self.untimedMessage("Rating: %.0f" % (self.getRating(),))
77 sendMessage(const.MESSAGETYPE_INFORMATION, "Flight stage: " + s, 3)
78
79 def fault(self, faultID, timestamp, what, score):
80 """Report a fault.
81
82 faultID as a unique ID for the given kind of fault. If another fault of
83 this ID has been reported earlier, it will be reported again only if
84 the score is greater than last time. This ID can be, e.g. the checker
85 the report comes from."""
86 if faultID in self._faults:
87 if score<=self._faults[faultID]:
88 return
89 self._faults[faultID] = score
90 if score==Logger.NO_GO_SCORE:
91 text = "%s (NO GO)" % (what)
92 else:
93 text = "%s (%.1f)" % (what, score)
94 lineIndex = self.message(timestamp, "%s (NO GO)" % (what))
95 self._faultLineIndexes.append(lineIndex)
96 (messageType, duration) = (const.MESSAGETYPE_NOGO, 10) \
97 if score==Logger.NO_GO_SCORE \
98 else (const.MESSAGETYPE_FAULT, 5)
99 sendMessage(messageType, text, duration)
100
101 def noGo(self, faultID, timestamp, what):
102 """Report a No-Go fault."""
103 self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
104
105 def getRating(self):
106 """Get the rating of the flight so far."""
107 totalScore = 100
108 for (id, score) in self._faults.iteritems():
109 if score==Logger.NO_GO_SCORE:
110 return -score
111 else:
112 totalScore -= score
113 return totalScore
114
115 def updateLine(self, index, line):
116 """Update the line at the given index with the given string."""
117 (timeStr, _line) = self._lines[index]
118 self._lines[index] = (timeStr, line)
119 self._output.updateFlightLogLine(index, timeStr, line)
120
121 def _logLine(self, line, timeStr = None):
122 """Log the given line."""
123 index = len(self._lines)
124 self._lines.append((timeStr, line))
125 self._output.addFlightLogLine(timeStr, line)
126 return index
127
128#--------------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.