From 63bd43fa5290f5dab386a11d449cd7ac91468894 Mon Sep 17 00:00:00 2001 From: faluco Date: Wed, 23 Dec 2020 16:09:01 +0100 Subject: [PATCH] Upgrade the swapping logic of the file sink to use the new srslog functionality. --- srsue/test/ttcn3/hdr/swappable_log.h | 37 ---------------------- srsue/test/ttcn3/hdr/swappable_sink.h | 45 +++++++++++++++++++++++++++ srsue/test/ttcn3/hdr/ttcn3_syssim.h | 7 ++--- srsue/test/ttcn3/src/ttcn3_dut.cc | 26 ++++++---------- srsue/test/ttcn3/src/ttcn3_syssim.cc | 16 ++++++---- 5 files changed, 68 insertions(+), 63 deletions(-) delete mode 100644 srsue/test/ttcn3/hdr/swappable_log.h create mode 100644 srsue/test/ttcn3/hdr/swappable_sink.h diff --git a/srsue/test/ttcn3/hdr/swappable_log.h b/srsue/test/ttcn3/hdr/swappable_log.h deleted file mode 100644 index 0ad1e76f1..000000000 --- a/srsue/test/ttcn3/hdr/swappable_log.h +++ /dev/null @@ -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 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 new_log) { l = std::move(new_log); } - -private: - std::unique_ptr l; -}; - -#endif // SRSUE_TTCN3_SWAPPABLE_LOG_H diff --git a/srsue/test/ttcn3/hdr/swappable_sink.h b/srsue/test/ttcn3/hdr/swappable_sink.h new file mode 100644 index 000000000..5e1f0433a --- /dev/null +++ b/srsue/test/ttcn3/hdr/swappable_sink.h @@ -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 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 diff --git a/srsue/test/ttcn3/hdr/ttcn3_syssim.h b/srsue/test/ttcn3/hdr/ttcn3_syssim.h index d9f4431b2..bfbfb70dc 100644 --- a/srsue/test/ttcn3/hdr/ttcn3_syssim.h +++ b/srsue/test/ttcn3/hdr/ttcn3_syssim.h @@ -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; @@ -252,7 +251,7 @@ private: // For events/actions that need to be carried out in a specific TTI typedef std::queue task_queue_t; typedef std::map tti_action_map_t; - tti_action_map_t tti_actions; + tti_action_map_t tti_actions; // Map between the cellId (name) used by 3GPP test suite and srsLTE cell struct class syssim_cell_t diff --git a/srsue/test/ttcn3/src/ttcn3_dut.cc b/srsue/test/ttcn3/src/ttcn3_dut.cc index 93df4e4c5..c7fd95e72 100644 --- a/srsue/test/ttcn3/src/ttcn3_dut.cc +++ b/srsue/test/ttcn3/src/ttcn3_dut.cc @@ -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 #include @@ -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(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(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(); diff --git a/srsue/test/ttcn3/src/ttcn3_syssim.cc b/srsue/test/ttcn3/src/ttcn3_syssim.cc index ed77c0e64..cc2fc890b 100644 --- a/srsue/test/ttcn3/src/ttcn3_syssim.cc +++ b/srsue/test/ttcn3/src/ttcn3_syssim.cc @@ -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 -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(new srslte::srslog_wrapper(*c))); + const std::string& file_tc_name = get_filename_with_tc_name(local_args.log.filename, run_id, tc_name); + 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(swp_sink)->swap_sink(file_tc_name); logger = &logger_file; }