lo: rlo: more spaghetti

This commit is contained in:
ed 2026-07-26 16:49:05 +00:00
parent 537a99df52
commit b6abc33fb3
2 changed files with 30 additions and 6 deletions

View file

@ -1042,7 +1042,12 @@ def get_sects():
.1 = when necessary, append a dot followed by a single digit
.1! = counter is always added, even when not necessary
-3 = a hyphen followed by three-digit counter
no = disable counter; overwrite existing logfile
no = disable counter; append to existing file
del = delete existing logfile and create new
over = overwrite existing logfile
append is not possible for .xz-compressed logfiles;
if logfile is *.xz and rlo is no then rlo will be .1
"""
),
],
@ -1755,7 +1760,7 @@ def add_logging(ap):
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("--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("--rlo", metavar="TXT", type=u, default=".1", help="logrotate counter format; see \033[33m--help-rlo\033[0m")
ap2.add_argument("--rlo", metavar="TXT", type=u, default="no", help="logrotate counter format; see \033[33m--help-rlo\033[0m")
ap2.add_argument("--logrot-sig", metavar="S", type=u, default="", help="immediately logrotate when unix-signal \033[33mS\033[0m is received; examples: [\033[32mSIGHUP\033[0m], [\033[32mHUP\033[0m], [\033[32m1\033[0m]")
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")

View file

@ -233,10 +233,14 @@ class SvcHub(object):
self.lo1 = self.lo2 = ""
if args.lo:
do_xz = args.lo.replace("%R", "").lower().endswith(".xz")
if "%R" not in args.lo:
args.lo += "%R"
if args.rlo in ("", "no"):
args.rlo = ""
if do_xz and not args.rlo:
args.rlo = ".1"
if args.rlo in ("", "del", "over"):
args.lo = args.lo.replace("%R", "")
try:
self.lo1, self.lo2 = args.lo.split("%R")
@ -1443,7 +1447,7 @@ class SvcHub(object):
def _setup_logfile(self) -> None:
base_fn = fn = self._logname()
sel_fn = fn + self.lo2
do_xz = sel_fn.lower().endswith(".xz")
do_xz = sel_fn.replace("%R", "").lower().endswith(".xz")
if "%R" in self.args.lo:
# yup this is a race; if started sufficiently concurrently, two
# copyparties can grab the same logfile (considered and ignored)
@ -1459,18 +1463,31 @@ class SvcHub(object):
except:
pass
if self.args.rlo in ("del", "over"):
tmode = "wt"
bmode = "w"
else:
tmode = "at"
bmode = "a"
if self.args.rlo == "del":
try:
bos.unlink(fn)
except:
pass
try:
if do_xz:
import lzma
lh = lzma.open(fn, "wt", encoding="utf-8", errors="replace", preset=0)
lh = lzma.open(fn, tmode, encoding="utf-8", errors="replace", preset=0)
self.args.no_logflush = True
else:
lh = open(fn, "wt", encoding="utf-8", errors="replace")
lh = open(fn, tmode, encoding="utf-8", errors="replace")
except:
import codecs
lh = codecs.open(fn, "w", encoding="utf-8", errors="replace")
lh = codecs.open(fn, bmode, encoding="utf-8", errors="replace")
if getattr(self.args, "free_umask", False):
os.fchmod(lh.fileno(), 0o644)
@ -1485,6 +1502,8 @@ class SvcHub(object):
printed = "".join(lprinted) + msg
t = "t0: {:.3f}\nargv: {}\n\n{}"
lh.write(t.format(self.E.t0, " ".join(argv), printed))
if not self.args.no_logflush:
lh.flush()
self.logf = lh
self.logf_base_fn = base_fn
print(msg, end="")