Skip to content

Instantly share code, notes, and snippets.

@VIRUXE
Last active January 16, 2023 22:24
Show Gist options
  • Save VIRUXE/257e9615199246c9175d33a54912f521 to your computer and use it in GitHub Desktop.
Save VIRUXE/257e9615199246c9175d33a54912f521 to your computer and use it in GitHub Desktop.
Parse a SA-MP (San Andreas Multiplayer) Scavenge and Survive server log and make it all gay with colours, while viewing it live.
import os
import time
class Colors:
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[0;33m"
BLUE = "\033[0;34m"
MAGENTA = "\033[0;35m"
CYAN = "\033[0;36m"
WHITE = "\033[0;37m"
BOLD = "\033[1m"
BOLD_BLACK = "\033[1;30m"
BOLD_RED = "\033[1;31m"
BOLD_GREEN = "\033[1;32m"
BOLD_YELLOW = "\033[1;33m"
BOLD_BLUE = "\033[1;34m"
BOLD_MAGENTA = "\033[1;35m"
BOLD_CYAN = "\033[1;36m"
BOLD_WHITE = "\033[1;37m"
RESET = "\033[0m"
colors = {
Colors.BLACK : ["connection", "part", "logout"],
Colors.RED : ["kill", "death", "knockout", "wound", "OnVehicleDeath", "debug"],
Colors.GREEN : ["info", "server", "save", "REGISTER"],
#Colors.YELLOW : ["focus"],
Colors.BLUE : ["wakeup", "construct"],
Colors.MAGENTA : ["vehicle", "VLOCK", "_SaveVehicle", "DestroyWorldVehicle"],
Colors.CYAN : ["join", "spawn", "login", "loginprompt", "displayloginprompt", "loadaccount", "DisplayRegisterPrompt", "RegisterPrompt"],
Colors.BOLD : ["chat","command"],
Colors.BOLD_BLACK : ["focus"],
Colors.BOLD_YELLOW: ["deffail", "newchar", "SPECTATE"],
Colors.BOLD_RED : ["additem", "EXPLOSIVE"],
}
# Make sure the file exists before doing anything else.
while not os.path.exists("server_log.txt"):
print("Waiting for server_log.txt to be created...")
time.sleep(1)
# Open the file and seek to the end.
f = open("server_log.txt", "r", encoding="utf-8")
f.seek(0, os.SEEK_END)
while True:
# Read the file and print any new lines.
# Account for when unable to decode the line
try:
line = f.readline()
except UnicodeDecodeError:
line = f.readline()
if not line: continue
content = line[11:]
if line.find("ERROR:") != -1:
print(Colors.RED + line + Colors.RESET, end="")
continue
elif line.find("INFO:") != -1:
print(Colors.BLUE + line + Colors.RESET, end="")
continue
# Check if there is a tag
elif line[11] == "[":
tag = line[12:line.find("]", 11)]
if tag == "chat": continue
# Set Color according to tag
for color in colors:
# Lowercase the tag and check against the list of tags for that color, in lowercase.
if tag.lower() in [t.lower() for t in colors[color]]:
print(color + line + Colors.RESET, end="")
break
else:
print(line, end="") # No color found for this tag, print normally.
else: # No identifier, print normally.
print(Colors.BOLD + line[:10] + Colors.YELLOW + line[10:] + Colors.RESET, end="")
# If the file is deleted, wait for it to be recreated.
# Might happen when the server restarts.
if not os.path.exists("server_log.txt"):
print("server_log.txt was deleted. Waiting for it to be recreated...")
f.close()
while not os.path.exists("server_log.txt"):
print("Waiting for server_log.txt to be created...")
time.sleep(1)
f = open("server_log.txt", "r", encoding="utf-8")
f.seek(0, os.SEEK_END)
time.sleep(0.1) # Wait a bit before reading the file again.
# Close the file when done. * Meh this will never happen.
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment