diff --git a/client.py b/client.py index 4abecd2..96b516f 100644 --- a/client.py +++ b/client.py @@ -13,6 +13,7 @@ from aiohttp import web from slackclient import SlackClient import hooks +import settings import slack_util """ @@ -172,7 +173,7 @@ class ClientWrapper(object): # If we get a coro back, then task it up and set consumption appropriately if coro is not None: - log.debug("Spawned task. Now {} running total.".format(len(asyncio.all_tasks()))) + logging.debug("Spawned task. Now {} running total.".format(len(asyncio.all_tasks()))) yield asyncio.create_task(_exception_printing_task(coro)) if hook.consumes: break @@ -253,7 +254,7 @@ class ClientWrapper(object): text = "_Block message. Open slack client to view_" # Begin constructing kwargs with fields that _must_ exist - kwargs = {"channel": channel_id, "text": text, "as_user": True} + kwargs = {"channel": channel_id, "text": text, "as_user": True, "parse": True} # Deduce thread stuff if thread: @@ -405,7 +406,9 @@ def get_slack() -> ClientWrapper: Miscellania """ -A, B, C = TypeVar("A"), TypeVar("B"), TypeVar("C") +A = TypeVar("A") +B = TypeVar("B") +C = TypeVar("C") # Prints exceptions instead of silently dropping them in async tasks diff --git a/main.py b/main.py index 7d5a901..6ab2288 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ import slack_util import logging -logging.basicConfig(filename="run.log", filemode="w", level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') +logging.basicConfig(filename=settings.LOGFILE, filemode="w", level=logging.DEBUG, format='#!# %(levelname)s - %(asctime)s \n%(message)s \n', datefmt='%m/%d/%Y %I:%M:%S %p') def main() -> None: wrap = client.get_slack() @@ -27,6 +27,7 @@ def main() -> None: # Add kill switch wrap.add_hook(management_commands.reboot_hook) + wrap.add_hook(management_commands.log_hook) # Add towel rolling wrap.add_hook(slavestothemachine.count_work_hook) diff --git a/plugins/management_commands.py b/plugins/management_commands.py index d68131e..1eb1373 100644 --- a/plugins/management_commands.py +++ b/plugins/management_commands.py @@ -2,6 +2,7 @@ from typing import Match import hooks import client +import settings import slack_util @@ -13,7 +14,54 @@ async def reboot_callback(event: slack_util.Event, match: Match) -> None: exit(0) +async def post_log_callback(event: slack_util.Event, match: Match) -> None: + # Get the last 500 lines of log of the specified severity or higher + count = 500 + lines = [] + + # numerically rank the debug severity + severity_codex = { + "CRITICAL": 50, + "ERROR": 40, + "WARNING": 30, + "INFO": 20, + "DEBUG": 10, + "NOTSET": 0 + } + curr_rating = 0 + + # Get the min rating if one exists + min_rating = 0 + rating_str = match.group(1).upper().strip() + for severity_name, severity_value in severity_codex.items(): + if severity_name in rating_str: + min_rating = severity_value + break + + with open(settings.LOGFILE, 'r') as f: + for line in f: + # Update the current rating if necessary + if line[:3] == "#!#": + for k, v in severity_codex.items(): + if k in line: + curr_rating = v + break + + # Add the line if its severity is at or above the required minimum + if curr_rating >= min_rating: + lines.append(line) + if len(lines) > count: + del lines[0] + + # Spew them out + client.get_slack().reply(event, "```" + ''.join(lines) + "```") + + # Make hooks reboot_hook = hooks.ChannelHook(reboot_callback, patterns=r"reboot", channel_whitelist=["#command-center"]) + +log_hook = hooks.ChannelHook(post_log_callback, + patterns=["post logs(.*)", "logs(.*)", "post_logs(.*)"], + channel_whitelist=["#botzone"]) diff --git a/settings.py b/settings.py index 0ea4e75..4085f0c 100644 --- a/settings.py +++ b/settings.py @@ -9,3 +9,5 @@ SINGLE_THREAD_TASKS = False # Note that this occasionally will give us warnings if SINGLE_THREAD_TAKS is False (which it usually is) # howver, these warnings are harmless, as regardless of if aa task is awaited it still does its job USE_ASYNC_DEBUG_MODE = False + +LOGFILE = "run.log" \ No newline at end of file