diff mbox series

[4/8] btrfs-progs: add struct va_format support to our btrfs_no_printk helper

Message ID 4e645609d525e855ca2a0b87d23c5fc2d1329d54.1681938911.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: sync basic code from the kernel | expand

Commit Message

Josef Bacik April 19, 2023, 9:17 p.m. UTC
We use the struct va_format to do nested printk's internally with our
message handling.  Add the appropriate user space code to make this work
properly so when we start copying this code into btrfs-progs we get the
proper messages.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 common/messages.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

David Sterba May 2, 2023, 9:35 p.m. UTC | #1
On Wed, Apr 19, 2023 at 05:17:15PM -0400, Josef Bacik wrote:
> We use the struct va_format to do nested printk's internally with our
> message handling.  Add the appropriate user space code to make this work
> properly so when we start copying this code into btrfs-progs we get the
> proper messages.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  common/messages.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/common/messages.c b/common/messages.c
> index 8bcfcc6a..aff5ce48 100644
> --- a/common/messages.c
> +++ b/common/messages.c
> @@ -16,6 +16,7 @@
>  
>  #include <stdio.h>
>  #include <stdarg.h>
> +#include <printf.h>

This header is not available on musl libc

    [CC]     common/messages.o
common/messages.c:19:10: fatal error: printf.h: No such file or directory
   19 | #include <printf.h>
      |          ^~~~~~~~~~

but for sake of merging all the patches I'll leave it temporarily broken. We
can either insert a fixup patch or update this one with some stub/ifdef once we
figure it out.
diff mbox series

Patch

diff --git a/common/messages.c b/common/messages.c
index 8bcfcc6a..aff5ce48 100644
--- a/common/messages.c
+++ b/common/messages.c
@@ -16,6 +16,7 @@ 
 
 #include <stdio.h>
 #include <stdarg.h>
+#include <printf.h>
 #include "common/messages.h"
 #include "common/utils.h"
 
@@ -25,6 +26,8 @@  static const char *common_error_string[] = {
 	[ERROR_MSG_COMMIT_TRANS] = "failed to commit transaction",
 };
 
+static int va_modifier = -1;
+
 __attribute__ ((format (printf, 1, 2)))
 void __btrfs_printf(const char *fmt, ...)
 {
@@ -35,11 +38,39 @@  void __btrfs_printf(const char *fmt, ...)
 	va_end(args);
 }
 
+static int print_va_format(FILE *stream, const struct printf_info *info,
+			   const void *const *args)
+{
+	const struct va_format *fmt;
+
+	if (!(info->user & va_modifier))
+		return -2;
+
+	fmt = *((const struct va_format **)(args[0]));
+	return vfprintf(stream, fmt->fmt, *(fmt->va));
+}
+
+static int print_va_format_arginfo(const struct printf_info *info,
+				   size_t n, int *argtypes, int *size)
+{
+	if (n > 0) {
+		argtypes[0] = PA_POINTER;
+		size[0] = sizeof(struct va_format *);
+	}
+	return 1;
+ }
+
 __attribute__ ((format (printf, 2, 3)))
 void btrfs_no_printk(const void *fs_info, const char *fmt, ...)
 {
 	va_list args;
 
+	if (va_modifier == -1) {
+		register_printf_specifier('V', print_va_format,
+					  print_va_format_arginfo);
+		va_modifier = register_printf_modifier(L"p");
+	}
+
 	va_start(args, fmt);
 	vfprintf(stderr, fmt, args);
 	va_end(args);