Skip to content

Commit

Permalink
Merge pull request #747 from serokell/krendelhoff/#705-better-show-logs
Browse files Browse the repository at this point in the history
[#705] Better show the error logs of the wizards
  • Loading branch information
krendelhoff2 authored Nov 16, 2023
2 parents 1e48cf8 + 31fa791 commit 73a2e2d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
55 changes: 26 additions & 29 deletions baking/src/tezos_baking/tezos_setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os, sys, shutil
import readline
import re
import traceback
import time
import urllib.request
import json
Expand Down Expand Up @@ -601,37 +600,31 @@ def get_snapshot_metadata(self, name, json_url):
snapshot_metadata = self.extract_relevant_snapshot(snapshot_array)

if snapshot_metadata is None:
message = f"No suitable snapshot found from the {name} provider."
print(
color(
message,
color_red,
)
print_and_log(
f"No suitable snapshot found from the {name} provider.",
log=logging.warning,
colorcode=color_yellow,
)
logging.warning(message)
else:
self.config["snapshots"][name] = snapshot_metadata

except urllib.error.URLError:
message = f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n"
print(
color(
message,
color_red,
)
print_and_log(
f"\nCouldn't collect snapshot metadata from {json_url} due to networking issues.\n",
log=logging.error,
colorcode=color_red,
)
logging.error(message)
except ValueError:
message = f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n"
print(
color(
message,
color_red,
)
print_and_log(
f"\nCouldn't collect snapshot metadata from {json_url} due to format mismatch.\n",
log=logging.error,
colorcode=color_red,
)
logging.error(message)
except Exception as e:
print_and_log(f"\nUnexpected error handling snapshot metadata:\n{e}\n")
print_and_log(
f"\nUnexpected error handling snapshot metadata:\n{e}\n",
log=logging.error,
)

def output_snapshot_metadata(self, name):
from datetime import datetime
Expand Down Expand Up @@ -1063,12 +1056,16 @@ def main():
+ setup.config["network"]
+ ".service"
)
logging.error(f"{str(e)}")
print_and_log("Error in Tezos Setup Wizard, exiting.")
logfile = "tezos_setup.log"
with open(logfile, "a") as f:
f.write(traceback.format_exc() + "\n")
print("The error has been logged to", os.path.abspath(logfile))

print_and_log(
"Error in the Tezos Setup Wizard, exiting.",
log=logging.error,
colorcode=color_red,
)

log_exception(exception=e, logfile="tezos-setup.log")

logging.info("Exiting the Tezos Setup Wizard.")
sys.exit(1)


Expand Down
15 changes: 9 additions & 6 deletions baking/src/tezos_baking/tezos_voting_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,16 @@ def main():
logging.info("Exiting the Tezos Voting Wizard.")
sys.exit(1)
except Exception as e:
print("Error in Tezos Voting Wizard, exiting.")
logging.error(f"{str(e)}")

print_and_log(
"Error in the Tezos Voting Wizard, exiting.",
log=logging.error,
colorcode=color_red,
)

log_exception(exception=e, logfile="tezos-vote.log")

logging.info("Exiting the Tezos Voting Wizard.")
logfile = "tezos_vote.log"
with open(logfile, "a") as f:
f.write(str(e) + "\n")
print("The error has been logged to", os.path.abspath(logfile))
sys.exit(1)


Expand Down
1 change: 1 addition & 0 deletions baking/src/tezos_baking/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def color(input, colorcode):

color_red = "\x1b[1;31m"
color_green = "\x1b[1;32m"
color_yellow = "\x1b[1;33m"


def yes_or_no(prompt, default=None):
Expand Down
35 changes: 31 additions & 4 deletions baking/src/tezos_baking/wizard_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def search_json_with_default(json_filepath, field, default):


def setup_logger(log_file):
log_dir = f"{os.getenv('HOME')}/.tezos-logs/"
log_dir = f"{os.getenv('HOME')}/.tezos-logs/.debug"
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, log_file)
logging.basicConfig(
Expand All @@ -107,9 +107,36 @@ def setup_logger(log_file):
)


def print_and_log(s, log=logging.info):
print(s)
log(s)
def print_and_log(message, log=logging.info, colorcode=None):
print(color(message, colorcode) if colorcode else message)
log(message)


def log_exception(exception, logfile):
import traceback
from datetime import datetime

logging.error(f"{str(exception)}")

error_output = traceback.format_exc()

print("\nHere are last 10 lines of the error output:")
print("\n".join(error_output.splitlines()[-9:]))

log_dir = f".tezos-logs/"

with open(os.path.join(os.getenv("HOME"), log_dir, logfile), "a") as f:
f.write(datetime.now().strftime("%H:%M:%S %d/%m/%Y:"))
f.write("\n")
f.write(error_output)
f.write("\n")

print(
"\nThe error has been logged to the log file:",
os.path.join("~", log_dir, logfile),
)
print("To see the full log, please run:")
print(f"> cat {os.path.join('~', log_dir, logfile)}")


class Setup:
Expand Down

0 comments on commit 73a2e2d

Please sign in to comment.