diff mbox

[U-Boot,08/23] Add pr_fmt() macro

Message ID 1408346196-30419-9-git-send-email-thierry.reding@gmail.com
State Superseded
Delegated to: Tom Warren
Headers show

Commit Message

Thierry Reding Aug. 18, 2014, 7:16 a.m. UTC
From: Thierry Reding <treding@nvidia.com>

This macro can be overridden in source files (before including common.h)
and can be used to specify a prefix for debug and error messages. An
example of how to use this is shown below:

	#define pr_fmt(fmt) "foo: " fmt

	#include <common.h>

	...
	debug("bar");

The resulting message will read:

	foo: bar

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 include/common.h | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

Comments

Simon Glass Aug. 18, 2014, 6:24 p.m. UTC | #1
On 18 August 2014 01:16, Thierry Reding <thierry.reding@gmail.com> wrote:
> From: Thierry Reding <treding@nvidia.com>
>
> This macro can be overridden in source files (before including common.h)
> and can be used to specify a prefix for debug and error messages. An
> example of how to use this is shown below:
>
>         #define pr_fmt(fmt) "foo: " fmt
>
>         #include <common.h>
>
>         ...
>         debug("bar");
>
> The resulting message will read:
>
>         foo: bar
>
> Signed-off-by: Thierry Reding <treding@nvidia.com>

Acked-by: Simon Glass <sjg@chromium.org>

Seems OK. But I wonder if a string might be simpler?

#define DEBUG_PREFIX "foo: "

+#ifndef DEBUG_PREFIX
+#define DEBUG_PREFIX ""
+#endif

...
                     printf(DEBUG_PREFIX ##args);    \

> ---
>  include/common.h | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/include/common.h b/include/common.h
> index 1d6cb48ff078..65db04b452d7 100644
> --- a/include/common.h
> +++ b/include/common.h
> @@ -99,15 +99,19 @@ typedef volatile unsigned char      vu_char;
>  #define _DEBUG 0
>  #endif
>
> +#ifndef pr_fmt
> +#define pr_fmt(fmt) fmt
> +#endif
> +
>  /*
>   * Output a debug text when condition "cond" is met. The "cond" should be
>   * computed by a preprocessor in the best case, allowing for the best
>   * optimization.
>   */
> -#define debug_cond(cond, fmt, args...)         \
> -       do {                                    \
> -               if (cond)                       \
> -                       printf(fmt, ##args);    \
> +#define debug_cond(cond, fmt, args...)                 \
> +       do {                                            \
> +               if (cond)                               \
> +                       printf(pr_fmt(fmt), ##args);    \
>         } while (0)
>
>  #define debug(fmt, args...)                    \
> @@ -129,7 +133,7 @@ void __assert_fail(const char *assertion, const char *file, unsigned line,
>                 __assert_fail(#x, __FILE__, __LINE__, __func__); })
>
>  #define error(fmt, args...) do {                                       \
> -               printf("ERROR: " fmt "\nat %s:%d/%s()\n",               \
> +               printf("ERROR: " pr_fmt(fmt) "\nat %s:%d/%s()\n",       \
>                         ##args, __FILE__, __LINE__, __func__);          \
>  } while (0)
>
> --
> 2.0.4
>

Regards,
Simon
Thierry Reding Aug. 19, 2014, 12:27 p.m. UTC | #2
On Mon, Aug 18, 2014 at 12:24:06PM -0600, Simon Glass wrote:
> On 18 August 2014 01:16, Thierry Reding <thierry.reding@gmail.com> wrote:
> > From: Thierry Reding <treding@nvidia.com>
> >
> > This macro can be overridden in source files (before including common.h)
> > and can be used to specify a prefix for debug and error messages. An
> > example of how to use this is shown below:
> >
> >         #define pr_fmt(fmt) "foo: " fmt
> >
> >         #include <common.h>
> >
> >         ...
> >         debug("bar");
> >
> > The resulting message will read:
> >
> >         foo: bar
> >
> > Signed-off-by: Thierry Reding <treding@nvidia.com>
> 
> Acked-by: Simon Glass <sjg@chromium.org>
> 
> Seems OK. But I wonder if a string might be simpler?
> 
> #define DEBUG_PREFIX "foo: "
> 
> +#ifndef DEBUG_PREFIX
> +#define DEBUG_PREFIX ""
> +#endif

The advantage of using the macro is that it can perform more complicated
transformations on the format string, like this for example:

	#define pr_fmt(fmt) "foo: %s():" fmt, __func__

Thierry
Simon Glass Aug. 19, 2014, 12:58 p.m. UTC | #3
Hi Thierry,

On 19 August 2014 06:27, Thierry Reding <thierry.reding@gmail.com> wrote:
> On Mon, Aug 18, 2014 at 12:24:06PM -0600, Simon Glass wrote:
>> On 18 August 2014 01:16, Thierry Reding <thierry.reding@gmail.com> wrote:
>> > From: Thierry Reding <treding@nvidia.com>
>> >
>> > This macro can be overridden in source files (before including common.h)
>> > and can be used to specify a prefix for debug and error messages. An
>> > example of how to use this is shown below:
>> >
>> >         #define pr_fmt(fmt) "foo: " fmt
>> >
>> >         #include <common.h>
>> >
>> >         ...
>> >         debug("bar");
>> >
>> > The resulting message will read:
>> >
>> >         foo: bar
>> >
>> > Signed-off-by: Thierry Reding <treding@nvidia.com>
>>
>> Acked-by: Simon Glass <sjg@chromium.org>
>>
>> Seems OK. But I wonder if a string might be simpler?
>>
>> #define DEBUG_PREFIX "foo: "
>>
>> +#ifndef DEBUG_PREFIX
>> +#define DEBUG_PREFIX ""
>> +#endif
>
> The advantage of using the macro is that it can perform more complicated
> transformations on the format string, like this for example:
>
>         #define pr_fmt(fmt) "foo: %s():" fmt, __func__

OK, I figured you might want something like that.

>
> Thierry

Regards,
Simon
diff mbox

Patch

diff --git a/include/common.h b/include/common.h
index 1d6cb48ff078..65db04b452d7 100644
--- a/include/common.h
+++ b/include/common.h
@@ -99,15 +99,19 @@  typedef volatile unsigned char	vu_char;
 #define _DEBUG	0
 #endif
 
+#ifndef pr_fmt
+#define pr_fmt(fmt) fmt
+#endif
+
 /*
  * Output a debug text when condition "cond" is met. The "cond" should be
  * computed by a preprocessor in the best case, allowing for the best
  * optimization.
  */
-#define debug_cond(cond, fmt, args...)		\
-	do {					\
-		if (cond)			\
-			printf(fmt, ##args);	\
+#define debug_cond(cond, fmt, args...)			\
+	do {						\
+		if (cond)				\
+			printf(pr_fmt(fmt), ##args);	\
 	} while (0)
 
 #define debug(fmt, args...)			\
@@ -129,7 +133,7 @@  void __assert_fail(const char *assertion, const char *file, unsigned line,
 		__assert_fail(#x, __FILE__, __LINE__, __func__); })
 
 #define error(fmt, args...) do {					\
-		printf("ERROR: " fmt "\nat %s:%d/%s()\n",		\
+		printf("ERROR: " pr_fmt(fmt) "\nat %s:%d/%s()\n",	\
 			##args, __FILE__, __LINE__, __func__);		\
 } while (0)