printf has already a way to pad spaces. If we use it, we can avoid accidentally adding extra logs because we did srslte::logmap::get("MAC") instead of srslte::logmap::get("MAC ")

master
Francisco Paisana 5 years ago committed by Francisco Paisana
parent 4baeee10c5
commit 8e837e173d

@ -60,8 +60,6 @@ private:
class logmap : public singleton_t<logmap> class logmap : public singleton_t<logmap>
{ {
static const size_t servicename_minimum_length = 4;
public: public:
// Access to log map by servicename. If servicename does not exist, create a new log_filter with default cfg // Access to log map by servicename. If servicename does not exist, create a new log_filter with default cfg
// Access to the map is protected by a mutex // Access to the map is protected by a mutex

@ -159,16 +159,16 @@ public:
{ {
std::unique_ptr<Log> l{new Log{std::forward<Args>(args)...}}; std::unique_ptr<Log> l{new Log{std::forward<Args>(args)...}};
// store previous log, and register the newly created one // store previous log, and register the newly created one
prev_log = srslte::logmap::get_instance()->deregister_log(l->get_service_name()); prev_log = srslte::logmap::deregister_log(l->get_service_name());
current_log = l.get(); current_log = l.get();
srslte::logmap::get_instance()->register_log(std::move(l)); srslte::logmap::register_log(std::move(l));
} }
scoped_log(scoped_log<Log>&&) noexcept = default; scoped_log(scoped_log<Log>&&) noexcept = default;
~scoped_log() ~scoped_log()
{ {
srslte::logmap::get_instance()->deregister_log(current_log->get_service_name()); srslte::logmap::deregister_log(current_log->get_service_name());
if (prev_log != nullptr) { if (prev_log != nullptr) {
srslte::logmap::get_instance()->register_log(std::move(prev_log)); srslte::logmap::register_log(std::move(prev_log));
} }
} }

@ -65,6 +65,11 @@ log_filter::log_filter(std::string layer, logger* logger_, bool tti) : log()
void log_filter::init(std::string layer, logger* logger_, bool tti) void log_filter::init(std::string layer, logger* logger_, bool tti)
{ {
// strip trailing white spaces
size_t last_char_pos = layer.find_last_not_of(' ');
if (last_char_pos != layer.size() - 1) {
layer.erase(last_char_pos + 1, layer.size());
}
service_name = std::move(layer); service_name = std::move(layer);
logger_h = logger_; logger_h = logger_;
do_tti = tti; do_tti = tti;
@ -99,7 +104,7 @@ void log_filter::all_log(srslte::LOG_LEVEL_ENUM level,
snprintf(log_str->str(), snprintf(log_str->str(),
log_str->get_buffer_size(), log_str->get_buffer_size(),
"%s [%s] %s %s%s%s%s%s", "%s [%-4s] %s %s%s%s%s%s",
buffer_time, buffer_time,
get_service_name().c_str(), get_service_name().c_str(),
log_level_text_short[level], log_level_text_short[level],

@ -40,9 +40,10 @@ logmap::logmap() : logger_stdout_val(new logger_stdout{})
log_ref SRSLTE_EXPORT logmap::get(std::string servicename) log_ref SRSLTE_EXPORT logmap::get(std::string servicename)
{ {
logmap* pool = get_instance(); logmap* pool = get_instance();
if (servicename.size() < servicename_minimum_length) { // strip trailing white spaces
// pad right of string with spaces size_t last_char_pos = servicename.find_last_not_of(' ');
servicename.insert(servicename.end(), servicename_minimum_length - servicename.length(), ' '); if (last_char_pos != servicename.size() - 1) {
servicename.erase(last_char_pos + 1, servicename.size());
} }
return pool->get_impl(std::move(servicename)); return pool->get_impl(std::move(servicename));
} }

@ -166,6 +166,21 @@ int test_log_singleton()
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }
int test_log_ref()
{
// Check if trailing whitespaces are correctly removed
srslte::log_ref t1_log{"T1"};
TESTASSERT(t1_log->get_service_name() == "T1");
TESTASSERT(t1_log.get() == srslte::logmap::get("T1").get());
TESTASSERT(t1_log.get() == srslte::logmap::get("T1 ").get());
{
scoped_log<srslte::nullsink_log> null_log{"T2"};
TESTASSERT(null_log->get_service_name() == "T2");
}
return SRSLTE_SUCCESS;
}
int full_test() int full_test()
{ {
bool result; bool result;
@ -191,6 +206,7 @@ int main(int argc, char** argv)
TESTASSERT(basic_hex_test() == SRSLTE_SUCCESS); TESTASSERT(basic_hex_test() == SRSLTE_SUCCESS);
TESTASSERT(full_test() == SRSLTE_SUCCESS); TESTASSERT(full_test() == SRSLTE_SUCCESS);
TESTASSERT(test_log_singleton() == SRSLTE_SUCCESS); TESTASSERT(test_log_singleton() == SRSLTE_SUCCESS);
TESTASSERT(test_log_ref() == SRSLTE_SUCCESS);
return SRSLTE_SUCCESS; return SRSLTE_SUCCESS;
} }

@ -149,14 +149,14 @@ private:
// UE stack logging // UE stack logging
srslte::logger* logger = nullptr; srslte::logger* logger = nullptr;
srslte::log_ref stack_log; ///< our own log filter srslte::log_ref stack_log{"STCK"}; ///< our own log filter
srslte::log_ref mac_log; srslte::log_ref mac_log{"MAC"};
srslte::log_ref rlc_log; srslte::log_ref rlc_log{"RLC"};
srslte::log_ref pdcp_log; srslte::log_ref pdcp_log{"PDCP"};
srslte::log_ref rrc_log; srslte::log_ref rrc_log{"RRC"};
srslte::log_ref usim_log; srslte::log_ref usim_log{"USIM"};
srslte::log_ref nas_log; srslte::log_ref nas_log{"NAS"};
srslte::log_ref pool_log; srslte::log_ref pool_log{"POOL"};
// stack components // stack components
srsue::mac mac; srsue::mac mac;

@ -35,14 +35,6 @@ ue_stack_lte::ue_stack_lte() :
running(false), running(false),
args(), args(),
logger(nullptr), logger(nullptr),
stack_log("STCK"),
pool_log("POOL"),
mac_log("MAC "),
rlc_log("RLC "),
pdcp_log("PDCP"),
rrc_log("RRC "),
usim_log("USIM"),
nas_log("NAS "),
usim(nullptr), usim(nullptr),
phy(nullptr), phy(nullptr),
rlc(rlc_log.get()), rlc(rlc_log.get()),
@ -96,7 +88,7 @@ int ue_stack_lte::init(const stack_args_t& args_, srslte::logger* logger_)
// init own log // init own log
stack_log->set_level(srslte::LOG_LEVEL_INFO); stack_log->set_level(srslte::LOG_LEVEL_INFO);
pool_log->set_level(srslte::LOG_LEVEL_ERROR); pool_log->set_level(srslte::LOG_LEVEL_WARNING);
byte_buffer_pool::get_instance()->set_log(pool_log.get()); byte_buffer_pool::get_instance()->set_log(pool_log.get());
// init layer logs // init layer logs

Loading…
Cancel
Save