def truncate_fuzzer_output(output, limit):
  """Truncate output in the middle according to limit."""
  if len(output) < limit:
    return output

  separator = '\n...truncated...\n'
  reduced_limit = limit - len(separator)
  left = reduced_limit / 2 + reduced_limit % 2
  right = reduced_limit / 2

  assert reduced_limit > 0

  return ''.join([output[:left], separator, output[-right:]])