kernel日志时间转换python脚本(MTK)

写这篇文章的原因

在我们看log的时候main.log和sys.log 的时间都是 格林时间,而kernel 的日志时间为毫秒,如果是出问题两个时间看起来和转换起来就非常费劲,所以就想找到一个脚本来实现这个功能,如下这脚本就是实现这个功能的:

import time
import sys
import os
 
def usage():
    print('''Help Information:
             kmsg_translate inputfile:   input file  to parse        
                        ''')
if len(sys.argv) < 2:
    usage()
    sys.exit(2)
inpath = sys.argv[1]
print "parameter"
print inpath
print "parameter"
 
def calc_delta(stream):
    global s_second
    global s_microsecond
    global a_time
    global outfile
    if a_time ==None:
        print("Can't convert to android time")
        exit(-1)
    for line in stream:
        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
                outputfile.write(new_line)
            except:
                outputfile.write(line)
 
 
def get_atime(stream):
    global s_second
    global s_microsecond
    global a_time
    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;
                break
 
def main():
    global inputfile
    global outputfile
    if inpath == None:
        usage()
        sys.exit(2)
    inputfile = open(inpath, 'r')
    outputfile = open( inpath + '_translated', 'w')
    get_atime(inputfile)
    inputfile.seek(0)
    calc_delta(inputfile)
    inputfile.close()
    outputfile.close()
if __name__ == "__main__":
    main()

脚本的用法:

保存代码成任意XXX.py,然后执行python XXX.px a.log,其中a.log是要转换的kmsg文件名称,然后会在XXX.py所在目录生成aa_translated.log文件.

适用的范围:

MTK 平台和高通平台适用,展讯平台没有测试。
转载于下面链接:
https://blog.csdn.net/junjle/article/details/74735180

猜你喜欢

转载自blog.csdn.net/houxian1103/article/details/84948075