From 2cf79b79126c0845067abce9ab24500810772404 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 10 Jun 2016 10:47:28 +1200 Subject: [PATCH] Remove timestamps from pathoc output Pathoc is an interactive tool, no need for a long leading timestamp. More generally, make timestamps optional in the logging mechanism so we can configure this with command-line flags or something down the track. --- pathod/log.py | 26 +++++++++++++------------- pathod/pathoc.py | 8 ++++++-- pathod/pathod.py | 2 +- test/pathod/test_log.py | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pathod/log.py b/pathod/log.py index 5bf55de49..1d3ec3565 100644 --- a/pathod/log.py +++ b/pathod/log.py @@ -1,17 +1,14 @@ -import datetime +import time import six -from netlib import strutils - -TIMEFMT = '%d-%m-%y %H:%M:%S' +from netlib import strutils, human -def write_raw(fp, lines): +def write_raw(fp, lines, timestamp=True): if fp: - fp.write( - "%s: " % datetime.datetime.now().strftime(TIMEFMT) - ) + if timestamp: + fp.write(human.format_timestamp(time.time())) for i in lines: fp.write(i) fp.write("\n") @@ -20,11 +17,12 @@ def write_raw(fp, lines): class LogCtx(object): - def __init__(self, fp, hex, rfile, wfile): + def __init__(self, fp, hex, timestamp, rfile, wfile): self.lines = [] self.fp = fp self.suppressed = False self.hex = hex + self.timestamp = timestamp self.rfile, self.wfile = rfile, wfile def __enter__(self): @@ -50,7 +48,8 @@ class LogCtx(object): self.fp, [ "\n".join(self.lines), - ] + ], + timestamp = self.timestamp ) if exc_value: six.reraise(exc_type, exc_value, traceback) @@ -71,13 +70,14 @@ class LogCtx(object): class ConnectionLogger: - def __init__(self, fp, hex, rfile, wfile): + def __init__(self, fp, hex, timestamp, rfile, wfile): self.fp = fp self.hex = hex self.rfile, self.wfile = rfile, wfile + self.timestamp = timestamp def ctx(self): - return LogCtx(self.fp, self.hex, self.rfile, self.wfile) + return LogCtx(self.fp, self.hex, self.timestamp, self.rfile, self.wfile) def write(self, lines): - write_raw(self.fp, lines) + write_raw(self.fp, lines, timestamp=self.timestamp) diff --git a/pathod/pathoc.py b/pathod/pathoc.py index 3eb916375..884056989 100644 --- a/pathod/pathoc.py +++ b/pathod/pathoc.py @@ -18,7 +18,7 @@ from netlib.exceptions import HttpException, TcpDisconnect, TcpTimeout, TlsExcep NetlibException from netlib.http import http1, http2 -from . import log, language +from pathod import log, language import logging from netlib.tutils import treq @@ -100,6 +100,7 @@ class WebsocketFrameReader(threading.Thread): self.logger = log.ConnectionLogger( self.logfp, self.hexdump, + False, rfile if showresp else None, None ) @@ -216,7 +217,8 @@ class Pathoc(tcp.TCPClient): self.fp, "HTTP/2 requires ALPN support. " "Please use OpenSSL >= 1.0.2. " - "Pathoc might not be working as expected without ALPN." + "Pathoc might not be working as expected without ALPN.", + timestamp = False ) self.protocol = http2.HTTP2Protocol(self, dump_frames=self.http2_framedump) else: @@ -372,6 +374,7 @@ class Pathoc(tcp.TCPClient): logger = log.ConnectionLogger( self.fp, self.hexdump, + False, None, self.wfile if self.showreq else None, ) @@ -412,6 +415,7 @@ class Pathoc(tcp.TCPClient): logger = log.ConnectionLogger( self.fp, self.hexdump, + False, self.rfile if self.showresp else None, self.wfile if self.showreq else None, ) diff --git a/pathod/pathod.py b/pathod/pathod.py index ed35a92e6..d1cc9980a 100644 --- a/pathod/pathod.py +++ b/pathod/pathod.py @@ -266,7 +266,7 @@ class PathodHandler(tcp.BaseHandler): lr = self.rfile if self.server.logreq else None lw = self.wfile if self.server.logresp else None - logger = log.ConnectionLogger(self.logfp, self.server.hexdump, lr, lw) + logger = log.ConnectionLogger(self.logfp, self.server.hexdump, True, lr, lw) self.settings.protocol = self.protocol diff --git a/test/pathod/test_log.py b/test/pathod/test_log.py index 29801eb3d..0cd5b3b02 100644 --- a/test/pathod/test_log.py +++ b/test/pathod/test_log.py @@ -16,7 +16,7 @@ class DummyIO(six.StringIO): def test_disconnect(): outf = DummyIO() rw = DummyIO() - l = log.ConnectionLogger(outf, False, rw, rw) + l = log.ConnectionLogger(outf, False, True, rw, rw) try: with l.ctx() as lg: lg("Test")