Featured image of post 为Android Kernel Log添加时间戳

为Android Kernel Log添加时间戳

将下面的代码保存为kernel.py, 加入可执行权限, 执行./kernel.py -i kernel_log -o kernel.txt , 打开kernel.txt 就可以看到(与android logcat -v time一样的)时间戳

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
import time
import datetime
import sys
import getopt
import os
 
a_time = None
s_second = None
s_microsecone = None
abs_time = 0.0
inputfile = None
outputfile = None
 
def usage():
    print('''Help Information:
             -h, --help:        Show help information
             -i, --inputfile:   input file  to parse
             -o, --outputfile:  output file parsed
                        ''')
 
 
def calc_delta(stream):
    global s_second
    global s_microsecond
    global a_time
    global outfile
    begin_index = None
    end_index = None
    delta_second = 0
    delta_mircosecond = 0
    delta_time = 0
    d_time = None
    new_line = None
    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))
                delta_time = datetime.timedelta(seconds=delta_second,microseconds=delta_microsecond)
                d_time = a_time + delta_time
                new_line = d_time.strftime("%m-%d %H:%M:%S.%f")+' ' + line
                outputfile.write(new_line)
            except:
                outputfile.write(line)
 
 
def get_atime(stream):
    global s_second
    global s_microsecond
    global a_time
    a_time_op = None
    begin_index = None
    end_index = None
    for line in stream:
        if line:
            a_time_op = line.find('android time')
            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+13:].strip()
                abs_time = line[begin_index + 1 :end_index]
                [s_second,s_microsecond] = abs_time.split('.')
                a_time = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S.%f")
                break
 
def main(argv):
    global inputfile
    global outputfile
    inputpath = None
    outputpath = None
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["help","inputfile=","outputfile="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        if opt in ("-i", "--inputfile"):
            inputpath = arg
        if opt in ("-o", "outputfile"):
            outputpath = arg
    if inputpath == None:
        usage()
        sys.exit()
    if outputpath == None:
        outputpath = os.getcwd()+"/out.txt"
 
    inputfile = open(inputpath, 'r')
    outputfile = open(outputpath, 'w')
    get_atime(inputfile)
    inputfile.seek(0)
    calc_delta(inputfile)
    inputfile.close()
    outputfile.close()
if __name__ == "__main__":
    main(sys.argv[1:])
Built with Hugo
Theme Stack designed by Jimmy