[ 
https://issues.apache.org/jira/browse/LOGCXX-553?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17535860#comment-17535860
 ] 

Nir Arad commented on LOGCXX-553:
---------------------------------

Of course, brilliant. Tested and working.

The only issue I had is that I do want the fully qualified function name 
provided by {_}__{_}PRETTY_FUNCTION__ (namespaces and all), and not the 
stripped down name returned by LocationInfo::getMethodName(), so I include that 
in the macro as well. It would have been nice to have a method that retrieves 
the full methodName string.

Here's my final code for reference, after some additional cleanup: 
{code:c++}
#if defined( LOG4CXX_THRESHOLD ) && LOG4CXX_THRESHOLD <= 5000 
#define MY_TRACER( logger )         \
    LogTracer __log_tracer( \
        logger, LOG4CXX_LOCATION, __PRETTY_FUNCTION__ )
#else
#define MY_TRACER( logger )
#endif

class LogTracer {
public:
  LogTracer( LoggerPtr logger, log4cxx::spi::LocationInfo loc, std::string fn )
      : logger( logger ), location_info( loc ), fn( fn ) {
    if ( !LOG4CXX_UNLIKELY( logger->isTraceEnabled() ) )
      return;

    message = "Entering " + fn;
    logger->forcedLog( log4cxx::Level::getTrace(), message, location_info );
  }

  ~LogTracer() {
    if ( !LOG4CXX_UNLIKELY( logger->isTraceEnabled() ) )
      return;

    message = "Leaving " + fn;
    logger->forcedLog( log4cxx::Level::getTrace(), message, location_info );
  }

private:
  LoggerPtr   logger;
  std::string fn;
  std::string message;

  log4cxx::spi::LocationInfo location_info;
}; {code}
 

With that, as far as I'm concerned, this issue can be closed. Thank you again 
for all your help!

 

> log4cxx::spi::LocationInfo constructor backward compatibility
> -------------------------------------------------------------
>
>                 Key: LOGCXX-553
>                 URL: https://issues.apache.org/jira/browse/LOGCXX-553
>             Project: Log4cxx
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 0.13.0
>            Reporter: Nir Arad
>            Priority: Major
>
> h1. Issue
> The LocationInfo constructor was changed from 0.12.1 to 0.13.0 in a 
> non-backward compatible way.
> In 0.12.1:
> {code:c++}
> LocationInfo( const char* const fileName, const char* const functionName, int 
> lineNumber);{code}
> In 0.13.0:
> {code:c++}
> LocationInfo (const char *const fileName, const char *const shortFileName, 
> const char *const functionName, int lineNumber);
> {code}
> Specifically, a new argument (shortFileName) was added to the constructor 
> argument list.
> [https://logging.apache.org/log4cxx/latest_stable/classlog4cxx_1_1spi_1_1LocationInfo.html]
> h1. Requested change
> Add back the old constructor, so both constructors would work.
> h1. Motivation
> Using version 0.11.0, I wrote a tracer class that reports +entry and exit+ 
> from functions in my code, when TRACE log level is enabled. The way it works 
> is roughly this:
> Traced function code:
>  
> {code:c++}
> // a LoggerPtr named logger was previously created and is available in this 
> scope
> void my_func() {
>     MY_TRACER( logger );
>     // some code
> }
> {code}
> Note that I don't need to place any code at the exit from the function.
> The definition of MY_TRACER macro is:
> {code:c++}
> #define MY_TRACER( logger )         \
>   LogTracer __log_tracer( logger, __PRETTY_FUNCTION__, __FILE__, __LINE__ 
> ){code}
> The idea is that an object of type LogTracer (named __log_tracer) is created 
> at the beginning of the function. Its constructor reports entry to the 
> function, and as it goes out of scope it reports the exit from the function.
> The LogTracer class looks like this:
> {code:c++}
> class LogTracer {
> public:
>   LogTracer( LoggerPtr logger, std::string fn, std::string file, int line ) {
>     std::stringstream message;
>     this->logger = logger;
>     this->fn     = fn;
>     this->file   = file;
>     this->line   = line;
>     auto location_info =
>         ::log4cxx::spi::LocationInfo( file.c_str(), fn.c_str(), line );
>     message << "Entering " << fn;
>     if ( LOG4CXX_UNLIKELY( logger->isTraceEnabled() ) ) {
>       ::log4cxx::helpers::MessageBuffer oss_;
>       logger->forcedLog(
>           ::log4cxx::Level::getTrace(),
>           oss_.str( oss_ << message.str() ),
>           location_info );
>     }
>   }
>   ~LogTracer() {
>     std::stringstream message;
>     message << "Leaving " << fn;
>     auto location_info =
>         ::log4cxx::spi::LocationInfo( file.c_str(), fn.c_str(), line );
>     if ( LOG4CXX_UNLIKELY( logger->isTraceEnabled() ) ) {
>       ::log4cxx::helpers::MessageBuffer oss_;
>       logger->forcedLog(
>           ::log4cxx::Level::getTrace(),
>           oss_.str( oss_ << message.str() ),
>           location_info );
>     }
>   }
> private:
>   LoggerPtr   logger;
>   std::string fn;
>   std::string file;
>   int         line;
> };
> {code}
>  
> As you can see, I use the line number of the MY_TRACE() statement in both the 
> entry and the exit, since I have no way to know in what line of code did the 
> function end, and that's ok. In fact it's better, since if there is a problem 
> with the function, I'd rather jump to its beginning to inspect the code.
> The point is that to create a meaningful entry with forcedLog(), I need to 
> create a LocationInfo object.
> The change in constructor broke my code when I tried to deploy it to a new 
> environment with log4cxx version 0.13.0.
> h1. Additional thoughts / suggestions
> I don't know if log4cxx provides macros with the version numbers. I couldn't 
> find any. That would have allowed me to make my code compatible between 
> versions.
> I believe I read that the log4cxx::spi interface is supposed to an internal, 
> private interface. I would suggest to make LocationInfo a public interface 
> for uses like I described here. I realize it is not a common or intended use, 
> but it does seem to be something that other people may want to do.
> I am aware of the horrendous performance implications of instrumenting all 
> the functions in my code like this. I prevent that with guard code controlled 
> by a preprocessor flag that enables this macro or maps it to nothing.
> Suggestions for a better implementation are welcome.
>  



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to