Updated Logging to newest TermOut version

This commit is contained in:
PhilipTrauner 2016-01-04 03:49:51 +01:00
parent 3917a81ba1
commit 08bcbce2d6

View file

@ -1,59 +1,101 @@
import time from __future__ import print_function
import inspect from time import strftime
from inspect import getmodule
from inspect import stack
from sys import stderr
from sys import stdout
""" # Formatting
HEADER = '\033[95m' Magenta BOLD = "\033[1m"
OKBLUE = '\033[94m' Blue HIDDEN = "\033[2m"
OKGREEN = '\033[92m' Green UNDERLINE = "\033[4m"
WARNING = '\033[93m' Yellow BLINK = "\033[5m"
FAIL = '\033[91m' Red INVERTED = "\033[7m"
ENDC = '\033[0m' HIDDEN = "\033[8m"
BOLD = '\033[1m'
""" # Backgrounds
BACKGROUND_DEFAULT = "\033[49m"
BACKGROUND_BLACK = "\033[40m"
BACKGROUND_RED = "\033[41m"
BACKGROUND_GREEN = "\033[42m"
BACKGROUND_YELLOW = "\033[43m"
BACKGROUND_BLUE = "\033[44m"
BACKGROUND_MAGENTA = "\033[45m"
BACKGROUND_CYAN = "\033[46m"
BACKGROUND_LIGHT_GRAY = "\033[47m"
BACKGROUND_DARK_GRAY = "\033[100m"
BACKGROUND_LIGHT_RED = "\033[101m"
BACKGROUND_LIGHT_GREEN = "\033[102m"
BACKGROUND_LIGHT_YELLOW = "\033[103m"
BACKGROUND_LIGHT_BLUE = "\033[104m"
BACKGROUND_LIGHT_MAGENTA = "\033[105m"
BACKGROUND_LIGHT_CYAN = "\033[106m"
BACKGROUND_WHITE = "\033[107m"
# Colors
DEFAULT = "\033[39m"
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
LIGHT_GREY = "\033[37m"
DARK_GRAY = "\033[90m"
LIGHT_RED = "\033[91m"
LIGHT_GREEN = "\033[92m"
LIGHT_YELLOW = "\033[93m"
LIGHT_BLUE = "\033[94m"
LIGHT_MAGENTA = "\033[95m"
LIGHT_CYAN = "\033[96m"
# If the stack becomes too complex to figure out a caller we go through and assume the first valid module is the caller. # If the stack becomes too complex to figure out a caller we go through and assume the first valid module is the caller.
# This works reasonably well but isn't 100% accurate and will only happen if the caller is a thread. # This works reasonably well but isn't 100% accurate and will only happen if the caller is a thread.
def print_out(message, color): def print_out(message, color, file):
stack = inspect.stack() stack_ = stack()
# Interestingly the if statement below is not executed when excepting KeyboardInterrupts. Weird. # Interestingly the if statement below is not executed when excepting KeyboardInterrupts. Weird.
# To prevent a crash we assume the module's name is 'Unknown' # To prevent a crash we assume the module's name is 'Unknown'
module = "Unknown" module = "Unknown"
if inspect.getmodule(stack[2][0]) == None: if getmodule(stack_[2][0]) == None:
for i in stack[2:]: for i in stack_[2:]:
if inspect.getmodule(i[0]) != None: if getmodule(i[0]) != None:
module = inspect.getmodule(i[0]).__name__ module = getmodule(i[0]).__name__
else: else:
module = inspect.getmodule(stack[2][0]).__name__ module = getmodule(stack_[2][0]).__name__
print("[%s] %s: %s%s\033[0m" % (time.strftime("%x %H:%M:%S"), module, color, message)) print("[%s] %s: %s%s\033[0m" % (strftime("%H:%M:%S"), module, color, message), file=file)
file.flush()
def info(message): def info(message, color=""):
print_out(message, '') print_out(message, color, stdout)
def header(message): def header(message):
print_out(message, '\033[95m') print_out(message, MAGENTA, stdout)
def warning(message): def warning(message):
print_out(message, '\033[93m') print_out(message, YELLOW, stderr)
def error(message): def error(message):
print_out(message, '\033[91m') print_out(message, RED, stderr)
def success(message, color="green"): def success(message):
if color == "green": print_out(message, GREEN, stdout)
print_out(message, '\033[92m')
elif color == "blue":
print_out(message, '\033[94m') if __name__ == "__main__":
import sys
info("Hi!", color=UNDERLINE+BACKGROUND_GREEN+RED)
header("This is a header")
warning("This is a warning") # > stderr
error("This is an error") # > stderr
success("Great success!")
def bold(message):
print_out(message, '\033[1m')
def underline(message):
print_out(message, '\033[1m')