Add logging to stderr

This commit is contained in:
László Károlyi 2024-08-06 22:55:16 +02:00
parent 3d0b7e1b24
commit d230f8cfde
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE

View file

@ -9,6 +9,7 @@ from signal import SIGTERM, signal
from socket import AF_INET6, IPPROTO_IPV6, IPV6_V6ONLY, socket from socket import AF_INET6, IPPROTO_IPV6, IPV6_V6ONLY, socket
from sqlite3 import connect from sqlite3 import connect
from string import ascii_lowercase from string import ascii_lowercase
from sys import stderr
from tempfile import gettempdir from tempfile import gettempdir
from threading import Thread from threading import Thread
from types import FrameType from types import FrameType
@ -23,6 +24,11 @@ graph_printf %3.0lf
""" """
def _log_message(message: str) -> None:
'Do a simple log on `stderr`.'
stderr.write(f'=== {message}\n')
class _JailInfo(NamedTuple): class _JailInfo(NamedTuple):
id: int id: int
name: str name: str
@ -130,7 +136,7 @@ server = DualStackThreadingHttpServer(
def shutdown(signalnum: int, frame: FrameType | None): def shutdown(signalnum: int, frame: FrameType | None):
'Shut down upon SIGTERM.' 'Shut down upon SIGTERM.'
print('Shutting down.') _log_message('Shutting down.')
server.shutdown() server.shutdown()
@ -138,10 +144,10 @@ if __name__ == '__main__':
signal(signalnum=SIGTERM, handler=shutdown) signal(signalnum=SIGTERM, handler=shutdown)
run_thread = Thread( run_thread = Thread(
target=server.serve_forever, kwargs=dict(poll_interval=1)) target=server.serve_forever, kwargs=dict(poll_interval=1))
print('Serving.') _log_message('Serving.')
run_thread.start() run_thread.start()
try: try:
run_thread.join() run_thread.join()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
print('Server is shut down.') _log_message('Server is shut down.')