diff options
| author | Maximilian Hils <git@maximilianhils.com> | 2016-02-15 14:58:49 +0100 |
|---|---|---|
| committer | Maximilian Hils <git@maximilianhils.com> | 2016-02-15 14:58:49 +0100 |
| commit | 175ce43a30559115c08e41e5d87519e957ff96f6 (patch) | |
| tree | 9231c6396d15b70601492458b33f44bfe579233b /pathod/libpathod/log.py | |
| parent | 43c3e164ecf13b2d0909f748c2c61f39e197659e (diff) | |
| parent | e8598f5f7a613d7d27130de970b4c0045b3ee0d1 (diff) | |
| download | mitmproxy-175ce43a30559115c08e41e5d87519e957ff96f6.tar.gz mitmproxy-175ce43a30559115c08e41e5d87519e957ff96f6.tar.bz2 mitmproxy-175ce43a30559115c08e41e5d87519e957ff96f6.zip | |
add pathod
Diffstat (limited to 'pathod/libpathod/log.py')
| -rw-r--r-- | pathod/libpathod/log.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/pathod/libpathod/log.py b/pathod/libpathod/log.py new file mode 100644 index 00000000..f203542f --- /dev/null +++ b/pathod/libpathod/log.py @@ -0,0 +1,83 @@ +import datetime + +import netlib.utils +import netlib.tcp +import netlib.http + +TIMEFMT = '%d-%m-%y %H:%M:%S' + + +def write_raw(fp, lines): + if fp: + fp.write( + "%s: " % datetime.datetime.now().strftime(TIMEFMT) + ) + for i in lines: + fp.write(i) + fp.write("\n") + fp.flush() + + +class LogCtx(object): + + def __init__(self, fp, hex, rfile, wfile): + self.lines = [] + self.fp = fp + self.suppressed = False + self.hex = hex + self.rfile, self.wfile = rfile, wfile + + def __enter__(self): + if self.wfile: + self.wfile.start_log() + if self.rfile: + self.rfile.start_log() + return self + + def __exit__(self, exc_type, exc_value, traceback): + wlog = self.wfile.get_log() if self.wfile else None + rlog = self.rfile.get_log() if self.rfile else None + if self.suppressed or not self.fp: + return + if wlog: + self("Bytes written:") + self.dump(wlog, self.hex) + if rlog: + self("Bytes read:") + self.dump(rlog, self.hex) + if self.lines: + write_raw( + self.fp, + [ + "\n".join(self.lines), + ] + ) + if exc_value: + raise exc_type, exc_value, traceback + + def suppress(self): + self.suppressed = True + + def dump(self, data, hexdump): + if hexdump: + for line in netlib.utils.hexdump(data): + self("\t%s %s %s" % line) + else: + for i in netlib.utils.clean_bin(data).split("\n"): + self("\t%s" % i) + + def __call__(self, line): + self.lines.append(line) + + +class ConnectionLogger: + def __init__(self, fp, hex, rfile, wfile): + self.fp = fp + self.hex = hex + self.rfile, self.wfile = rfile, wfile + + def ctx(self): + return LogCtx(self.fp, self.hex, self.rfile, self.wfile) + + def write(self, lines): + write_raw(self.fp, lines) |
