xlog文件的解码操作步骤 xlog是什么文件电脑怎么打开

前面介绍过引入xlog,将日志打印成一个xlog文件,这里就来介绍一下怎么将xlog文件解码成正常的可读日志文件 。
对于不加密的xlog进行解码如果在Android代码里初始化xlog的时候,使用的是不加密的方式,比如这样:
Xlog.open(false, Xlog.LEVEL_DEBUG, Xlog.AppednerModeAsync, "", logPath, "dbxLog", "");最后一个入参表示pubkey,传空就代表不对日志内容进行加密 。
那就可以直接用mars工程里的
decode_mars_nocrypt_log_file.py文件直接转换,但是mars提供的这个python工具是python2的版本 。现在多数都是用python3的,如果你愿意在本地配多个python版本,那就配置一个python2,添加一些需要的库,直接执行那个就可以 。
如果你不想配置python2,就想用python3执行,也可以手动改一下这个python文件,这是我改完后的:
#!/usr/bin/pythonimport sysimport osimport globimport zlibimport structimport binasciiimport tracebackMAGIC_NO_COMPRESS_START = 0x03MAGIC_NO_COMPRESS_START1 = 0x06MAGIC_NO_COMPRESS_NO_CRYPT_START = 0x08MAGIC_COMPRESS_START = 0x04MAGIC_COMPRESS_START1 = 0x05MAGIC_COMPRESS_START2 = 0x07MAGIC_COMPRESS_NO_CRYPT_START = 0x09MAGIC_END = 0x00lastseq = 0def IsGoodLogBuffer(_buffer, _offset, count):if _offset == len(_buffer): return (True, '')magic_start = _buffer[_offset]if MAGIC_NO_COMPRESS_START == magic_start or MAGIC_COMPRESS_START == magic_start or MAGIC_COMPRESS_START1 == magic_start:crypt_key_len = 4elif MAGIC_COMPRESS_START2 == magic_start or MAGIC_NO_COMPRESS_START1 == magic_start or MAGIC_NO_COMPRESS_NO_CRYPT_START == magic_start or MAGIC_COMPRESS_NO_CRYPT_START == magic_start:crypt_key_len = 64else:return (False, '_buffer[%d]:%d != MAGIC_NUM_START' % (_offset, _buffer[_offset]))headerLen = 1 + 2 + 1 + 1 + 4 + crypt_key_lenif _offset + headerLen + 1 + 1 > len(_buffer): return (False, 'offset:%d > len(buffer):%d' % (_offset, len(_buffer)))start = _offset + headerLen - 4 - crypt_key_lenlength = struct.unpack_from("I", memoryview(_buffer)[start:start + 4].tobytes())[0]if _offset + headerLen + length + 1 > len(_buffer):return (False,'log length:%d, end pos %d > len(buffer):%d' % (length, _offset + headerLen + length + 1, len(_buffer)))if MAGIC_END != _buffer[_offset + headerLen + length]: return (False,'log length:%d, buffer[%d]:%d != MAGIC_END' % (length, _offset + headerLen + length,_buffer[_offset + headerLen + length]))if (1 >= count):return (True, '')else:return IsGoodLogBuffer(_buffer, _offset + headerLen + length + 1, count - 1)def GetLogStartPos(_buffer, _count):offset = 0while True:if offset >= len(_buffer): breakif MAGIC_NO_COMPRESS_START == _buffer[offset] or MAGIC_NO_COMPRESS_START1 == _buffer[offset] or MAGIC_COMPRESS_START == _buffer[offset] or MAGIC_COMPRESS_START1 == _buffer[offset] or MAGIC_COMPRESS_START2 == _buffer[offset] or MAGIC_COMPRESS_NO_CRYPT_START == _buffer[offset] or MAGIC_NO_COMPRESS_NO_CRYPT_START == _buffer[offset]:if IsGoodLogBuffer(_buffer, offset, _count)[0]: return offsetoffset += 1return -1def DecodeBuffer(_buffer, _offset, _outbuffer):if _offset >= len(_buffer): return -1# if _offset + 1 + 4 + 1 + 1 > len(_buffer): return -1ret = IsGoodLogBuffer(_buffer, _offset, 1)if not ret[0]:fixpos = GetLogStartPos(_buffer[_offset:], 1)if -1 == fixpos:return -1else:_outbuffer.extend("[F]decode_log_file.py decode error len=%d, result:%s \n" % (fixpos, ret[1]))_offset += fixposmagic_start = _buffer[_offset]if MAGIC_NO_COMPRESS_START == magic_start or MAGIC_COMPRESS_START == magic_start or MAGIC_COMPRESS_START1 == magic_start:crypt_key_len = 4elif MAGIC_COMPRESS_START2 == magic_start or MAGIC_NO_COMPRESS_START1 == magic_start or MAGIC_NO_COMPRESS_NO_CRYPT_START == magic_start or MAGIC_COMPRESS_NO_CRYPT_START == magic_start:crypt_key_len = 64else:_outbuffer.extend('in DecodeBuffer _buffer[%d]:%d != MAGIC_NUM_START' % (_offset, magic_start))return -1headerLen = 1 + 2 + 1 + 1 + 4 + crypt_key_lenstart = _offset + headerLen - 4 - crypt_key_lenlength = struct.unpack_from("I", memoryview(_buffer)[start:start + 4])[0]tmpbuffer = bytearray(length)seq = struct.unpack_from("H", memoryview(_buffer)[start - 2 - 2: start - 2])[0]begin_hour = struct.unpack_from("c", memoryview(_buffer)[start - 1 - 1:start - 1])[0]end_hour = struct.unpack_from("c", memoryview(_buffer)[start - 1:start])[0]global lastseqif seq != 0 and seq != 1 and lastseq != 0 and seq != (lastseq + 1):_outbuffer.extend("[F]decode_log_file.py log seq:%d-%d is missing\n" % (lastseq + 1, seq - 1))if seq != 0:lastseq = seqtmpbuffer[:] = _buffer[_offset + headerLen:_offset + headerLen + length]try:decompressor = zlib.decompressobj(-zlib.MAX_WBITS)if MAGIC_NO_COMPRESS_START1 == _buffer[_offset] or MAGIC_COMPRESS_START2 == _buffer[_offset]:print("use wrong decode script")elif MAGIC_COMPRESS_START == _buffer[_offset] or MAGIC_COMPRESS_NO_CRYPT_START == _buffer[_offset]:tmpbuffer = decompressor.decompress(bytes(tmpbuffer))elif MAGIC_COMPRESS_START1 == _buffer[_offset]:decompress_data = https://www.jinnalai.com/n/bytearray()while len(tmpbuffer)> 0:single_log_len = struct.unpack_from("H", memoryview(tmpbuffer)[0:2])[0]decompress_data.extend(tmpbuffer[2:single_log_len + 2])tmpbuffer[:] = tmpbuffer[single_log_len + 2:len(tmpbuffer)]tmpbuffer = decompressor.decompress(str(decompress_data))else:pass# _outbuffer.extend('seq:%d, hour:%d-%d len:%d decompress:%d\n' %(seq, ord(begin_hour), ord(end_hour), length, len(tmpbuffer)))except Exception as e:traceback.print_exc()_outbuffer.extend("[F]decode_log_file.py decompress err, " + str(e) + "\n")return _offset + headerLen + length + 1_outbuffer.extend(tmpbuffer)return _offset + headerLen + length + 1def ParseFile(_file, _outfile):fp = open(_file, "rb")_buffer = bytearray(os.path.getsize(_file))fp.readinto(_buffer)fp.close()startpos = GetLogStartPos(_buffer, 2)if -1 == startpos:returnoutbuffer = bytearray()while True:startpos = DecodeBuffer(_buffer, startpos, outbuffer)if -1 == startpos: break;if 0 == len(outbuffer): returnfpout = open(_outfile, "wb")fpout.write(outbuffer)fpout.close()def main(args):global lastseqif 1 == len(args):if os.path.isdir(args[0]):filelist = glob.glob(args[0] + "/*.xlog")for filepath in filelist:lastseq = 0ParseFile(filepath, filepath + ".log")else:ParseFile(args[0], args[0] + ".log")elif 2 == len(args):ParseFile(args[0], args[1])else:filelist = glob.glob("*.xlog")for filepath in filelist:lastseq = 0ParseFile(filepath, filepath + ".log")if __name__ == "__main__":main(sys.argv[1:])

推荐阅读