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

  • SEARCH
  • Home
  • 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 7636521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:42:10+00:00 2026-05-31T07:42:10+00:00

I am using the macros __FILE__ and __func__ for retrieving the file and function

  • 0

I am using the macros __FILE__ and __func__ for retrieving the file and function location.
My code is structured like this… (note i am using a game engine )

Test.h

class TestClass
{
...
void Log(int status);
...
};

Test.cpp

...
void TestClass::Log(int status)
{
printf("%s %s %i",__FILE__,__func__, status);
}
...

.

and i create a class object and use it to log whatever i want to.

example.cpp

#include "Test.h"
    ...
TestClass tt;
    ...
    tt.Log((int)1);
    ...

the problem is that the output pastes the file and function name of the Test.cpp, and note the filename and function of example.cpp.

Is there a way around this or do i have to directly use the printf statement again and again?

Thanks

  • 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-31T07:42:11+00:00Added an answer on May 31, 2026 at 7:42 am

    You don’t need to use printf wherever in the code, but you will need to call TestClass::Log with extra parameters __FILE__ and __func__.

    Don’t threat however, you can create a macro for that so you don’t have to write down the extra parameters on each call:

    #define TEST_CLASS_LOG(testClassInstance,status) \
                  testClassInstance.Log((int)status, __FILE__, __func__);
    

    and instead of calling

    tt.Log((int)1);
    

    you would call

    TEST_CLASS_LOG(tt,1);
    

    You’d need to change the Log signature to account for the extra parameters of course:

    void Log(int status, const char* fileName, const char* functionName);
    

    Also, I see Log isn’t accessing any class members, so you can make it even easier and make it static:

    class TestClass
    {
       static void Log(int status);
    };
    
    void TestClass::Log(int status, const char* file, const char* func)
    {
       printf("%s %s %i", file, func, status);
    }
    
    #define TEST_CLASS_LOG(status) \
                      TestClass::Log((int)status, __FILE__, __func__);
    

    So, if you do it like this, you’d only need to call:

    TEST_CLASS_LOG(1);
    

    Unfortunately there’s no way to do this automatically, without modifying your code, as macros expand at runtime. What your compiler sees now is:

    void TestClass::Log(int status)
    {
       printf("%s %s %i","Test.cpp","Log", status);
    }
    

    that is why you need the file name and function name passed as parameters.

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

Sidebar

Related Questions

I'm having some problems with users cheating my online game by using macros to
With using plain C preprocessor macros, is it possible to create something like: INIT_BASE(0x100)
When using C preprocessor one can stringify macro argument like this: #define TO_STRING(x) a
I've been using NVelocity as a stand-alone templating engine in my text file generator.
Besides __LINE__ and __FILE__ , are there other useful pre-defined macros, like __FUNCTION_NAME__ ?
I am (was) using the __FILE__ and __LINE__ macros for printing diagnostic messages out
I have a question about using #undef to redefine macros. I have a file
I am using the following to search for a file defined as a macro
I am using macros in my twig to achieve the dynamically adding rows functionality
I've already implemented, using macros, a C++ property system that satisfied the following requirements:

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.