]> git.sesse.net Git - casparcg/blobdiff - common/except.cpp
Fixed copy-paste error
[casparcg] / common / except.cpp
index 82bdf638fba4cc8d0bd7dade5b285ccf45b6e43b..7257e79971162bb2fa5663e527e1818bfa975884 100644 (file)
-#include "stdafx.h"\r
-\r
-#include "except.h"\r
-\r
-#include "os/windows/windows.h"\r
-\r
-namespace caspar {\r
-       \r
-void win32_exception::install_handler() \r
-{\r
-//#ifndef _DEBUG\r
-       _set_se_translator(win32_exception::Handler);\r
-//#endif\r
-}\r
-\r
-void win32_exception::Handler(unsigned int errorCode, EXCEPTION_POINTERS* pInfo) {\r
-       switch(errorCode)\r
-       {\r
-       case EXCEPTION_ACCESS_VIOLATION:\r
-               throw win32_access_violation(*(pInfo->ExceptionRecord));\r
-               break;\r
-\r
-       default:\r
-               throw win32_exception(*(pInfo->ExceptionRecord));\r
-       }\r
-}\r
-\r
-win32_exception::win32_exception(const EXCEPTION_RECORD& info) : message_("Win32 exception"), location_(info.ExceptionAddress), errorCode_(info.ExceptionCode)\r
-{\r
-       switch(info.ExceptionCode)\r
-       {\r
-       case EXCEPTION_ACCESS_VIOLATION:\r
-               message_ = "Access violation";\r
-               break;\r
-       case EXCEPTION_FLT_DIVIDE_BY_ZERO:\r
-       case EXCEPTION_INT_DIVIDE_BY_ZERO:\r
-               message_ = "Divide by zero";\r
-               break;\r
-       }\r
-}\r
-\r
-win32_access_violation::win32_access_violation(const EXCEPTION_RECORD& info) : win32_exception(info), isWrite_(false), badAddress_(0) \r
-{\r
-       isWrite_ = info.ExceptionInformation[0] == 1;\r
-       badAddress_ = reinterpret_cast<win32_exception::address>(info.ExceptionInformation[1]);\r
-}\r
-\r
-const char* win32_access_violation::what() const\r
-{\r
-       sprintf_s<>(messageBuffer_, "Access violation at %p, trying to %s %p", location(), isWrite_?"write":"read", badAddress_);\r
-\r
-       return messageBuffer_;\r
-}\r
-\r
-}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "stdafx.h"
+
+#include "except.h"
+
+#include <boost/thread/tss.hpp>
+#include <boost/algorithm/string/join.hpp>
+
+namespace {
+
+boost::thread_specific_ptr<std::list<std::string>>& context_stacks_per_thread()
+{
+       static boost::thread_specific_ptr<std::list<std::string>> instances;
+
+       return instances;
+}
+
+}
+
+namespace caspar {
+
+std::list<std::string>& context_stack_for_thread()
+{
+       auto local = context_stacks_per_thread().get();
+
+       if (!local)
+       {
+               local = new std::list<std::string>();
+               context_stacks_per_thread().reset(local);
+       }
+
+       return *local;
+}
+
+std::string get_context()
+{
+       return boost::join(context_stack_for_thread(), "");
+}
+
+scoped_context::scoped_context()
+       : scoped_context::scoped_context("")
+{
+}
+
+scoped_context::scoped_context(std::string msg)
+       : for_thread_(context_stack_for_thread())
+{
+       for_thread_.push_back(std::move(msg));
+       msg_ = &for_thread_.back();
+}
+
+void scoped_context::replace_msg(std::string msg)
+{
+       if (&for_thread_ != &context_stack_for_thread())
+               CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Called from wrong thread"));
+
+       *msg_ = std::move(msg);
+}
+
+void scoped_context::clear_msg()
+{
+       replace_msg("");
+}
+
+scoped_context::~scoped_context()
+{
+       for_thread_.pop_back();
+}
+
+
+
+std::string get_message_and_context(const caspar_exception& e)
+{
+       std::string result;
+
+       auto msg = boost::get_error_info<msg_info_t>(e);
+       auto ctx = boost::get_error_info<context_info_t>(e);
+
+       if (msg)
+               result += *msg;
+
+       if (ctx && !ctx->empty())
+       {
+               result += " (";
+               result += *ctx;
+               result += ")";
+       }
+
+       if (!result.empty() && result.back() != '.')
+               result += ".";
+
+       return result;
+}
+
+}