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)
参考文档: