]> git.sesse.net Git - casparcg/blob - common/except.cpp
Remove most of boost::lexical_cast.
[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/algorithm/string/join.hpp>
27
28 namespace {
29
30 std::list<std::string>& context_stacks_per_thread()
31 {
32         static thread_local std::list<std::string> instances;
33
34         return instances;
35 }
36
37 }
38
39 namespace caspar {
40
41 std::list<std::string>& context_stack_for_thread()
42 {
43         return context_stacks_per_thread();
44 }
45
46 std::string get_context()
47 {
48         return boost::join(context_stack_for_thread(), "");
49 }
50
51 scoped_context::scoped_context()
52         : scoped_context::scoped_context("")
53 {
54 }
55
56 scoped_context::scoped_context(std::string msg)
57         : for_thread_(context_stack_for_thread())
58 {
59         for_thread_.push_back(std::move(msg));
60         msg_ = &for_thread_.back();
61 }
62
63 void scoped_context::replace_msg(std::string msg)
64 {
65         if (&for_thread_ != &context_stack_for_thread())
66                 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Called from wrong thread"));
67
68         *msg_ = std::move(msg);
69 }
70
71 void scoped_context::clear_msg()
72 {
73         replace_msg("");
74 }
75
76 scoped_context::~scoped_context()
77 {
78         for_thread_.pop_back();
79 }
80
81
82
83 std::string get_message_and_context(const caspar_exception& e)
84 {
85         std::string result;
86
87         auto msg = boost::get_error_info<msg_info_t>(e);
88         auto ctx = boost::get_error_info<context_info_t>(e);
89
90         if (msg)
91                 result += *msg;
92
93         if (ctx && !ctx->empty())
94         {
95                 result += " (";
96                 result += *ctx;
97                 result += ")";
98         }
99
100         if (!result.empty() && result.back() != '.')
101                 result += ".";
102
103         return result;
104 }
105
106 }