Solved

Converting a function into a macro.

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

#define VERBOSE_LEVEL 5
int my_printk(int verbosity,char *buffer)
{
  if ((verbosity > VERBOSE_LEVEL) {
    printk("%s",buffer);
  }
  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.


0
Comment
Question by:Risky101
  • 2
4 Comments
 

Author Comment

by:Risky101
Comment Utility
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); \
#endif;

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

Expert Comment

by:avizit
Comment Utility
http://tinyurl.com/3majq   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)
#else
void pmesg(int level, char *format, ...);
/* print a message, if it is considered significant enough.
      Adapted from [K&R2], p. 174 */
#endif

#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. */
#else
void pmesg(int level, char* format, ...) {
#ifdef NDEBUG
      /* Empty body, so a good compiler will optimise calls
         to pmesg away */
#else
        va_list args;

        if (level>msglevel)
                return;

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

Accepted Solution

by:
sunnycoder earned 250 total points
Comment Utility
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
>time.
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..)
#endif

should serve the same purpose
0
 

Author Comment

by:Risky101
Comment Utility
I've figured out a solution that satisfies the requirements in the original question. Thanks for your clues.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now