Re: [PATCH 5/5] printk: provide a filtering macro for printk

2009-09-03 Thread Marco Stornelli
2009/9/2 Marc Andre Tanner m...@brain-dump.org:
 On Wed, Sep 02, 2009 at 06:44:19PM +0200, Marco Stornelli wrote:


 Marc Andre Tanner ha scritto:
  +#define printk(fmt, ...) (                                                
           \

 Shouldn't it be an and?

 Don't think so. If the expression isn't constant we are unable to filter it
 and therefore printk is called anyway. However if the expression is constant
 the second part of the condition is evaluated and we filter based on the
 verbosity level.


Yes, you're right.

Marco
--
To unsubscribe from this list: send the line unsubscribe linux-embedded in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 5/5] printk: provide a filtering macro for printk

2009-09-02 Thread Marc Andre Tanner
The macro filters out printk messages based on a configurable verbosity
level (CONFIG_PRINTK_VERBOSITY).

Signed-off-by: Marc Andre Tanner m...@brain-dump.org
---
 include/linux/kernel.h |   23 +++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c2b3047..1ecc338 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -242,6 +242,29 @@ asmlinkage int printk(const char * fmt, ...)
 asmlinkage int printk_unfiltered(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2))) __cold;
 
+#if defined(CONFIG_PRINTK_VERBOSITY)  CONFIG_PRINTK_VERBOSITY  0
+/*
+ * The idea here is to wrap the actual printk function with a macro which
+ * will filter out all messages above a certain verbosity level. Because
+ * the if condition evaluates to a constant expression the compiler will be
+ * able to eliminate it and the resulting kernel image will be smaller.
+ */
+
+#include linux/stringify.h
+
+#define PRINTK_FILTER(fmt) (   
\
+   (((const char *)(fmt))[0] != ''  CONFIG_PRINTK_VERBOSITY = 4) ||
\
+   (((const char *)(fmt))[0] == ''  
\
+((const char *)(fmt))[1] = *__stringify(CONFIG_PRINTK_VERBOSITY)) 
\
+)
+
+#define printk(fmt, ...) ( 
\
+   (!__builtin_constant_p(PRINTK_FILTER((fmt))) || PRINTK_FILTER((fmt))) ? 
\
+   printk((fmt), ##__VA_ARGS__) : 0
\
+)
+
+#endif /* CONFIG_PRINTK_VERBOSITY */
+
 extern struct ratelimit_state printk_ratelimit_state;
 extern int printk_ratelimit(void);
 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
-- 
1.6.3.3

--
To unsubscribe from this list: send the line unsubscribe linux-embedded in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html