From nobody Sun Sep 28 15:29:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1758795584; cv=none; d=zohomail.com; s=zohoarc; b=RpghgPF+uMCGdfJKfHK16Kgl27+xvWhEjZ7ZFLnVaPK7/2lj2KdSNTSKxBLU98fWhAYeEEFzaVh6TXtQguyy8NwJ4BJsmwLxWSQSI3+5N7OTOq1k7QPBnJeSs6RxmOaCTsfYa38LEN5VWTUxgSNr3eLs15GCVurLjfHvo+emQAg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1758795584; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id; bh=XvkLe5PlKeo0HdSyG7FFTgKImW+3jifmX4pZMoyAdkQ=; b=So4VdziEyIj1/7gYSmG4Olvlx/uujR7AkZdg5penqzVNcRGZCLFXfWL/KUNUjoZ7jVAOb16xc5RTJIQDc+C3/TkpJw0geQvBWt/dhEp4VPpaXZUmdYuTwhpH6jF7hQKsWTLo/Kt/DMdY5qq5zP+7ExfDWC/7riRxi+py2m/o6Bw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1758795584122886.0994056736779; Thu, 25 Sep 2025 03:19:44 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 2BA11419A9; Thu, 25 Sep 2025 06:19:43 -0400 (EDT) Received: from [172.19.199.14] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id 1D78643EFA; Thu, 25 Sep 2025 05:49:13 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 4CB7643EFA; Thu, 25 Sep 2025 05:48:24 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 5BBC543E4D for ; Thu, 25 Sep 2025 05:45:47 -0400 (EDT) Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-126-tUPuQASIN2CtD6daej_t6g-1; Thu, 25 Sep 2025 05:45:43 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ECA18195605B; Thu, 25 Sep 2025 09:45:41 +0000 (UTC) Received: from toolbx.redhat.com (unknown [10.42.28.163]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ACEE830002D1; Thu, 25 Sep 2025 09:45:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758793547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XvkLe5PlKeo0HdSyG7FFTgKImW+3jifmX4pZMoyAdkQ=; b=NWYnNPN0WD2rotL+XpIkdCuBIZ8H2QOFW7W9flIvSWUBM42MNptBu7E7t+gcKitwBn/Ws2 ni7CLQw/joxQLWLqXKqYY3osUpHfnoWumUf0p2+gNRxqgun2he3Pl0scGv7BE6zZ/GaVaL 9DxF6ULK6TGUphcm3iWG7+qYF6hohfM= X-MC-Unique: tUPuQASIN2CtD6daej_t6g-1 X-Mimecast-MFC-AGG-ID: tUPuQASIN2CtD6daej_t6g_1758793542 To: qemu-devel@nongnu.org Subject: [PATCH v4 09/23] util: introduce some API docs for logging APIs Date: Thu, 25 Sep 2025 10:44:27 +0100 Message-ID: <20250925094441.1651372-10-berrange@redhat.com> In-Reply-To: <20250925094441.1651372-1-berrange@redhat.com> References: <20250925094441.1651372-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Message-ID-Hash: 7VNHK4M3R23DZNMPYYAYXM7HC6VOBYDZ X-Message-ID-Hash: 7VNHK4M3R23DZNMPYYAYXM7HC6VOBYDZ X-MailFrom: berrange@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , qemu-rust@nongnu.org, Richard Henderson , Markus Armbruster , Christian Schoenebeck , qemu-block@nongnu.org, Stefan Weil , "Dr. David Alan Gilbert" , Manos Pitsidianakis , Gerd Hoffmann , devel@lists.libvirt.org, Paolo Bonzini X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9_via_Devel?= Reply-To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1758795586693116600 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 Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Eric Blake --- include/qemu/log-for-trace.h | 35 ++++++++++++++++++++++++++++++++++- include/qemu/log.h | 26 ++++++++++++++++++++++++++ rust/util/src/log.rs | 7 +++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/include/qemu/log-for-trace.h b/include/qemu/log-for-trace.h index d47c9cd446..4e05b2e26f 100644 --- a/include/qemu/log-for-trace.h +++ b/include/qemu/log-for-trace.h @@ -29,7 +29,40 @@ static inline bool qemu_loglevel_mask(int mask) return (qemu_loglevel & mask) !=3D 0; } =20 -/* main logging function */ +/** + * qemu_log: report a log message + * @fmt: the format string for the message + * @...: the format string arguments + * + * This will emit a log message to the current output stream. + * + * The @fmt string should normally represent a complete line + * of text, ending with a newline character. + * + * If intending to call this function multiple times to + * incrementally construct a line of text, locking must + * be used to ensure that output from different threads + * is not interleaved. + * + * This is achieved by calling qemu_log_trylock() before + * starting the log line; calling qemu_log() multiple + * times with the last call having a newline at the end + * of @fmt; finishing with a call to qemu_log_unlock(). + * + * 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 =3D qemu_log_trylock() + * + * qemu_log("Something"); + * qemu_log("Something"); + * qemu_log("Something"); + * qemu_log("The end\n"); + * + * qemu_log_unlock(f); + * + */ void G_GNUC_PRINTF(1, 2) qemu_log(const char *fmt, ...); =20 #endif diff --git a/include/qemu/log.h b/include/qemu/log.h index aae72985f0..867fe327e4 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -41,7 +41,33 @@ bool qemu_log_separate(void); =20 /* Lock/unlock output. */ =20 +/** + * Acquires a lock on the current log output stream. + * The returned FILE object must be passed to + * qemu_log_unlock() to later release the lock. + * + * This should be used to protect a sequence of calls + * to qemu_log(), if they are being used to incrementally + * output a single line of text. For qemu_log() calls which + * output a complete line of text it is not required to + * take explicit locks. + * + * The returned FILE object may be used to directly + * output log messages, however, doing so will prevent + * the inclusion of configured log message prefixes. + * It is thus recommended that this be used sparingly, + * only in cases where it is required to dump large + * data volumes. Use of qemu_log() is preferred for + * most output tasks. + * + * Returns: the current FILE if available, NULL on error + */ FILE *qemu_log_trylock(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); =20 /* Logging functions: */ diff --git a/rust/util/src/log.rs b/rust/util/src/log.rs index af9a3e9123..eaf493f0df 100644 --- a/rust/util/src/log.rs +++ b/rust/util/src/log.rs @@ -55,6 +55,13 @@ impl LogGuard { /// writeln!(log, "test"); /// } /// ``` + /// + /// Note that directly writing to the log output will prevent the + /// inclusion of configured log prefixes. It is thus recommended + /// that this be used sparingly, only in cases where it is required + /// to dump large data volumes. Use of [`log_mask_ln!()`](crate::log_m= ask_ln) + /// macro() is preferred for most output tasks. + pub fn new() -> Option { let f =3D unsafe { bindings::qemu_log_trylock() }.cast(); NonNull::new(f).map(Self) --=20 2.50.1