add --flo (logfile format)

This commit is contained in:
ed
2026-01-18 21:03:43 +00:00
parent d90d021969
commit 826e84c8ec
2 changed files with 35 additions and 11 deletions

View File

@@ -1637,6 +1637,7 @@ def add_logging(ap):
ap2 = ap.add_argument_group("logging options") ap2 = ap.add_argument_group("logging options")
ap2.add_argument("-q", action="store_true", help="quiet; disable most STDOUT messages") ap2.add_argument("-q", action="store_true", help="quiet; disable most STDOUT messages")
ap2.add_argument("-lo", metavar="PATH", type=u, default="", help="logfile; use .txt for plaintext or .xz for compressed. Example: \033[32mcpp-%%Y-%%m%%d-%%H%%M%%S.txt.xz\033[0m (NB: some errors may appear on STDOUT only)") ap2.add_argument("-lo", metavar="PATH", type=u, default="", help="logfile; use .txt for plaintext or .xz for compressed. Example: \033[32mcpp-%%Y-%%m%%d-%%H%%M%%S.txt.xz\033[0m (NB: some errors may appear on STDOUT only)")
ap2.add_argument("--flo", metavar="N", type=int, default=1, help="log format for \033[33m-lo\033[0m; [\033[32m1\033[0m]=classic/colors, [\033[32m2\033[0m]=no-color")
ap2.add_argument("--no-ansi", action="store_true", default=not VT100, help="disable colors; same as environment-variable NO_COLOR") 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("--ansi", action="store_true", help="force colors; overrides environment-variable NO_COLOR")
ap2.add_argument("--no-logflush", action="store_true", help="don't flush the logfile after each write; tiny bit faster") ap2.add_argument("--no-logflush", action="store_true", help="don't flush the logfile after each write; tiny bit faster")

View File

@@ -124,6 +124,7 @@ class SvcHub(object):
self.argv = argv self.argv = argv
self.E: EnvParams = args.E self.E: EnvParams = args.E
self.no_ansi = args.no_ansi self.no_ansi = args.no_ansi
self.flo = args.flo
self.tz = UTC if args.log_utc else None self.tz = UTC if args.log_utc else None
self.logf: Optional[typing.TextIO] = None self.logf: Optional[typing.TextIO] = None
self.logf_base_fn = "" self.logf_base_fn = ""
@@ -1571,21 +1572,38 @@ class SvcHub(object):
dt.microsecond // self.log_div, dt.microsecond // self.log_div,
) )
if c and not self.args.no_ansi: if self.flo == 1:
if isinstance(c, int): fmt = "@%s [%-21s] %s\n"
if not c:
if "\033" in msg:
msg += "\033[0m"
elif isinstance(c, int):
msg = "\033[3%sm%s\033[0m" % (c, msg) msg = "\033[3%sm%s\033[0m" % (c, msg)
elif "\033" not in c: elif "\033" not in c:
msg = "\033[%sm%s\033[0m" % (c, msg) msg = "\033[%sm%s\033[0m" % (c, msg)
else: else:
msg = "%s%s\033[0m" % (c, msg) msg = "%s%s\033[0m" % (c, msg)
if "\033" in src: if "\033" in src:
src += "\033[0m" src += "\033[0m"
else:
if not c:
fmt = "@%s LOG [%-21s] %s\n"
elif c == 1:
fmt = "@%s CRIT [%-21s] %s\n"
elif c == 3:
fmt = "@%s WARN [%-21s] %s\n"
elif c == 6:
fmt = "@%s BTW [%-21s] %s\n"
else:
fmt = "@%s LOG [%-21s] %s\n"
if "\033" in msg: if "\033" in src:
msg += "\033[0m" src = RE_ANSI.sub("", src)
if "\033" in msg:
msg = RE_ANSI.sub("", msg)
self.logf.write("@%s [%-21s] %s\n" % (ts, src, msg)) self.logf.write(fmt % (ts, src, msg))
if not self.args.no_logflush: if not self.args.no_logflush:
self.logf.flush() self.logf.flush()
@@ -1615,9 +1633,10 @@ class SvcHub(object):
if self.logf: if self.logf:
self.logf.write(zs) self.logf.write(zs)
fmt = "\033[36m%s \033[33m%-21s \033[0m%s\n"
if self.no_ansi: if self.no_ansi:
if c == 1: if not c:
fmt = "%s %-21s LOG: %s\n"
elif c == 1:
fmt = "%s %-21s CRIT: %s\n" fmt = "%s %-21s CRIT: %s\n"
elif c == 3: elif c == 3:
fmt = "%s %-21s WARN: %s\n" fmt = "%s %-21s WARN: %s\n"
@@ -1625,12 +1644,16 @@ class SvcHub(object):
fmt = "%s %-21s BTW: %s\n" fmt = "%s %-21s BTW: %s\n"
else: else:
fmt = "%s %-21s LOG: %s\n" fmt = "%s %-21s LOG: %s\n"
if "\033" in msg: if "\033" in msg:
msg = RE_ANSI.sub("", msg) msg = RE_ANSI.sub("", msg)
if "\033" in src: if "\033" in src:
src = RE_ANSI.sub("", src) src = RE_ANSI.sub("", src)
elif c: else:
if isinstance(c, int): fmt = "\033[36m%s \033[33m%-21s \033[0m%s\n"
if not c:
pass
elif isinstance(c, int):
msg = "\033[3%sm%s\033[0m" % (c, msg) msg = "\033[3%sm%s\033[0m" % (c, msg)
elif "\033" not in c: elif "\033" not in c:
msg = "\033[%sm%s\033[0m" % (c, msg) msg = "\033[%sm%s\033[0m" % (c, msg)