Files
qemu-reims-vgpu/include/qemu/log.h
T
Daniel P. Berrangé 338f63e5f0 util: avoid repeated prefix on incremental qemu_log calls
There are three general patterns to QEMU log output

 1. Single complete message calls

      qemu_log("Some message\n");

 2. Direct use of fprintf

      FILE *f = qemu_log_trylock()
      fprintf(f, "...");
      fprintf(f, "...");
      fprintf(f, "...\n");
      qemu_log_unlock(f)

 3. Mixed use of qemu_log_trylock/qemu_log()

      FILE *f = qemu_log_trylock()
      qemu_log("....");
      qemu_log("....");
      qemu_log("....\n");
      qemu_log_unlock(f)

When message prefixes are enabled, the timestamp will be
unconditionally emitted for all qemu_log() calls. This
works fine in the 1st case, and has no effect in the 2nd
case. In the 3rd case, however, we get the timestamp
printed over & over in each fragment.

One can suggest that pattern (3) is pointless as it is
functionally identical to (2) but with extra indirection
and overhead. None the less we have a fair bit of code
that does this.

The qemu_log() call itself is nothing more than a wrapper
which does pattern (2) with a single fprintf() call.

One might question whether (2) should include the message
prefix in the same way that (1), but there are scenarios
where this could be inappropriate / unhelpful such as the
CPU register dumps or linux-user strace output.

This patch fixes the problem in pattern (3) by keeping
track of the call depth of qemu_log_trylock() and then
only emitting the the prefix when the starting depth
was zero. In doing this qemu_log_trylock_context() is
also introduced as a variant of qemu_log_trylock()
that emits the prefix. Callers doing to batch output
can thus choose whether a prefix is appropriate or
not.

Fixes: 012842c075 (log: make '-msg timestamp=on' apply to all qemu_log usage)
Reported-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2026-03-05 17:40:24 +00:00

141 lines
4.3 KiB
C

#ifndef QEMU_LOG_H
#define QEMU_LOG_H
/* A small part of this API is split into its own header */
#include "qemu/log-for-trace.h"
/*
* The new API:
*/
/* Returns true if qemu_log() will really write somewhere. */
bool qemu_log_enabled(void);
/* Returns true if qemu_log() will write somewhere other than stderr. */
bool qemu_log_separate(void);
#define CPU_LOG_TB_OUT_ASM (1u << 0)
#define CPU_LOG_TB_IN_ASM (1u << 1)
#define CPU_LOG_TB_OP (1u << 2)
#define CPU_LOG_TB_OP_OPT (1u << 3)
#define CPU_LOG_INT (1u << 4)
#define CPU_LOG_EXEC (1u << 5)
#define CPU_LOG_PCALL (1u << 6)
#define CPU_LOG_TB_CPU (1u << 8)
#define CPU_LOG_RESET (1u << 9)
#define LOG_UNIMP (1u << 10)
#define LOG_GUEST_ERROR (1u << 11)
#define CPU_LOG_MMU (1u << 12)
#define CPU_LOG_TB_NOCHAIN (1u << 13)
#define CPU_LOG_PAGE (1u << 14)
/* LOG_TRACE (1 << 15) is defined in log-for-trace.h */
#define CPU_LOG_TB_OP_IND (1u << 16)
#define CPU_LOG_TB_FPU (1u << 17)
#define CPU_LOG_PLUGIN (1u << 18)
/* LOG_STRACE is used for user-mode strace logging. */
#define LOG_STRACE (1u << 19)
#define LOG_PER_THREAD (1u << 20)
#define CPU_LOG_TB_VPU (1u << 21)
#define LOG_TB_OP_PLUGIN (1u << 22)
#define LOG_INVALID_MEM (1u << 23)
/* Lock/unlock output. */
/**
* Acquires a lock on the current log output stream.
* The returned FILE object should be used with the
* fprintf() function to output the log message, and
* then qemu_log_unlock() called to release the lock.
*
* The primary use case is to be able to incrementally
* output fragments of a complete log message in an
* efficient and race free manner.
*
* The simpler qemu_log() method should normally only
* be used to output complete log messages, and not
* within scope of a qemu_log_trylock() call.
*
* A typical usage pattern would be
*
* FILE *f = qemu_log_trylock()
*
* fprintf(f, "Something ");
* fprintf(f, "Something ");
* fprintf(f, "Something ");
* fprintf(f, "The end\n");
*
* qemu_log_unlock(f);
*
* Returns: the current FILE if available, NULL on error
*/
FILE *qemu_log_trylock(void) G_GNUC_WARN_UNUSED_RESULT;
/**
* As qemu_log_trylock(), but will also print the message
* context, if any is configured and this caused the
* acquisition of the FILE lock
*/
FILE *qemu_log_trylock_with_context(void) G_GNUC_WARN_UNUSED_RESULT;
/**
* Releases the lock on the log output, previously
* acquired by qemu_log_trylock().
*/
void qemu_log_unlock(FILE *fd);
/* Logging functions: */
/* log only if a bit is set on the current loglevel mask:
* @mask: bit to check in the mask
* @fmt: printf-style format string
* @args: optional arguments for format string
*/
#define qemu_log_mask(MASK, FMT, ...) \
do { \
if (unlikely(qemu_loglevel_mask(MASK))) { \
qemu_log(FMT, ## __VA_ARGS__); \
} \
} while (0)
/* log only if a bit is set on the current loglevel mask
* and we are in the address range we care about:
* @mask: bit to check in the mask
* @addr: address to check in dfilter
* @fmt: printf-style format string
* @args: optional arguments for format string
*/
#define qemu_log_mask_and_addr(MASK, ADDR, FMT, ...) \
do { \
if (unlikely(qemu_loglevel_mask(MASK)) && \
qemu_log_in_addr_range(ADDR)) { \
qemu_log(FMT, ## __VA_ARGS__); \
} \
} while (0)
/* Maintenance: */
/* define log items */
typedef struct QEMULogItem {
int mask;
const char *name;
const char *help;
} QEMULogItem;
extern const QEMULogItem qemu_log_items[];
ssize_t rust_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
bool qemu_set_log(int log_flags, Error **errp);
bool qemu_set_log_filename(const char *filename, Error **errp);
bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp);
void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
bool qemu_log_in_addr_range(uint64_t addr);
int qemu_str_to_log_mask(const char *str);
/* Print a usage message listing all the valid logging categories
* to the specified FILE*.
*/
void qemu_print_log_usage(FILE *f);
#endif