Upgrade the swapping logic of the file sink to use the new srslog functionality.

This commit is contained in:
faluco 2020-12-23 16:09:01 +01:00 committed by faluco
parent 54a864e021
commit 63bd43fa52
5 changed files with 68 additions and 63 deletions

View File

@ -1,37 +0,0 @@
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2020 Software Radio Systems Limited
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the distribution.
*
*/
#ifndef SRSUE_TTCN3_SWAPPABLE_LOG_H
#define SRSUE_TTCN3_SWAPPABLE_LOG_H
#include "srslte/common/logger_srslog_wrapper.h"
/// This is a log wrapper that allows hot swapping the underlying log instance.
class swappable_log : public srslte::logger
{
public:
explicit swappable_log(std::unique_ptr<srslte::srslog_wrapper> log) : l(std::move(log)) {}
void log(unique_log_str_t msg) override
{
assert(l && "Missing log instance");
l->log(std::move(msg));
}
/// Swaps the underlying log wrapper.
void swap_log(std::unique_ptr<srslte::srslog_wrapper> new_log) { l = std::move(new_log); }
private:
std::unique_ptr<srslte::srslog_wrapper> l;
};
#endif // SRSUE_TTCN3_SWAPPABLE_LOG_H

View File

@ -0,0 +1,45 @@
/**
*
* \section COPYRIGHT
*
* Copyright 2013-2020 Software Radio Systems Limited
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the distribution.
*
*/
#ifndef SRSUE_TTCN3_SWAPPABLE_SINK_H
#define SRSUE_TTCN3_SWAPPABLE_SINK_H
#include "srslte/srslog/sink.h"
/// A custom sink implementation that allows hot swapping file sinks so that loggers can write to different files
/// dynamically.
class swappable_sink : public srslog::sink
{
public:
swappable_sink(const std::string& filename, std::unique_ptr<srslog::log_formatter> f) :
srslog::sink(std::move(f)), s(&srslog::fetch_file_sink(filename))
{}
/// Identifier of this custom sink.
static const char* name() { return "swappable_sink"; }
srslog::detail::error_string write(srslog::detail::memory_buffer buffer) override { return s->write(buffer); }
srslog::detail::error_string flush() override { return s->flush(); }
/// Swaps the current file sink with a new sink that will write to the specified file name.
void swap_sink(const std::string& filename)
{
srslog::flush();
s = &srslog::fetch_file_sink(filename);
}
private:
srslog::sink* s;
};
#endif // SRSUE_TTCN3_SWAPPABLE_SINK_H

View File

@ -17,7 +17,6 @@
#include "srslte/test/ue_test_interfaces.h"
#include "srslte/upper/pdcp.h"
#include "srslte/upper/rlc.h"
#include "swappable_log.h"
#include "ttcn3_common.h"
#include "ttcn3_drb_interface.h"
#include "ttcn3_ip_ctrl_interface.h"
@ -39,7 +38,7 @@ class ttcn3_syssim : public syssim_interface_phy,
public srslte::pdu_queue::process_callback
{
public:
ttcn3_syssim(swappable_log& logger_file_, srslte::logger& logger_stdout_, ttcn3_ue* ue_);
ttcn3_syssim(srslte::logger& logger_file_, srslte::logger& logger_stdout_, ttcn3_ue* ue_);
~ttcn3_syssim();
@ -211,7 +210,7 @@ private:
// Logging stuff
srslte::logger& logger_stdout;
swappable_log& logger_file;
srslte::logger& logger_file;
srslte::logger* logger = nullptr;
srslte::log_ref log;
srslte::log_filter ut_log;

View File

@ -14,7 +14,7 @@
#include "srslte/common/logmap.h"
#include "srslte/srslog/srslog.h"
#include "srsue/hdr/ue.h"
#include "swappable_log.h"
#include "swappable_sink.h"
#include "ttcn3_syssim.h"
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
@ -114,26 +114,20 @@ int main(int argc, char** argv)
ttcn3_dut_args_t dut_args = {};
all_args_t ue_args = parse_args(&dut_args, argc, argv);
// Setup logging.
srslog::sink* log_file_sink = srslog::create_file_sink(dut_args.log_filename);
if (!log_file_sink) {
// Create a swappable sink, install it and use it as the default one.
if (!srslog::install_custom_sink(swappable_sink::name(),
std::unique_ptr<swappable_sink>(new swappable_sink(
dut_args.log_filename, srslog::get_default_log_formatter())))) {
return SRSLTE_ERROR;
}
srslog::log_channel* file_chan = srslog::create_log_channel("file_channel", *log_file_sink);
if (!file_chan) {
return SRSLTE_ERROR;
}
srslog::sink* stdout_sink = srslog::create_stdout_sink();
if (!stdout_sink) {
return SRSLTE_ERROR;
}
srslog::log_channel* stdout_chan = srslog::create_log_channel("stdout_channel", *stdout_sink);
if (!stdout_chan) {
auto* default_sink = srslog::find_sink(swappable_sink::name());
if (!default_sink) {
return SRSLTE_ERROR;
}
srslog::set_default_sink(*default_sink);
swappable_log file_wrapper(std::unique_ptr<srslte::srslog_wrapper>(new srslte::srslog_wrapper(*file_chan)));
srslte::srslog_wrapper stdout_wrapper(*stdout_chan);
srslte::srslog_wrapper file_wrapper(srslog::fetch_log_channel("file_channel"));
srslte::srslog_wrapper stdout_wrapper(srslog::fetch_log_channel("stdout_channel", srslog::fetch_stdout_sink(), {}));
// Start the log backend.
srslog::init();

View File

@ -18,6 +18,7 @@
#include "srslte/test/ue_test_interfaces.h"
#include "srslte/upper/pdcp.h"
#include "srslte/upper/rlc.h"
#include "swappable_sink.h"
#include "ttcn3_common.h"
#include "ttcn3_drb_interface.h"
#include "ttcn3_ip_ctrl_interface.h"
@ -28,7 +29,7 @@
#include "ttcn3_ut_interface.h"
#include <functional>
ttcn3_syssim::ttcn3_syssim(swappable_log& logger_file_, srslte::logger& logger_stdout_, ttcn3_ue* ue_) :
ttcn3_syssim::ttcn3_syssim(srslte::logger& logger_file_, srslte::logger& logger_stdout_, ttcn3_ue* ue_) :
log{"SS "},
mac_msg_ul(20, ss_mac_log),
mac_msg_dl(20, ss_mac_log),
@ -395,11 +396,14 @@ void ttcn3_syssim::tc_start(const char* name)
if (args.log.filename == "stdout") {
logger = &logger_stdout;
} else {
// Create a new log wrapper that writes to the new test case file and swap it with the old one.
const std::string& file_tc_name = get_filename_with_tc_name(local_args.log.filename, run_id, tc_name);
srslog::sink* s = srslog::create_file_sink(file_tc_name);
srslog::log_channel* c = srslog::create_log_channel(file_tc_name, *s);
logger_file.swap_log(std::unique_ptr<srslte::srslog_wrapper>(new srslte::srslog_wrapper(*c)));
auto* swp_sink = srslog::find_sink(swappable_sink::name());
if (!swp_sink) {
log->error("Unable to find the swappable sink\n");
srslte::console("Unable to find the swappable sink\n");
return;
}
static_cast<swappable_sink*>(swp_sink)->swap_sink(file_tc_name);
logger = &logger_file;
}