From 117f01e71f56e558739def474c8c3dc7fba913df Mon Sep 17 00:00:00 2001 From: ibuler Date: Sat, 28 Nov 2015 21:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E6=96=87=E6=92=AD=E6=94=BEbug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- connect.py | 11 +++++++---- jlog/log_api.py | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/connect.py b/connect.py index 96418f99d..1c7218dd8 100644 --- a/connect.py +++ b/connect.py @@ -15,6 +15,7 @@ import readline import django import paramiko import struct, fcntl, signal, socket, select +from io import open as copen os.environ['DJANGO_SETTINGS_MODULE'] = 'jumpserver.settings' if django.get_version() != '1.6': @@ -225,6 +226,8 @@ class Tty(object): raise ServerError('Create %s failed, Please modify %s permission.' % (today_connect_log_dir, tty_log_dir)) try: + # log_file_f = copen(log_file_path + '.log', mode='at', encoding='utf-8', errors='replace') + # log_time_f = copen(log_file_path + '.time', mode='at', encoding='utf-8', errors='replace') log_file_f = open(log_file_path + '.log', 'a') log_time_f = open(log_file_path + '.time', 'a') except IOError: @@ -245,7 +248,7 @@ class Tty(object): log.pid = log.id log.save() - log_file_f.write('Start at %s\n' % datetime.datetime.now()) + log_file_f.write('Start at %s\r\n' % datetime.datetime.now()) return log_file_f, log_time_f, log def get_connect_info(self): @@ -366,13 +369,13 @@ class SshTty(Tty): self.vim_data += x sys.stdout.write(x) sys.stdout.flush() - es_x = escapeString(x) now_timestamp = time.time() - log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(es_x))) + log_time_f.write('%s %s\n' % (round(now_timestamp-pre_timestamp, 4), len(x))) + log_time_f.flush() log_file_f.write(x) + log_file_f.flush() pre_timestamp = now_timestamp log_file_f.flush() - log_time_f.flush() if input_mode and not self.is_output(x): data += x diff --git a/jlog/log_api.py b/jlog/log_api.py index 578ea684e..cbcb38a26 100644 --- a/jlog/log_api.py +++ b/jlog/log_api.py @@ -3,7 +3,7 @@ from argparse import ArgumentParser, FileType from contextlib import closing -from codecs import open as copen +from io import open as copen from json import dumps from math import ceil import re @@ -46,20 +46,20 @@ def scriptToJSON(scriptf, timing=None): ret = [] with closing(scriptf): - scriptf.readline() # ignore first header line from script file + print "# %s #" % scriptf.readline() # ignore first header line from script file offset = 0 for t in timing: dt = scriptf.read(t[1]) data = escapeString(dt) - # print ('###### (%s, %s)' % (t[1], data)) + print ('###### (%s, %s)' % (t[1], repr(data))) offset += t[0] ret.append((data, offset)) return dumps(ret) def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatename=DEFAULT_TEMPLATE): - # with copen(script_path, encoding='utf-8', errors='replace') as scriptf: - with open(script_path) as scriptf: + with copen(script_path, encoding='utf-8', errors='replace', newline='\r\n') as scriptf: + # with open(script_path) as scriptf: with open(time_file_path) as timef: timing = getTiming(timef) json = scriptToJSON(scriptf, timing)