added logs.py and changed EventHook objects to know their name.

This commit is contained in:
jesopo 2016-07-13 07:31:09 +01:00
parent e604a8de31
commit 369b784a0d
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
2 changed files with 23 additions and 4 deletions

View file

@ -1,7 +1,8 @@
class Event(object):
def __init__(self, bot, **kwargs):
def __init__(self, bot, subevent, **kwargs):
self.bot = bot
self.subevent = subevent
self.kwargs = kwargs
self.eaten = False
def __getitem__(self, key):
@ -34,8 +35,9 @@ class MultipleEventHook(object):
event_hook.call(max, **kwargs)
class EventHook(object):
def __init__(self, bot):
def __init__(self, bot, name=None):
self.bot = bot
self.name = name
self._children = {}
self._hooks = []
self._hook_notify = None
@ -56,7 +58,7 @@ class EventHook(object):
return multiple_event_hook
return self.get_child(subevent)
def call(self, max=None, **kwargs):
event = Event(self.bot, **kwargs)
event = Event(self.bot, self.name, **kwargs)
if self._call_notify:
self._call_notify(self, event)
called = 0
@ -72,7 +74,8 @@ class EventHook(object):
def get_child(self, child_name):
child_name_lower = child_name.lower()
if not child_name_lower in self._children:
self._children[child_name_lower] = EventHook(self.bot)
self._children[child_name_lower] = EventHook(self.bot,
child_name)
if self._child_notify:
self._child_notify(self, self._children[
child_name_lower])

16
modules/logs.py Normal file
View file

@ -0,0 +1,16 @@
import datetime
class Module(object):
def __init__(self, bot):
bot.events.on("log").on("info", "warn", "error").hook(self.log)
def timestamp(self):
return datetime.datetime.utcnow().isoformat()+"Z"
def log(self, event):
log_level = event.name
timestamp = self.timestamp()
message = event["message"]
with open("bot.log", "a") as log_file:
log_file.write("%s [%s] %s" % (timestamp, log_level,
message))