From 0624fa59b29414d7a284411021093a8e3f05efe2 Mon Sep 17 00:00:00 2001 From: Michael Geitz Date: Wed, 30 Mar 2022 21:21:12 -0400 Subject: [PATCH] Log All Line Matches (#113) :book: * Log All Line Matches - Adds a new debug level - Adds file size limits on debug files * fix who_player matched bug --- README.md | 2 +- eqa/eqalert.py | 27 ++++++++++++--- eqa/lib/action.py | 88 ++++++++++++++++++++++++++++++++++++++++++----- eqa/lib/config.py | 2 +- eqa/lib/parser.py | 2 +- setup.py | 2 +- 6 files changed, 106 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b2eeca70..fec77b3a 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ An Everquest Emulator Log Parser with NCurses Interface for Linux Install from pypi ```sh $ # Install Stable -$ pip3 install eqalert==2.4.10 +$ pip3 install eqalert==2.5.5 $ $ # Install whatever I just pushed to pypi $ pip3 install eqalert diff --git a/eqa/eqalert.py b/eqa/eqalert.py index 36a36282..629828a1 100755 --- a/eqa/eqalert.py +++ b/eqa/eqalert.py @@ -27,7 +27,6 @@ import threading import time import queue -from datetime import datetime import eqa.lib.action as eqa_action import eqa.lib.config as eqa_config @@ -79,9 +78,14 @@ def startup(base_path): # Set log file logging.basicConfig(filename=log_path + "eqalert.log", level=logging.INFO) + # Make the debug directory + if not os.path.exists(base_path + "log/debug/"): + print(" - making a place for optional debug logs") + os.makedirs(base_path + "log/debug/") + # Make the sound directory if not os.path.exists(sound_path): - print(" - making some sounds") + print(" - making a home for alert sounds") os.makedirs(sound_path) # Make the tmp sound directory @@ -116,8 +120,9 @@ def startup(base_path): elif not config["settings"]["version"] == str( pkg_resources.get_distribution("eqalert").version ): + old_version = str(config["settings"]["version"]).replace(".", "-") archive_config = ( - base_path + "config_" + str(datetime.now().date()) + ".json" + base_path + "config_" + old_version + ".json" ) print("Your config.json was generated by a older version of eqalert.") print("Archiving old config to " + archive_config) @@ -558,11 +563,23 @@ def system_debug(base_path, state, display_q, sound_q, new_message): eqa_settings.eqa_time(), "event", "events", - "Debug mode enabled", + "Logging all unmatched lines", ) ) - sound_q.put(eqa_struct.sound("speak", "Debug mode enabled")) + sound_q.put(eqa_struct.sound("speak", "Logging all unmatched lines")) elif state.debug == "true" and new_message.rx == "toggle": + state.set_debug("all") + eqa_config.set_last_state(state, base_path) + display_q.put( + eqa_struct.display( + eqa_settings.eqa_time(), + "event", + "events", + "Logging all line types", + ) + ) + sound_q.put(eqa_struct.sound("speak", "Logging all line types")) + elif state.debug == "all" and new_message.rx == "toggle": state.set_debug("false") eqa_config.set_last_state(state, base_path) display_q.put( diff --git a/eqa/lib/action.py b/eqa/lib/action.py index bdebe995..544a4e97 100644 --- a/eqa/lib/action.py +++ b/eqa/lib/action.py @@ -22,6 +22,8 @@ import sys import time import re +import os +from datetime import datetime import eqa.lib.config as eqa_config import eqa.lib.settings as eqa_settings @@ -60,8 +62,11 @@ def process( check_line = new_message.payload # Line specific checks - if line_type == "undetermined" and state.debug == "true": - action_undetermined(check_line, base_path) + if state.debug != "false": + if line_type == "undetermined": + action_undetermined(check_line, base_path) + if state.debug == "all": + action_matched(line_type, check_line, base_path) elif line_type == "location": action_location(system_q, check_line) elif line_type == "direction": @@ -98,7 +103,13 @@ def process( action_you_say_commands(system_q, check_line, config, mute_list) elif line_type == "you_new_zone": action_you_new_zone( - base_path, system_q, display_q, sound_q, state, config, check_line + base_path, + system_q, + display_q, + sound_q, + state, + config, + check_line, ) # If line_type is a parsable type @@ -736,7 +747,9 @@ def action_you_char_bound(system_q, check_line): ) -def action_you_new_zone(base_path, system_q, display_q, sound_q, state, config, check_line): +def action_you_new_zone( + base_path, system_q, display_q, sound_q, state, config, check_line +): """Perform actions for you new zone line types""" try: @@ -790,13 +803,72 @@ def action_you_new_zone(base_path, system_q, display_q, sound_q, state, config, ) +def action_matched(line_type, line, base_path): + """Debug function to log all log lines and matches log lines""" + + try: + + matched_log = base_path + "log/debug/matched-lines.txt" + if os.path.exists(matched_log): + file_size = os.path.getsize(matched_log) + if file_size > 5000000: + version = str(pkg_resources.get_distribution("eqalert").version).replace( + ".", "-" + ) + archived_log = ( + base_path + + "log/debug/matched-lines_" + + version + + "_" + + str(datetime.now().date()) + + ".txt" + ) + os.rename(matched_log, archived_log) + matched_log_file = open(matched_log, "a") + matched_log_file.write('%-30s : %-70s\n' % (line_type, line)) + matched_log_file.close() + + except Exception as e: + eqa_settings.log( + "action matched: Error on line " + + str(sys.exc_info()[-1].tb_lineno) + + ": " + + str(e) + ) + + def action_undetermined(line, base_path): """Debug function to log unmatched log lines""" - undetermined_log = base_path + "log/undetermined.txt" - undetermined_log_file = open(undetermined_log, "a") - undetermined_log_file.write(line + "\n") - undetermined_log_file.close() + try: + + undetermined_log = base_path + "log/debug/undetermined-lines.txt" + if os.path.exists(undetermined_log): + file_size = os.path.getsize(undetermined_log) + if file_size > 5000000: + version = str(pkg_resources.get_distribution("eqalert").version).replace( + ".", "-" + ) + archived_log = ( + base_path + + "log/debug/undetermined-lines_" + + version + + "_" + + str(datetime.now().date()) + + ".txt" + ) + os.rename(undetermined_log, archived_log) + undetermined_log_file = open(undetermined_log, "a") + undetermined_log_file.write(line + "\n") + undetermined_log_file.close() + + except Exception as e: + eqa_settings.log( + "action undetermined: Error on line " + + str(sys.exc_info()[-1].tb_lineno) + + ": " + + str(e) + ) if __name__ == "__main__": diff --git a/eqa/lib/config.py b/eqa/lib/config.py index ff536751..8d30a8c2 100644 --- a/eqa/lib/config.py +++ b/eqa/lib/config.py @@ -1223,7 +1223,7 @@ def build_config(base_path): "sound": "%ssound/", "tmp_sound": "/tmp/eqa/sound/" }, - "version": "2.4.11" + "version": "2.5.5" }, "zones": { "An Arena (PVP) Area": "false", diff --git a/eqa/lib/parser.py b/eqa/lib/parser.py index 0f53e888..72becd64 100644 --- a/eqa/lib/parser.py +++ b/eqa/lib/parser.py @@ -886,7 +886,7 @@ def check_who(line): return "who_line_friends" elif ( re.fullmatch( - r"^(AFK |\|AFK |)\[(\d+ [a-zA-Z\s]+|ANONYMOUS)\] \w+( \([a-zA-Z\s]+\)|)( \<[a-zA-Z\s]+\>|)( ZONE\: \w+|)( LFG|)$", + r"^( AFK |\| AFK |)\[(\d+ [a-zA-Z\s]+|ANONYMOUS)\] \w+( \([a-zA-Z\s]+\)|)( \<[a-zA-Z\s]+\>| \<[a-zA-Z\s]+\>|)( ZONE\: \w+|)( LFG|)$", line, ) is not None diff --git a/setup.py b/setup.py index ea9c7e6f..9bc702a7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="eqalert", - version="2.4.11", + version="2.5.5", author="Michael Geitz", author_email="git@geitz.xyz", install_requires=[