You can now write code like this: FLOOD_COUNTER(errcount, 100); ... NO_FLOOD(errcount, error_report("oops, something bad happened"), error_report("further errors suppressed"));
which would print the "oops, ..." error message up to 100 times, followed by the "further errors suppressed" message once, and then nothing else. Signed-off-by: Richard W.M. Jones <rjo...@redhat.com> --- include/qemu/no-flood.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 include/qemu/no-flood.h diff --git a/include/qemu/no-flood.h b/include/qemu/no-flood.h new file mode 100644 index 0000000..90a24da --- /dev/null +++ b/include/qemu/no-flood.h @@ -0,0 +1,34 @@ +/* + * Suppress error floods. + * + * Copyright (C) 2015 Red Hat, Inc. + * + * Author: Richard W.M. Jones <rjo...@redhat.com> + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef __QEMU_NO_FLOOD_H +#define __QEMU_NO_FLOOD_H 1 + +#include "qemu/atomic.h" + +/* Suggested initial value is 100 */ +#define FLOOD_COUNTER(counter_name, initial) \ + static int counter_name = (initial) + +#define NO_FLOOD(counter_name, code, suppress_message) \ + do { \ + int t_##counter_name = atomic_fetch_add(&counter_name, 0); \ + if (t_##counter_name > 0) { \ + code; \ + if (t_##counter_name == 1) { \ + suppress_message; \ + } \ + atomic_dec(&counter_name); \ + } \ +} while (0) + +#endif -- 2.3.1