1. 原理说明:参考了这篇文章http://blog.youkuaiyun.com/transformer_han/article/details/24741605,但因其只对mtk平台kernel log有效。qcom平台kernel log无效。因为qcom平台kernel log中根本就没有写包含android time标签的信息。所以自己改了一下使其支持qcom平台,原理基本一致区别就是搜索'audit('关键字获取时间戳,然后通过与改时间戳对应的kernel时间的差进行计算得到格式化时间;
2: 使用方式,保存代码成任意XXX.py,然后执行python XXX.px inputfilename,其中inputfilename是要转换的kmsg文件名称,然后会在XXX.py所在目录生成inputfilename_translated文件
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(os.getcwd() + '/' + inpath + '_translated', 'w')
get_atime(inputfile)
inputfile.seek(0)
calc_delta(inputfile)
inputfile.close()
outputfile.close()
if __name__ == "__main__":
main()