""" Utility classes and functions Copyright (C) 2023 Benjamin Burkhardt This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ # Logger class that has 7 log levels: trace/all (6), debug (5), info(4), warning (3), error (2), fatal (1), off (0) # log "level" -1 is always printed out ("On") LOG_LVL_FALLBACK = 4 # the default level for logging class Logger: def __init__(self, log_level=LOG_LVL_FALLBACK): self.mappings = { # mappings for the color and syntax of the log -1: { "prefix": "", "space_between": "", "prefix_colors": ShellColors.F_Default + ShellColors.B_Default, "text_colors": ShellColors.F_Default + ShellColors.B_Default, }, 1: { "prefix": "[FATAL]", "space_between": " ", "prefix_colors": ShellColors.F_White + ShellColors.B_Red, "text_colors": ShellColors.B_Red + ShellColors.F_White, }, 2: { "prefix": "[ERROR]", "space_between": " ", "prefix_colors": ShellColors.F_White + ShellColors.B_Red, "text_colors": ShellColors.B_Red + ShellColors.F_White, }, 3: { "prefix": "[WARN]", "space_between": " ", "prefix_colors": ShellColors.F_LightRed + ShellColors.B_Default, "text_colors": ShellColors.F_Default + ShellColors.B_Default, }, 4: { "prefix": "[INFO]", "space_between": " ", "prefix_colors": ShellColors.F_Green + ShellColors.B_Default, "text_colors": ShellColors.F_Default + ShellColors.B_Default, }, 5: { "prefix": "[DEBUG]", "space_between": " ", "prefix_colors": ShellColors.F_LightGray + ShellColors.B_Default, "text_colors": ShellColors.F_Default + ShellColors.B_Default, }, 6: { "prefix": "[TRACE]", "space_between": " ", "prefix_colors": ShellColors.F_Default + ShellColors.B_Default, "text_colors": ShellColors.F_Default + ShellColors.B_Default, }, } if -1 <= log_level <= 6: self.log_level = log_level else: # if the given level exeeds the allowed scope self.log_level = LOG_LVL_FALLBACK self.log(f"The log level exeeds the allowed scope. Defaulting to {LOG_LVL_FALLBACK}", 3) def log(self, message, log_level=4): if log_level <= self.log_level: # only log when supposed to (if the given lvl is lower/equal (than) the maximum wanted) prefix_colors = self.mappings[log_level]['prefix_colors'] prefix = self.mappings[log_level]['prefix'] space_between = self.mappings[log_level]['space_between'] text_colors = self.mappings[log_level]['text_colors'] end_color = ShellColors.Reset print(f"{prefix_colors}{prefix}{text_colors}{space_between}{message}{end_color}") # just for prettier shell outputs (especially log) # source: https://stackoverflow.com/questions/39473297/how-do-i-print-colored-output-with-python-3 class ShellColors: # Foreground F_Default = "\x1b[39m" F_Black = "\x1b[30m" F_Red = "\x1b[31m" F_Green = "\x1b[32m" F_Yellow = "\x1b[33m" F_Blue = "\x1b[34m" F_Magenta = "\x1b[35m" F_Cyan = "\x1b[36m" F_LightGray = "\x1b[37m" F_DarkGray = "\x1b[90m" F_LightRed = "\x1b[91m" F_LightGreen = "\x1b[92m" F_LightYellow = "\x1b[93m" F_LightBlue = "\x1b[94m" F_LightMagenta = "\x1b[95m" F_LightCyan = "\x1b[96m" F_White = "\x1b[97m" # Background B_Default = "\x1b[49m" B_Black = "\x1b[40m" B_Red = "\x1b[41m" B_Green = "\x1b[42m" B_Yellow = "\x1b[43m" B_Blue = "\x1b[44m" B_Magenta = "\x1b[45m" B_Cyan = "\x1b[46m" B_LightGray = "\x1b[47m" B_DarkGray = "\x1b[100m" B_LightRed = "\x1b[101m" B_LightGreen = "\x1b[102m" B_LightYellow = "\x1b[103m" B_LightBlue = "\x1b[104m" B_LightMagenta = "\x1b[105m" B_LightCyan = "\x1b[106m" B_White = "\x1b[107m" # Formatting Bold = "\x1b[1m" Dim = "\x1b[2m" Italic = "\x1b[3m" Underlined = "\x1b[4m" Blink = "\x1b[5m" Reverse = "\x1b[7m" Hidden = "\x1b[8m" # Reset part Reset = "\x1b[0m" Reset_Bold = "\x1b[21m" Reset_Dim = "\x1b[22m" Reset_Italic = "\x1b[23m" Reset_Underlined = "\x1b[24" Reset_Blink = "\x1b[25m" Reset_Reverse = "\x1b[27m" Reset_Hidden = "\x1b[28m"