全部博文(2005)
分类: Python/Ruby
2008-12-17 19:13:30
import os,sys,string,getopt,binascii,struct def format_string(key, str): return converted_string_lists[log_format][key][0](key, str) def parse_string(str): if dry_run_level: return str for key in converted_string_lists[log_format].keys(): if key in str: return format_string(key, str) return str def translate_step0(str, lineno): if bool(filter_watch_words): for subs in filter_watch_words: if subs in str: break subs = '' if not bool(subs): return else: for subs in filter_subs: if subs in str: return print "%d: %s" % (lineno, parse_string(str)), def get_next_line(): global lineno result = [] try: text = fd.next() except: return result result.append(lineno) lineno = lineno + 1 if log_format == 'kernel': if ']' not in text or \ '[' not in text or \ text.index(']') - text.index('[') != 13: return [-1, ' '] result.append(text[text.index(']')+2:]) else: result.append(text) return result def printfile(filename): global fd fd = file(filename) lineno = 1 while 1: result = get_next_line() if bool(result): if result[0] != -1: translate_step0(result[1], result[0]) else: break fd.close() def process_kernel_interrupt(key, str): return 'interrupt\n' def process_kernel_sdio_ireg(key, str): # return ':'.join(converted_string_lists[log_format][key][1:])+'\n' try: status = string.atoi(str[str.index(' = ')+3:], 16) except: return str switch = { 0x00:'none interrupt', 0x01:'U interrupt', 0x02:'D interrupt', 0x03:'U+D interrupt', } try: return switch[status] + '\n' except: return str def process_kernel_event(key, str): # return ':'.join(converted_string_lists[log_format][key][1:])+'\n' return str #typedef struct _PS_CMD_ConfirmSleep { # u16 Command; # u16 Size; # u16 SeqNum; # u16 Result; # u16 Action; # u16 Reserved1; # u16 MultipleDtim; # u16 Reserved; # u16 LocalListenInterval; #} __ATTRIB_PACK__ PS_CMD_ConfirmSleep, *PPS_CMD_ConfirmSleep; def process_kernel_sleepcfm(key, str): # data = key PS_CMD_ConfirmSleep = ( ('=', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',), ('Command', 'Size', 'SeqNum', 'Result', 'Action', 'Reserved1', 'MultipleDtim', 'Reserved', 'LocalListenInterval'), ) data = '' data = data + get_next_line()[1].strip() data = data + get_next_line()[1].strip() data = data.replace(' ','') data = binascii.a2b_hex(data) PS_CMD_ConfirmSleep_result = struct.unpack(''.join(PS_CMD_ConfirmSleep[0]), data) # return key, zip(PS_CMD_ConfirmSleep[1], PS_CMD_ConfirmSleep_result) # return key + data data = key + '\n' for i in range(len(PS_CMD_ConfirmSleep[1])): data = data + '\t%-20s=%x\n' % (PS_CMD_ConfirmSleep[1][i], PS_CMD_ConfirmSleep_result[i]) return data def process_kernel_int_status_reg(key, str): try: status = string.atoi(str[str.index('0x'):], 16) except: return str fmts = [] if status & 0x01: fmts.append('Rx upload ready') if status & 0x02: fmts.append('Tx download ready') if status & 0x04: fmts.append('Command download ready') if status & 0x08: fmts.append('Card event') if status & 0x10: fmts.append('Command upload ready') return '[' + ' : '.join(fmts) + ']\n' def process_kernel_tx(key, str): # data = key data = '' data = data + get_next_line()[1].strip() data = data + get_next_line()[1].strip() data = data + get_next_line()[1].strip() data = data.strip().replace(' ','') # data = binascii.a2b_hex(data) return key + data + '\n' log_format_default = 'plain' converted_string_lists = { 'kernel':{ '*\n' :[process_kernel_interrupt, 'luther', '1.0'], # * 'sdio_ireg = ' :[process_kernel_sdio_ireg, 'luther', '1.0'], # sdio_ireg = 0x3 'EVENT: ' :[process_kernel_event, 'luther', '1.0'], # EVENT: SLEEP 'INT: status = ' :[process_kernel_int_status_reg, 'luther', '1.0'], # INT: status = 0xb 'HOST_INT_STATUS_REG ' :[process_kernel_int_status_reg, 'luther', '1.0'], # HOST_INT_STATUS_REG 0xa 'SLEEP_CFM:' :[process_kernel_sleepcfm, 'luther', '1.0'], # SLEEP_CFM:\n 21 00 12 00 01 00 00 00 34 'Tx:' :[process_kernel_tx, 'luther', '1.0'], # 'TX Data:' :[process_kernel_tx, 'luther', '1.0'], # }, log_format_default:{ }, } filter_subs = [ 'Enter:', 'Leave:', 'Micco:', 'micco', 'main-thread', 'sbi_write_ioreg()', 'sbi_read_ioreg()', '*\n', 'state now.', 'GSENSOR', 'finger_', 'wake lock', 'alarm', 'brightness', 'cleartouch', 'android_power', 'Wakeup device...', 'save exit:', 'On Key', ] def Usage(): print ''' Options only support: -d level or --dry level -f filter_word or --filter -w watch_word or --watch -p or --print -r word or --remove --remove-all-filter-words --show-filters --log type 1.kernel 2.plain logfile.txt ''' fd = 0 lineno = 1 dry_run_level = 0 filter_watch_words = [] log_format = 'kernel' if __name__ == "__main__": if len(sys.argv) < 2: Usage() sys.exit() try: opts,args=getopt.gnu_getopt(sys.argv[1:],"d:f:w:pr:", ["dry=", "filter=", "watch=", "print", "remove=", "show-filters", "remove-all-filter-words", "log=",]) except: Usage() sys.exit() show = 0 for o,a in opts: if o in ('-d', '--dry'): try: dry_run_level = string.atoi(a) except: print o,a,'is invalide' sys.exit() elif o in ('-f', '--filter'): filter_subs.append(a) elif o in ('-w', '--watch'): filter_watch_words.append(a) elif o in ('-p', '--print'): print '\n'+'\n'.join(filter_subs) elif o in ('-r', '--remove'): try: filter_subs.remove(a) except: print '\ndoes not exist ', a elif o in ('--show-filters'): show = 1 elif o in ('--remove-all-filter-words'): filter_subs = [] elif o in ('--log'): log_format = a try: converted_string_lists[log_format] except: log_format = log_format_default if show: if bool(filter_subs): print '\n'+'\n'.join(filter_subs) else: print '\nfilter_subs is empty\n' for filename in args: printfile(filename) |