From 211decd2aa99f3c408fe33e5726f80a194d50c3e Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 5 Jul 2015 13:44:48 -0700 Subject: [PATCH] everything: Improve logging --- main.py | 6 +++--- plugins/commands.py | 7 ++++--- plugins/hooks.py | 5 +++-- protocols/inspircd.py | 4 ++-- tests/test_proto_common.py | 5 +++-- utils.py | 35 ++++++++++++++--------------------- 6 files changed, 29 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index d7b79cc..218ab8c 100755 --- a/main.py +++ b/main.py @@ -83,12 +83,12 @@ class Irc(): log.error('Failed to load plugin %r: the plugin could not be found.', plugin) else: log.error('Failed to load plugin %r: import error %s', plugin, str(e)) - print("loaded plugins: %s" % self.loaded) + log.info("loaded plugins: %s", self.loaded) if __name__ == '__main__': - print('PyLink starting...') + log.info('PyLink starting...') if conf['login']['password'] == 'changeme': - print("You have not set the login details correctly! Exiting...") + log.critical("You have not set the login details correctly! Exiting...") sys.exit(2) protoname = conf['server']['protocol'] diff --git a/plugins/commands.py b/plugins/commands.py index b0b9db3..e55153f 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -6,12 +6,13 @@ import logging sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import utils from conf import conf +from log import log @utils.add_cmd def debug(irc, source, args): - print('user index: %s' % irc.users) - print('server index: %s' % irc.servers) - print('channels index: %s' % irc.channels) + log.debug('user index: %s' % irc.users) + log.debug('server index: %s' % irc.servers) + log.debug('channels index: %s' % irc.channels) utils.msg(irc, source, 'Debug info printed to console.') @utils.add_cmd diff --git a/plugins/hooks.py b/plugins/hooks.py index 1312cd2..851a949 100644 --- a/plugins/hooks.py +++ b/plugins/hooks.py @@ -2,11 +2,12 @@ import sys, os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import utils +from log import log def hook_join(irc, source, command, args): channel = args['channel'] users = args['users'] - print('%s joined channel %s (JOIN hook caught)' % (users, channel)) + log.info('%s joined channel %s (JOIN hook caught)' % (users, channel)) utils.add_hook(hook_join, 'JOIN') def hook_privmsg(irc, source, command, args): @@ -14,5 +15,5 @@ def hook_privmsg(irc, source, command, args): text = args['text'] if utils.isChannel(channel) and irc.pseudoclient.nick in text: utils.msg(irc, channel, 'hi there!') - print('%s said my name on channel %s (PRIVMSG hook caught)' % (source, channel)) + log.info('%s said my name on channel %s (PRIVMSG hook caught)' % (source, channel)) utils.add_hook(hook_privmsg, 'PRIVMSG') diff --git a/protocols/inspircd.py b/protocols/inspircd.py index c83c57d..d395de2 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -417,11 +417,11 @@ def handle_events(irc, data): hook_cmd = command if command in hook_map: hook_cmd = hook_map[command] - print('Parsed args %r received from %s handler (calling hook %s)' % (parsed_args, command, hook_cmd)) + log.debug('Parsed args %r received from %s handler (calling hook %s)', parsed_args, command, hook_cmd) # Iterate over hooked functions, catching errors accordingly for hook_func in utils.command_hooks[hook_cmd]: try: - print('Calling function %s' % hook_func) + log.debug('Calling function %s', hook_func) hook_func(irc, numeric, command, parsed_args) except Exception: # We don't want plugins to crash our servers... diff --git a/tests/test_proto_common.py b/tests/test_proto_common.py index e4d6d5d..84a3bf9 100644 --- a/tests/test_proto_common.py +++ b/tests/test_proto_common.py @@ -1,6 +1,7 @@ import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from log import log import main import classes @@ -35,12 +36,12 @@ class FakeIRC(main.Irc): def run(self, data): """Queues a message to the fake IRC server.""" - print('-> ' + data) + log.debug('-> ' + data) self.proto.handle_events(self, data) def send(self, data): self.messages.append(data) - print('<- ' + data) + log.debug('<- ' + data) def takeMsgs(self): """Returns a list of messages sent by the protocol module since diff --git a/utils.py b/utils.py index 48a05dc..0acf20b 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,8 @@ import string import re from collections import defaultdict +from log import log + global bot_commands, command_hooks # This should be a mapping of command names to functions bot_commands = {} @@ -98,13 +100,12 @@ def parseModes(irc, target, args): return ValueError('No modes supplied in parseModes query: %r' % modes) args = args[1:] if usermodes: + log.debug('Using irc.umodes for this query: %s', irc.umodes) supported_modes = irc.umodes else: + log.debug('Using irc.cmodes for this query: %s', irc.cmodes) supported_modes = irc.cmodes - print('supported modes: %s' % supported_modes) res = [] - for x in ('A', 'B', 'C', 'D'): - print('%s modes: %s' % (x, supported_modes['*'+x])) for mode in modestring: if mode in '+-': prefix = mode @@ -112,62 +113,54 @@ def parseModes(irc, target, args): arg = None if mode in (supported_modes['*A'] + supported_modes['*B']): # Must have parameter. - print('%s: Must have parameter.' % mode) + log.debug('%s: This mode must have parameter.', mode) arg = args.pop(0) elif mode in irc.prefixmodes and not usermodes: # We're setting a prefix mode on someone (e.g. +o user1) - print('%s: prefixmode.' % mode) + log.debug('%s: This mode is a prefix mode.', mode) arg = args.pop(0) elif prefix == '+' and mode in supported_modes['*C']: # Only has parameter when setting. - print('%s: Only has parameter when setting.' % mode) + log.debug('%s: Only has parameter when setting.', mode) arg = args.pop(0) res.append((prefix + mode, arg)) return res def applyModes(irc, target, changedmodes): usermodes = not isChannel(target) - print('usermodes? %s' % usermodes) + log.debug('usermodes? %s' % usermodes) if usermodes: modelist = irc.users[target].modes else: modelist = irc.channels[target].modes - print('Initial modelist: %s' % modelist) - print('Changedmodes: %r' % changedmodes) + log.debug('(%s) Applying modes %r on %s (initial modelist: %s)', irc.name, changedmodes, target, modelist) for mode in changedmodes: if not usermodes: pmode = '' for m in ('owner', 'admin', 'op', 'halfop', 'voice'): if m in irc.cmodes and mode[0][1] == irc.cmodes[m]: pmode = m+'s' - print('pmode? %s' % pmode) if pmode: - print('pmode == True') - print(mode) - print(irc.channels[target].prefixmodes) pmodelist = irc.channels[target].prefixmodes[pmode] - print(pmodelist) - print('Initial pmodelist: %s' % pmodelist) + log.debug('(%s) Initial prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) if mode[0][0] == '+': pmodelist.add(mode[1]) - print('+') else: pmodelist.discard(mode[1]) - print('-') - print('Final pmodelist: %s' % pmodelist) + log.debug('(%s) Final prefixmodes list: %s', irc.name, irc.channels[target].prefixmodes) if mode[0][1] in irc.prefixmodes: # Ignore other prefix modes such as InspIRCd's +Yy continue if mode[0][0] == '+': # We're adding a mode modelist.add(mode) - print('Adding mode %r' % str(mode)) + log.debug('(%s) Adding mode %r on %s', irc.name, mode, target) else: # We're removing a mode mode[0] = mode[0].replace('-', '+') modelist.discard(mode) - print('Removing mode %r' % str(mode)) - print('Final modelist: %s' % modelist) + log.debug('(%s) Removing mode %r on %s', irc.name, mode, target) + log.debug('Final modelist: %s' % modelist) def joinModes(modes): modelist = ''