From 3e2de6dd3ebb6e6ee0adffdc9609732bf83cecca Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Mon, 27 Jan 2020 17:20:12 +0000 Subject: [PATCH] new logmap class to store globally accessible logs --- lib/include/srslte/common/log.h | 57 +--------------- lib/include/srslte/common/logmap.h | 105 +++++++++++++++++++++++++++++ lib/src/common/log_filter.cc | 2 +- lib/test/common/log_filter_test.cc | 23 ++++--- 4 files changed, 119 insertions(+), 68 deletions(-) create mode 100644 lib/include/srslte/common/logmap.h diff --git a/lib/include/srslte/common/log.h b/lib/include/srslte/common/log.h index c01c4aee2..a28a5aff5 100644 --- a/lib/include/srslte/common/log.h +++ b/lib/include/srslte/common/log.h @@ -68,27 +68,12 @@ public: level = LOG_LEVEL_NONE; hex_limit = 0; add_string_en = false; - log::register_log(this); } 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); - } + virtual ~log() = default; // This function shall be called at the start of every tti for printing tti void step(uint32_t tti_) @@ -160,17 +145,6 @@ 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: uint32_t tti; LOG_LEVEL_ENUM level; @@ -178,35 +152,6 @@ protected: 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; }; diff --git a/lib/include/srslte/common/logmap.h b/lib/include/srslte/common/logmap.h new file mode 100644 index 000000000..cccee84aa --- /dev/null +++ b/lib/include/srslte/common/logmap.h @@ -0,0 +1,105 @@ +/* + * Copyright 2013-2019 Software Radio Systems Limited + * + * This file is part of srsLTE. + * + * srsLTE is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * srsLTE 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 Affero General Public License for more details. + * + * A copy of the GNU Affero General Public License can be found in + * the LICENSE file in the top-level directory of this distribution + * and at http://www.gnu.org/licenses/. + * + */ + +#include "logger.h" +#include +#include + +#ifndef SRSLTE_LOGMAP_H +#define SRSLTE_LOGMAP_H + +namespace srslte { + +class log_filter; + +class logmap +{ +public: + // Access to log map by servicename. If servicename does not exist, create a new log_filter with default cfg + static log* get(const std::string& servicename) + { + logmap* pool = get_instance(); + std::lock_guard lock(pool->mutex); + auto it = pool->log_map.find(servicename); + if (it == pool->log_map.end()) { + // create a new logger with default cfg + std::unique_ptr filter(new log_filter{servicename, pool->default_logger}); + filter->set_level(pool->default_log_level); + filter->set_hex_limit(pool->default_hex_limit); + auto ret = pool->log_map.insert(std::make_pair(servicename, std::move(filter))); + std::unique_ptr& inserted = ret.first->second; + return inserted.get(); + } + return it->second.get(); + } + + static logmap* get_instance() + { + static logmap* instance = new logmap{}; + return instance; + } + + // set default logger + void set_default_logger(logger* logger_) + { + std::lock_guard lock(mutex); + default_logger = logger_; + } + + // set default log level + void set_default_log_level(LOG_LEVEL_ENUM l) + { + std::lock_guard lock(mutex); + default_log_level = l; + } + + // register manually created log + void register_log(std::unique_ptr log_ptr) + { + std::lock_guard lock(mutex); + log_map[log_ptr->get_service_name()] = std::move(log_ptr); + } + + bool deregister_log(const std::string& servicename) + { + std::lock_guard lock(mutex); + return log_map.erase(servicename) > 0; + } + +private: + logmap() : default_logger(&logger_stdout_val) {} + + // consts + logger_stdout logger_stdout_val; + + // default cfg + logger* default_logger = nullptr; + srslte::LOG_LEVEL_ENUM default_log_level = LOG_LEVEL_WARNING; + int default_hex_limit = 1024; + + // state + std::mutex mutex; + std::unordered_map > log_map; +}; + +} // namespace srslte + +#endif // SRSLTE_LOGMAP_H diff --git a/lib/src/common/log_filter.cc b/lib/src/common/log_filter.cc index d783d13e5..2c54a1b7e 100644 --- a/lib/src/common/log_filter.cc +++ b/lib/src/common/log_filter.cc @@ -60,7 +60,7 @@ log_filter::log_filter(std::string layer, logger* logger_, bool tti) : log() void log_filter::init(std::string layer, logger* logger_, bool tti) { - set_service_name(layer); + service_name = std::move(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 0e59ea541..ddeec5f15 100644 --- a/lib/test/common/log_filter_test.cc +++ b/lib/test/common/log_filter_test.cc @@ -24,6 +24,7 @@ #include "srslte/common/log_filter.h" #include "srslte/common/logger_file.h" +#include "srslte/common/logmap.h" #include "srslte/common/test_common.h" #include @@ -141,17 +142,17 @@ int basic_hex_test() 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); + srslte::logmap::get_instance()->set_default_log_level(LOG_LEVEL_DEBUG); + srslte::log* log_ptr = srslte::logmap::get("LAYER1"); + TESTASSERT(log_ptr->get_service_name() == "LAYER1"); + TESTASSERT(log_ptr->get_level() == LOG_LEVEL_DEBUG); + // register logger manually + std::unique_ptr log_ptr2(new srslte::log_filter("LAYER2")); + log_ptr2->set_level(LOG_LEVEL_WARNING); + TESTASSERT(srslte::logmap::get("LAYER2")->get_level() == LOG_LEVEL_DEBUG); + srslte::logmap::get_instance()->register_log(std::move(log_ptr2)); + TESTASSERT(srslte::logmap::get("LAYER2")->get_level() == LOG_LEVEL_WARNING); + return SRSLTE_SUCCESS; }