Add timestamps to the log output.
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
import struct
|
import struct
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from notification_server.timestamp import Timestamp
|
||||||
|
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
@@ -46,8 +47,9 @@ class Server:
|
|||||||
self.__ip, self.__producer_port
|
self.__ip, self.__producer_port
|
||||||
)
|
)
|
||||||
|
|
||||||
self.console.print(f'[yellow b][INFO][/] Serves on {self.__ip}:{self.__port} for clients.')
|
now = Timestamp.now()
|
||||||
self.console.print(f'[yellow b][INFO][/] Receives notifications from producers at {self.__ip}:{self.__producer_port}.')
|
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:
|
async with self.__producer_socket, self.__client_socket:
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
@@ -72,7 +74,8 @@ class Server:
|
|||||||
|
|
||||||
async def handle_producer(self, reader, writer, notification_dir):
|
async def handle_producer(self, reader, writer, notification_dir):
|
||||||
addr = writer.get_extra_info('peername')
|
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:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -80,7 +83,8 @@ class Server:
|
|||||||
message = await self.receive_message(reader)
|
message = await self.receive_message(reader)
|
||||||
|
|
||||||
if title == '' and message == '':
|
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
|
break
|
||||||
|
|
||||||
await self.broadcast_message(title)
|
await self.broadcast_message(title)
|
||||||
@@ -89,12 +93,14 @@ class Server:
|
|||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
finally:
|
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()
|
writer.close()
|
||||||
|
|
||||||
async def handle_client(self, reader, writer):
|
async def handle_client(self, reader, writer):
|
||||||
addr = writer.get_extra_info('peername')
|
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)
|
self.__client_writers.append(writer)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -105,7 +111,8 @@ class Server:
|
|||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
finally:
|
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)
|
self.__client_writers.remove(writer)
|
||||||
writer.close()
|
writer.close()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class Timestamp:
|
||||||
|
@staticmethod
|
||||||
|
def now() -> str:
|
||||||
|
return str(datetime.now())
|
||||||
|
|
||||||
Reference in New Issue
Block a user