python script for parse log

#!/usr/bin/env python
import os
import sys
import os
'''
import matplotlib.pyplot as plt
'''

class Unit:
    def __init__(self):
        self._channum = 0
        self._fbts = 0
        self._refTs = 0
        self._PE = 0

        self._rawfbts = ""
        self._rawrefTs = ""

    def load(self, strUnit):
        offset = 0
        temp = ""
        data = strUnit.split()

        for i in range(0, 36):
            if (i == 0 or i == 9 or i== 18 or i == 27):
                continue
            temp += data[i + offset]
            if (i == 4):
                self._channum = temp
                temp = ""

            if (i == 8):
                self._rawfbts = temp
                self._fbts = int(temp,16)>>4
                temp = ""

            if (i == 13):
                self._rawrefTs = temp
                self._refTs = int(temp,16)>>4
                temp = ""

            if (i == 17):
                valPE = int(temp,16)>>4
                if (valPE > 0xf0000000):
                    self._PE = 0 - (((~(valPE))&0x0FFFFFFFFFFFFFFF) + 1)
                else:
                    self._PE = valPE
                temp = ""


    def dump(self):
         print("chan :", self._channum)
         print("fbts :", self._rawfbts, "dec:", self._fbts)
         print("refTs:", self._rawrefTs, "dec:", self._refTs)
         print("pe   :", self._PE)

class GdpllLog:
    def __init__(self):
        self._unitlist = []
        self._fbtschange = []
        self._refschange = []
        self._fbtslist = []

    def load_file(self, file):
        with open(file, 'r') as f:
            data = f.read().split()
            temp = ""
            offset = 0
            rangevar = int(len(data) / 36)
            #rangevar = 30
            for k in range(0, rangevar):
                for i in range(0, 36):
                    if (i == 0):
                        temp = temp + data[i + offset]
                    else:
                        temp = temp + " " + data[i + offset]
                    #print("i=", i, "value:", data[i])
                offset= offset + 36
                #print(temp)
                unit =Unit()
                unit.load(temp)
                #unit.dump()
                self._unitlist.append(unit)
                temp = ""

    def dump_change(self, file):
        with open(file, 'w') as f:
            f.writelines("            fbts              refs  fb_dif  re_dif  PE\n")
            last = self._unitlist[0]
            i = 1
            while(i < len(self._unitlist)):
                str1 = self._unitlist[i]._rawfbts + "  " + self._unitlist[i]._rawrefTs \
                      + "  " + str(self._unitlist[i]._fbts - self._unitlist[i-1]._fbts) \
                      + "  " + str(self._unitlist[i]._refTs - self._unitlist[i - 1]._refTs) \
                      + "  " + str(self._unitlist[i]._PE) + "\n"
                f.writelines(str1)

                self._fbtschange.append(self._unitlist[i]._fbts - self._unitlist[i-1]._fbts)
                self._refschange.append(self._unitlist[i]._refTs - self._unitlist[i - 1]._refTs)
                self._fbtslist.append(self._unitlist[i]._fbts - self._unitlist[0]._fbts)
                i += 1

    def figure(self):
        #fbts = []
        #for unit in self._unitlist:
            #fbts.append(unit._fbts)

        #plt.plot(self._fbtslist, self._fbtschange)
        #plt.plot(self._fbtschange, self._refschange)
        #plt.plot(self._refschange)

        #plt.show()
        pass


def usage():
    print('parse_gpll_log.py inputfile outfile')
    sys.exit(1)


if __name__ == '__main__':

    if len(sys.argv) < 3:
        usage()

    file = sys.argv[1]
    #record = (int)sys.argv[2]
    changeFile = sys.argv[2]

    print("inputfile=", file)
    print("outputfile=", changeFile)

    gdpllog = GdpllLog()
    gdpllog.load_file(file)
    gdpllog.dump_change(changeFile)
    #gdpllog.figure()

 

你可能感兴趣的:(SCRIPT)