from format_util_ import FormatComponents


class CompileLog:

    def __init__(self, error_map, log_components):

        self.error_map = error_map
        self.log_components = log_components

    def build_log(self, message, error_level, exception_traceback=None):

        error_level = error_level or "UNKNOWN"

        if error_level in self.error_map:

            return ''.join([
                FormatComponents.get_color(
                    self.error_map, error_level) if self.log_components["color"] else "",
                FormatComponents.get_timestamp(
                    self.log_components) if self.log_components["timestamp"] else "",
                f"[{error_level}] " if self.log_components["level"] else "",
                f"{message}" if self.log_components["message"] else "",
                FormatComponents.get_traceback(
                    exception_traceback) if self.error_map[error_level]["traceback"] and exception_traceback else ""
            ])

        return f"[{error_level}] {message}" + (f"\n{FormatComponents.get_traceback(exception_traceback)}" if exception_traceback else "")
