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()
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())
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()
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)
- 1910 "/usr/local/include/log4cxx/logger.h" 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
- duplicates
-
LOGCXX-319 Please make sure that the LOG4CXX_* macro's can be used as ordinary statements.
- Resolved