Uploaded image for project: 'Log4cxx'
  1. Log4cxx
  2. LOGCXX-393

Strange compile error w/ log4cxx, version 0.10.0,gcc version 4.2.1 20070719 [FreeBSD]

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 0.10.0
    • None
    • None
    • None
    • gcc 4.2.1
      FreeBSD 8.2-RELEASE-p4 (amd64)

    Description

      Quite strange (but minor) issue using log4cxx. The following code snippet fails to compile:

      #include <log4cxx/logger.h>
      log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("fn");
      void fn()

      { if (logger->isTraceEnabled()) LOG4CXX_TRACE(logger, "Trace"); else if (logger->isDebugEnabled()) LOG4CXX_DEBUG(logger, "Debug"); }

      Compiled with the following command: g++ -c -I/usr/local/include t2.cpp

      Results in:

      t2.cpp: In function 'void fn()':
      t2.cpp:6: error: expected primary-expression before 'else'
      t2.cpp:6: error: expected `;' before 'else'
      t2.cpp:8: error: expected `}' at end of input

      You can solve the compile error, by enclosing the LOG4CXX_TRACE statement in braces, thusly:

      #include <log4cxx/logger.h>
      log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("fn");
      void fn() {
      if (logger->isTraceEnabled())

      { LOG4CXX_TRACE(logger, "Trace"); }

      else if (logger->isDebugEnabled())
      LOG4CXX_DEBUG(logger, "Debug");
      }

      Here's where it get's really strange....the compiler only generates the error, when you have the 'else if' statement immediately following the LOG4CXX_TRACE() macro. So this piece of code also compiles without errors.

      #include <log4cxx/logger.h>
      log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("fn");
      void fn()

      { if (logger->isTraceEnabled()) LOG4CXX_TRACE(logger, "Trace"); }

      I strongly suspect it's some sort of strange artifact of the pre-processor, but I'm having difficulty figuring out what it specifically doesn't like. I've attached the pre-processor output below.

      Jim C.


      (REMOVED MOST OF THE CPP OUTPUT FOR BREVITY)

      1. 1910 "/usr/local/include/log4cxx/logger.h" 2
      2. 2 "t2.cpp" 2
        log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("fn");
        void fn() {
        if (logger->isTraceEnabled())
        { if (_builtin_expect(logger->isTraceEnabled(), 0)) { ::log4cxx::helpers::MessageBuffer oss; logger->forcedLog(::log4cxx::Level::getTrace(), oss_.str(oss_ << "Trace"), ::log4cxx::spi::LocationInfo("t2.cpp", _PRETTY_FUNCTION_, 5)); }};
        else if (logger->isDebugEnabled())
        { if (_builtin_expect(logger->isDebugEnabled(), 0)) { ::log4cxx::helpers::MessageBuffer oss; logger->forcedLog(::log4cxx::Level::getDebug(), oss_.str(oss_ << "Debug"), ::log4cxx::spi::LocationInfo("t2.cpp", _PRETTY_FUNCTION_, 7)); }};
        }

      Attachments

        Issue Links

          Activity

            People

              carnold@apache.org Curt Arnold
              jamercee Jim Carroll
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: