Android kernel log转换

android kernel log的日志时间为毫秒,看起来很不方便,可使用如下脚本进行转换

import time
import sys
import os

def usage():
    print('''Help Information:
             kmsg_translate inputfile:   input file  to parse        
                        ''')
def calc_delta(kernellogPath,outputfile,s_second,s_microsecond,a_time):
    if a_time ==None:
        print("Can't convert to android time")
        exit(-1)
    with open(kernellogPath,'r', encoding="utf8",errors='ignore') as iftream , open(outputfile,'w', encoding="utf8",errors='ignore') as ostream:
        for line in iftream:
            if line:
                try:
                    begin_index =  line.index('[')
                    end_index = line[begin_index+1:].index(']')+begin_index+1
                    time_string = line[begin_index + 1 :end_index]
                    [d_second,d_microsecond] = time_string.split('.')
                    delta_second = int(int(d_second) - int(s_second))
                    delta_microsecond = int(int(d_microsecond)-int(s_microsecond))
                    [t_second, t_microsecond] = a_time.split('.')
                    seconds = (delta_second + int(t_second))
                    microseconds = (delta_microsecond + int(t_microsecond) * 1000)
                    if microseconds < 0:
                        microseconds = microseconds + 1000000
                        seconds = seconds - 1
                    times = str(seconds)
                    x = time.localtime(float(times))
                    realtime = time.strftime('%Y-%m-%d %H:%M:%S', x)
                    new_line = realtime+ "." + str(microseconds) +' ' + line
                    ostream.write(new_line)
                except:
                    ostream.write(line)

def get_atime(kernellogPath):
    with open(kernellogPath,"r") as stream:
        for line in stream:
            if line:
                a_time_op = line.find('audit(')
                if a_time_op>=1:
                    begin_index =  line.index('[')
                    end_index = line[begin_index+1:].index(']')+begin_index+1
                    date_string = line[a_time_op + 6 :a_time_op+20]
                    abs_time = line[begin_index + 1 :end_index]
                    s_second,s_microsecond = abs_time.split('.')
                    a_time = date_string
                    return s_second,s_microsecond,a_time

def convertKernelTime(kernellogPath):
    if os.path.exists(kernellogPath)  is None:
        print("%s is not exist"%(kernellogPath))
        sys.exit(2)  
    outputfile =  kernellogPath + '_convert'
    s_second,s_microsecond,a_time = get_atime(kernellogPath)
    calc_delta(kernellogPath,outputfile,s_second,s_microsecond,a_time)
if __name__ == "__main__":
    if len(sys.argv) < 2:
        usage()
        sys.exit(2)
    kernellogPath = sys.argv[1]
    convertKernelTime(kernellogPath)

参考文档:

kernel日志时间转换python脚本(MTK)_51CTO博客_python unix时间转换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值