[8] | 1 |
|
---|
[919] | 2 | from .fs import sendMessage
|
---|
| 3 | from . import const
|
---|
| 4 | from . import util
|
---|
[8] | 5 |
|
---|
| 6 | import sys
|
---|
| 7 | import time
|
---|
[345] | 8 | import bisect
|
---|
[921] | 9 | from functools import total_ordering
|
---|
[8] | 10 |
|
---|
| 11 | #--------------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
[298] | 13 | ## @package mlx.logger
|
---|
| 14 | #
|
---|
| 15 | # The module for the logger.
|
---|
| 16 | #
|
---|
| 17 | # While the program itself is "logger", it contains an internal logger, which
|
---|
| 18 | # maintains the textual log containing information on the various events and is
|
---|
| 19 | # the reason why the program is called "logger".
|
---|
| 20 | #
|
---|
| 21 | # The log is made up of lines containing an optional timestamp and the text of
|
---|
| 22 | # the message. A line can be updated after having been put into the log by
|
---|
| 23 | # referring to its index.
|
---|
| 24 | #
|
---|
| 25 | # The logger object also maintains a separate set of faults and ensures that
|
---|
| 26 | # one fault type has only one score, even if that fault has been reported
|
---|
| 27 | # multiple times.
|
---|
| 28 |
|
---|
| 29 | #--------------------------------------------------------------------------------------
|
---|
| 30 |
|
---|
[8] | 31 | class Logger(object):
|
---|
[345] | 32 | """The class with the interface to log the various events.
|
---|
| 33 |
|
---|
| 34 | It contains a list of entries ordered by their timestamps and their ever
|
---|
| 35 | increasing IDs."""
|
---|
| 36 |
|
---|
[921] | 37 | @total_ordering
|
---|
[345] | 38 | class Entry(object):
|
---|
| 39 | """An entry in the log."""
|
---|
| 40 |
|
---|
| 41 | # The ID of the next entry to be created
|
---|
| 42 | _nextID = 1
|
---|
| 43 |
|
---|
| 44 | def __init__(self, timestamp, text, showTimestamp = True,
|
---|
| 45 | faultID = None, faultScore = 0, id = None):
|
---|
| 46 | """Construct the entry."""
|
---|
| 47 | if id is None:
|
---|
| 48 | self._id = self._nextID
|
---|
| 49 | Logger.Entry._nextID += 1
|
---|
| 50 | else:
|
---|
| 51 | self._id = id
|
---|
| 52 |
|
---|
| 53 | self._timestamp = timestamp
|
---|
| 54 | self._text = text
|
---|
| 55 | self._showTimestamp = showTimestamp
|
---|
| 56 |
|
---|
| 57 | self._faultID = faultID
|
---|
| 58 | self._faultScore = faultScore
|
---|
| 59 |
|
---|
| 60 | @property
|
---|
| 61 | def id(self):
|
---|
| 62 | """Get the ID of the entry."""
|
---|
| 63 | return self._id
|
---|
| 64 |
|
---|
| 65 | @property
|
---|
| 66 | def timestamp(self):
|
---|
| 67 | """Get the timestamp of this entry."""
|
---|
| 68 | return self._timestamp
|
---|
| 69 |
|
---|
| 70 | @property
|
---|
| 71 | def timestampString(self):
|
---|
| 72 | """Get the timestamp string of this entry.
|
---|
| 73 |
|
---|
| 74 | It returns None, if the timestamp of the entry is not visible."""
|
---|
| 75 | return util.getTimestampString(self._timestamp) \
|
---|
| 76 | if self._showTimestamp else None
|
---|
| 77 |
|
---|
| 78 | @property
|
---|
| 79 | def text(self):
|
---|
| 80 | """Get the text of this entry."""
|
---|
| 81 | return self._text
|
---|
| 82 |
|
---|
| 83 | @property
|
---|
| 84 | def isFault(self):
|
---|
| 85 | """Determine if this is a log entry about a fault."""
|
---|
| 86 | return self._faultID is not None
|
---|
| 87 |
|
---|
| 88 | @property
|
---|
| 89 | def faultID(self):
|
---|
| 90 | """Get the fault ID of the entry.
|
---|
| 91 |
|
---|
| 92 | It may be None, if the entry is not a fault entry."""
|
---|
| 93 | return self._faultID
|
---|
| 94 |
|
---|
| 95 | @property
|
---|
| 96 | def faultScore(self):
|
---|
| 97 | """Get the fault score of the entry, if it is a fault."""
|
---|
| 98 | return self._faultScore
|
---|
| 99 |
|
---|
[346] | 100 | def copy(self, timestamp = None, clearTimestamp = False, text = None,
|
---|
[349] | 101 | faultID = None, faultScore = None, clearFault = False):
|
---|
[345] | 102 | """Create a copy of this entry with the given values changed."""
|
---|
[346] | 103 | return Logger.Entry(None if clearTimestamp
|
---|
| 104 | else self._timestamp if timestamp is None
|
---|
| 105 | else timestamp,
|
---|
| 106 |
|
---|
[345] | 107 | self._text if text is None else text,
|
---|
[346] | 108 |
|
---|
[345] | 109 | showTimestamp = self._showTimestamp,
|
---|
[346] | 110 |
|
---|
[349] | 111 | faultID =
|
---|
| 112 | None if clearFault
|
---|
| 113 | else self._faultID if faultID is None
|
---|
| 114 | else faultID,
|
---|
[346] | 115 |
|
---|
| 116 | faultScore =
|
---|
[349] | 117 | None if clearFault
|
---|
| 118 | else self._faultScore if faultScore is None
|
---|
[346] | 119 | else faultScore,
|
---|
| 120 |
|
---|
[345] | 121 | id = self._id)
|
---|
| 122 |
|
---|
[921] | 123 | def __eq__(self, other):
|
---|
| 124 | """Equality comparison"""
|
---|
| 125 | return self._timestamp == other.timestamp and \
|
---|
| 126 | self._id == other._id
|
---|
[345] | 127 |
|
---|
[921] | 128 | def __ne__(self, other):
|
---|
| 129 | """Non-equality comparison"""
|
---|
| 130 | return self._timestamp != other.timestamp or \
|
---|
| 131 | self._id != other._id
|
---|
| 132 |
|
---|
| 133 | def __lt__(self, other):
|
---|
| 134 | """Less-than comparison"""
|
---|
| 135 | return self._timestamp < other.timestamp or \
|
---|
| 136 | (self._timestamp == other.timestamp and
|
---|
| 137 | self._id < other._id)
|
---|
[345] | 138 |
|
---|
| 139 | class Fault(object):
|
---|
| 140 | """Information about a fault.
|
---|
| 141 |
|
---|
| 142 | It contains the list of log entries that belong to this fault. The list
|
---|
| 143 | is ordered so that the first element contains the entry with the
|
---|
| 144 | highest score, so that it should be easy to find the actual score."""
|
---|
| 145 | def __init__(self, entry):
|
---|
| 146 | """Construct the fault info with the given log entry as its only
|
---|
| 147 | one."""
|
---|
| 148 | self._entries = [entry]
|
---|
| 149 |
|
---|
| 150 | @property
|
---|
| 151 | def score(self):
|
---|
| 152 | """Get the score of this fault, i.e. the score of the entry with
|
---|
| 153 | the highest score."""
|
---|
| 154 | return self._entries[0].faultScore if self._entries else 0
|
---|
| 155 |
|
---|
| 156 | def addEntry(self, entry):
|
---|
| 157 | """Add an entry to this fault.
|
---|
| 158 |
|
---|
| 159 | The entries will be sorted."""
|
---|
| 160 | entries = self._entries
|
---|
| 161 | entries.append(entry)
|
---|
[400] | 162 | entries.sort(key = lambda entry: entry.faultScore, reverse = True)
|
---|
[345] | 163 |
|
---|
| 164 | def removeEntry(self, entry):
|
---|
| 165 | """Remove the given entry.
|
---|
| 166 |
|
---|
| 167 | Returns True if at least one entry remains, False otherwise."""
|
---|
| 168 | entries = self._entries
|
---|
| 169 | for index in range(0, len(entries)):
|
---|
| 170 | if entry is entries[index]:
|
---|
| 171 | del entries[index]
|
---|
| 172 | break
|
---|
| 173 |
|
---|
| 174 | return len(entries)>0
|
---|
| 175 |
|
---|
[346] | 176 | def getLatestEntry(self):
|
---|
| 177 | """Get the entry with the highest score."""
|
---|
| 178 | return self._entries[0]
|
---|
| 179 |
|
---|
[11] | 180 | # FIXME: shall we use const.stage2string() instead?
|
---|
[8] | 181 | _stages = { const.STAGE_BOARDING : "Boarding",
|
---|
| 182 | const.STAGE_PUSHANDTAXI : "Pushback and Taxi",
|
---|
| 183 | const.STAGE_TAKEOFF : "Takeoff",
|
---|
| 184 | const.STAGE_RTO : "RTO",
|
---|
| 185 | const.STAGE_CLIMB : "Climb",
|
---|
| 186 | const.STAGE_CRUISE : "Cruise",
|
---|
| 187 | const.STAGE_DESCENT : "Descent",
|
---|
| 188 | const.STAGE_LANDING : "Landing",
|
---|
| 189 | const.STAGE_TAXIAFTERLAND : "Taxi",
|
---|
| 190 | const.STAGE_PARKING : "Parking",
|
---|
| 191 | const.STAGE_GOAROUND : "Go-Around",
|
---|
| 192 | const.STAGE_END : "End" }
|
---|
[345] | 193 |
|
---|
[30] | 194 | NO_GO_SCORE = 10000
|
---|
[11] | 195 |
|
---|
[634] | 196 | NO_SCORE = 9999
|
---|
| 197 |
|
---|
[96] | 198 | def __init__(self, output):
|
---|
[8] | 199 | """Construct the logger."""
|
---|
[345] | 200 | self._entries = {}
|
---|
[96] | 201 | self._lines = []
|
---|
[345] | 202 |
|
---|
[11] | 203 | self._faults = {}
|
---|
[345] | 204 |
|
---|
[8] | 205 | self._output = output
|
---|
| 206 |
|
---|
[97] | 207 | @property
|
---|
| 208 | def lines(self):
|
---|
| 209 | """Get the lines of the log."""
|
---|
[345] | 210 | return [(entry.timestampString, entry.text) for entry in self._lines]
|
---|
[97] | 211 |
|
---|
| 212 | @property
|
---|
| 213 | def faultLineIndexes(self):
|
---|
[345] | 214 | """Get the sorted array of the indexes of those log lines that contain
|
---|
| 215 | a fault."""
|
---|
| 216 | faultLineIndexes = []
|
---|
| 217 | lines = self._lines
|
---|
| 218 | for index in range(0, len(lines)):
|
---|
| 219 | if lines[index].isFault:
|
---|
| 220 | faultLineIndexes.append(index)
|
---|
| 221 | return faultLineIndexes
|
---|
[24] | 222 |
|
---|
| 223 | def reset(self):
|
---|
| 224 | """Reset the logger.
|
---|
| 225 |
|
---|
| 226 | The faults logged so far will be cleared."""
|
---|
[345] | 227 | self._entries = {}
|
---|
[96] | 228 | self._lines = []
|
---|
[345] | 229 | self._faults = {}
|
---|
| 230 |
|
---|
| 231 | def message(self, timestamp, msg):
|
---|
| 232 | """Put a simple textual message into the log with the given timestamp.
|
---|
[8] | 233 |
|
---|
[345] | 234 | Returns an ID of the message so that it could be referred to later."""
|
---|
| 235 | return self._addEntry(Logger.Entry(timestamp, msg))
|
---|
| 236 |
|
---|
| 237 | def untimedMessage(self, msg):
|
---|
[17] | 238 | """Put an untimed message into the log."""
|
---|
[345] | 239 | timestamp = self._lines[-1].timestamp if self._lines else 0
|
---|
| 240 | return self._addEntry(Logger.Entry(timestamp, msg,
|
---|
| 241 | showTimestamp = False))
|
---|
[17] | 242 |
|
---|
[9] | 243 | def debug(self, msg):
|
---|
[8] | 244 | """Log a debug message."""
|
---|
[919] | 245 | print("[DEBUG]", msg)
|
---|
[8] | 246 |
|
---|
| 247 | def stage(self, timestamp, stage):
|
---|
| 248 | """Report a change in the flight stage."""
|
---|
| 249 | s = Logger._stages[stage] if stage in Logger._stages else "<Unknown>"
|
---|
| 250 | self.message(timestamp, "--- %s ---" % (s,))
|
---|
[24] | 251 | if stage==const.STAGE_END:
|
---|
[31] | 252 | self.untimedMessage("Rating: %.0f" % (self.getRating(),))
|
---|
[134] | 253 | else:
|
---|
[315] | 254 | messageType = \
|
---|
| 255 | const.MESSAGETYPE_INFLIGHT if stage in \
|
---|
| 256 | [const.STAGE_CLIMB, const.STAGE_CRUISE, \
|
---|
| 257 | const.STAGE_DESCENT, const.STAGE_LANDING] \
|
---|
| 258 | else const.MESSAGETYPE_INFORMATION
|
---|
| 259 | sendMessage(messageType, "Flight stage: " + s, 3)
|
---|
[345] | 260 |
|
---|
[346] | 261 | def fault(self, faultID, timestamp, what, score,
|
---|
[349] | 262 | updatePrevious = False, updateID = None):
|
---|
[11] | 263 | """Report a fault.
|
---|
[8] | 264 |
|
---|
[11] | 265 | faultID as a unique ID for the given kind of fault. If another fault of
|
---|
| 266 | this ID has been reported earlier, it will be reported again only if
|
---|
| 267 | the score is greater than last time. This ID can be, e.g. the checker
|
---|
[345] | 268 | the report comes from.
|
---|
| 269 |
|
---|
[346] | 270 | If updatePrevious is True, and an instance of the given fault is
|
---|
| 271 | already in the log, only that instance will be updated with the new
|
---|
| 272 | timestamp and score. If there are several instances, the latest one
|
---|
| 273 | (with the highest score) will be updated. If updatePrevious is True,
|
---|
| 274 | and the new score is not greater than the latest one, the ID of the
|
---|
| 275 | latest one is returned.
|
---|
| 276 |
|
---|
[349] | 277 | If updateID is given, the log entry with the given ID will be
|
---|
| 278 | 'upgraded' to be a fault with the given data.
|
---|
| 279 |
|
---|
[345] | 280 | Returns an ID of the fault, or -1 if it was not logged."""
|
---|
[346] | 281 | fault = self._faults[faultID] if faultID in self._faults else None
|
---|
| 282 |
|
---|
[634] | 283 | text = "%s (NO GO)" % (what) if score==Logger.NO_GO_SCORE \
|
---|
| 284 | else "%s" % (what,) if score==Logger.NO_SCORE \
|
---|
| 285 | else "%s (%.1f)" % (what, score)
|
---|
| 286 |
|
---|
| 287 | if score==Logger.NO_SCORE:
|
---|
| 288 | score = 0
|
---|
| 289 |
|
---|
[346] | 290 | if fault is not None and score<=fault.score:
|
---|
| 291 | return fault.getLatestEntry().id if updatePrevious else -1
|
---|
[345] | 292 |
|
---|
[346] | 293 | if updatePrevious and fault is not None:
|
---|
| 294 | latestEntry = fault.getLatestEntry()
|
---|
| 295 | id = latestEntry.id
|
---|
| 296 | newEntry = latestEntry.copy(timestamp = timestamp,
|
---|
| 297 | text = text,
|
---|
| 298 | faultScore = score)
|
---|
| 299 | self._updateEntry(id, newEntry)
|
---|
[604] | 300 | if latestEntry.isFault:
|
---|
| 301 | self._output.updateFault(id, newEntry.timestampString, text)
|
---|
| 302 | else:
|
---|
| 303 | self._output.addFault(id, newEntry.timestampString, text)
|
---|
[349] | 304 | elif updateID is not None:
|
---|
| 305 | id = updateID
|
---|
[604] | 306 | oldEntry = self._entries[id]
|
---|
| 307 | newEntry = oldEntry.copy(timestamp = timestamp,
|
---|
| 308 | text = text, faultID = faultID,
|
---|
| 309 | faultScore = score)
|
---|
[349] | 310 | self._updateEntry(id, newEntry)
|
---|
[604] | 311 | if oldEntry.isFault:
|
---|
| 312 | self._output.updateFault(id, newEntry.timestampString, text)
|
---|
| 313 | else:
|
---|
| 314 | self._output.addFault(id, newEntry.timestampString, text)
|
---|
[346] | 315 | else:
|
---|
[604] | 316 | entry = Logger.Entry(timestamp, text, faultID = faultID,
|
---|
| 317 | faultScore = score)
|
---|
| 318 | id = self._addEntry(entry)
|
---|
| 319 | self._output.addFault(id, entry.timestampString, text)
|
---|
[345] | 320 |
|
---|
[349] | 321 | if updateID is None:
|
---|
| 322 | (messageType, duration) = (const.MESSAGETYPE_NOGO, 10) \
|
---|
| 323 | if score==Logger.NO_GO_SCORE \
|
---|
| 324 | else (const.MESSAGETYPE_FAULT, 5)
|
---|
| 325 | sendMessage(messageType, text, duration)
|
---|
[345] | 326 |
|
---|
| 327 | return id
|
---|
[11] | 328 |
|
---|
[30] | 329 | def noGo(self, faultID, timestamp, what):
|
---|
[8] | 330 | """Report a No-Go fault."""
|
---|
[345] | 331 | return self.fault(faultID, timestamp, what, Logger.NO_GO_SCORE)
|
---|
[8] | 332 |
|
---|
[31] | 333 | def getRating(self):
|
---|
| 334 | """Get the rating of the flight so far."""
|
---|
[30] | 335 | totalScore = 100
|
---|
[919] | 336 | for fault in self._faults.values():
|
---|
[345] | 337 | score = fault.score
|
---|
[30] | 338 | if score==Logger.NO_GO_SCORE:
|
---|
| 339 | return -score
|
---|
| 340 | else:
|
---|
| 341 | totalScore -= score
|
---|
| 342 | return totalScore
|
---|
[96] | 343 |
|
---|
[345] | 344 | def updateLine(self, id, line):
|
---|
| 345 | """Update the line with the given ID with the given string.
|
---|
| 346 |
|
---|
| 347 | Note, that it does not change the status of the line as a fault!"""
|
---|
| 348 | self._updateEntry(id, self._entries[id].copy(text = line))
|
---|
| 349 |
|
---|
[349] | 350 | def clearFault(self, id, text):
|
---|
| 351 | """Update the line with the given ID to contain the given string,
|
---|
| 352 | and clear its fault state."""
|
---|
| 353 | newEntry = self._entries[id].copy(text = text, clearFault = True)
|
---|
| 354 | self._updateEntry(id, newEntry)
|
---|
[604] | 355 | self._output.clearFault(id)
|
---|
[349] | 356 |
|
---|
[345] | 357 | def _addEntry(self, entry):
|
---|
| 358 | """Add the given entry to the log.
|
---|
| 359 |
|
---|
| 360 | @return the ID of the new entry."""
|
---|
| 361 | assert entry.id not in self._entries
|
---|
| 362 |
|
---|
| 363 | self._entries[entry.id] = entry
|
---|
| 364 |
|
---|
| 365 | if not self._lines or entry>self._lines[-1]:
|
---|
| 366 | index = len(self._lines)
|
---|
| 367 | self._lines.append(entry)
|
---|
| 368 | else:
|
---|
| 369 | index = bisect.bisect_left(self._lines, entry)
|
---|
| 370 | self._lines.insert(index, entry)
|
---|
| 371 |
|
---|
| 372 | if entry.isFault:
|
---|
| 373 | self._addFault(entry)
|
---|
| 374 |
|
---|
| 375 | self._output.insertFlightLogLine(index, entry.timestampString,
|
---|
| 376 | entry.text, entry.isFault)
|
---|
| 377 |
|
---|
| 378 | return entry.id
|
---|
[96] | 379 |
|
---|
[345] | 380 | def _updateEntry(self, id, newEntry):
|
---|
| 381 | """Update the entry with the given ID from the given new entry."""
|
---|
| 382 | self._removeEntry(id)
|
---|
| 383 | self._addEntry(newEntry)
|
---|
| 384 |
|
---|
| 385 | def _removeEntry(self, id):
|
---|
| 386 | """Remove the entry with the given ID."""
|
---|
| 387 | assert id in self._entries
|
---|
| 388 |
|
---|
| 389 | entry = self._entries[id]
|
---|
| 390 | del self._entries[id]
|
---|
| 391 |
|
---|
| 392 | for index in range(len(self._lines)-1, -1, -1):
|
---|
| 393 | if self._lines[index] is entry:
|
---|
| 394 | break
|
---|
| 395 | del self._lines[index]
|
---|
| 396 |
|
---|
| 397 | if entry.isFault:
|
---|
| 398 | faultID = entry.faultID
|
---|
| 399 | fault = self._faults[faultID]
|
---|
| 400 | if not fault.removeEntry(entry):
|
---|
| 401 | del self._faults[faultID]
|
---|
| 402 |
|
---|
| 403 | self._output.removeFlightLogLine(index)
|
---|
| 404 |
|
---|
| 405 | def _addFault(self, entry):
|
---|
| 406 | """Add the given fault entry to the fault with the given ID."""
|
---|
| 407 | faultID = entry.faultID
|
---|
| 408 | if faultID in self._faults:
|
---|
| 409 | self._faults[faultID].addEntry(entry)
|
---|
| 410 | else:
|
---|
| 411 | self._faults[faultID] = Logger.Fault(entry)
|
---|
| 412 |
|
---|
[8] | 413 | #--------------------------------------------------------------------------------------
|
---|