OpenRAM/compiler/debug.py

86 lines
2.6 KiB
Python
Raw Normal View History

2016-11-08 18:57:35 +01:00
import os
import inspect
import globals
import sys
# the debug levels:
# 0 = minimum output (default)
# 1 = major stages
# 2 = verbose
# n = custom setting
2019-01-23 00:24:38 +01:00
def check(check, str):
if not check:
(frame, filename, line_number, function_name, lines,
index) = inspect.getouterframes(inspect.currentframe())[1]
2019-01-23 00:24:38 +01:00
sys.stderr.write("ERROR: file {0}: line {1}: {2}\n".format(
os.path.basename(filename), line_number, str))
2019-02-14 00:21:16 +01:00
logger.log("ERROR: file {0}: line {1}: {2}\n".format(
2019-01-23 00:24:38 +01:00
os.path.basename(filename), line_number, str))
2019-01-13 23:34:46 +01:00
assert 0
2016-11-08 18:57:35 +01:00
2019-01-23 00:24:38 +01:00
def error(str, return_value=0):
2016-11-08 18:57:35 +01:00
(frame, filename, line_number, function_name, lines,
index) = inspect.getouterframes(inspect.currentframe())[1]
2019-01-23 00:24:38 +01:00
sys.stderr.write("ERROR: file {0}: line {1}: {2}\n".format(
os.path.basename(filename), line_number, str))
2019-02-14 00:21:16 +01:00
logger.log("ERROR: file {0}: line {1}: {2}\n".format(
2019-01-23 00:24:38 +01:00
os.path.basename(filename), line_number, str))
assert return_value == 0
2019-01-13 23:34:46 +01:00
2016-11-08 18:57:35 +01:00
def warning(str):
(frame, filename, line_number, function_name, lines,
index) = inspect.getouterframes(inspect.currentframe())[1]
2019-01-23 00:24:38 +01:00
sys.stderr.write("WARNING: file {0}: line {1}: {2}\n".format(
os.path.basename(filename), line_number, str))
2019-02-14 00:21:16 +01:00
logger.log("WARNING: file {0}: line {1}: {2}\n".format(
2019-01-23 00:24:38 +01:00
os.path.basename(filename), line_number, str))
2019-01-13 23:34:46 +01:00
2016-11-08 18:57:35 +01:00
2019-01-13 23:34:46 +01:00
def print_raw(str):
print(str)
log(str)
def log(str):
2019-02-14 00:21:16 +01:00
if globals.OPTS.output_name != '':
if log.create_file:
compile_log = open(globals.OPTS.output_path +
globals.OPTS.output_name + '.log', "w+")
log.create_file = 0
else:
compile_log = open(globals.OPTS.output_path +
globals.OPTS.output_name + '.log', "a")
if len(log.setup_output) != 0:
for line in log.setup_output:
compile_log.write(line)
log.setup_output = []
compile_log.write(str + '\n')
2019-01-23 00:24:38 +01:00
else:
2019-02-14 00:21:16 +01:00
log.setup_output.append(str + "\n")
2019-01-23 00:24:38 +01:00
2019-02-14 00:21:16 +01:00
# use a static list of strings to store messages until the global paths are set up
log.setup_output = []
log.create_file = 1
2019-01-23 00:24:38 +01:00
2016-11-08 18:57:35 +01:00
def info(lev, str):
from globals import OPTS
2016-11-08 18:57:35 +01:00
if (OPTS.debug_level >= lev):
frm = inspect.stack()[1]
mod = inspect.getmodule(frm[0])
2019-01-23 00:24:38 +01:00
# classname = frm.f_globals['__name__']
if mod.__name__ == None:
2019-01-23 00:24:38 +01:00
class_name = ""
else:
2019-01-23 00:24:38 +01:00
class_name = mod.__name__
2019-02-14 00:21:16 +01:00
print_raw("[{0}/{1}]: {2}".format(class_name,
frm[0].f_code.co_name, str))