class LogAnalyzer(Analyzer): """ A simple analyzer of log events """ def __init__(self, listener_callback): Analyzer.__init__(self) # a message dump has no analyzer in from/to_list # it only has a single callback for the source self.msg_logs = [] # in-memory message log self.src = OfflineReplayer() self.set_source(self.src) self.add_source_callback(self.__dump_message) self.listener_callback = listener_callback self.supported_types = set(dm_collector_c.log_packet_types) for st in self.supported_types: self.src.enable_log(st) # def AnalyzeFile(self, fileName,selectedTypes): # self.selectedTypes = selectedTypes # self.msg_logs = [] # self.src.set_input_path(fileName) # self.src.run() # if self.listener_callback: # self.listener_callback() def AnalyzeFile(self, Paths, selectedTypes): if Paths.__class__.__name__ != 'list': Paths = [Paths] self.selectedTypes = selectedTypes self.msg_logs = [] for fileName in Paths: self.src.set_input_path(fileName) self.src.run() if self.listener_callback: self.listener_callback() else: self.log_warning("no listener_callback ...") def __dump_message(self, msg): """ Print the received message :param msg: the received message """ if self.selectedTypes \ and msg.type_id not in self.selectedTypes: return payload = msg.data.decode_xml() timestamp_beg = payload.find("timestamp\">") + 11 timestamp_end = payload.find("</", timestamp_beg) msg_timestamp = payload[timestamp_beg:timestamp_end] self.msg_logs.append({ 'Timestamp': msg_timestamp, 'TypeID': msg.type_id, 'Payload': payload })
#!/usr/bin/python # Filename: offline-analysis-filtering.py import os import sys """ Offline analysis: save the log as a new one with pre-defined filter """ # Import MobileInsight modules from mobile_insight.monitor import OfflineReplayer if __name__ == "__main__": # Initialize a 3G/4G monitor src = OfflineReplayer() src.set_input_path("./offline_log_example.mi2log") # Configure the log to be saved src.enable_log("LTE_NAS_ESM_OTA_Incoming_Packet") src.enable_log("LTE_RRC_Serv_Cell_Info") src.enable_log("LTE_RRC_OTA_Packet") src.enable_log("WCDMA_RRC_Serv_Cell_Info") src.enable_log("WCDMA_RRC_OTA_Packet") # Save log as src.save_log_as("./filtered_log.mi2log") # Start the monitoring src.run()