I use log4cplus-1.1.2 built with g++ 4.9.2 and installed at /usr/local to handle logs on Debian 8. When I use distcc
to compile my code, it prints so many format warning which NOT detected by g++ with the same compile option before! I checked the source code and found the macro LOG4CPLUS_MACRO_FMT_BODY()
in log4cplus/loggingmacros.h
CANNOT detects the log format parameters, although helpers::snprintf_buf::print()
includes LOG4CPLUS_FORMAT_ATTRIBUTE (__printf__, 2, 3)
. The test code:
#include <log4cplus/loggingmacros.h>
//#include "loggingmacros.h"
#include <log4cplus/logger.h>
#include <log4cplus/streams.h>
#include <log4cplus/tstring.h>
#include <log4cplus/loglevel.h>
#include <log4cplus/thread/threads.h>
#define LOG4CPLUS_LOGGER() \
log4cplus::Logger::getInstance(log4cplus::thread::getCurrentThreadName2())
using namespace std;
using namespace log4cplus;
int main()
{
size_t i = 1;
LOG4CPLUS_MACRO_FMT_BODY(LOG4CPLUS_LOGGER(), DEBUG_LOG_LEVEL, "%s", i); // Format warning!!!
/*
helpers::snprintf_buf &sbuf = log4cplus::detail::get_macro_body_snprintf_buf ();
sbuf.print("%s", i);
*/
return 0;
}
I compile it
g++ -g -Wall -std=c++11 -c main.cpp`
No format warning output, but get main.o
!
However it is very strange for me if I compile it with -no-integrated-cpp
option, format warning found!
g++ -no-integrated-cpp -g -Wall -std=c++11 -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:19:394: warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
LOG4CPLUS_MACRO_FMT_BODY(LOG4CPLUS_LOGGER(), DEBUG_LOG_LEVEL, "%s", i);
And also if I just copy the loggingmacros.h
from /usr/local/log4cplus-1.1.2/include/log4cplus
to my test directory, and change the include file #include "loggingmacros.h"
instead of #include <log4cplus/loggingmacros.h>
, it also works!!!
g++ -g -Wall -std=c++11 -c main.cpp
In file included from main.cpp:2:0:
main.cpp: In function ‘int main()’:
loggingmacros.h:245:45: warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
= _snpbuf.print (__VA_ARGS__); \
^
main.cpp:19:5: note: in expansion of macro ‘LOG4CPLUS_MACRO_FMT_BODY’
LOG4CPLUS_MACRO_FMT_BODY(LOG4CPLUS_LOGGER(), DEBUG_LOG_LEVEL, "%s", i);
The configure option when build log4cplus:
./configure CXXFLAGS=-std=c++11 --prefix=/usr/local/log4cplus-1.1.2 --with-working-locale | tee configure.log.`date +%Y%m%d`
The g++ version:
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.9.2-10' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Debian 4.9.2-10)