From 5fb7f47b0a34ae5bc89207bf3172d15e06c86430 Mon Sep 17 00:00:00 2001 From: Helge Norberg Date: Fri, 13 Nov 2015 16:39:54 +0100 Subject: [PATCH] Implemented a TCP server, simply sending every log line to the remote client. If using the default casparcg.config it will listen on 3250. Any data sent by the client is ignored. --- common/log.cpp | 39 ++++++++++++ common/log.h | 3 + protocol/CMakeLists.txt | 5 ++ protocol/log/tcp_logger_protocol_strategy.cpp | 60 +++++++++++++++++++ protocol/log/tcp_logger_protocol_strategy.h | 34 +++++++++++ shell/casparcg.config | 4 ++ shell/server.cpp | 5 +- 7 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 protocol/log/tcp_logger_protocol_strategy.cpp create mode 100644 protocol/log/tcp_logger_protocol_strategy.h diff --git a/common/log.cpp b/common/log.cpp index 82211663c2..3b857caa5c 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -181,6 +181,45 @@ void add_file_sink(const std::wstring& folder) } } +std::shared_ptr add_preformatted_line_sink(std::function formatted_line_sink) +{ + class sink_backend : public boost::log::sinks::basic_formatted_sink_backend + { + std::function formatted_line_sink_; + public: + sink_backend(std::function formatted_line_sink) + : formatted_line_sink_(std::move(formatted_line_sink)) + { + } + + void consume(const boost::log::record_view& rec, const std::string& formatted_message) + { + try + { + formatted_line_sink_(formatted_message); + } + catch (...) + { + std::cerr << "[sink_backend] Error while consuming formatted message: " << formatted_message << std::endl << std::endl; + } + } + }; + + typedef boost::log::sinks::synchronous_sink sink_type; + + auto sink = boost::make_shared(std::move(formatted_line_sink)); + bool print_all_characters = true; + + sink->set_formatter(boost::bind(&my_formatter, print_all_characters, _1, _2)); + + boost::log::core::get()->add_sink(sink); + + return std::shared_ptr(nullptr, [=](void*) + { + boost::log::core::get()->remove_sink(sink); + }); +} + void set_log_level(const std::wstring& lvl) { if (boost::iequals(lvl, L"trace")) diff --git a/common/log.h b/common/log.h index 441837116d..2704e3823b 100644 --- a/common/log.h +++ b/common/log.h @@ -32,6 +32,8 @@ #include #include +#include +#include namespace caspar { namespace log { @@ -61,6 +63,7 @@ inline std::basic_string replace_nonprintable_copy(std::basic_string add_preformatted_line_sink(std::function formatted_line_sink); typedef boost::log::sources::wseverity_logger_mt caspar_logger; diff --git a/protocol/CMakeLists.txt b/protocol/CMakeLists.txt index 9aa96d00d9..fc1e0a1960 100644 --- a/protocol/CMakeLists.txt +++ b/protocol/CMakeLists.txt @@ -14,6 +14,8 @@ set(SOURCES clk/clk_commands.cpp clk/clk_command_processor.cpp + log/tcp_logger_protocol_strategy.cpp + osc/oscpack/OscOutboundPacketStream.cpp osc/oscpack/OscPrintReceivedElements.cpp osc/oscpack/OscReceivedElements.cpp @@ -43,6 +45,8 @@ set(HEADERS clk/clk_commands.h clk/clk_command_processor.h + log/tcp_logger_protocol_strategy.h + osc/oscpack/MessageMappingOscPacketListener.h osc/oscpack/OscException.h osc/oscpack/OscHostEndianness.h @@ -75,6 +79,7 @@ include_directories(${TBB_INCLUDE_PATH}) source_group(sources\\amcp amcp/*) source_group(sources\\cii cii/*) source_group(sources\\clk clk/*) +source_group(sources\\log log/*) source_group(sources\\osc\\oscpack osc/oscpack/*) source_group(sources\\osc osc/*) source_group(sources\\util util/*) diff --git a/protocol/log/tcp_logger_protocol_strategy.cpp b/protocol/log/tcp_logger_protocol_strategy.cpp new file mode 100644 index 0000000000..15390624b5 --- /dev/null +++ b/protocol/log/tcp_logger_protocol_strategy.cpp @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2011 Sveriges Television AB +* +* 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 . +* +* Author: Helge Norberg, helge.norberg@svt.se +*/ + +#include "../StdAfx.h" + +#include "tcp_logger_protocol_strategy.h" + +#include + +namespace caspar { namespace protocol { namespace log { + +class tcp_logger_protocol_strategy : public IO::protocol_strategy +{ + spl::shared_ptr> client_connection_; + std::shared_ptr log_sink_ = caspar::log::add_preformatted_line_sink([=](std::string line) + { + handle_log_line(std::move(line)); + }); +public: + tcp_logger_protocol_strategy(spl::shared_ptr> client_connection) + : client_connection_(std::move(client_connection)) + { + } + + void handle_log_line(std::string line) + { + line += "\r\n"; + client_connection_->send(std::move(line)); + } + + void parse(const std::string& data) override + { + } +}; + +spl::shared_ptr> tcp_logger_protocol_strategy_factory::create( + const spl::shared_ptr>& client_connection) +{ + return spl::make_shared(client_connection); +} + +}}} diff --git a/protocol/log/tcp_logger_protocol_strategy.h b/protocol/log/tcp_logger_protocol_strategy.h new file mode 100644 index 0000000000..d6e2fc3654 --- /dev/null +++ b/protocol/log/tcp_logger_protocol_strategy.h @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2011 Sveriges Television AB +* +* 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 . +* +* Author: Helge Norberg, helge.norberg@svt.se +*/ + +#pragma once + +#include "../util/protocol_strategy.h" + +namespace caspar { namespace protocol { namespace log { + +struct tcp_logger_protocol_strategy_factory : public IO::protocol_strategy_factory +{ + spl::shared_ptr> create( + const spl::shared_ptr>& client_connection) override; +}; + +}}} diff --git a/shell/casparcg.config b/shell/casparcg.config index c69af225a5..3691c67cfe 100644 --- a/shell/casparcg.config +++ b/shell/casparcg.config @@ -27,6 +27,10 @@ 5250 AMCP + + 3250 + LOG + diff --git a/shell/server.cpp b/shell/server.cpp index 53814d1428..1645d2d2c6 100644 --- a/shell/server.cpp +++ b/shell/server.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include #include @@ -411,7 +412,9 @@ struct server::impl : boost::noncopyable return spl::make_shared( "ISO-8859-1", spl::make_shared(channels_, cg_registry_, producer_registry_)); - + else if (boost::iequals(name, L"LOG")) + return spl::make_shared(); + CASPAR_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol")); }