From: Nick Andrew <nick@nick-andrew.net>
To: Andrew Morton <akpm@linux-foundation.org>,
Linus Torvalds <torvalds@linux-foundation.org>
Cc: <linux-kernel@vger.kernel.org>
Subject: [RFC PATCH v1 2/3] Add %v support to vsnprintf()
Date: Sat, 06 Dec 2008 18:00:03 +1100 [thread overview]
Message-ID: <20081206070003.29149.50293.stgit@marcab.local.tull.net> (raw)
In-Reply-To: <20081206065922.29149.63380.stgit@marcab.local.tull.net>
Add %v support to vsnprintf()
vsnprintf() is extended to support a '%v' in the format string. '%v'
takes two arguments, the first is a format string and the second is a
va_list. vsnprintf() is recursively called to process the specified
format string and arguments, before returning to the original format
string.
This allows functions like the following, which prepend a
subsystem-specific string to an error message:
int subsystem_error(char *fmt, ...) {
va_list ap;
int rc;
va_start(ap, fmt);
rc = printk("%s: %v\n", "SUBSYSTEM", fmt, ap);
va_end(ap);
return rc;
}
Functions printing errors inside the subsystem would do like:
subsystem_error("Failed to get widget: %d", 3);
The resulting kernel message would be:
SUBSYSTEM: Failed to get widget: 3\n
The advantage over using say 2 printk() calls is that message output
will be contiguous, whereas separate printk() calls might be broken
by an intervening message from another kernel thread. Also a single
message buffer is used, which avoids double-handling and excessive
memory use.
Signed-off-by: Nick Andrew <nick@nick-andrew.net>
---
lib/vsprintf.c | 14 ++++++++++++++
1 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 1e2dcaf..e779f79 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -632,6 +632,7 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field
* %pS output the name of a text symbol
* %pF output the name of a function pointer
* %pR output the address range in a struct resource
+ * %v processes a supplied format string and args, recursively
*
*/
@@ -787,6 +788,19 @@ static char *__vsnprintfr(char *buf, char *str, char *end,
case 'u':
break;
+ case 'v': {
+ char *fmt2;
+ va_list args2;
+
+ fmt2 = va_arg(args, char *);
+ args2 = va_arg(args, va_list);
+ /* recurse to format the (fmt,args) pair */
+ str = __vsnprintfr(buf, str, end, fmt2, args2);
+ va_end(args2);
+
+ continue;
+ }
+
default:
if (str < end)
*str = '%';
next prev parent reply other threads:[~2008-12-06 7:00 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-06 6:59 [RFC] Recursive printk Nick Andrew
2008-12-06 6:59 ` [RFC PATCH v1 1/3] Split the vsnprintf function into two parts Nick Andrew
2008-12-06 7:00 ` Nick Andrew [this message]
2008-12-06 17:27 ` [RFC PATCH v1 2/3] Add %v support to vsnprintf() Linus Torvalds
2008-12-06 7:00 ` [RFC PATCH v1 3/3] Sample refactor of socket.c to use recursive printk Nick Andrew
2008-12-06 7:03 ` David Miller
2008-12-06 7:18 ` Valdis.Kletnieks
2008-12-06 7:40 ` Nick Andrew
2008-12-06 7:20 ` [RFC] Recursive printk Andrew Morton
2008-12-06 7:33 ` Willy Tarreau
2008-12-06 7:41 ` Andrew Morton
2008-12-06 8:16 ` Willy Tarreau
2008-12-06 9:43 ` Takashi Iwai
2008-12-06 7:42 ` Joe Perches
2008-12-06 8:40 ` Nick Andrew
2008-12-06 9:11 ` Joe Perches
2008-12-06 23:16 ` Nick Andrew
2008-12-06 23:24 ` Linus Torvalds
2008-12-06 19:35 ` Al Viro
2008-12-06 8:30 ` Nick Andrew
2008-12-08 1:42 ` Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20081206070003.29149.50293.stgit@marcab.local.tull.net \
--to=nick@nick-andrew.net \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome