我使用用g ++ 4.9.2构建的log4cplus-1.1.2并安装在/ usr / local来处理Debian 8上的日志。当我使用distcc
编译我的代码时,它打印了这么多 g ++格式警告以前使用相同的编译选项检测到的 NOT !我查看了源代码,发现LOG4CPLUS_MACRO_FMT_BODY()
CAN NOT 中的宏log4cplus/loggingmacros.h
检测到日志格式参数,但helpers::snprintf_buf::print()
包含LOG4CPLUS_FORMAT_ATTRIBUTE (__printf__, 2, 3)
。测试代码:
#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;
}
我编译它
g++ -g -Wall -std=c++11 -c main.cpp`
没有格式警告输出,但获取main.o
!
然而,如果我用-no-integrated-cpp
选项编译它,发现格式警告,对我来说很奇怪!
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);
如果我只是将loggingmacros.h
从/usr/local/log4cplus-1.1.2/include/log4cplus
复制到我的测试目录,并更改包含文件#include "loggingmacros.h"
而不是#include <log4cplus/loggingmacros.h>
,它也可以!
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);
构建log4cplus时的配置选项:
./configure CXXFLAGS=-std=c++11 --prefix=/usr/local/log4cplus-1.1.2 --with-working-locale | tee configure.log.`date +%Y%m%d`
g ++版本:
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)