Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add log-events.py script #368

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions hack/log-events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import json
import re
import sys
from datetime import datetime
from enum import Enum

class ActionType(Enum):
PUSH = "PUSH"
PULL = "PULL"

def process_log(log_message, push_pull):
try:
# Remove escape characters
log_message = log_message.replace('\\"', '"')

# Extract JSON part from the log message
json_part = re.search(r'{.*}', log_message).group()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should check if this return result

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_match = re.search(r'{.*}', log_message)
if not json_match:
print("No JSON part found in log message.")
return

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

if not json_part:
print("No JSON part found in log message: " + log_message)
return

# Decode the JSON message
log_entry = json.loads(json_part)

# ANSI escape code for green color
green_color = "\033[32m"
reset_color = "\033[0m"

# Print the value of "type" in green color
type_value = log_entry["type"]
time_value = log_entry["time"]
direction_value = ""
if push_pull == ActionType.PULL:
direction_value = "⬅️"
elif push_pull == ActionType.PUSH:
direction_value = "➡️"


# Format the time value to only 4 digits of precision after the decimal point
time_value_fixed = re.sub(r"(\.\d{4})\d+", r"\1", time_value)
print(f"{direction_value} {time_value_fixed}: {green_color}{type_value}{reset_color}")
except Exception as e:
print(f"Error processing log message: {e}")

def main():
print("Listening for events...")
try:
for log_message in sys.stdin:
log_message = log_message.strip()
if "received event" in log_message and log_message:
process_log(log_message, ActionType.PUSH)
if "Got CurrentState" in log_message and log_message:
process_log(log_message, ActionType.PULL)

except KeyboardInterrupt:
print("\nStopping log message processing.")

if __name__ == "__main__":
main()