I have incorporated libiniparser
library in to my Android NDK application. One problem this library write logs directly to stdout
/stderr
.
I did not want to heavily modify the code so I wrote a macro to log in logcat
#include <android/log.h>
#define LOG_TAG "libinipaser"
#define fprintf(pipe,...) \
if (pipe == stdout) \
__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__); \
else if (pipe == stderr) \
__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__); \
else \
fprintf(pipe,__VA_ARGS__)
Until the last moment I was not sure it would work, but it works. I have checked preprocessor output (gcc -E) it looks like I expected
fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
line above after preprocessing looks:
if (f == (&__sF[1])) __android_log_print(ANDROID_LOG_INFO,"libinipaser","[%s]=[%s]\n", d->key[i], d->val[i]); else if (f == (&__sF[2])) __android_log_print(ANDROID_LOG_ERROR,"libinipaser","[%s]=[%s]\n", d->key[i], d->val[i]); else fprintf(f,"[%s]=[%s]\n", d->key[i], d->val[i]);
Could someone explain:
- Does c-preprocessor support recursive macros?
- How it happened that the
LOG_TAG
define was replaced but innerfprintf
wasn't? - Will this define works everywhere?
- Is this a good approach at all or not?