Skip to content

Commit

Permalink
[Chore] Abstract coloured print and log pattern
Browse files Browse the repository at this point in the history
Problem: Lots of times we need to print coloured and log the same
message.

Solution: Add colorcode argument to the `print_and_log` function.
  • Loading branch information
krendelhoff2 committed Nov 16, 2023
1 parent e1d5ee8 commit 31fa791
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
46 changes: 21 additions & 25 deletions baking/src/tezos_baking/tezos_setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,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,9 +1057,11 @@ def main():
+ ".service"
)

message = "Error in the Tezos Setup Wizard, exiting."
print("\n" + color(message, color_red))
logging.error(message)
print_and_log(
"Error in the Tezos Setup Wizard, exiting.",
log=logging.error,
colorcode=color_red,
)

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

Expand Down
9 changes: 6 additions & 3 deletions baking/src/tezos_baking/tezos_voting_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,12 @@ def main():
logging.info("Exiting the Tezos Voting Wizard.")
sys.exit(1)
except Exception as e:
message = "Error in the Tezos Voting Wizard, exiting."
print("\n" + color(message, color_red))
logging.error(message)

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

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

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
6 changes: 3 additions & 3 deletions baking/src/tezos_baking/wizard_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ 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):
Expand Down

0 comments on commit 31fa791

Please sign in to comment.