|
| 1 | +""" |
| 2 | +Convert the raw log expoerted by our Quectel Debug Log Decoder to UELogViewer compatible format, |
| 3 | +so that one can import it into the UELogViewer or UEMonitor. |
| 4 | +Created: 2019-04-01 |
| 5 | +""" |
| 6 | +import csv |
| 7 | +import os |
| 8 | +import time |
| 9 | +from utils import get_file_list |
| 10 | + |
| 11 | +class RawLogFormatConvertor(object): |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.abs_work_dir = os.getcwd() |
| 15 | + self.output_dir = self.abs_work_dir + '/output_files/' |
| 16 | + self.dir_list = [] |
| 17 | + self.prepare_dirs() |
| 18 | + |
| 19 | + def prepare_dirs(self): |
| 20 | + temp_list = os.listdir(self.output_dir) |
| 21 | + self.dir_list = [x for x in temp_list if '.' not in x] # exclude the normal files |
| 22 | + # self.dir_list.sort() |
| 23 | + # print(self.dir_list) |
| 24 | + |
| 25 | + def convert_one_raw_file(self, folder_name): |
| 26 | + print('Project name:', folder_name) |
| 27 | + raw_log_file_name = self.output_dir + folder_name + '/debug_log_raw.txt' |
| 28 | + if os.path.exists(raw_log_file_name): |
| 29 | + debug_log_raw_uem = self.output_dir + folder_name + '/debug_log_raw_uem.txt' # export_file |
| 30 | + if os.path.exists(debug_log_raw_uem): |
| 31 | + print('Already exported. Pass.') |
| 32 | + return False # overwrite the existing UEM.txt files |
| 33 | + # The converted file does not exists, working on it. |
| 34 | + raw_log_file = open(raw_log_file_name, 'r') |
| 35 | + with open(debug_log_raw_uem, 'w', newline='') as f_out: |
| 36 | + for line in raw_log_file: |
| 37 | + if len(line) > 2000: |
| 38 | + print('Abnormal line', line[:100]) |
| 39 | + continue |
| 40 | + line_buf = line.split(',') |
| 41 | + # Assume it is UTC+8 time zone. Change accordingly |
| 42 | + seq_num = line_buf[0] |
| 43 | + seq_num_str = self.int_to_hex_bytes(int(seq_num)) |
| 44 | + # print(seq_num_str) |
| 45 | + try: |
| 46 | + second_frac = float(line_buf[1].split('.')[0]) |
| 47 | + except TypeError: |
| 48 | + print('Not a time stamp.') |
| 49 | + continue |
| 50 | + if second_frac < 1500000000: |
| 51 | + print('Error, continue') |
| 52 | + continue |
| 53 | + millisecond_frac = line_buf[1].split('.')[1] |
| 54 | + time_stamp = time.strftime(';%Y-%m-%dT%H:%M:%S', time.localtime(float(second_frac))) |
| 55 | + time_stamp += '.{0:.7s}+08:00;'.format(millisecond_frac) # Assume it is UTC+8 time zone. Change accordingly |
| 56 | + time_tick_hex = line_buf[2] |
| 57 | + time_tick_str = self.int_to_hex_bytes(int(time_tick_hex)) |
| 58 | + # msg = time_stamp + line_buf[3] |
| 59 | + byte_list = line_buf[3] |
| 60 | + if byte_list == 'APPLICATION_REPORT': |
| 61 | + byte_list = line_buf[4] |
| 62 | + byte_list = '00-00-A0-7F-00-00-' + self.process_app_rep_log(byte_list) |
| 63 | + # '00-00-A0-7F-00-00' is for APPLICATION_REPORT |
| 64 | + f_out.write(time_stamp + time_tick_str + seq_num_str + byte_list) |
| 65 | + f_out.flush() |
| 66 | + f_out.close() |
| 67 | + raw_log_file.close() |
| 68 | + return True |
| 69 | + else: |
| 70 | + return False |
| 71 | + |
| 72 | + def process_app_rep_log(self, byte_list): |
| 73 | + byte_count = (len(byte_list) + 1)//3 |
| 74 | + byte_count_hex = hex(byte_count)[2:].upper() |
| 75 | + prepend = '' |
| 76 | + if len(byte_count_hex) == 1: |
| 77 | + prepend = '0' + byte_count_hex + '-00-' |
| 78 | + elif len(byte_count_hex) == 2: |
| 79 | + prepend = byte_count_hex + '-00-' |
| 80 | + elif len(byte_count_hex) == 3: |
| 81 | + prepend = byte_count_hex[1:] + '-0' + byte_count_hex[1] + '-' |
| 82 | + else: |
| 83 | + print('[WARN] abnormal application report length.') |
| 84 | + return prepend + byte_list |
| 85 | + |
| 86 | + def int_to_hex_bytes(self, int_input): |
| 87 | + x = hex(int_input)[2:] |
| 88 | + # print(x) |
| 89 | + byte_list = [] |
| 90 | + for i in range(len(x)//2): |
| 91 | + byte_list.append(x[-2:].upper()) |
| 92 | + x = x[:-2] # remove the tail bytes |
| 93 | + if len(x) == 1: |
| 94 | + byte_list.append('0' + x) |
| 95 | + # else: |
| 96 | + # byte_list.append(x) |
| 97 | + # print(byte_list) |
| 98 | + while len(byte_list) < 4: |
| 99 | + byte_list.append('00') |
| 100 | + str_msg = '' |
| 101 | + for b in byte_list: |
| 102 | + str_msg += b + '-' |
| 103 | + # print(tick_msg) |
| 104 | + return str_msg |
| 105 | + |
| 106 | + def convert_all_files(self): |
| 107 | + success_list = [] |
| 108 | + for dir in self.dir_list: |
| 109 | + if self.convert_one_raw_file(dir) == 1: |
| 110 | + success_list.append(dir) |
| 111 | + print('Successfully convert the raw log in these folders:', success_list) |
| 112 | + print('# of converted files:', len(success_list)) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + my_rlfc = RawLogFormatConvertor() |
| 117 | + # my_rlfc.convert_one_raw_file('190329_233635') |
| 118 | + my_rlfc.convert_all_files() |
| 119 | + |
0 commit comments