3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

WIP: use a proper logging module

This commit is contained in:
James Lu 2015-05-30 22:35:00 -07:00
parent d158143cea
commit 17fc05cc9a
4 changed files with 27 additions and 17 deletions

View File

@ -4,6 +4,8 @@ bot:
user: pylink user: pylink
realname: PyLink Service Client realname: PyLink Service Client
prefix: "@" prefix: "@"
# Console log verbosity: see https://docs.python.org/3/library/logging.html#logging-levels
loglevel: DEBUG
login: login:
# PyLink administrative login - Change this, or the service will not start! # PyLink administrative login - Change this, or the service will not start!

18
main.py
View File

@ -3,18 +3,20 @@
import yaml import yaml
import imp import imp
import os import os
import threading
import socket import socket
import multiprocessing
import time import time
import sys import sys
import logging
import proto import proto
print('PyLink starting...')
with open("config.yml", 'r') as f: with open("config.yml", 'r') as f:
conf = yaml.load(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': # if conf['login']['password'] == 'changeme':
# print("You have not set the login details correctly! Exiting...") # print("You have not set the login details correctly! Exiting...")
@ -33,7 +35,7 @@ class Irc():
ip = self.serverdata["ip"] ip = self.serverdata["ip"]
port = self.serverdata["port"] port = self.serverdata["port"]
self.sid = self.serverdata["sid"] 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 = socket.socket()
self.socket.connect((ip, port)) self.socket.connect((ip, port))
@ -54,16 +56,16 @@ class Irc():
break break
while '\n' in buf: while '\n' in buf:
line, buf = buf.split('\n', 1) line, buf = buf.split('\n', 1)
print("<- {}".format(line)) logger.debug("<- {}".format(line))
proto.handle_events(self, line) proto.handle_events(self, line)
except socket.error as e: except socket.error as e:
print('Received socket.error: %s, exiting.' % str(e)) logger.error('Received socket.error: %s, exiting.' % str(e))
break break
sys.exit(1) sys.exit(1)
def send(self, data): def send(self, data):
data = data.encode("utf-8") + b"\n" 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) self.socket.send(data)
def load_plugins(self): def load_plugins(self):
@ -74,7 +76,7 @@ class Irc():
for plugin in to_load: for plugin in to_load:
moduleinfo = imp.find_module(plugin, plugins_folder) moduleinfo = imp.find_module(plugin, plugins_folder)
self.loaded.append(imp.load_source(plugin, moduleinfo[1])) 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() irc_obj = Irc()

View File

@ -1,8 +1,13 @@
# commands.py: base PyLink commands # 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__)))) sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import proto import proto
logger = logging.getLogger('pylinklogger')
@proto.add_cmd @proto.add_cmd
def tell(irc, source, args): def tell(irc, source, args):
try: try:
@ -18,5 +23,5 @@ def tell(irc, source, args):
@proto.add_cmd @proto.add_cmd
def debug(irc, source, args): def debug(irc, source, args):
proto._sendFromUser(irc, 'NOTICE %s :Debug info printed to console.' % (source)) proto._sendFromUser(irc, 'NOTICE %s :Debug info printed to console.' % (source))
print(irc.users) logger.debug(irc.users)
print(irc.servers) logger.debug(irc.servers)

View File

@ -1,11 +1,12 @@
import threading
import socket import socket
import time import time
import re
import sys import sys
from utils import * from utils import *
import logging
from copy import copy from copy import copy
logger = logging.getLogger('pylinklogger')
global bot_commands global bot_commands
# This should be a mapping of command names to functions # This should be a mapping of command names to functions
bot_commands = {} bot_commands = {}
@ -99,7 +100,7 @@ def handle_privmsg(irc, source, command, args):
_sendFromUser(irc, 'PRIVMSG %s :unknown command %r' % (source, cmd)) _sendFromUser(irc, 'PRIVMSG %s :unknown command %r' % (source, cmd))
def handle_error(irc, numeric, command, args): def handle_error(irc, numeric, command, args):
print('Received an ERROR, killing!') logger.error('Received an ERROR, killing!')
irc.connected = False irc.connected = False
sys.exit(1) sys.exit(1)
@ -156,15 +157,15 @@ def handle_nick(irc, numeric, command, args):
def handle_squit(irc, numeric, command, args): def handle_squit(irc, numeric, command, args):
# :70M SQUIT 1ML :Server quit by GL!gl@0::1 # :70M SQUIT 1ML :Server quit by GL!gl@0::1
split_server = args[0] split_server = args[0]
print('Splitting server %s' % split_server) logger.info('Splitting server %s' % split_server)
# Prevent RuntimeError: dictionary changed size during iteration # Prevent RuntimeError: dictionary changed size during iteration
old_servers = copy(irc.servers) old_servers = copy(irc.servers)
for sid, data in old_servers.items(): for sid, data in old_servers.items():
if data.uplink == split_server: 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]) handle_squit(irc, sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid])
for user in irc.servers[split_server].users: 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.users[user]
del irc.servers[split_server] del irc.servers[split_server]