From 04a1f7040ef52f432cf834c375e11f1aa3111f26 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 14 Aug 2023 17:22:22 +0200 Subject: [PATCH] adjustable timestamp resolution in log messages --- copyparty/__main__.py | 1 + copyparty/svchub.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index df01fb99..3081bcfe 100755 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -995,6 +995,7 @@ def add_logging(ap): ap2.add_argument("--no-ansi", action="store_true", default=not VT100, help="disable colors; same as environment-variable NO_COLOR") ap2.add_argument("--ansi", action="store_true", help="force colors; overrides environment-variable NO_COLOR") ap2.add_argument("--no-voldump", action="store_true", help="do not list volumes and permissions on startup") + ap2.add_argument("--log-tdec", type=int, default=3, help="timestamp resolution / number of timestamp decimals") ap2.add_argument("--log-conn", action="store_true", help="debug: print tcp-server msgs") ap2.add_argument("--log-htp", action="store_true", help="debug: print http-server threadpool scaling") ap2.add_argument("--ihead", metavar="HEADER", type=u, action='append', help="dump incoming header") diff --git a/copyparty/svchub.py b/copyparty/svchub.py index e2d7b9bf..b6c3d8e0 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -133,6 +133,9 @@ class SvcHub(object): if not self._process_config(): raise Exception("bad config") + self.log_div = 10 ** (6 - args.log_tdec) + self.log_efmt = "%02d:%02d:%02d.%0{}d".format(args.log_tdec) + self.log_dfmt = "%04d-%04d-%06d.%0{}d".format(args.log_tdec) self.log = self._log_disabled if args.q else self._log_enabled if args.lo: self._setup_logfile(printed) @@ -673,11 +676,11 @@ class SvcHub(object): with self.log_mutex: zd = datetime.utcnow() - ts = "%04d-%04d-%06d.%03d" % ( + ts = self.log_dfmt % ( zd.year, zd.month * 100 + zd.day, (zd.hour * 100 + zd.minute) * 100 + zd.second, - zd.microsecond // 1000, + zd.microsecond // self.log_div, ) self.logf.write("@%s [%s\033[0m] %s\n" % (ts, src, msg)) @@ -729,11 +732,11 @@ class SvcHub(object): msg = "%s%s\033[0m" % (c, msg) zd = datetime.utcfromtimestamp(now) - ts = "%02d:%02d:%02d.%03d" % ( + ts = self.log_efmt % ( zd.hour, zd.minute, zd.second, - zd.microsecond // 1000, + zd.microsecond // self.log_div, ) msg = fmt % (ts, src, msg) try: