Commit 77088a4e authored by Ronan's avatar Ronan

fix(Exception): clean ugly code + fix windows build

parent b1876be8
Pipeline #293 passed with stage
in 0 seconds
...@@ -20,55 +20,50 @@ ...@@ -20,55 +20,50 @@
#define exception_h #define exception_h
#include <exception> #include <exception>
#include <string>
#include <iostream>
#include <sstream> #include <sstream>
#include <ostream>
/**
/** * @brief General pupose exception saving backtrace.
* @brief General pupose exception saving backtrace. *
* * sample of use:
* sample of use: * try {
* try { * throw BCTBX_EXCEPTION << "Hello, this is my exception";
* throw BCTBX_EXCEPTION << "Hello, this is my exception"; * } catch (BctbxException e&) {
* } catch (BctbxException e&) { * BCTOOLBOX_SLOGD("mylogdomain") << "Exception cauth"<< e;
* BCTOOLBOX_SLOGD("mylogdomain") << "Exception cauth"<< e; * }
* } *
* *
* */
*/
class BctbxException : public std::exception { class BctbxException : public std::exception {
public: public:
BctbxException(); BctbxException(const std::string &message = "");
BctbxException(const std::string &message);
BctbxException(const char *message);
virtual ~BctbxException() throw();
BctbxException(const BctbxException &other); BctbxException(const BctbxException &other);
virtual ~BctbxException() = default;
/** /**
* print stack strace to stderr * print stack strace to stderr
* */ * */
void printStackTrace() const; void printStackTrace() const;
void printStackTrace(std::ostream &os) const; void printStackTrace(std::ostream &os) const;
const char *what() const throw(); const char *what() const noexcept override;
const std::string &str() const;
std::string str() const {
return mOs.str();
}
/* same as osstringstream, but as osstream does not have cp contructor, BctbxException can't inherit from /* same as osstringstream, but as osstream does not have cp contructor, BctbxException can't inherit from
* osstream*/ * osstream*/
template <typename T2> BctbxException &operator<<(const T2 &val) { template <typename T2> BctbxException &operator<<(const T2 &val) {
mOs << val; mOs << val;
return *this; return *this;
} }
protected:
int mOffset; /*to hide last stack traces*/
private: private:
void *mArray[20]; void *mArray[20];
size_t mSize; size_t mSize;
std::ostringstream mOs; std::ostringstream mOs;
mutable std::string mMessage;
}; };
std::ostream &operator<<(std::ostream &__os, const BctbxException &e); std::ostream &operator<<(std::ostream &__os, const BctbxException &e);
......
...@@ -17,20 +17,17 @@ ...@@ -17,20 +17,17 @@
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif // ifdef HAVE_CONFIG_H
#ifdef HAVE_EXECINFO #ifdef HAVE_EXECINFO
#include <execinfo.h> #include <execinfo.h>
#include <dlfcn.h>
#include <cxxabi.h>
#include <libgen.h>
#include <iomanip>
#endif #endif
#include <unistd.h>
#include <dlfcn.h>
#include <cxxabi.h>
#include <exception>
#include <iomanip>
#include <libgen.h>
#include "bctoolbox/exception.hh" #include "bctoolbox/exception.hh"
#include "bctoolbox/logging.h" #include "bctoolbox/logging.h"
...@@ -50,13 +47,13 @@ static void uncaught_handler() { ...@@ -50,13 +47,13 @@ static void uncaught_handler() {
} }
#endif #endif
BctbxException::BctbxException(const char *message) : mOffset(1), mSize(0) { BctbxException::BctbxException(const std::string &message) : mSize(0) {
#ifdef HAVE_EXECINFO #ifdef HAVE_EXECINFO
mSize = backtrace(mArray, sizeof(mArray) / sizeof(void *)); mSize = backtrace(mArray, sizeof(mArray) / sizeof(void *));
#else #else
mSize = 0; mSize = 0;
#endif #endif
if (message) if (!message.empty())
mOs << message; mOs << message;
#ifdef HAVE_EXECINFO #ifdef HAVE_EXECINFO
#if __clang #if __clang
...@@ -66,54 +63,16 @@ BctbxException::BctbxException(const char *message) : mOffset(1), mSize(0) { ...@@ -66,54 +63,16 @@ BctbxException::BctbxException(const char *message) : mOffset(1), mSize(0) {
#endif #endif
} }
BctbxException::BctbxException(const BctbxException &other) : mOffset(other.mOffset), mSize(other.mSize) { BctbxException::BctbxException(const BctbxException &other) : mSize(other.mSize) {
memcpy(mArray, other.mArray, sizeof(mArray)); memcpy(mArray, other.mArray, sizeof(mArray));
mOs << other.str(); mOs << other.str();
} }
#if __cplusplus > 199711L
BctbxException::BctbxException(const string &msg) : BctbxException(msg.c_str()) {
mOffset++;
}
#else
BctbxException::BctbxException(const string &message) : mOffset(2) {
#ifdef HAVE_EXECINFO
mSize = backtrace(mArray, sizeof(mArray) / sizeof(void *));
#else
mSize = 0;
#endif
*this << message;
set_terminate(uncaught_handler); // invoke in case of uncautch exception for this thread
}
#endif
BctbxException::~BctbxException() throw() {
// nop
}
#if __cplusplus > 199711L
BctbxException::BctbxException() : BctbxException("") {
mOffset++;
}
#else
BctbxException::BctbxException() : mOffset(2) {
#ifdef HAVE_EXECINFO
mSize = backtrace(mArray, sizeof(mArray) / sizeof(void *));
#else
mSize = 0;
#endif
*this << "";
#ifdef HAVE_EXECINFO
set_terminate(uncaught_handler); // invoke in case of uncautch exception for this thread
#endif
}
#endif
void BctbxException::printStackTrace() const { void BctbxException::printStackTrace() const {
#ifdef HAVE_EXECINFO #ifdef HAVE_EXECINFO
backtrace_symbols_fd(mArray + mOffset, mSize - mOffset, STDERR_FILENO); backtrace_symbols_fd(mArray + 1, mSize - 1, STDERR_FILENO);
#else #else
std::cout << "stack trace not available on this platform"; std::cerr << "stack trace not available on this platform" << std::endl;
#endif #endif
} }
...@@ -121,7 +80,7 @@ void BctbxException::printStackTrace(std::ostream &os) const { ...@@ -121,7 +80,7 @@ void BctbxException::printStackTrace(std::ostream &os) const {
#ifdef HAVE_EXECINFO #ifdef HAVE_EXECINFO
char **bt = backtrace_symbols(mArray, mSize); char **bt = backtrace_symbols(mArray, mSize);
int position=0; int position=0;
for (unsigned int i = mOffset; i < mSize; ++i) { for (unsigned int i = 1; i < mSize; ++i) {
Dl_info info; Dl_info info;
char *demangled = NULL; char *demangled = NULL;
int status = -1; int status = -1;
...@@ -147,15 +106,10 @@ void BctbxException::printStackTrace(std::ostream &os) const { ...@@ -147,15 +106,10 @@ void BctbxException::printStackTrace(std::ostream &os) const {
#endif #endif
} }
const std::string &BctbxException::str() const { const char *BctbxException::what() const noexcept {
mMessage = mOs.str(); // avoid returning a reference to temporary
return mMessage;
}
const char *BctbxException::what() const throw() {
return str().c_str(); return str().c_str();
} }
// Class BctbxException
std::ostream &operator<<(std::ostream &__os, const BctbxException &e) { std::ostream &operator<<(std::ostream &__os, const BctbxException &e) {
__os << e.str() << std::endl; __os << e.str() << std::endl;
e.printStackTrace(__os); e.printStackTrace(__os);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment