On Thu, Sep 25, 2025 at 10:44:27AM +0100, Daniel P. Berrangé wrote:
> This addresses two key gotchas with the logging APIs:
>
> * Safely outputting a single line of text using
> multiple qemu_log() calls requires use of the
> qemu_log_trylock/unlock functions to avoid
> interleaving between threads
>
> * Directly outputting to the FILE object returned
> by qemu_log_trylock() must be discouraged because
> it prevents the inclusion of configured log message
> prefixes.
>
> Reported-by: Markus Armbruster <[email protected]>
> Signed-off-by: Daniel P. Berrangé <[email protected]>
> ---
> +++ b/include/qemu/log-for-trace.h
> @@ -29,7 +29,40 @@ static inline bool qemu_loglevel_mask(int mask)
> return (qemu_loglevel & mask) != 0;
> }
>
> -/* main logging function */
> +/**
> + * qemu_log: report a log message
> + *
> + * The FILE object returned by qemu_log_trylock() does
> + * not need to be used for outputting text directly,
> + * it is merely used to associate the lock.
> + *
> + * FILE *f = qemu_log_trylock()
> + *
> + * qemu_log("Something");
> + * qemu_log("Something");
> + * qemu_log("Something");
> + * qemu_log("The end\n");
> + *
> + * qemu_log_unlock(f);
Should this example include spaces, as in qemu_log("Something ");, so
that copy-and-paste coding is less likely to
ResultInCompressedSentences across the log calls?
Adjusting a comment is trivial, so:
Reviewed-by: Eric Blake <[email protected]>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org