《浅析python实现log日志文件加工初步源代码之2》《浅析python实现log日志文件加工初步源代码之3》import os,sys
filter_subs = [ 'Enter:', 'Leave:', 'Micco:', 'main-thread', 'sbi_write_ioreg()', 'sbi_read_ioreg()', ]
converted_string_lists = { 'sdio_ireg = ':'luther', 'sbi_interrupt: ':'gliethttp', }
def format_string(key,str): return converted_string_lists[key]+'\n' def parse_string(str): for key in converted_string_lists.keys(): if key in str: return format_string(key, str) return str
def tranlate_step0(str, lineno):
# index = str.find(']')
# if index < 0:
# return
# try: # index = str.index('] ') # except: # return if ']' not in str: return index = str.index(']')//index命令支持字符串匹配,比如str.index('abc')
str = str[index+2:]
# if bool(str1):
# print lineno,'\b: '+str1,
for subs in filter_subs:
# index = str.find(subs)
# if index >= 0:
if subs in str:
return
# print lineno,'\b: '+ parse_string(str),
print "%d: %s" % (lineno, parse_string(str)),
def readfile(filename):
'''Print a file to the standard output.'''
# print '==================%s : %s==================' % (filename, os.path.abspath(filename))
fd = file(filename)
lineno = 1
while True:
try:
text = fd.next()
except:
break
tranlate_step0(text, lineno)
lineno = lineno + 1
# print text, # comma is for ignoring '\n'
# while True:
# line = fd.readline()
# if len(line) == 0:
# break
# print line, # notice comma
fd.close()
if __name__ == "__main__":
# os.chdir(os.path.expanduser("~"))
if len(sys.argv) < 2:
print 'please use command: '
sys.exit()
# for argvs in sys.argv[:]:
# print argvs
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.0'
elif option == 'help':
print '''
Options include:
--version : Display version number
--help : Display help
python wpr.py filename : parse the file'''
else:
print 'Unknown option.'
sys.exit()
else:
for filename in sys.argv[1:]:
readfile(filename)
# print os.getcwd()
|
luther@gliethttp:~/python$ python wpr.py log.txt
4: sbi_interrupt: HOST_INT_STATUS_REG 0xa
7: EVENT: AWAKE
10: sbi_write_ioreg() priv=c550a360 func=1 reg=0x3 dat=0x0
19: *
22: sbi_read_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x1
24: sdio_ireg = 0x1
26: sbi_write_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x2
29: sbi_read_ioreg() priv=c550a360 func=1 reg=0x42 dat=0x1
34: sbi_interrupt: HOST_INT_STATUS_REG 0x8
37: EVENT: SLEEP
40: SLEEP_CFM:
41: 21 00 12 00 01 00 00 00 34 00 00 00 00 00 00 00
42: 00 00
51: *
54: sbi_read_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x3
56: sdio_ireg = 0x3
58: sbi_write_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x0
61: sbi_read_ioreg() priv=c550a360 func=1 reg=0x42 dat=0x1
66: sbi_interrupt: HOST_INT_STATUS_REG 0xa
68: *
71: sbi_read_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x1
73: sdio_ireg = 0x1
75: sbi_write_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x2
78: sbi_read_ioreg() priv=c550a360 func=1 reg=0x42 dat=0x1
83: sbi_interrupt: HOST_INT_STATUS_REG 0x8
86: EVENT: SLEEP
89: SLEEP_CFM:
90: 21 00 12 00 01 00 00 00 34 00 00 00 00 00 00 00
91: 00 00
99: *
......
2263: EVENT: SLEEP
2266: SLEEP_CFM:
2267: 21 00 12 00 01 00 00 00 34 00 00 00 00 00 00 00
2268: 00 00
2276: *
2280: sbi_read_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x3
2282: sdio_ireg = 0x3
2284: sbi_write_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x0
2287: sbi_read_ioreg() priv=c550a360 func=1 reg=0x42 dat=0x1
2292: sbi_interrupt: HOST_INT_STATUS_REG 0xa
2294: *
2297: sbi_read_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x1
2299: sdio_ireg = 0x1
2301: sbi_write_ioreg() priv=c550a360 func=1 reg=0x5 dat=0x2
2304: sbi_read_ioreg() priv=c550a360 func=1 reg=0x42 dat=0x1
2309: EVENT: AWAKE
2313: sbi_interrupt: HOST_INT_STATUS_REG 0x8
2315: sbi_write_ioreg() priv=c550a360 func=1 reg=0x3 dat=0x0
2324: EVENT: unknown event id: 0x0
阅读(1929) | 评论(0) | 转发(0) |