Added lookup caching and converted indentation to tabs

This commit is contained in:
Philip Trauner 2016-09-11 15:20:06 +02:00
parent 617e336f0b
commit 13b4e0235c

View file

@ -5,7 +5,6 @@ from inspect import stack
from sys import stderr from sys import stderr
from sys import stdout from sys import stdout
try: try:
import sublime import sublime
print_fallback = True print_fallback = True
@ -57,6 +56,8 @@ LIGHT_BLUE = "\033[94m"
LIGHT_MAGENTA = "\033[95m" LIGHT_MAGENTA = "\033[95m"
LIGHT_CYAN = "\033[96m" LIGHT_CYAN = "\033[96m"
CACHED_MODULES = {}
# 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, file): def print_out(message, color, file):
@ -67,9 +68,17 @@ def print_out(message, color, file):
if getmodule(stack_[2][0]) == None: if getmodule(stack_[2][0]) == None:
for i in stack_[2:]: for i in stack_[2:]:
if getmodule(i[0]) != None: if getmodule(i[0]) != None:
try:
module = CACHED_MODULES[i[0].f_code]
except KeyError:
module = getmodule(i[0]).__name__ module = getmodule(i[0]).__name__
CACHED_MODULES[i[0].f_code] = module
else: else:
try:
module = CACHED_MODULES[stack_[2][0].f_code]
except KeyError:
module = getmodule(stack_[2][0]).__name__ module = getmodule(stack_[2][0]).__name__
CACHED_MODULES[stack_[2][0].f_code] = module
if not print_fallback: if not print_fallback:
print("[%s] %s: %s%s\033[0m" % (strftime("%H:%M:%S"), module, color, message), file=file) print("[%s] %s: %s%s\033[0m" % (strftime("%H:%M:%S"), module, color, message), file=file)
file.flush() file.flush()