3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +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
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!

18
main.py
View File

@ -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()

View File

@ -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)

View File

@ -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]