added a singleton map to facilitate access to each layer logger

master
Francisco Paisana 5 years ago
parent c080cb3395
commit 07a9e889a4

@ -31,6 +31,7 @@
#define SRSLTE_LOG_H
#include <algorithm>
#include <map>
#include <stdint.h>
#include <string>
@ -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<std::string, log*>* get_pool_instance()
{
static std::map<std::string, log*>* log_pool = nullptr;
if (log_pool == nullptr) {
log_pool = new std::map<std::string, log*>;
}
return log_pool;
}
std::string service_name;
};
} // namespace srslte

@ -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);

@ -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;
}

@ -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;
}

Loading…
Cancel
Save