preprocessor


Making a macro expansion dependent on its arguments


I have inherited code which contains debugging macros.
#define DEBUG_LEVEL_NONE 0
#define DEBUG_LEVEL_ONE 1
#define DEBUG_LEVEL_TWO 2
#if DEBUG_LEVEL == DEBUG_LEVEL_NONE
#define DEBUG(print_level, str)
#else /* DEBUG_LEVEL */
#define DEBUG(print_level, str) \
{ \
if (DEBUG_LEVEL >= print_level) \
{ \
printf("%-25s %4d %2d:", __FILE__, __LINE__, print_level); \
printf(str); \
fflush(NULL); \
} \
}
#endif /* DEBUG_LEVEL */
Everything works fine, but I would like to avoid the extra code and test for lower debug levels.
I know that I can't put preprocessor tests within macro definitions. Is there any way to get an effect like
#define DEBUG(print_level, str) \
{ \
#if (DEBUG_LEVEL >= print_level) \
printf("%-25s %4d %2d:", __FILE__, __LINE__, print_level); \
printf(str); \
fflush(NULL); \
#endif \
}
thanks
You could use a plain "if", any decent compiler will constant-fold the condition and exclude the body.

Related Links

Preprocessing for non negative matrix factorization NMF
Progress ABL How to Test for WEBSPEED in the PRE-PROCESSOR
I want to nest include files inside include files. Why cant I do that in Inno Setup?
Discriminating between (small) numbers and everything else in C preprocessor
Defining preprocessor symbols for CLion analyzer
Check multiple conditions at once using m4 preprocessor
XC8 warning: (107) illegal # directive “foo”
Compiling with ocamlbuild and camlp5
How not to output comments using the -C operation in mcpp
C++ Builder File Version not correct
How do you a preprocess statement for #include
Does the preprocessor pass environment variables?
YAML preprocessor / macro processor
Pre-Processing using m4
Is there a practical reason for “#if defined(X) && (X != 0)”?
How to check for presence of a directory in Inno Setup preprocessor?

Categories

HOME
django
phantom-dsl
kentico
gridview
time-complexity
string
apache-jena
formal-languages
character-encoding
spring-data-redis
yandex
permissions
nested-if
opengl-3
vimdiff
reactive-programming
mel
mautic
dynatrace
android-intent
alpacajs
package.json
aws-sdk
video-editing
swagger-2.0
windows-xp
guzzle
jquery-ias
systemtime
postscript
hta
monit
oculus
browser-sync
mql
magento-1.8
cognos-bi
sammy.js
vue-chartjs
raft
apic
google-earth-engine
odroid
c9.io
ogc
stringtokenizer
odoo-website
skylink
android-exoplayer
wc
corruption
magic-draw
static-cast
appdomain
backbone-collections
lsyncd
jtree
baidu
visual-studio-2008-sp1
docpad
envi
jquery-jscrollpane
libvlc
lazybones
forwarding
phonertc
airbrake
apache-commons-daemon
ansi
user-permissions
openbabel
big-ip
jama
spring-remoting
clickbank
openargs
nebula
facebook-timeline
abstract-data-type
jquery-1.9
joomla3.1
os.system
appjs
infobright
ie-compatibility-mode
binomial-heap
addchild
escrow
firefox-3
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App