From 222acfb948d263e7c771bb1d2366998aaaec0532 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 9 May 2019 11:30:41 -0400 Subject: [PATCH] stdio versus systemd --- sopel_modules/SpiceBot_Logs/ErrorDisplay.py | 4 +- sopel_modules/SpiceBot_Logs/Logs.py | 42 ++++++++++++++++----- sopel_modules/SpiceBot_Logs/Logs_Command.py | 10 +++-- sopel_modules/SpiceBot_SBTools/__init__.py | 2 +- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/sopel_modules/SpiceBot_Logs/ErrorDisplay.py b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py index b676415..c31ab53 100644 --- a/sopel_modules/SpiceBot_Logs/ErrorDisplay.py +++ b/sopel_modules/SpiceBot_Logs/ErrorDisplay.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals, absolute_import, division, print_functi import sopel.module from sopel_modules.SpiceBot_Events.System import bot_events_recieved -from .Logs import errordisplay_fetch +from .Logs import systemd_logs_fetch @sopel.module.event('2005') @@ -13,7 +13,7 @@ from .Logs import errordisplay_fetch def bot_startup_monologue_start(bot, trigger): bot_events_recieved(bot, trigger.event) - debuglines = errordisplay_fetch(bot) + debuglines = systemd_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 474e5a7..ef5d18d 100644 --- a/sopel_modules/SpiceBot_Logs/Logs.py +++ b/sopel_modules/SpiceBot_Logs/Logs.py @@ -35,7 +35,7 @@ def setup(bot): def bot_logs_setup_check(bot): if 'SpiceBot_Logs' not in bot.memory: - bot.memory['SpiceBot_Logs'] = {"logs": {"Sopel_systemd": []}, "queue": []} + bot.memory['SpiceBot_Logs'] = {"logs": {"Sopel_systemd": [], "Sopel_stdio": []}, "queue": []} def shutdown(bot): @@ -66,18 +66,40 @@ def join_log_channel(bot, trigger): return -def errordisplay_fetch(bot): +def stdio_logs_fetch(bot): + + try: + log_file_lines = [] + log_file = open("/home/sopel.sopel/logs/stdio.log", '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) + debuglines = log_file_lines[last_start:] + except Exception as e: + debuglines = e + debuglines = [] + + 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'): - if not str(line).startswith("-- Logs begin at"): - line = str(line).split(str(os.uname()[1] + " "))[-1] - if not str(line).startswith("sudo"): - lineparts = str(line).split(": ") - del lineparts[0] - line = spicemanip.main(lineparts, 0) - if not line.isspace(): - debuglines.append(str(line)) + 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 diff --git a/sopel_modules/SpiceBot_Logs/Logs_Command.py b/sopel_modules/SpiceBot_Logs/Logs_Command.py index ad9436f..2d6ac9c 100644 --- a/sopel_modules/SpiceBot_Logs/Logs_Command.py +++ b/sopel_modules/SpiceBot_Logs/Logs_Command.py @@ -8,7 +8,7 @@ import spicemanip from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, command_permissions_check, inlist, inlist_match from sopel_modules.SpiceBot_Events.System import bot_events_check -from .Logs import errordisplay_fetch +from .Logs import systemd_logs_fetch @sopel.module.nickname_commands('logs', 'debug') @@ -30,10 +30,12 @@ def bot_command_action(bot, trigger): logtype = inlist_match(bot, logtype, bot.memory['SpiceBot_Logs'].keys()) - if logtype != "Sopel_systemd": - logindex = bot.memory['SpiceBot_Logs']["logs"][logtype] + if logtype == "Sopel_systemd": + logindex = systemd_logs_fetch(bot) + elif logtype == "Sopel_stdio": + logindex = stdio_logs_fetch(bot) else: - logindex = errordisplay_fetch(bot) + logindex = bot.memory['SpiceBot_Logs']["logs"][logtype] if not len(logindex): bot.osd("No logs found for " + str(logtype) + ".") diff --git a/sopel_modules/SpiceBot_SBTools/__init__.py b/sopel_modules/SpiceBot_SBTools/__init__.py index 321ee48..fb4054b 100644 --- a/sopel_modules/SpiceBot_SBTools/__init__.py +++ b/sopel_modules/SpiceBot_SBTools/__init__.py @@ -99,7 +99,7 @@ def command_permissions_check(bot, trigger, privslist): def bot_logging(bot, logtype, logentry, stdio=False): if 'SpiceBot_Logs' not in bot.memory: - bot.memory['SpiceBot_Logs'] = {"logs": {"Sopel_systemd": []}, "queue": []} + bot.memory['SpiceBot_Logs'] = {"logs": {"Sopel_systemd": [], "Sopel_stdio": []}, "queue": []} logmessage = "[" + logtype + "] " + logentry + ""