]> git.sesse.net Git - casparcg/blobdiff - common/except.h
[scene_producer] Added possibility to CALL/CG PLAY/CG STOP/CG NEXT/CG INVOKE layers...
[casparcg] / common / except.h
index b06b1cacf54a31856867cee90dba977e07a4d8c0..a830a9300020ce84397c8ab92f0e35e3378d6fb2 100644 (file)
 #include "utf.h"
 
 #include "os/stack_trace.h"
+#include "log.h"
 
 #include <exception>
+#include <list>
+
 #include <boost/exception/all.hpp>
 #include <boost/exception/error_info.hpp>
 #include <boost/throw_exception.hpp>
+#include <boost/noncopyable.hpp>
 
 namespace caspar {
 
-typedef boost::error_info<struct tag_arg_name_info, std::string>       arg_name_info_t;
-typedef boost::error_info<struct tag_arg_value_info, std::string>      arg_value_info_t;
-typedef boost::error_info<struct tag_msg_info, std::string>                    msg_info_t;
-typedef boost::error_info<struct tag_call_stack_info, std::string>     call_stack_info_t;
-typedef boost::error_info<struct tag_msg_info, std::string>                    error_info_t;
-typedef boost::error_info<struct tag_source_info, std::string>         source_info_t;
-typedef boost::error_info<struct tag_file_name_info, std::string>      file_name_info_t;
+typedef boost::error_info<struct tag_arg_name_info,            std::string>    arg_name_info_t;
+typedef boost::error_info<struct tag_arg_value_info,   std::string>    arg_value_info_t;
+typedef boost::error_info<struct tag_msg_info,                 std::string>    msg_info_t;
+typedef boost::error_info<struct tag_call_stack_info,  std::string>    call_stack_info_t;
+typedef boost::error_info<struct tag_error_info,               std::string>    error_info_t;
+typedef boost::error_info<struct tag_source_info,              std::string>    source_info_t;
+typedef boost::error_info<struct tag_file_name_info,   std::string>    file_name_info_t;
+typedef boost::error_info<struct tag_context_info,             std::string>    context_info_t;
 
 template<typename T>
 inline arg_name_info_t         arg_name_info(const T& str)             {return arg_name_info_t(u8(str));}
@@ -53,12 +58,14 @@ inline error_info_t                 error_info(const T& str)                {return error_info_t(u8(str));}
 template<typename T>
 inline source_info_t           source_info(const T& str)               {return source_info_t(u8(str));}
 template<typename T>
-inline file_name_info_t        file_name_info(const T& str)    {return file_name_info_t(u8(str));}
+inline file_name_info_t                file_name_info(const T& str)    {return file_name_info_t(u8(str));}
+template<typename T>
+inline context_info_t          context_info(const T& str)              {return context_info_t(u8(str));}
 
 typedef boost::error_info<struct tag_line_info, size_t>                                                line_info;
 typedef boost::error_info<struct tag_nested_exception_, std::exception_ptr> nested_exception;
 
-struct caspar_exception                        : virtual boost::exception, virtual std::exception 
+struct caspar_exception                        : virtual boost::exception, virtual std::exception
 {
        caspar_exception(){}
        const char* what() const throw() override
@@ -83,10 +90,44 @@ struct invalid_operation            : virtual caspar_exception {};
 struct operation_failed                        : virtual caspar_exception {};
 struct timed_out                               : virtual caspar_exception {};
 
-struct not_supported                   : virtual caspar_exception {};
 struct not_implemented                 : virtual caspar_exception {};
 
-#define CASPAR_THROW_EXCEPTION(e) BOOST_THROW_EXCEPTION(e << call_stack_info(caspar::get_call_stack()))
+struct user_error                              : virtual caspar_exception {};
+struct expected_user_error             : virtual user_error {};
+struct not_supported                   : virtual user_error {};
 
-}
+std::string get_context();
+
+class scoped_context : boost::noncopyable
+{
+public:
+       scoped_context();
+       scoped_context(std::string msg);
+       template <typename Str>
+       scoped_context(Str msg)
+               : scoped_context(u8(std::move(msg)))
+       {
+       }
 
+       ~scoped_context();
+       void replace_msg(std::string msg);
+       template <typename Str>
+       void replace_msg(std::string msg)
+       {
+               replace_msg(u8(std::move(msg)));
+       }
+       void clear_msg();
+private:
+       std::list<std::string>& for_thread_;
+       std::string*                    msg_;
+};
+
+#define _CASPAR_GENERATE_UNIQUE_IDENTIFIER_CAT(name, line) name##line
+#define _CASPAR_GENERATE_UNIQUE_IDENTIFIER(name, line) _CASPAR_GENERATE_UNIQUE_IDENTIFIER_CAT(name, line)
+#define CASPAR_SCOPED_CONTEXT_MSG(ctx_msg) ::caspar::scoped_context _CASPAR_GENERATE_UNIQUE_IDENTIFIER(SCOPED_CONTEXT, __LINE__)(u8(ctx_msg));
+
+#define CASPAR_THROW_EXCEPTION(e) ::boost::exception_detail::throw_exception_((e << call_stack_info(caspar::get_call_stack()) << context_info(get_context())), BOOST_THROW_EXCEPTION_CURRENT_FUNCTION, ::caspar::log::remove_source_prefix(__FILE__), __LINE__)
+
+std::string get_message_and_context(const caspar_exception& e);
+
+}