Browse Source

Make logging more consistent

master
Forest Belton 3 years ago
parent
commit
f915fd2265
7 changed files with 27 additions and 12 deletions
  1. +1
    -1
      paircd/client.py
  2. +2
    -1
      paircd/handle.py
  3. +7
    -4
      paircd/handler/join.py
  4. +2
    -3
      paircd/handler/nick.py
  5. +4
    -1
      paircd/handler/privmsg.py
  6. +4
    -2
      paircd/handler/user.py
  7. +7
    -0
      paircd/log.py

+ 1
- 1
paircd/client.py View File

@ -1,6 +1,6 @@
from asyncio import StreamReader, StreamWriter, Queue from asyncio import StreamReader, StreamWriter, Queue
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any, Set
from typing import Set
@dataclass @dataclass

+ 2
- 1
paircd/handle.py View File

@ -2,6 +2,7 @@ import logging
from typing import Any, Dict from typing import Any, Dict
from paircd.client import Client from paircd.client import Client
from paircd.log import log_client
from paircd.message import Message from paircd.message import Message
from paircd.server import Server from paircd.server import Server
@ -16,6 +17,6 @@ def register_cmd_handler(cmd: str, argc: int, handler) -> None:
async def handle_cmd(server: Server, client: Client, msg: Message) -> None: async def handle_cmd(server: Server, client: Client, msg: Message) -> None:
if msg.cmd not in CMD_HANDLERS: if msg.cmd not in CMD_HANDLERS:
logging.warning(f"Unknown command: {msg.cmd}")
log_client(client, f"used unknown command {msg.cmd}", level=logging.WARN)
return return
await CMD_HANDLERS[msg.cmd](server, client, msg) await CMD_HANDLERS[msg.cmd](server, client, msg)

+ 7
- 4
paircd/handler/join.py View File

@ -3,23 +3,26 @@ import logging
from paircd.channel import Channel from paircd.channel import Channel
from paircd.client import Client from paircd.client import Client
from paircd.log import log_client
from paircd.message import Message from paircd.message import Message
from paircd.server import Server from paircd.server import Server
async def handle_join(server: Server, client: Client, msg: Message) -> None: async def handle_join(server: Server, client: Client, msg: Message) -> None:
channel_name = msg.args[0]
if not client.registered: if not client.registered:
raise RuntimeError("JOIN: not registered")
log_client(client, "join: not registered", level=logging.WARN)
return
channel_name = msg.args[0]
if not channel_name.startswith("#"): if not channel_name.startswith("#"):
raise RuntimeError("invalid channel name")
log_client(client, "tried to join invalid channel", level=logging.WARN)
return
channel = server.get_channel_by_name(channel_name) channel = server.get_channel_by_name(channel_name)
channel.add_client(client) channel.add_client(client)
client.channels.add(channel_name) client.channels.add(channel_name)
logging.info(f"{client.hostname} ({client.id()}) joined {channel_name}")
log_client(client, f"joined {channel_name}")
await channel.msg_queue.put( await channel.msg_queue.put(
Message(cmd="JOIN", args=[channel_name], prefix=client.id()).encode() Message(cmd="JOIN", args=[channel_name], prefix=client.id()).encode()

+ 2
- 3
paircd/handler/nick.py View File

@ -1,6 +1,5 @@
import logging
from paircd.client import Client from paircd.client import Client
from paircd.log import log_client
from paircd.message import Message from paircd.message import Message
from paircd.server import Server from paircd.server import Server
@ -17,4 +16,4 @@ async def handle_nick(server: Server, client: Client, msg: Message) -> None:
if client.username and client.realname: if client.username and client.realname:
client.registered = True client.registered = True
logging.info(f"{client.hostname} ({client.id()}) registered")
log_client(client, "registered")

+ 4
- 1
paircd/handler/privmsg.py View File

@ -1,3 +1,6 @@
import logging
from paircd.log import log_client
from paircd.client import Client from paircd.client import Client
from paircd.message import Message from paircd.message import Message
from paircd.server import Server from paircd.server import Server
@ -19,4 +22,4 @@ async def handle_privmsg(server: Server, client: Client, msg: Message) -> None:
channel.msg_queue.put_nowait(msg) channel.msg_queue.put_nowait(msg)
return return
raise RuntimeError(f"Unknown recipient {recipient}")
log_client(client, "unknown recipient", level=logging.WARN)

+ 4
- 2
paircd/handler/user.py View File

@ -1,17 +1,19 @@
import logging import logging
from paircd.client import Client from paircd.client import Client
from paircd.log import log_client
from paircd.message import Message from paircd.message import Message
from paircd.server import Server from paircd.server import Server
async def handle_user(server: Server, client: Client, msg: Message) -> None: async def handle_user(server: Server, client: Client, msg: Message) -> None:
if client.registered: if client.registered:
raise RuntimeError("USER command issued after registration")
log_client(client, "USER issued after registration", level=logging.WARN)
return
client.username = msg.args[0] client.username = msg.args[0]
client.realname = msg.args[3] client.realname = msg.args[3]
if client.nickname: if client.nickname:
client.registered = True client.registered = True
logging.info(f"{client.hostname} ({client.id()}) registered")
log_client(client, "registered")

+ 7
- 0
paircd/log.py View File

@ -0,0 +1,7 @@
import logging
from paircd.client import Client
def log_client(client: Client, msg: str, level: int = logging.INFO) -> None:
logging.log(level, f"{client.hostname} ({client.id()}) {msg}")

Loading…
Cancel
Save