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

Improve stdout logs (color, alignment, compacity) #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 18 additions & 18 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import os
import sys
import time
import atexit
import logging
import traceback
from datetime import datetime

import tank
from tank.log import LogManager
Expand All @@ -37,7 +37,7 @@


ENGINE_NAME = "tk-blender"
ENGINE_NICE_NAME = "Shotgun Blender Engine"
ENGINE_NICE_NAME = "SG Blender Engine"
APPLICATION_NAME = "Blender"

# env variable that control if to show the compatibility warning dialog
Expand All @@ -49,33 +49,41 @@
# own risk if needed.
MIN_COMPATIBILITY_VERSION = 2.8

# Codes used for colorful logs in the console.
class Ansi:
RED = "\u001b[1;31m"
YELLOW = "\u001b[1;33m"
CYAN = "\u001b[1;36m"
BLUE = "\u001b[1;34m"
RESET = "\u001b[0m"

# Although the engine has logging already, this logger is needed for logging
# where an engine may not be present.
logger = LogManager.get_logger(__name__)


# logging functionality
def display_message(level, msg):
t = time.asctime(time.localtime())
print("%s | %s | %s | %s " % (t, level, ENGINE_NICE_NAME, msg))
def display_message(level, msg, color=Ansi.RESET):
time = datetime.now().isoformat()
colored_level = f"{color}{level: ^7}{Ansi.RESET}"
print(f"{time} | {colored_level} | {ENGINE_NICE_NAME} | {msg} ")


def display_error(msg):
display_message("Error", msg)
display_message("ERROR", msg, Ansi.RED)


def display_warning(msg):
display_message("Warning", msg)
display_message("WARNING", msg, Ansi.YELLOW)


def display_info(msg):
display_message("Info", msg)
display_message("INFO", msg, Ansi.CYAN)


def display_debug(msg):
if os.environ.get("TK_DEBUG") == "1":
display_message("Debug", msg)
display_message("DEBUG", msg, Ansi.BLUE)


# methods to support the state when the engine cannot start up
Expand Down Expand Up @@ -646,15 +654,7 @@ def _emit_log_message(self, handler, record):
:param record: Standard python logging record.
:type record: :class:`~python.logging.LogRecord`
"""
# Give a standard format to the message:
# Shotgun <basename>: <message>
# where "basename" is the leaf part of the logging record name,
# for example "tk-multi-shotgunpanel" or "qt_importer".
if record.levelno < logging.INFO:
formatter = logging.Formatter("Debug: Shotgun %(basename)s: %(message)s")
else:
formatter = logging.Formatter("Shotgun %(basename)s: %(message)s")

formatter = logging.Formatter("%(message)s")
msg = formatter.format(record)

# Select Blender display function to use according to the logging
Expand Down