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.
This commit is contained in:
Aldo Cortesi 2016-06-10 10:47:28 +12:00
parent c421c41307
commit 2cf79b7912
4 changed files with 21 additions and 17 deletions

View File

@ -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)

View File

@ -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,
)

View File

@ -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

View File

@ -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")