Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15529485
  • 博文数量: 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-17 19:13:30

《浅析python实现log日志文件加工初步源代码之1》
《浅析python实现log日志文件加工初步源代码之2》
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)
luther@gliethttp:~/python$ python wpr.py log.txt
1: [Tx download ready : Card event]
4: EVENT: AWAKE
21: U interrupt
31: [Card event]
34: EVENT: SLEEP
37: SLEEP_CFM:
    Command             =21
    Size                =12
    SeqNum              =1
    Result              =0
    Action              =34
    Reserved1           =0
    MultipleDtim        =0
    Reserved            =0
    LocalListenInterval =0
53: U+D interrupt
63: [Tx download ready : Card event]
70: U interrupt
80: [Card event]
83: EVENT: SLEEP
......
2190: U+D interrupt
2200: [Tx download ready : Card event]
2207: U interrupt
2217: [Card event]
2220: EVENT: SLEEP
2223: SLEEP_CFM:
    Command             =21
    Size                =12
    SeqNum              =1
    Result              =0
    Action              =34
    Reserved1           =0
    MultipleDtim        =0
    Reserved            =0
    LocalListenInterval =0
2239: U+D interrupt
2249: [Tx download ready : Card event]
2256: U interrupt
2266: EVENT: AWAKE
2270: [Card event]
2281: EVENT: unknown event id: 0x0
2296: wlan_do_ioctl: ioctl cmd = 0x8bfd
2317: wlan_do_ioctl: ioctl cmd = 0x8bfd
阅读(3148) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-04-21 14:41:24

wow gold:wow gold,wow gold website:wow gold tks

chinaunix网友2009-04-21 14:40:20

wow gold:wow gold,wow gold website:wow gold tks