All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] Coding style for errors
Date: Wed, 21 Oct 2015 18:48:27 +0200	[thread overview]
Message-ID: <87si54i2w4.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <8737x4p8l9.fsf@fimbulvetr.bsc.es> ("Lluís Vilanova"'s message of "Wed, 21 Oct 2015 17:03:30 +0200")

Lluís Vilanova <vilanova@ac.upc.edu> writes:

> Hi,
>
> I was wondering what is the proper way (or ways, depending on the subsystem) of
> reporting and signalling errors in QEMU. The coding style file does not seem to
> mention it, and the code uses all kinds of forms for that:
>
> * printf + exit(1)
> * fprintf(stderr) + exit(1)
> * error_report + exit(1)
> * cpu_abort
> * Some other I probably forgot

cpu_abort() and hw_error() are fancy ways to abort().  Terminating with
abort() on "this can't be happening" conditions is perfectly sensible,
and doing it in fancy ways can be useful.  For other errors, it's
inappropriate.

qemu/error-report.h is for reporting errors to the user.  Why not simply
fprintf(stderr, ...)?  Several reasons:

* error_report() & friends report errors in a uniform format.

* They do the right thing inside monitor commands: report the error to
  the monitor instead of stderr.

* They can add location information.

* They can add timestamps (-msg timestamp=on).

* If we ever do proper logging, they'll log the error.

There are many places left that fprintf().  Please don't add more.

qapi/error.h is for propagating errors up the call chain.  At some
point, you'll either recover and throw away the error, or you report it.
Convenience function error_report_err() makes that easy, but it's really
just a thin wrapper around error_report().

Another convenience feature makes reporting *fatal* errors easy:
&error_fatal.  Likewise, for programming errors: &error_abort.

When a simpler method for reporting success/failure to the caller
suffices, it's perfectly fine to use it.  E.g. returning a valid pointer
on success and null pointer on failure, or non-negative integer on
success and negative errno code on failure.

> So, is there any agreement on what should be used? If so, could that please be
> added to CODING_STYLE?

I think HACKING would be a better fit.

  parent reply	other threads:[~2015-10-21 16:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-21 15:03 [Qemu-devel] Coding style for errors Lluís Vilanova
2015-10-21 15:14 ` Eric Blake
2015-10-21 19:41   ` Lluís Vilanova
2015-10-21 16:48 ` Markus Armbruster [this message]
2015-10-21 20:16   ` Peter Maydell
2015-10-22 13:30   ` Lluís Vilanova
2015-10-23 16:01     ` Stefan Hajnoczi
2015-10-23 16:12       ` Laszlo Ersek
2015-10-23 17:02       ` Dr. David Alan Gilbert
2015-10-28 16:44         ` Thomas Huth
2015-10-23 17:34       ` Lluís Vilanova
2015-10-26 11:39         ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87si54i2w4.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.