From cc0bc2676b8a2e25ecf5f30275850d405af49efc Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Sat, 18 May 2019 09:07:36 -0400 Subject: [PATCH] update logging --- sopel_modules/SpiceBot_Logs/ErrorDisplay.py | 4 +- sopel_modules/SpiceBot_Logs/Logs.py | 65 --------------------- sopel_modules/SpiceBot_Logs/Logs_Command.py | 18 +++--- 3 files changed, 11 insertions(+), 76 deletions(-) diff --git a/sopel_modules/SpiceBot_Logs/ErrorDisplay.py b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py index 7218672..543973e 100644 --- a/sopel_modules/SpiceBot_Logs/ErrorDisplay.py +++ b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py @@ -5,14 +5,14 @@ import sopel.module from sopel_modules.SpiceBot_Events.System import botevents -from .Logs import botlogs, stdio_logs_fetch +from .Logs import botlogs @sopel.module.event(botevents.BOT_STARTUPMONOLOGUE) @sopel.module.rule('.*') def bot_startup_monologue_start(bot, trigger): - debuglines = stdio_logs_fetch(bot) + debuglines = botlogs.stdio_logs_fetch(bot) searchphrasefound = [] for line in debuglines: diff --git a/sopel_modules/SpiceBot_Logs/Logs.py b/sopel_modules/SpiceBot_Logs/Logs.py index 0c5c043..76627cd 100644 --- a/sopel_modules/SpiceBot_Logs/Logs.py +++ b/sopel_modules/SpiceBot_Logs/Logs.py @@ -122,68 +122,3 @@ def join_log_channel(bot, trigger): else: botlogs.sopel_config["logging_channel"] = False botlogs.SpiceBot_Logs["queue"] = [] - - -def stdio_logs_fetch(bot): - - stdio_ignore = [] - for logtype in bot.memory['SpiceBot_Logs']["logs"].keys(): - stdio_ignore.append("[" + logtype + "]") - - logfile = os.path.os.path.join(bot.config.core.logdir, 'stdio.log') - - try: - log_file_lines = [] - log_file = open(logfile, 'r') - lines = log_file.readlines() - for line in lines: - log_file_lines.append(line) - log_file.close() - - currentline, linenumindex = 0, [] - for line in log_file_lines: - if line.startswith("Welcome to Sopel. Loading modules..."): - linenumindex.append(currentline) - currentline += 1 - last_start = max(linenumindex) - filelines = log_file_lines[last_start:] - except Exception as e: - debuglines = e - filelines = [] - - debuglines = [] - loadedmodules = [] - for line in filelines: - if line.startswith("Loaded:"): - loadedmodules.append(str(line).split("Loaded:")[-1]) - else: - if not line.startswith(tuple(stdio_ignore)) and not line.isspace(): - debuglines.append(str(line)) - loadedmodules = "Loaded: " + spicemanip.main(loadedmodules, 'andlist') - debuglines.insert(1, loadedmodules) - - return debuglines - - -def systemd_logs_fetch(bot): - servicepid = get_running_pid(bot) - debuglines = [] - for line in os.popen(str("sudo journalctl _PID=" + str(servicepid))).read().split('\n'): - line = str(line).split(str(os.uname()[1] + " "))[-1] - lineparts = str(line).split(": ") - del lineparts[0] - line = spicemanip.main(lineparts, 0) - if not line.isspace(): - debuglines.append(str(line)) - return debuglines - - -def get_running_pid(bot): - try: - filename = "/run/sopel/" + str(bot.nick) + ".pid" - with open(filename, 'r') as pid_file: - pidnum = int(pid_file.read()) - except Exception as e: - pidnum = e - pidnum = str(os.popen("systemctl show " + str(bot.nick) + " --property=MainPID").read()).split("=")[-1] - return pidnum diff --git a/sopel_modules/SpiceBot_Logs/Logs_Command.py b/sopel_modules/SpiceBot_Logs/Logs_Command.py index 443b08c..2f78af7 100644 --- a/sopel_modules/SpiceBot_Logs/Logs_Command.py +++ b/sopel_modules/SpiceBot_Logs/Logs_Command.py @@ -5,9 +5,9 @@ import sopel.module import spicemanip -from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match, similar_list +from .Logs import botlogs from sopel_modules.SpiceBot_Events.System import botevents -from .Logs import systemd_logs_fetch, stdio_logs_fetch +from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match, similar_list @botevents.check_ready([botevents.BOT_LOGS]) @@ -22,11 +22,11 @@ def bot_command_logs(bot, trigger): logtype = spicemanip.main(triggerargs, 1) or None if not logtype: - bot.osd("Current valid log(s) include: " + spicemanip.main(bot.memory['SpiceBot_Logs']["logs"].keys(), 'andlist'), trigger.sender, 'action') + bot.osd("Current valid log(s) include: " + spicemanip.main(botlogs.SpiceBot_Logs["list"].keys(), 'andlist'), trigger.sender, 'action') return - if not inlist(bot, logtype, bot.memory['SpiceBot_Logs']["logs"].keys()): - closestmatches = similar_list(bot, logtype, bot.memory['SpiceBot_Logs']["logs"].keys(), 10, 'reverse') + if not inlist(bot, logtype, botlogs.SpiceBot_Logs["list"].keys()): + closestmatches = similar_list(bot, logtype, botlogs.SpiceBot_Logs["list"].keys(), 10, 'reverse') if not len(closestmatches): bot.notice("No valid logs match " + str(logtype) + ".", trigger.nick) else: @@ -34,14 +34,14 @@ def bot_command_logs(bot, trigger): return - logtype = inlist_match(bot, logtype, bot.memory['SpiceBot_Logs']["logs"].keys()) + logtype = inlist_match(bot, logtype, botlogs.SpiceBot_Logs["list"].keys()) if logtype == "Sopel_systemd": - logindex = systemd_logs_fetch(bot) + logindex = botlogs.systemd_logs_fetch(bot) elif logtype == "Sopel_stdio": - logindex = stdio_logs_fetch(bot) + logindex = botlogs.stdio_logs_fetch(bot) else: - logindex = bot.memory['SpiceBot_Logs']["logs"][logtype] + logindex = botlogs.SpiceBot_Logs["list"][logtype] if not len(logindex): bot.osd("No logs found for " + str(logtype) + ".")