]> git.sesse.net Git - casparcg/blob - common/except.cpp
Created abstraction for property tree get(), get_child() and get_value() to be able...
[casparcg] / common / except.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "stdafx.h"
23
24 #include "except.h"
25
26 #include <boost/thread/tss.hpp>
27 #include <boost/algorithm/string/join.hpp>
28
29 namespace {
30
31 boost::thread_specific_ptr<std::list<std::string>>& context_stacks_per_thread()
32 {
33         static boost::thread_specific_ptr<std::list<std::string>> instances;
34
35         return instances;
36 }
37
38 }
39
40 namespace caspar {
41
42 std::list<std::string>& context_stack_for_thread()
43 {
44         auto local = context_stacks_per_thread().get();
45
46         if (!local)
47         {
48                 local = new std::list<std::string>();
49                 context_stacks_per_thread().reset(local);
50         }
51
52         return *local;
53 }
54
55 std::string get_context()
56 {
57         return boost::join(context_stack_for_thread(), "");
58 }
59
60 scoped_context::scoped_context()
61         : scoped_context::scoped_context("")
62 {
63 }
64
65 scoped_context::scoped_context(std::string msg)
66         : for_thread_(context_stack_for_thread())
67 {
68         for_thread_.push_back(std::move(msg));
69         msg_ = &for_thread_.back();
70 }
71
72 void scoped_context::replace_msg(std::string msg)
73 {
74         if (&for_thread_ != &context_stack_for_thread())
75                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Called from wrong thread"));
76
77         *msg_ = std::move(msg);
78 }
79
80 void scoped_context::clear_msg()
81 {
82         replace_msg("");
83 }
84
85 scoped_context::~scoped_context()
86 {
87         for_thread_.pop_back();
88 }
89
90
91
92 std::string get_message_and_context(const caspar_exception& e)
93 {
94         std::string result;
95
96         auto msg = boost::get_error_info<msg_info_t>(e);
97         auto ctx = boost::get_error_info<context_info_t>(e);
98
99         if (msg)
100                 result += *msg;
101
102         if (ctx && !ctx->empty())
103         {
104                 result += " (";
105                 result += *ctx;
106                 result += ")";
107         }
108
109         if (!result.empty() && result.back() != '.')
110                 result += ".";
111
112         return result;
113 }
114
115 }