Jake
Jake

Reputation: 690

Redefining Macros for logging

I have what feels like a simple problem, but I just can't seem figure out how to solve it. I have a large application that I'm porting to Android. The code is littered with many log calls that looks like the following:

LOG_WARN(s_debugHandle, ("OpenGL error detected on entry. (Error:0x%x).",glError));

I want to redefine LOG_WARN to be combatible with Android. What I've done looks like this:

#define LOG_WARN(handle, ...)   __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)

When I compile this the compiler tells me this:

error: invalid conversion from 'int' to 'const char*'
error: initializing argument 3 of 'int __android_log_print(int, const char*, const char*, ...)'

glError is an int, but other calls with other variables types give the same conversion error but from that type instead. What I have tried:

  1. Exchanging ... and VA_ARGS for just "msg". Same problem.
  2. Removing the parenthesis around the second argument of LOG_WARN. This actually solves it, but it is not a valid solution in my case since that would break the logging macroes of other platforms.
  3. Both 1 and 2 - complains about too many arguments.

Please help!

UPDATE:

After preprocessing, the call looks like this:

__android_log_print(ANDROID_LOG_WARN, "debug", ("OpenGL error detected on entry. (Error:0x%x).",glError));

Upvotes: 0

Views: 1317

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477000

You could install your own, intermediate function, using the variadic version of the print primitive:

#include <cstdarg>

void forward_debug(const char * fmt, ...)
{
    std::va_list ap;
    va_start(ap, fmt);
    __android_log_vprint(ANDROID_LOG_WARN, LOG_TAG, fmt, ap);
    va_end(ap);
}

#define LOG_WARN(ignore, x) forward_debug x

Update: Here's another idea:

#define SPLIT(...) __VA_ARGS__
#define LOG_WARN(ignore, x) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, SPLIT x)

Upvotes: 1

Related Questions