From 17fc05cc9ade49ae6a9f3dfd49fd24cf9de3f067 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 30 May 2015 22:35:00 -0700 Subject: [PATCH] WIP: use a proper logging module --- config.yml.example | 2 ++ main.py | 18 ++++++++++-------- plugins/commands.py | 11 ++++++++--- proto.py | 13 +++++++------ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/config.yml.example b/config.yml.example index 0b52fdf..76daa56 100644 --- a/config.yml.example +++ b/config.yml.example @@ -4,6 +4,8 @@ bot: user: pylink realname: PyLink Service Client prefix: "@" + # Console log verbosity: see https://docs.python.org/3/library/logging.html#logging-levels + loglevel: DEBUG login: # PyLink administrative login - Change this, or the service will not start! diff --git a/main.py b/main.py index 629d75b..14aa58d 100755 --- a/main.py +++ b/main.py @@ -3,18 +3,20 @@ import yaml import imp import os -import threading import socket -import multiprocessing import time import sys +import logging import proto -print('PyLink starting...') with open("config.yml", 'r') as f: conf = yaml.load(f) +logger = logging.getLogger('pylinklogger') +# logger.setLevel(getattr(logging, conf['bot']['loglevel'])) +logger.info('PyLink starting...') + # if conf['login']['password'] == 'changeme': # print("You have not set the login details correctly! Exiting...") @@ -33,7 +35,7 @@ class Irc(): ip = self.serverdata["ip"] port = self.serverdata["port"] self.sid = self.serverdata["sid"] - print("Connecting to network %r on %s:%s" % (self.name, ip, port)) + logger.info("Connecting to network %r on %s:%s" % (self.name, ip, port)) self.socket = socket.socket() self.socket.connect((ip, port)) @@ -54,16 +56,16 @@ class Irc(): break while '\n' in buf: line, buf = buf.split('\n', 1) - print("<- {}".format(line)) + logger.debug("<- {}".format(line)) proto.handle_events(self, line) except socket.error as e: - print('Received socket.error: %s, exiting.' % str(e)) + logger.error('Received socket.error: %s, exiting.' % str(e)) break sys.exit(1) def send(self, data): data = data.encode("utf-8") + b"\n" - print("-> {}".format(data.decode("utf-8").strip("\n"))) + logger.debug("-> {}".format(data.decode("utf-8").strip("\n"))) self.socket.send(data) def load_plugins(self): @@ -74,7 +76,7 @@ class Irc(): for plugin in to_load: moduleinfo = imp.find_module(plugin, plugins_folder) self.loaded.append(imp.load_source(plugin, moduleinfo[1])) - print("loaded plugins: %s" % self.loaded) + logger.info("loaded plugins: %s" % self.loaded) irc_obj = Irc() diff --git a/plugins/commands.py b/plugins/commands.py index 15f04a7..7d59d06 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -1,8 +1,13 @@ # commands.py: base PyLink commands -import sys, os +import sys +import os +import logging + sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import proto +logger = logging.getLogger('pylinklogger') + @proto.add_cmd def tell(irc, source, args): try: @@ -18,5 +23,5 @@ def tell(irc, source, args): @proto.add_cmd def debug(irc, source, args): proto._sendFromUser(irc, 'NOTICE %s :Debug info printed to console.' % (source)) - print(irc.users) - print(irc.servers) + logger.debug(irc.users) + logger.debug(irc.servers) diff --git a/proto.py b/proto.py index 4538d08..dc7225a 100644 --- a/proto.py +++ b/proto.py @@ -1,11 +1,12 @@ -import threading import socket import time -import re import sys from utils import * +import logging from copy import copy +logger = logging.getLogger('pylinklogger') + global bot_commands # This should be a mapping of command names to functions bot_commands = {} @@ -99,7 +100,7 @@ def handle_privmsg(irc, source, command, args): _sendFromUser(irc, 'PRIVMSG %s :unknown command %r' % (source, cmd)) def handle_error(irc, numeric, command, args): - print('Received an ERROR, killing!') + logger.error('Received an ERROR, killing!') irc.connected = False sys.exit(1) @@ -156,15 +157,15 @@ def handle_nick(irc, numeric, command, args): def handle_squit(irc, numeric, command, args): # :70M SQUIT 1ML :Server quit by GL!gl@0::1 split_server = args[0] - print('Splitting server %s' % split_server) + logger.info('Splitting server %s' % split_server) # Prevent RuntimeError: dictionary changed size during iteration old_servers = copy(irc.servers) for sid, data in old_servers.items(): if data.uplink == split_server: - print('Server %s also hosts server %s, splitting that too?!' % (split_server, sid)) + logger.info('Server %s also hosts server %s, splitting that too...' % (split_server, sid)) handle_squit(irc, sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid]) for user in irc.servers[split_server].users: - print("Removing user %s from server %s" % (user, split_server)) + logger.debug("Removing user %s from server %s" % (user, split_server)) del irc.users[user] del irc.servers[split_server]