|
- #pragma once
-
- #include <string>
- #include <cstdint>
- #include <iomanip>
- #include <string.h>
- #include <execinfo.h>
-
- #include "flags.h"
-
- namespace cppcore
- {
-
- template<size_t N>
- struct stack_trace
- {
- public:
- enum class print_flag
- {
- print_trace, //!< print the stack trace
- print_number, //!< add a number before the stack entry
- resolve_symbol, //!< resolve the symbol
- resolve_filename, //!< resolve the filename
- };
- using print_flags = shifted_flags<print_flag>;
-
- private:
- using symbols = std::unique_ptr<char*, decltype(&free)>;
-
- private:
- void* _stack[N]; //!< Stack addresses.
- size_t _stack_size; //!< Number of elements actually stored in _stack.
- mutable symbols _symbols; //!< List of resolved symbols.
-
- public:
- static const size_t max_stack_size = N;
-
- /**
- * @brief Constructor.
- */
- inline stack_trace();
-
- /**
- * @brief Move constructor.
- */
- inline stack_trace(stack_trace&& other);
-
- /**
- * @brief Copy constructor.
- */
- inline stack_trace(const stack_trace& other);
-
- /**
- * @brief Move assignment constructor.
- */
- inline stack_trace& operator=(stack_trace&& other);
-
- /**
- * @brief Copy assigment constructor.
- */
- inline stack_trace& operator=(const stack_trace& other);
-
- /**
- * @brief Get the number of stack entries.
- */
- inline size_t size() const;
-
- /**
- * @brief Get the stack entry at the given index.
- */
- inline void* address(size_t index) const;
-
- /**
- * @brief Get the symbol at the given index.
- */
- inline const char* symbol(size_t index) const;
-
- /**
- * @brief Get the filename at the given index.
- */
- inline std::string filename(size_t index) const;
-
- /**
- * @brief Print the whole stack trace to the passed stream.
- *
- * @param[in] os Stream to print exception to.
- * @param[in] flags Flags to use for printing.
- */
- inline void print(
- std::ostream& os,
- const print_flags& flags) const;
-
- /**
- * @brief Convert the stack trace to string.
- *
- * @param[in] flags Flags to use for printing.
- *
- * @return String representation of the exception.
- */
- inline std::string print(
- const print_flags& flags) const;
-
- /**
- * @brief Print the whole stack trace to the passed stream.
- *
- * @param[in] os Stream to print exception to.
- * @param[in] index Index of trace element to print.
- * @param[in] flags Flags to use for printing.
- */
- inline void print(
- std::ostream& os,
- size_t index,
- const print_flags& flags) const;
-
- /**
- * @brief Convert the stack trace to string.
- *
- * @param[in] index Index of trace element to print.
- * @param[in] flags Flags to use for printing.
- *
- * @return String representation of the exception.
- */
- inline std::string print(
- size_t index,
- const print_flags& flags) const;
- };
-
- /**
- * @brief Basic exception with stack trace and some more small features.
- */
- struct exception
- : public std::exception
- {
- public:
- static constexpr size_t max_stack_size = 15;
-
- using stack_trace = cppcore::stack_trace<max_stack_size>;
- using print_flag = typename stack_trace::print_flag;
- using print_flags = typename stack_trace::print_flags;
-
- /**
- * @brief Get the default exception print flags.
- */
- static inline const print_flags& default_print_flags();
-
- /**
- * @brief Print the exception to the passed stream;
- *
- * @param[in] os Stream to print exception to.
- * @param[in] ex Exception to print to stream.
- *
- * @return The stream received as parameter (for further processing).
- */
- inline friend std::ostream& operator <<(std::ostream& os, const exception& ex);
-
- private:
- mutable bool _msg_cache_empty; //!< Indicates if the message cache was created or not.
- mutable std::string _msg_cache; //!< Caches the message of the exception.
-
- public:
- std::string message; //!< message of the exception
- stack_trace trace; //!< stack trace
-
- public:
- /**
- * @brief Default constructor.
- */
- inline exception();
-
- /**
- * @brief Constructor to create an exception.
- */
- inline exception(std::string msg);
-
- /**
- * @brief Destructor.
- */
- virtual inline ~exception() = default;
-
- /**
- * @brief Print the whole exception to the passed stream.
- *
- * @param[in] os Stream to print exception to.
- * @param[in] flags Flags to use for printing.
- */
- inline void print(
- std::ostream& os,
- const stack_trace::print_flags& flags = default_print_flags()) const;
-
- /**
- * @brief Convert the exception to string.
- *
- * @param[in] flags Flags to use for printing.
- *
- * @return String representation of the exception.
- */
- inline std::string print(
- const stack_trace::print_flags& flags = default_print_flags()) const;
-
- /**
- * @brief Convert the exception to a string.
- */
- inline const std::string& to_string() const;
-
- /**
- * @brief Get the message of the exception as c-string.
- */
- inline const char* what() const throw() override;
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- virtual inline void print_message(std::ostream& os) const;
- };
-
-
-
- /**
- * @brief Error exception. Wrapps the errors from the operating system.
- */
- struct error_exception
- : public exception
- {
- public:
- int error; //!< Error received from the operating system
-
- public:
- /**
- * @brief Constructor to create a error exception.
- *
- * @param[in] e Error code received from the operating system.
- */
- inline error_exception(int e);
-
- /**
- * @brief Constructor to create a error exception.
- *
- * @param[in] msg Message of the error exception.
- * @param[in] e Error code received from the operating system.
- */
- inline error_exception(const std::string& msg, int e);
- };
-
-
-
- /**
- * @brief Range exception.
- */
- struct range_exception
- : public exception
- {
- public:
- size_t min; //!< minimum valid value of the range
- size_t max; //!< maximum valid value of the range
- size_t index; //!< actual index that was used
-
- public:
- /**
- * @brief Constructor to create a range exception.
- */
- inline range_exception(size_t mi, size_t ma, size_t idx, std::string msg = "");
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- inline void print_message(std::ostream& os) const override;
- };
-
-
-
- /**
- * @brief Argument exception.
- */
- struct argument_exception
- : public exception
- {
- public:
- std::string argument; //!< argument that caused the exception
-
- public:
- /**
- * @brief Constructor to create the argument exception.
- *
- * @param[in] arg Argument that caused the exception.
- * @param[in] msg Message of the exception.
- */
- inline argument_exception(std::string arg, std::string msg = "");
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- inline void print_message(std::ostream& os) const override;
- };
-
-
-
- /**
- * @brief Invalid operation exception.
- */
- struct invalid_operation_exception
- : public exception
- {
- using exception::exception;
- };
-
-
-
- /**
- * @brief Convert exception.
- */
- struct convert_exception
- : public exception
- {
- using exception::exception;
- };
-
- }
-
- #include "exception.inl"
|