Adjusted approach to make use of new exception scheme. Encapsulates JSON output with success, error, and results fields regardless of error or success. Disables logging globally.

pull/222/head
David Pokora 6 years ago
parent a3506b9d4c
commit 49273ca6ef
No known key found for this signature in database
GPG Key ID: 3CED48D1BB21BDD7
  1. 41
      slither/__main__.py

@ -100,10 +100,18 @@ def process_files(filenames, args, detector_classes, printer_classes):
################################################################################### ###################################################################################
def wrap_json_stdout(success, error_message, results=None):
return {
"success": success,
"error": error_message,
"results": results
}
def output_json(results, filename): def output_json(results, filename):
if filename is None: if filename is None:
# Write json to console # Write json to console
print(json.dumps(results)) print(json.dumps(wrap_json_stdout(True, None, results)))
else: else:
# Write json to file # Write json to file
if os.path.isfile(filename): if os.path.isfile(filename):
@ -504,11 +512,10 @@ def main_impl(all_detector_classes, all_printer_classes):
# Set colorization option # Set colorization option
set_colorization_enabled(not args.disable_color) set_colorization_enabled(not args.disable_color)
# If we are outputting json to stdout, we'll want to override all logger levels. # If we are outputting json to stdout, we'll want to disable any logging.
stdout_json = args.json == "-" stdout_json = args.json == "-"
override_level = None
if stdout_json: if stdout_json:
override_level = logging.ERROR logging.disable()
printer_classes = choose_printers(args, all_printer_classes) printer_classes = choose_printers(args, all_printer_classes)
detector_classes = choose_detectors(args, all_detector_classes) detector_classes = choose_detectors(args, all_detector_classes)
@ -529,17 +536,17 @@ def main_impl(all_detector_classes, all_printer_classes):
#('CryticCompile', default_log) #('CryticCompile', default_log)
]: ]:
l = logging.getLogger(l_name) l = logging.getLogger(l_name)
l.setLevel(l_level if override_level is None else override_level) l.setLevel(l_level)
console_handler = logging.StreamHandler() console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO if override_level is None else override_level) console_handler.setLevel(logging.INFO)
console_handler.setFormatter(FormatterCryticCompile()) console_handler.setFormatter(FormatterCryticCompile())
crytic_compile_error = logging.getLogger(('CryticCompile')) crytic_compile_error = logging.getLogger(('CryticCompile'))
crytic_compile_error.addHandler(console_handler) crytic_compile_error.addHandler(console_handler)
crytic_compile_error.propagate = False crytic_compile_error.propagate = False
crytic_compile_error.setLevel(logging.INFO if override_level is None else override_level) crytic_compile_error.setLevel(logging.INFO)
try: try:
filename = args.filename filename = args.filename
@ -582,15 +589,23 @@ def main_impl(all_detector_classes, all_printer_classes):
return return
exit(results) exit(results)
except SlitherException as e: except SlitherException as se:
logging.error(red('Error:')) # Output our error accordingly, via JSON or logging.
logging.error(red(e)) if stdout_json:
logging.error('Please report an issue to https://github.com/crytic/slither/issues') print(wrap_json_stdout(False, repr(se), []))
else:
logging.error(red('Error:'))
logging.error(red(se))
logging.error('Please report an issue to https://github.com/crytic/slither/issues')
sys.exit(-1) sys.exit(-1)
except Exception: except Exception:
logging.error('Error in %s' % args.filename) # Output our error accordingly, via JSON or logging.
logging.error(traceback.format_exc()) if stdout_json:
print(wrap_json_stdout(False, traceback.format_exc(), []))
else:
logging.error('Error in %s' % args.filename)
logging.error(traceback.format_exc())
sys.exit(-1) sys.exit(-1)

Loading…
Cancel
Save