From 07a9e889a47ef1903a666859e7d3a03863fe3aa0 Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Wed, 11 Dec 2019 11:52:08 +0000 Subject: [PATCH] added a singleton map to facilitate access to each layer logger --- lib/include/srslte/common/log.h | 65 ++++++++++++++++++++++++-- lib/include/srslte/common/log_filter.h | 1 - lib/src/common/log_filter.cc | 8 ++-- lib/test/common/log_filter_test.cc | 18 ++++++- 4 files changed, 82 insertions(+), 10 deletions(-) diff --git a/lib/include/srslte/common/log.h b/lib/include/srslte/common/log.h index 9cdb4e130..c01c4aee2 100644 --- a/lib/include/srslte/common/log.h +++ b/lib/include/srslte/common/log.h @@ -31,6 +31,7 @@ #define SRSLTE_LOG_H #include +#include #include #include @@ -67,9 +68,27 @@ public: level = LOG_LEVEL_NONE; hex_limit = 0; add_string_en = false; + log::register_log(this); } - virtual ~log() = default; + log(const log&) = delete; + log& operator=(const log&) = delete; + + virtual ~log() + { + if (not service_name.empty()) { + log::deregister_log(service_name); + } + } + + void set_service_name(std::string service_name_) + { + if (not service_name.empty()) { + log::deregister_log(service_name); + } + service_name = std::move(service_name_); + log::register_log(this); + } // This function shall be called at the start of every tti for printing tti void step(uint32_t tti_) @@ -109,6 +128,7 @@ public: } LOG_LEVEL_ENUM get_level() { return level; } + std::string get_service_name() { return service_name; } void set_hex_limit(int limit) { hex_limit = limit; } int get_hex_limit() { return hex_limit; } @@ -140,15 +160,54 @@ public: error("debug_hex not implemented.\n"); } + // Access to log pool + static log* get(const std::string& servicename) + { + auto* pool = log::get_pool_instance(); + auto it = pool->find(servicename); + if (it == pool->end()) { + return nullptr; + } + return it->second; + } + protected: - std::string get_service_name() { return service_name; } uint32_t tti; LOG_LEVEL_ENUM level; int hex_limit; - std::string service_name; bool add_string_en; std::string add_string_val; + + static bool register_log(log* log_ptr) + { + auto* pool = log::get_pool_instance(); + auto it = pool->find(log_ptr->get_service_name()); + if (it == pool->end()) { + pool->insert(std::make_pair(log_ptr->get_service_name(), log_ptr)); + return true; + } + it->second = log_ptr; + return false; + } + + static bool deregister_log(const std::string& servicename) + { + auto* pool = log::get_pool_instance(); + return pool->erase(servicename) > 0; + } + +private: + static std::map* get_pool_instance() + { + static std::map* log_pool = nullptr; + if (log_pool == nullptr) { + log_pool = new std::map; + } + return log_pool; + } + + std::string service_name; }; } // namespace srslte diff --git a/lib/include/srslte/common/log_filter.h b/lib/include/srslte/common/log_filter.h index 50a59d35e..6239bc23d 100644 --- a/lib/include/srslte/common/log_filter.h +++ b/lib/include/srslte/common/log_filter.h @@ -48,7 +48,6 @@ public: log_filter(); log_filter(std::string layer); log_filter(std::string layer, logger* logger_, bool tti = false); - ~log_filter(); void init(std::string layer, logger* logger_, bool tti = false); diff --git a/lib/src/common/log_filter.cc b/lib/src/common/log_filter.cc index 4862f3ab4..d783d13e5 100644 --- a/lib/src/common/log_filter.cc +++ b/lib/src/common/log_filter.cc @@ -50,19 +50,17 @@ log_filter::log_filter(std::string layer) : log() init(layer, &def_logger_stdout, do_tti); } -log_filter::log_filter(std::string layer, logger* logger_, bool tti) +log_filter::log_filter(std::string layer, logger* logger_, bool tti) : log() { do_tti = false; time_src = NULL; time_format = TIME; - init(layer, logger_, tti); + init(std::move(layer), logger_, tti); } -log_filter::~log_filter() {} - void log_filter::init(std::string layer, logger* logger_, bool tti) { - service_name = layer; + set_service_name(layer); logger_h = logger_; do_tti = tti; } diff --git a/lib/test/common/log_filter_test.cc b/lib/test/common/log_filter_test.cc index 55b61d6b4..0e59ea541 100644 --- a/lib/test/common/log_filter_test.cc +++ b/lib/test/common/log_filter_test.cc @@ -139,6 +139,22 @@ int basic_hex_test() return SRSLTE_SUCCESS; } +int test_log_singleton() +{ + { + log_filter test_log("LAYER1"); + srslte::log* log_ptr = srslte::log::get("LAYER1"); + TESTASSERT(log_ptr == &test_log); + TESTASSERT(log_ptr->get_service_name() == "LAYER1"); + test_log.set_service_name("LAYER2"); + // log_ptr should now point to LAYER2 + TESTASSERT(srslte::log::get("LAYER1") == nullptr); + TESTASSERT(srslte::log::get("LAYER2") == &test_log); + } + TESTASSERT(srslte::log::get("LAYER2") == nullptr); + return SRSLTE_SUCCESS; +} + int full_test() { bool result; @@ -158,11 +174,11 @@ int full_test() return SRSLTE_SUCCESS; } - int main(int argc, char** argv) { TESTASSERT(basic_hex_test() == SRSLTE_SUCCESS); TESTASSERT(full_test() == SRSLTE_SUCCESS); + TESTASSERT(test_log_singleton() == SRSLTE_SUCCESS); return SRSLTE_SUCCESS; }