OpenRAM/compiler/debug.py

103 lines
3.4 KiB
Python
Raw Normal View History

# See LICENSE for licensing information.
#
2019-06-14 17:43:41 +02:00
# Copyright (c) 2016-2019 Regents of the University of California and The Board
# of Regents for the Oklahoma Agricultural and Mechanical College
# (acting for and on behalf of Oklahoma State University)
# All rights reserved.
#
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-16 06:45:05 +01:00
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-16 06:45:05 +01:00
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-16 06:45:05 +01:00
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-16 06:45:05 +01:00
if globals.OPTS.output_name != '':
2019-02-14 00:21:16 +01:00
if log.create_file:
2019-02-21 19:23:30 +01:00
# We may have not yet read the config, so we need to ensure
# it ends with a /
# This is also done in read_config if we change the path
# FIXME: There's actually a bug here. The first few lines
# could be in one log file and after read_config it could be
# in another log file if the path or name changes.
if not globals.OPTS.output_path.endswith('/'):
globals.OPTS.output_path += "/"
if not os.path.isdir(globals.OPTS.output_path):
os.mkdir(globals.OPTS.output_path)
2019-02-14 00:21:16 +01:00
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 = []
2019-07-05 17:18:58 +02:00
log.create_file = True
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))