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 6742657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:52:10+00:00 2026-05-26T11:52:10+00:00

I have the following macro function in vanilla C : #define GLOG(format_string, …) {

  • 0

I have the following macro function in vanilla C:

#define GLOG(format_string, ...) { \
  const char *file = strrchr(__FILE__, '/'); \
  char format[256] = "%s:%s!%d\t"; \
  strncat(format, format_string, 248); \
  strcat(format, "\n"); \
  printf(format, __FUNCTION__, file ? file : __FILE__, __LINE__, ##__VA_ARGS__); \
}

which lets me print a debug message containing the current function, file and line number, e.g.

GLOG("count=%d", count);

might print

do_count:counter.c!123  count=456
  1. How can I modify the function to print all local variables if caller omits format_string? e.g.

    GLOG();
    

    might print

    do_count:counter.c!123  count=456, message="Hello world", array=[7, 8] structure={ptr=0xACE0FBA5E, coord={x=9, y=0}}
    
  2. If that’s not possible, how can I modify it to print just the current function, file and line number? e.g.

    do_count:counter.c!123
    

    As is, this returns an error:

    error: expected expression before ‘,’ token

    as the strncat line is simply

    strncat(format, , 248);
    
  • 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-26T11:52:11+00:00Added an answer on May 26, 2026 at 11:52 am

    First, inspecting all the local variables at runtime by the process itself seems impossible because C doesn’t have any means for reflection.

    Second, you would be much better off if you wrote the logging macro like that:

    #include <stdio.h>
    
    #define STRINGIFY(x) #x
    #define TOSTRING(x) STRINGIFY(x)
    
    #define GLOGF(fmt, ...) \
      printf("%s:%s " fmt "\n", __func__, __FILE__ "!" TOSTRING(__LINE__), ##__VA_ARGS__)
    
    int main (void) {
      /* main:test.c!xx count=5 */
      GLOGF("count=%d", 5);
      /* main:test.c!xx */
      GLOGF();
      return 0;
    }
    

    It is simpler and doesn’t incur any additional runtime overhead since the string is concatenated at compile-time.

    Also note that I have used __func__ instead of __FUNCTION__, because the latter is non-standard.

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

Sidebar

Related Questions

I have the following function: void Register(Data* _pData, uint32 _Line, const char* _pFile, ...)
I have the following C macro from libpurple: #define yahoo_get16(buf) ((((*(buf))<<8)&0xff00) + ((*((buf)+1)) &
I have the following c macro from the libpurple yahoo plugin: #define yahoo_put16(buf, data)
I have the following Macro I want to call from within an AutoHotkey script
I have following text in a file 23456789 When I tried to replace the
Consider the following macro definitions and invocation: #define x x[0] #define y(arg) arg y(x)
I have some debugging code that looks like the following: #define STRINGIFY(x) #x #define
I have an Excel worksheet that has the following macro. I'd like to loop
I have a function written for th e x64 microsft macro assembler in visual
I have the following code to import a delimited file into an Access 2003

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.