1

I need to change a implementation of Macro(LOGGING_MACRO) printf into syslog.

Macro Usage :

LOGGING_MACRO(5,("Value of x,y=%d,%d\n",x,y));

Def1 :

#define LOGGING_MACRO(loglevel,str) printf str;

Def2 :

#define LOGGING_MACRO(loglevel,str) syslog(loglevel,str);

Note : I cannot change the macro format :(

Def2 throws error as syslog will not accept 'str'(2nd arg) with front & back braces. [But working fine in Def1 with printf ]

Kindly suggest how to remove the 1st & last braces from 'str' inside the macro before passing 'str' to syslog.

12
  • Give an example how you call your macro, because this approach printf str; is completelly failed. Commented Apr 21, 2017 at 7:01
  • @tilz0R that is given in the section "Macro Usage :" Commented Apr 21, 2017 at 7:02
  • Hello DYZ . Paranthesis is passed when calling the Macro. Commented Apr 21, 2017 at 7:04
  • This is not possible what you are asking directly, but if you use local variable then it can be done. Edit: No, it can't. You failed your usage. Commented Apr 21, 2017 at 7:05
  • Any other ways i can edit content of 'str' inside a macro ??? Commented Apr 21, 2017 at 7:08

1 Answer 1

2

Example below will work in single thread application:

char* custom_log(const char *fmt, ...) {
    static char outputString[200]; //Adjust your size for maximal log value
    va_list args;
    va_start(args, fmt);
    vsprintf(outputString, fmt, args);
    va_end(args);
    return outputString;
}

Then modify your macro to:

#define LOGGING_MACRO(loglevel,str) syslog(loglevel, custom_log str)

Remember, this works only in single-thread mode and make sure, custom_log function is visible where custom_log function is called.

For multi-thread, you might update it like this:

#define LOGGING_MACRO(loglevel,str) do {\
    mutex_lock(); /*Lock your mutex for debug print*/ \
    syslog(loglevel, custom_log str); /*Debug*/ \
    mutex_unlock(); /*Unlock mutex*/ \
} while (0)

Keep in mind that you have to write mutex_lock and mutex_unlock functions for your requirements in your system to lock only debug functions between multi threads.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the while loop, for?
Ya . Its Working fine . Thank you .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.