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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:41:23+00:00 2026-05-15T09:41:23+00:00

Heads up: This is a weird question. I’ve got some really useful macros that

  • 0

Heads up: This is a weird question.

I’ve got some really useful macros that I like to use to simplify some logging. For example I can do Log(@"My message with arguments: %@, %@, %@", @"arg1", @"arg2", @"arg3"), and that will get expanded into a more complex method invocation that includes things like self, _cmd, __FILE__, __LINE__, etc, so that I can easily track where things are getting logged. This works great.

Now I’d like to expand my macros to not only work with Objective-C methods, but general C functions. The problem is the self and _cmd portions that are in the macro expansion. These two parameters don’t exist in C functions. Ideally, I’d like to be able to use this same set of macros within C functions, but I’m running into problems. When I use (for example) my Log() macro, I get compiler warnings about self and _cmd being undeclared (which makes total sense).

My first thought was to do something like the following (in my macro):

if (thisFunctionIsACFunction) {
  DoLogging(nil, nil, format, ##__VA_ARGS__);
} else {
  DoLogging(self, _cmd, format, ##__VA_ARGS__);
}

This still produces compiler warnings, since the entire if() statement is substituted in place of the macro, resulting in errors with the self and _cmd keywords (even though they will never be executed during function execution).

My next thought was to do something like this (in my macro):

if (thisFunctionIsACFunction) {
  #define SELF nil
  #define CMD nil
} else {
  #define SELF self
  #define CMD _cmd
}
DoLogging(SELF, CMD, format, ##__VA_ARGS__);

That doesn’t work, unfortunately. I get “error: ‘#’ is not followed by a macro parameter” on my first #define.

My other thought was to create a second set of macros, specifically for use in C functions. This reeks of a bad code smell, and I really don’t want to do this.

Is there some way I can use the same set of macros from within both Objective-C methods and C functions, and only reference self and _cmd if the macro is in an Objective-C method?

edit more information:

thisFunctionIsACFunction is determined in a pretty rudimentary way (and I’m definitely open to improvements and suggestions). Basically it’s this:

BOOL thisFunctionIsACFunction == (__PRETTY_FUNCTION__[0] != '-' && __PRETTY_FUNCTION__[0] != '+');

It’s relying on the behavior of the compiler to prepend a ‘-‘ or ‘+’ for instance and class methods on Objective-C objects. Anything else must be a C function (since C functions can’t have names that begin with ‘-‘ or ‘+’).

I understand that this check is technically a runtime check, since __PRETTY_FUNCTION__ gets replaced with a char*, and this is probably the major roadblock to my request for help.

  • 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-15T09:41:24+00:00Added an answer on May 15, 2026 at 9:41 am

    The preprocessor does all of its work before the actual code is parsed. The preprocessor cannot know whether a function is C or obj-C because it runs before the code is parsed.

    For the same reason,

    if (thisFunctionIsACFunction) {
      #define SELF nil
      #define CMD nil
    } else {
      #define SELF self
      #define CMD _cmd
    }
    DoLogging(SELF, CMD, format, ##__VA_ARGS__);
    

    cannot work – the #defines are processed before the compilation stage.

    So, the code itself must contain a “runtime” check (though the compiler may optimise this out).

    I would suggest defining something like

    void *self = nil; //not sure about the types that
    SEL _cmd = nil;   //would be valid for obj-c
    

    at global scope; the C functions will “see” these definitions while the Objective-C methods will hopefully hide them with their own definitions.

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

Sidebar

Related Questions

I've been knocking my head against this for some time now. I'm not really
I have a very weird issue. I have a UserControl that has some controls
I've got an expandable css menu that is acting a bit weird in ie,
Bit of a weird one - bound to see some head scratching and wrinkled
I was experiencing some weird behaviour in some of my javascript code, but only
Ive been smashing my head with this for a while. I have 2 completely
I was kind of scratching my head at this a week ago, and now
I have been banging my head on this one all day. The C++ project
I'm about to put my head thru this sliding glass door. I can't figure
I've been scratching my head on this one for a while now and am

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.