Skip to content

Commit b599fba

Browse files
committed
use local timezone in log messages; closes #96
timezone can be changed with `export TZ=Europe/Oslo` before launch using naive timestamps like this appears to be safe as of 3.13-rc1, no deprecation warnings, just a tiny bit slower than assuming UTC
1 parent a8dabc9 commit b599fba

File tree

6 files changed

+28
-42
lines changed

6 files changed

+28
-42
lines changed

copyparty/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,7 @@ def add_logging(ap):
12851285
ap2.add_argument("--ansi", action="store_true", help="force colors; overrides environment-variable NO_COLOR")
12861286
ap2.add_argument("--no-logflush", action="store_true", help="don't flush the logfile after each write; tiny bit faster")
12871287
ap2.add_argument("--no-voldump", action="store_true", help="do not list volumes and permissions on startup")
1288+
ap2.add_argument("--log-utc", action="store_true", help="do not use local timezone; assume the TZ env-var is UTC (tiny bit faster)")
12881289
ap2.add_argument("--log-tdec", metavar="N", type=int, default=3, help="timestamp resolution / number of timestamp decimals")
12891290
ap2.add_argument("--log-badpwd", metavar="N", type=int, default=1, help="log failed login attempt passwords: 0=terse, 1=plaintext, 2=hashed")
12901291
ap2.add_argument("--log-conn", action="store_true", help="debug: print tcp-server msgs")

copyparty/svchub.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import argparse
55
import base64
6-
import calendar
76
import errno
87
import gzip
98
import logging
@@ -16,7 +15,7 @@
1615
import sys
1716
import threading
1817
import time
19-
from datetime import datetime, timedelta
18+
from datetime import datetime
2019

2120
# from inspect import currentframe
2221
# print(currentframe().f_lineno)
@@ -104,6 +103,7 @@ def __init__(
104103
self.argv = argv
105104
self.E: EnvParams = args.E
106105
self.no_ansi = args.no_ansi
106+
self.tz = UTC if args.log_utc else None
107107
self.logf: Optional[typing.TextIO] = None
108108
self.logf_base_fn = ""
109109
self.is_dut = False # running in unittest; always False
@@ -118,7 +118,8 @@ def __init__(
118118
self.httpsrv_up = 0
119119

120120
self.log_mutex = threading.Lock()
121-
self.next_day = 0
121+
self.cday = 0
122+
self.cmon = 0
122123
self.tstack = 0.0
123124

124125
self.iphash = HMaccas(os.path.join(self.E.cfg, "iphash"), 8)
@@ -791,7 +792,7 @@ def _setlimits(self) -> None:
791792
self.args.nc = min(self.args.nc, soft // 2)
792793

793794
def _logname(self) -> str:
794-
dt = datetime.now(UTC)
795+
dt = datetime.now(self.tz)
795796
fn = str(self.args.lo)
796797
for fs in "YmdHMS":
797798
fs = "%" + fs
@@ -1064,12 +1065,12 @@ def _log_disabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
10641065
return
10651066

10661067
with self.log_mutex:
1067-
zd = datetime.now(UTC)
1068+
dt = datetime.now(self.tz)
10681069
ts = self.log_dfmt % (
1069-
zd.year,
1070-
zd.month * 100 + zd.day,
1071-
(zd.hour * 100 + zd.minute) * 100 + zd.second,
1072-
zd.microsecond // self.log_div,
1070+
dt.year,
1071+
dt.month * 100 + dt.day,
1072+
(dt.hour * 100 + dt.minute) * 100 + dt.second,
1073+
dt.microsecond // self.log_div,
10731074
)
10741075

10751076
if c and not self.args.no_ansi:
@@ -1090,41 +1091,26 @@ def _log_disabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
10901091
if not self.args.no_logflush:
10911092
self.logf.flush()
10921093

1093-
now = time.time()
1094-
if int(now) >= self.next_day:
1095-
self._set_next_day()
1094+
if dt.day != self.cday or dt.month != self.cmon:
1095+
self._set_next_day(dt)
10961096

1097-
def _set_next_day(self) -> None:
1098-
if self.next_day and self.logf and self.logf_base_fn != self._logname():
1097+
def _set_next_day(self, dt: datetime) -> None:
1098+
if self.cday and self.logf and self.logf_base_fn != self._logname():
10991099
self.logf.close()
11001100
self._setup_logfile("")
11011101

1102-
dt = datetime.now(UTC)
1103-
1104-
# unix timestamp of next 00:00:00 (leap-seconds safe)
1105-
day_now = dt.day
1106-
while dt.day == day_now:
1107-
dt += timedelta(hours=12)
1108-
1109-
dt = dt.replace(hour=0, minute=0, second=0)
1110-
try:
1111-
tt = dt.utctimetuple()
1112-
except:
1113-
# still makes me hella uncomfortable
1114-
tt = dt.timetuple()
1115-
1116-
self.next_day = calendar.timegm(tt)
1102+
self.cday = dt.day
1103+
self.cmon = dt.month
11171104

11181105
def _log_enabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
11191106
"""handles logging from all components"""
11201107
with self.log_mutex:
1121-
now = time.time()
1122-
if int(now) >= self.next_day:
1123-
dt = datetime.fromtimestamp(now, UTC)
1108+
dt = datetime.now(self.tz)
1109+
if dt.day != self.cday or dt.month != self.cmon:
11241110
zs = "{}\n" if self.no_ansi else "\033[36m{}\033[0m\n"
11251111
zs = zs.format(dt.strftime("%Y-%m-%d"))
11261112
print(zs, end="")
1127-
self._set_next_day()
1113+
self._set_next_day(dt)
11281114
if self.logf:
11291115
self.logf.write(zs)
11301116

@@ -1143,12 +1129,11 @@ def _log_enabled(self, src: str, msg: str, c: Union[int, str] = 0) -> None:
11431129
else:
11441130
msg = "%s%s\033[0m" % (c, msg)
11451131

1146-
zd = datetime.fromtimestamp(now, UTC)
11471132
ts = self.log_efmt % (
1148-
zd.hour,
1149-
zd.minute,
1150-
zd.second,
1151-
zd.microsecond // self.log_div,
1133+
dt.hour,
1134+
dt.minute,
1135+
dt.second,
1136+
dt.microsecond // self.log_div,
11521137
)
11531138
msg = fmt % (ts, src, msg)
11541139
try:

scripts/docker/Dockerfile.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LABEL org.opencontainers.image.url="https://github.com/9001/copyparty" \
88
ENV XDG_CONFIG_HOME=/cfg
99

1010
RUN apk --no-cache add !pyc \
11-
wget \
11+
tzdata wget \
1212
py3-jinja2 py3-argon2-cffi py3-pillow \
1313
ffmpeg
1414

scripts/docker/Dockerfile.dj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ COPY i/bin/mtag/install-deps.sh ./
1111
COPY i/bin/mtag/audio-bpm.py /mtag/
1212
COPY i/bin/mtag/audio-key.py /mtag/
1313
RUN apk add -U !pyc \
14-
wget \
14+
tzdata wget \
1515
py3-jinja2 py3-argon2-cffi py3-pillow py3-pip py3-cffi \
1616
ffmpeg \
1717
vips-jxl vips-heif vips-poppler vips-magick \

scripts/docker/Dockerfile.im

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LABEL org.opencontainers.image.url="https://github.com/9001/copyparty" \
88
ENV XDG_CONFIG_HOME=/cfg
99

1010
RUN apk --no-cache add !pyc \
11-
wget \
11+
tzdata wget \
1212
py3-jinja2 py3-argon2-cffi py3-pillow py3-mutagen
1313

1414
COPY i/dist/copyparty-sfx.py innvikler.sh ./

scripts/docker/Dockerfile.iv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LABEL org.opencontainers.image.url="https://github.com/9001/copyparty" \
88
ENV XDG_CONFIG_HOME=/cfg
99

1010
RUN apk add -U !pyc \
11-
wget \
11+
tzdata wget \
1212
py3-jinja2 py3-argon2-cffi py3-pillow py3-pip py3-cffi \
1313
ffmpeg \
1414
vips-jxl vips-heif vips-poppler vips-magick \

0 commit comments

Comments
 (0)