Converting a function into a macro.

Posted on 2004-08-03
Last Modified: 2010-04-22
I wish to convert the following function into a macro:

int my_printk(int verbosity,char *buffer)
  if ((verbosity > VERBOSE_LEVEL) {
  return 0;

my_printk(1,"debugging message"); // does not get printed due to verbosity level
my_printk(6,"normal message"); // does get printed

Furthermore, I wish to have one additional feature: when a message is to be excluded due to VERBOSE_LEVEL being set too low, the macro must exclude the string from the driver binary, to save space and time. It must not do any calculations at run time.

Question by:Risky101
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Author Comment

ID: 11707696
I tried the following, not expecting it to work:

#define test(verbosity,y) \
#if verbosity>5 \
printk("verbose %s",y); \
#else \
//printk("alternate %s",y); \

Of course, ths violates ANSI C - any other idaes?
LVL 11

Expert Comment

ID: 11711745   might help. There is one implementation which I am pasting here.

File debug.h:

#ifndef DEBUG_H
#define DEBUG_H
#include <stdarg.h>

#if defined(NDEBUG) && defined(__GNUC__)
/* gcc's cpp has extensions; it allows for macros with a variable number of
   arguments. We use this extension here to preprocess pmesg away. */
#define pmesg(level, format, args...) ((void)0)
void pmesg(int level, char *format, ...);
/* print a message, if it is considered significant enough.
      Adapted from [K&R2], p. 174 */

#endif /* DEBUG_H */

File debug.c:

#include "debug.h"
#include <stdio.h>

extern int msglevel; /* the higher, the more messages... */

#if defined(NDEBUG) && defined(__GNUC__)
/* Nothing. pmesg has been "defined away" in debug.h already. */
void pmesg(int level, char* format, ...) {
#ifdef NDEBUG
      /* Empty body, so a good compiler will optimise calls
         to pmesg away */
        va_list args;

        if (level>msglevel)

        va_start(args, format);
        vfprintf(stderr, format, args);
#endif /* NDEBUG */
#endif /* NDEBUG && __GNUC__ */
LVL 45

Accepted Solution

sunnycoder earned 250 total points
ID: 11712402
exact replication of functionality is not possible since you cannot return values from a macro and there is no type checking in a macro so it will manipulate whatever argument it gets.

>Furthermore, I wish to have one additional feature: when a message is to be excluded due to VERBOSE_LEVEL being set too
>low, the macro must exclude the string from the driver binary, to save space and time. It must not do any calculations at run
this involved conditional compilations and would be #ifdefs .. you can embed the call to my_printk in the ifdef and then you can even do away with my_printk since you have already checked verbosity level before the call, you dont need to check it again and all that leaves in the function is a printk

#ifdef MY_LEVEL
printk (...blah..)

should serve the same purpose

Author Comment

ID: 11719974
I've figured out a solution that satisfies the requirements in the original question. Thanks for your clues.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chinese translation in RedHat 10 65
usage of %{name} in rpm spec file 4 58
read a word from a file in linux shell script 4 82
Problem to start Neon 20 158
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question