Add timestamps to the log output.

main
KKlochko 1 year ago
parent 6bd8219a9f
commit 152e4a852b

@ -20,6 +20,7 @@
import struct
from rich.console import Console
import asyncio
from notification_server.timestamp import Timestamp
class Server:
@ -46,8 +47,9 @@ class Server:
self.__ip, self.__producer_port
)
self.console.print(f'[yellow b][INFO][/] Serves on {self.__ip}:{self.__port} for clients.')
self.console.print(f'[yellow b][INFO][/] Receives notifications from producers at {self.__ip}:{self.__producer_port}.')
now = Timestamp.now()
self.console.print(f'[yellow b][INFO][/][#8abeb7 b][{now}][/] Serves on {self.__ip}:{self.__port} for clients.')
self.console.print(f'[yellow b][INFO][/][#8abeb7 b][{now}][/] Receives notifications from producers at {self.__ip}:{self.__producer_port}.')
async with self.__producer_socket, self.__client_socket:
await asyncio.gather(
@ -72,7 +74,8 @@ class Server:
async def handle_producer(self, reader, writer, notification_dir):
addr = writer.get_extra_info('peername')
self.console.print(f"[yellow b][INFO][/] A producer ({addr[0]}:{addr[1]}) connected.")
now = Timestamp.now()
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] A producer ({addr[0]}:{addr[1]}) connected.")
try:
while True:
@ -80,7 +83,8 @@ class Server:
message = await self.receive_message(reader)
if title == '' and message == '':
self.console.print(f"[yellow b][INFO][/] The producer ({addr[0]}:{addr[1]}) finished.")
now = Timestamp.now()
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) finished.")
break
await self.broadcast_message(title)
@ -89,12 +93,14 @@ class Server:
except asyncio.CancelledError:
pass
finally:
self.console.print(f"[yellow b][INFO][/] The producer ({addr[0]}:{addr[1]}) disconnected.")
now = Timestamp.now()
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) disconnected.")
writer.close()
async def handle_client(self, reader, writer):
addr = writer.get_extra_info('peername')
self.console.print(f"[yellow b][INFO][/] A client ({addr[0]}:{addr[1]}) connected.")
now = Timestamp.now()
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] A client ({addr[0]}:{addr[1]}) connected.")
self.__client_writers.append(writer)
try:
@ -105,7 +111,8 @@ class Server:
except asyncio.CancelledError:
pass
finally:
self.console.print(f"[yellow b][INFO][/] The client ({addr[0]}:{addr[1]}) disconnected.")
now = Timestamp.now()
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The client ({addr[0]}:{addr[1]}) disconnected.")
self.__client_writers.remove(writer)
writer.close()

@ -0,0 +1,7 @@
from datetime import datetime
class Timestamp:
@staticmethod
def now() -> str:
return str(datetime.now())
Loading…
Cancel
Save