如何从boost :: exception获得更好的诊断信息?

时间:2014-02-01 00:14:37

标签: c++ boost

我正在使用

投掷boost::exception
BOOST_THROW_EXCEPTION(trace_collector_error_t()
    << boost::errinfo_api_function("ioctl")
    << boost::errinfo_errno(errno)
    << errinfo_trace_params_t(trace_params));

之后

std::cerr << boost::diagnostic_information(e) << std::endl;

我得到了

: Throw in function trace_config_t set_up_trace(size_t, size_t)
Dynamic exception type: N5boost16exception_detail10clone_implI23trace_collector_error_tEE
std::exception::what: std::exception
[P23errinfo_trace_params_t_] = [ type: 14trace_params_t, size: 16, dump: 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 ]
22, "Invalid argument"[PN5boost21errinfo_api_function_E] = ioctl

我有两个问题:

  • 如何获得更好的类型名称?
  • 如何正确打印errinfo_errno?

我正在使用GCC 4.6和Boost 1.55.0。

2 个答案:

答案 0 :(得分:2)

嗯,这不是一个答案,但我无法将其放入评论中,而不会完全破坏格式。所以就是这样。

我在GCC-4.7.2和Boost-1.53​​上得到了以下结果。 更新:刚刚在Ubuntu 13.10中检查了GCC-4.8.1 / Boost-1.53​​,它的工作方式与下面的代码相同

#include <errno.h>

#include <boost/exception/all.hpp>

using namespace std;

struct trace_collector_error: virtual std::exception, virtual boost::exception {};

typedef boost::error_info<struct trace_tag_t, int> trace_info;

namespace a {
class A {
public:
        A() { clog << __PRETTY_FUNCTION__ << endl; }
        void f() {
                clog << __PRETTY_FUNCTION__ << endl;
                BOOST_THROW_EXCEPTION(trace_collector_error()
                        << boost::errinfo_api_function("ioctl")
                        << boost::errinfo_errno(errno)
                        << trace_info(1)
                );
        }
};
}

int main(void) {
        a::A a;
        try {
                a.f();
        } catch (const trace_collector_error& e) {
                std::cerr << boost::diagnostic_information(e) << std::endl;
        }
        return 0;
}

输出是:

alex@galene tmp/c++/tests $ build/boost_exception_test 
a::A::A()
void a::A::f()
/home/alex/tmp/c++/tests/boost_exception_test.cpp(23): Throw in function void a::A::f()
Dynamic exception type: boost::exception_detail::clone_impl
std::exception::what: std::exception
[trace_tag_t*] = 1
[boost::errinfo_errno_*] = 0, "Success"
[boost::errinfo_api_function_*] = ioctl

这是我的编译器版本/ build:

alex@galene tmp/c++/tests $ gcc -v
Using built-in specs.
COLLECT_GCC=i586-alt-linux-gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i586-alt-linux/4.7.2/lto-wrapper
Target: i586-alt-linux
Configured with: ../configure --host=i586-alt-linux --build=i586-alt-linux --target=i586-alt-linux --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var/lib --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --disable-dependency-tracking --without-included-gettext --enable-shared --enable-bootstrap --program-suffix=-4.7 --with-slibdir=/lib --with-bugurl=http://bugzilla.altlinux.org --enable-__cxa_atexit --enable-threads=posix --enable-checking=release --with-system-zlib --without-included-gettext --enable-multilib --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,fortran,objc,obj-c++,java,ada,go,lto --enable-plugin --enable-java-awt=gtk --with-native-libdir=/usr/lib/gcj-4.7 --with-ecj-jar=/usr/share/java/ecj.jar --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.7-1.5.0.0/jre --enable-libgcj-multifile --disable-libjava-multilib --enable-java-maintainer-mode --with-arch=i586 --with-tune=generic
Thread model: posix
gcc version 4.7.2 20121109 (ALT Linux 4.7.2-alt7) (GCC) 
alex@galene tmp/c++/tests $ 

答案 1 :(得分:0)

对于那些苦苦挣扎:切换到Boost版本1.53修复了问题。