2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "../StdAfx.h"
24 #include "subject_diagnostics.h"
26 #include "call_context.h"
28 #include <common/diagnostics/graph.h>
29 #include <common/timer.h>
31 #include <boost/lexical_cast.hpp>
35 #include <unordered_map>
41 static std::atomic<int64_t> counter { 0 };
48 namespace caspar { namespace core { namespace diagnostics {
50 static const double SECONDS_BETWEEN_FULL_STATE = 0.4;
52 class subject_graph : public caspar::diagnostics::spi::graph_sink
54 spl::shared_ptr<monitor::subject> subject_ = spl::make_shared<monitor::subject>("/" + boost::lexical_cast<std::string>(create_id()));
55 call_context context_ = call_context::for_thread();
58 std::unordered_map<std::string, int> colors_;
59 caspar::timer time_since_full_state_send_;
65 void activate() override
67 subject_->attach_parent(get_or_create_subject());
70 void set_text(const std::wstring& value) override
72 std::lock_guard<std::mutex> lock(mutex_);
76 *subject_ << monitor::message("/text") % text_;
79 void set_value(const std::string& name, double value) override
81 *subject_ << monitor::message("/value/" + name) % value;
83 send_full_state_if_long_ago();
86 void set_color(const std::string& name, int color) override
88 std::lock_guard<std::mutex> lock(mutex_);
90 colors_[name] = color;
92 *subject_ << monitor::message("/color/" + name) % color;
95 void set_tag(caspar::diagnostics::tag_severity severity, const std::string& name) override
97 std::string severity_path;
101 case caspar::diagnostics::tag_severity::INFO:
102 severity_path = "info/";
104 case caspar::diagnostics::tag_severity::WARNING:
105 severity_path = "warning/";
109 *subject_ << monitor::message("/tag/" + severity_path + name);
111 send_full_state_if_long_ago();
114 void auto_reset() override
118 monitor::subject& subject()
123 void send_full_state_if_long_ago()
125 std::lock_guard<std::mutex> lock(mutex_);
127 if (time_since_full_state_send_.elapsed() > SECONDS_BETWEEN_FULL_STATE)
133 void send_full_state()
135 *subject_ << monitor::message("/text") % text_;
137 if (context_.video_channel != -1)
138 *subject_ << monitor::message("/context/channel") % context_.video_channel;
140 if (context_.layer != -1)
141 *subject_ << monitor::message("/context/layer") % context_.layer;
143 for (const auto& color : colors_)
144 *subject_ << monitor::message("/color/" + color.first) % color.second;
146 time_since_full_state_send_.restart();
150 spl::shared_ptr<monitor::subject> get_or_create_subject()
152 static auto diag_subject = []()
154 auto subject = spl::make_shared<monitor::subject>("/diag");
156 caspar::diagnostics::spi::register_sink_factory([=]()
158 return spl::make_shared<subject_graph>();