From patchwork Thu Nov 10 16:41:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp251225wru; Thu, 10 Nov 2022 08:43:26 -0800 (PST) X-Google-Smtp-Source: AMsMyM6AJaCgrGuD942U1NbRi8Qe6R6ZO0i4jXtVTDV6iJqXA/AgpMSKk9Zqt0baNiThMNVxGzXs X-Received: by 2002:a17:906:c40b:b0:7aa:40ea:faae with SMTP id u11-20020a170906c40b00b007aa40eafaaemr3121645ejz.551.1668098606041; Thu, 10 Nov 2022 08:43:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098606; cv=none; d=google.com; s=arc-20160816; b=aqhazOrE+Dy6zRqNZkENuGN0ucFGm5qOYSPAlKmMxo3deFsTVPpWagbdEB7JTnkbJC huYJFno50L0xPyRqXToawDNHXuiaM/HZlgVo1aYOS09hC1RQY2S5YRPYSkr+kTO9lhhL AvT6/nXPktC77CqU/Pq6mRFspmuX8cJZp8udJf6rjjqtEBafsSN4VXUP2p9vhuh0b/Sw EsWok6hy/+VWV1ib/VyDBEZIlfdYqnTkggvzmaeNfKwBUvKOrkfbP0mjKbPm9Ih81ctv Gba9om7GR94vEdCHzi2ihCYxztwivHLi6SFjnrraKipEsaVKH8DWSD56udPZiysViHWw 3zuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=T8vUE+rfvjsgeeaSCxP8XDbKqPw79TWIqOYFgdciEhE=; b=zcroY7eImN5mSOWw476WZQSK9/4wJHpbhbwV7z2qn0MyVPdBhhrjxBf/36X6S63nw0 eDzgtNMEnDhZ3bA2TGdT76Qxv3GxotknrYapwwKHWUc+BeJA8ksJe50WtBjFC1GIGUsk cTQitX3NPRv0hqnEkOmJ1z37e3BJVV5nefNojhohJ1nBlYxin42vkld2/cjLeSAowYIu wVRYs9IaaCYQiTZWSumzJACqsQFwtQXjtJd2RAWm+0nGt4mPVkRiAIRjpAmZUzmh1R4d 3rOsWoH57zpaP9SaueM4YyNAyiaat5Ndu7MHo0FHMtHoNLPVlotUIOXn7oLExA8PdV3h invQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qSN7hMNk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb8-20020a1709076d8800b0078a4aad3141si19692621ejc.211.2022.11.10.08.43.00; Thu, 10 Nov 2022 08:43:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qSN7hMNk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232032AbiKJQmc (ORCPT + 99 others); Thu, 10 Nov 2022 11:42:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbiKJQm1 (ORCPT ); Thu, 10 Nov 2022 11:42:27 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5363B4198A; Thu, 10 Nov 2022 08:42:26 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id F31D9B8224F; Thu, 10 Nov 2022 16:42:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A029C433C1; Thu, 10 Nov 2022 16:42:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098543; bh=tXzOn+7cijuWIp52Zl+aCsVkYnNXzw4FG3zobVkUUqQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qSN7hMNk8q9hSnBx+GzZ7cd5NT3BZyBy1PRfMQ7WDV2U56OxMaBtdCJtI0o6qpdc1 HYo9izT4mpQlBG+mhWk0jGFd3QloBdRD1SmZScdb+dCqC/lskvyFaEvCvKFtbbvboh 3ugwX1amyZsGDwvOh8wcy1PtQtLz6zq8akqMJ1wOW0T+xMYWJXGsfyF49D8y7Cl9XV Cdog5uDSrbtLaV5qwPsJr4o8rC48T3IbyYoLI5zZK2UGb2MXuV74kNlE+Waet9CbG4 fTy5UMfDhKtpjbLeQrj5J/ZABTBQqgDoK7Ooo1+uGJ/G7dyeCSc9O1DSq3Q/wFbMau Zr7qp+bg0KBTQ== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye Subject: [PATCH v1 02/28] rust: print: add more `pr_*!` levels Date: Thu, 10 Nov 2022 17:41:14 +0100 Message-Id: <20221110164152.26136-3-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749128163521407422?= X-GMAIL-MSGID: =?utf-8?q?1749128163521407422?= Currently, only `pr_info!` (for the minimal sample) and `pr_emerg!` (for the panic handler) are there. Add the other levels as new macros, i.e. `pr_alert!`, `pr_crit!`, `pr_err!`, `pr_warn!`, `pr_notice!` and `pr_debug!`. Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Gary Guo Signed-off-by: Gary Guo Signed-off-by: Miguel Ojeda Reviewed-by: Boqun Feng Reviewed-by: Wei Liu Reviewed-by: Sergio Gonzalez Collado --- rust/kernel/prelude.rs | 2 +- rust/kernel/print.rs | 154 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index f8219285d8c0..6a1c6b38327f 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -17,7 +17,7 @@ pub use alloc::{boxed::Box, vec::Vec}; pub use macros::module; -pub use super::{pr_emerg, pr_info}; +pub use super::{pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn}; pub use super::error::{Error, Result}; diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 55db5a1ba752..694f51c6da5c 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -74,7 +74,13 @@ pub mod format_strings { // Furthermore, `static` instead of `const` is used to share the strings // for all the kernel. pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG); + pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT); + pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT); + pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR); + pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING); + pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE); pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO); + pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG); } /// Prints a message via the kernel's [`_printk`]. @@ -172,6 +178,126 @@ macro_rules! pr_emerg ( ) ); +/// Prints an alert-level message (level 1). +/// +/// Use this level if action must be taken immediately. +/// +/// Equivalent to the kernel's [`pr_alert`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_alert`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_alert +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_alert!("hello {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_alert ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::ALERT, $($arg)*) + ) +); + +/// Prints a critical-level message (level 2). +/// +/// Use this level for critical conditions. +/// +/// Equivalent to the kernel's [`pr_crit`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_crit`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_crit +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_crit!("hello {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_crit ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::CRIT, $($arg)*) + ) +); + +/// Prints an error-level message (level 3). +/// +/// Use this level for error conditions. +/// +/// Equivalent to the kernel's [`pr_err`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_err`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_err +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_err!("hello {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_err ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::ERR, $($arg)*) + ) +); + +/// Prints a warning-level message (level 4). +/// +/// Use this level for warning conditions. +/// +/// Equivalent to the kernel's [`pr_warn`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_warn`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_warn +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_warn!("hello {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_warn ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::WARNING, $($arg)*) + ) +); + +/// Prints a notice-level message (level 5). +/// +/// Use this level for normal but significant conditions. +/// +/// Equivalent to the kernel's [`pr_notice`] macro. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_notice`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_notice +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_notice!("hello {}\n", "there"); +/// ``` +#[macro_export] +macro_rules! pr_notice ( + ($($arg:tt)*) => ( + $crate::print_macro!($crate::print::format_strings::NOTICE, $($arg)*) + ) +); + /// Prints an info-level message (level 6). /// /// Use this level for informational messages. @@ -196,3 +322,31 @@ macro_rules! pr_info ( $crate::print_macro!($crate::print::format_strings::INFO, $($arg)*) ) ); + +/// Prints a debug-level message (level 7). +/// +/// Use this level for debug messages. +/// +/// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't support dynamic debug +/// yet. +/// +/// Mimics the interface of [`std::print!`]. See [`core::fmt`] and +/// `alloc::format!` for information about the formatting syntax. +/// +/// [`pr_debug`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_debug +/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html +/// +/// # Examples +/// +/// ``` +/// pr_debug!("hello {}\n", "there"); +/// ``` +#[macro_export] +#[doc(alias = "print")] +macro_rules! pr_debug ( + ($($arg:tt)*) => ( + if cfg!(debug_assertions) { + $crate::print_macro!($crate::print::format_strings::DEBUG, $($arg)*) + } + ) +);