Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15340278
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: Python/Ruby

2008-12-16 12:23:29

《浅析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

阅读(1880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~