minetestbot-modules/log.py

48 lines
1.3 KiB
Python
Raw Normal View History

2014-07-19 11:28:28 -07:00
#!/usr/bin/env python
"""
log.py - Phenny Logging Module
Copyright 2014, sfan5
Licensed under GNU General Public License v2.0
2014-07-19 11:28:28 -07:00
"""
import time
loglevels = {
2014-07-20 07:13:59 -07:00
'TEXT': 'TXT', # TEXT: purely informal message that appears in masses e.g. a command being executed
'EVENT': 'EVT', # EVENT: like TEXT events but more likely someone wants to see those
'ACTION': 'ACT', # ACTION: something the bot decided on doing automatically, requires attention
'WARNING': 'WRN', # WARNING: a warning
2014-07-19 11:28:28 -07:00
}
actionchannel = "##minetestbot"
actionhighlight = "sfan5"
2015-07-02 01:51:00 -07:00
def log(level, text, phenny=None):
2014-07-20 07:13:59 -07:00
level = level.upper()
2014-07-20 12:20:39 -07:00
f = open("bot.log", "ab")
f.write(bytes(time.strftime("%F %H:%M:%S %z") + "\t" + loglevels[level] + "\t" + text + "\n", 'utf-8', 'ignore'))
2014-07-20 07:13:59 -07:00
f.close()
2015-07-02 01:51:00 -07:00
if level == 'ACTION' and phenny is not None:
2014-07-20 07:13:59 -07:00
phenny.write(['PRIVMSG', actionchannel], actionhighlight + ": " + text)
2014-07-19 11:28:28 -07:00
def fmt_user(input):
2014-07-20 07:13:59 -07:00
return "%s(%s)" % (input.nick, input.hostmask)
2014-07-19 11:28:28 -07:00
class SomeObject(object):
2014-07-20 07:13:59 -07:00
pass
2014-07-19 11:28:28 -07:00
log_api = SomeObject()
log_api.log = log
log_api.fmt_user = fmt_user
_export = {
2014-07-20 07:13:59 -07:00
'log': log_api,
2014-07-19 11:28:28 -07:00
}
def log_text(phenny, input, func):
if func.event != "PRIVMSG": return True
log("text", "%s executes command '%s'" % (fmt_user(input), input.group(0)), phenny)
return True
log_text.hook = True