Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:35:31+00:00 2026-05-26T03:35:31+00:00

After searching quite a bit in the Internet for a solution I decided to

  • 0

After searching quite a bit in the Internet for a solution I decided to ask here if my solution is fine.

I’m trying to write a simple and modular C logging library intended to be simple to disable and specially helping PhD students and researchers to debug an algorithm reducing as much as possibile the impact of the logging system.

My problem is that I want make possible for the user of the library to disable the logging system at compile time producing an executable in which the cost of the logger is 0.

The C code would look like this:

...
logger_t * logger;

result = logger_init(logger);
if(result == -1) {...}
...

this will simply initialize the logger. Looking for an example code I have checked the assert.h header but that soulution results in a list of warnings in my case. In fact, if logger_init() is substituted by 0 using the macro this will result in the variable logger never used.

For this reason I’ve decided to use this approach:

int logger_init(logger_t *logger);

#ifndef NLOG /* NLOG not defined -> enable logging */
int logger_init(logger_t *logger) {
...
}
#else  /* NLOG defined --> the logging system must be disabled */
#define logger_init(logger)     (int)((logger = NULL) == NULL)
#endif /* NLOG */

this does not result in warnings and I also avoid the overhead of calling the function. In fact my first try was to do like this:

int logger_init(logger_t *logger) {
  #ifndef NLOG /* NLOG not defined -> enable logging */
  ...
  #endif
  return 0;
}

keep calling the function even if I do not need it.

Do you think my solution could be considered a good solution? Is there a better solution?

Thanks a lot, guys!
Cheers,
Armando

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:35:32+00:00Added an answer on May 26, 2026 at 3:35 am

    The standard idiom for that, at least in the 90s, was:

    #ifndef NLOG
    void logger_init(logger_t *logger);
    void logger_log(logger_t *logger, ...);
    #else
    #define logger_init (void)sizeof
    #define logger_log  (void)sizeof
    #endif
    

    Remember that sizeof operands are not evaluated, although they are syntax checked.
    This trick works also with variadic functions, because the sizeof operator will see an expresion with several comma operators:

    logger_log(log, 1, 2, 3);
    

    Converts to:

    (void)sizeof(log, 1, 2, 3);
    

    Those commas are not separating parameters (sizeof is not a function but an operator), but they are comma operators.

    Note that I changed the return value from int to void. No real need for that, but the sizeof return would be mostly meaningless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After quite a bit of searching I think my situation is different enough I
After searching online, the best solution I've found so far is to just make
I've spent quite a bit of time on this so here's where I'm stuck.
I've been searching around trying to find an answer both here and google, although
I hope this isn't a stupid question, but after searching around for quite a
After searching online for quite some time, I cannot find a good way to
After searching online and going through past stackoverflow posts for a suitable implementation for
After searching through some existing libraries for JSON, I have finally ended up with
After searching a lot i did not get any answers and finally i had
After searching a long time for a performance bug, I read about denormal floating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.