Author Topic: is there a location macro  (Read 5021 times)

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
is there a location macro
« on: March 15, 2007, 05:39:42 pm »
I am using MinGW with C::B.

If a create messages (info, error, fatal, debug, log, etc) in my application I add a location parameter to it.
Here is an example:

Code
void Class::DoSomething ()
{
  if ( something_is_wrong == TRUE )
     Message ("something is wrong", "Class::DoSomething()");
}

Is it possible to use a macro for the second parameter. That the preprocessor set the parameter itself?

Code
     Message ("something is wrong", MACRO_CODELOCATION);

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: is there a location macro
« Reply #1 on: March 15, 2007, 05:55:08 pm »
Try __FUNCTION__ and __PRETTY_FUNCTION__ (the second one displays the non-mangled name, i.e. human-readable).
Be patient!
This bug will be fixed soon...

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
Re: is there a location macro
« Reply #2 on: March 15, 2007, 07:28:05 pm »
Try __FUNCTION__ and __PRETTY_FUNCTION__ (the second one displays the non-mangled name, i.e. human-readable).

Nice, but I am using wxWidgets and there
__FUNCTION__ is converted to L__FUNCTION__ because of the _() macro.

sethjackson

  • Guest
Re: is there a location macro
« Reply #3 on: March 15, 2007, 09:20:36 pm »
Try __FUNCTION__ and __PRETTY_FUNCTION__ (the second one displays the non-mangled name, i.e. human-readable).

Nice, but I am using wxWidgets and there
__FUNCTION__ is converted to L__FUNCTION__ because of the _() macro.

Use _T() instead?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: is there a location macro
« Reply #4 on: March 16, 2007, 12:36:10 pm »
Try __FUNCTION__ and __PRETTY_FUNCTION__ (the second one displays the non-mangled name, i.e. human-readable).

Nice, but I am using wxWidgets and there
__FUNCTION__ is converted to L__FUNCTION__ because of the _() macro.
Welcome to the wonderful world of wxWidgets, where macros grow and things never work as expected ;)

But seriously, for once this is no wxWidgets' fault alone. The reason why this doesn't work is because __FUNCTION__ (unlike for example __FILE__) is technically not a preprocessor macro, but is a local const variable implicitly generated and implicitely replaced by the C/C++ translator at the compile stage, not at the preprocessor stage.
This has to do with the fact that a function's name may not be known to the preprocessor (due to name mangling and similar mechanisms).

Thus, _T(__FUNCTION__) will just do what you experienced, it will prepend L to the variable name which will of course fail. Macros are stupid, they just blindly replace text, and they don't care if it's legal or not.

Now, how to work around the problem? You have to evaluate and convert the variable at runtime. Unless you explicitely ask for something else (via a commandline option), gcc uses UTF-8. Thus, a line of code like this will do the job:
Code
wxString s(__FUNCTION__, wxConvUTF8);
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."