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

processing strings of text for neural network input
How to Set Active Target in XCode 4.5+
Preprocessor assertions in arduino
expanding a macro within a macro definition
GNU-M4: Strip empty lines
Escaping space in OpenCL compiler arguments
Does Corona have preprocessor statements so I can execute Lua code for specific devices?
Preprocessing in C++
Truncate string in cpp (preprocessor)
C++ precompiler conditionally include code
DDK preprocessor directives
Preprocessor, Postprocessor, Geometric Processor and NC processor
Gfortran pre-processor directives for Different Operating systems
Good data set for Pre-processing
IDE inserts incorrect #include statement
Save string in C preprocessor

Categories

HOME
go
turing-machines
apache-flink
angular-formly
angular2-material
hessian
jwplayer
composite-primary-key
gradient
vscode-extensions
r-googlesheets
appcelerator
newrelic
bookmarks
google-admin-sdk
opengl-3
flume
recordrtc
okhttp
appcelerator-studio
ycsb
rtf
dynatrace
delphi-xe5
oledb
windows-xp
asciidoctor-pdf
articulate-storyline
zxing
data.stackexchange.com
beautifier
quantlib
jaws
ntp
dst
kudu
weather
simplecv
papaparse
code-snippets
pop3
sessionstorage
tex
health-monitoring
levenshtein-distance
smoothstate.js
placeholder
facebook-social-plugins
puredata
jboss-4.2.x
openal
chai-as-promised
mach-o
bootstrap-tags-input
sandcastle
stripe-connect
macaulay2
synonym
appscale
deep
php-socket
data-protection
project-organization
sklearn-pandas
nullable
decimalformat
phonertc
.bash-profile
doctype
fail2ban
boost-program-options
wcm
jama
trdion2011
ignite-ui
ss7
cuba
paste
synapse
multiautocompletetextview
ecslidingviewcontroller
jeromq
script#
observium
mvc-editor-templates
panda3d
extreme-programming
veracity
rmiregistry
garbage
ilasm
jsr286
cgbitmapcontextcreate
squishit
load-time
rowlex
aspmenu
lxr
google-wave

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App