Skip to content

Commit

Permalink
Implemented a TCP server, simply sending every log line to the remote…
Browse files Browse the repository at this point in the history
… client. If using the default casparcg.config it will listen on 3250. Any data sent by the client is ignored.
  • Loading branch information
Helge Norberg committed Nov 13, 2015
1 parent 98153d4 commit 5fb7f47
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 1 deletion.
39 changes: 39 additions & 0 deletions common/log.cpp
Expand Up @@ -181,6 +181,45 @@ void add_file_sink(const std::wstring& folder)
}
}

std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string line)> formatted_line_sink)
{
class sink_backend : public boost::log::sinks::basic_formatted_sink_backend<char>
{
std::function<void(std::string line)> formatted_line_sink_;
public:
sink_backend(std::function<void(std::string line)> 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_backend> sink_type;

auto sink = boost::make_shared<sink_type>(std::move(formatted_line_sink));
bool print_all_characters = true;

sink->set_formatter(boost::bind(&my_formatter<boost::log::formatting_ostream>, print_all_characters, _1, _2));

boost::log::core::get()->add_sink(sink);

return std::shared_ptr<void>(nullptr, [=](void*)
{
boost::log::core::get()->remove_sink(sink);
});
}

void set_log_level(const std::wstring& lvl)
{
if (boost::iequals(lvl, L"trace"))
Expand Down
3 changes: 3 additions & 0 deletions common/log.h
Expand Up @@ -32,6 +32,8 @@

#include <string>
#include <locale>
#include <functional>
#include <memory>

namespace caspar { namespace log {

Expand Down Expand Up @@ -61,6 +63,7 @@ inline std::basic_string<T> replace_nonprintable_copy(std::basic_string<T, std::
}

void add_file_sink(const std::wstring& folder);
std::shared_ptr<void> add_preformatted_line_sink(std::function<void(std::string line)> formatted_line_sink);

typedef boost::log::sources::wseverity_logger_mt<boost::log::trivial::severity_level> caspar_logger;

Expand Down
5 changes: 5 additions & 0 deletions protocol/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/*)
Expand Down
60 changes: 60 additions & 0 deletions protocol/log/tcp_logger_protocol_strategy.cpp
@@ -0,0 +1,60 @@
/*
* 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: Helge Norberg, helge.norberg@svt.se
*/

#include "../StdAfx.h"

#include "tcp_logger_protocol_strategy.h"

#include <common/log.h>

namespace caspar { namespace protocol { namespace log {

class tcp_logger_protocol_strategy : public IO::protocol_strategy<char>
{
spl::shared_ptr<IO::client_connection<char>> client_connection_;
std::shared_ptr<void> 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<IO::client_connection<char>> 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<IO::protocol_strategy<char>> tcp_logger_protocol_strategy_factory::create(
const spl::shared_ptr<IO::client_connection<char>>& client_connection)
{
return spl::make_shared<tcp_logger_protocol_strategy>(client_connection);
}

}}}
34 changes: 34 additions & 0 deletions protocol/log/tcp_logger_protocol_strategy.h
@@ -0,0 +1,34 @@
/*
* 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: 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<char>
{
spl::shared_ptr<IO::protocol_strategy<char>> create(
const spl::shared_ptr<IO::client_connection<char>>& client_connection) override;
};

}}}
4 changes: 4 additions & 0 deletions shell/casparcg.config
Expand Up @@ -27,6 +27,10 @@
<port>5250</port>
<protocol>AMCP</protocol>
</tcp>
<tcp>
<port>3250</port>
<protocol>LOG</protocol>
</tcp>
</controllers>
</configuration>

Expand Down
5 changes: 4 additions & 1 deletion shell/server.cpp
Expand Up @@ -65,6 +65,7 @@
#include <protocol/util/AsyncEventServer.h>
#include <protocol/util/strategy_adapters.h>
#include <protocol/osc/client.h>
#include <protocol/log/tcp_logger_protocol_strategy.h>

#include <boost/algorithm/string.hpp>
#include <boost/thread.hpp>
Expand Down Expand Up @@ -411,7 +412,9 @@ struct server::impl : boost::noncopyable
return spl::make_shared<to_unicode_adapter_factory>(
"ISO-8859-1",
spl::make_shared<CLK::clk_protocol_strategy_factory>(channels_, cg_registry_, producer_registry_));

else if (boost::iequals(name, L"LOG"))
return spl::make_shared<protocol::log::tcp_logger_protocol_strategy_factory>();

CASPAR_THROW_EXCEPTION(caspar_exception() << arg_name_info(L"name") << arg_value_info(name) << msg_info(L"Invalid protocol"));
}

Expand Down

0 comments on commit 5fb7f47

Please sign in to comment.