code: def _handle_signal_gracefully(cls, signum, signame, traceback_lines):
      
      # Extract the stack, and format an entry to be written to the exception log.
      formatted_traceback = cls._format_traceback(traceback_lines=traceback_lines,
                                                  should_print_backtrace=True)
      signal_error_log_entry = cls._CATCHABLE_SIGNAL_ERROR_LOG_FORMAT.format(
        signum=signum,
        signame=signame,
        formatted_traceback=formatted_traceback)
      # TODO: determine the appropriate signal-safe behavior here (to avoid writing to our file
      # descriptors re-entrantly, which raises an IOError).
      # This method catches any exceptions raised within it.
      cls.log_exception(signal_error_log_entry)
  
      # Create a potentially-abbreviated traceback for the terminal or other interactive stream.
      formatted_traceback_for_terminal = cls._format_traceback(
        traceback_lines=traceback_lines,
        should_print_backtrace=cls._should_print_backtrace_to_terminal)
      terminal_log_entry = cls._CATCHABLE_SIGNAL_ERROR_LOG_FORMAT.format(
        signum=signum,
        signame=signame,
        formatted_traceback=formatted_traceback_for_terminal)
      # Exit, printing the output to the terminal.
      cls._exit_with_failure(terminal_log_entry)
docs: Signal handler for non-fatal signals which raises or logs an error and exits with failure.