From 3dcf2f59fe1a2859518faf947a345010556f20ae Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Wed, 5 May 2021 19:23:30 -0700 Subject: [PATCH] fix logger_level_set() --- src/log.cpp | 29 ++++++++++------- test/Makefile | 4 +++ test/testlog.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 test/testlog.cpp diff --git a/src/log.cpp b/src/log.cpp index 9c02e44..b43e8bd 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -189,24 +189,31 @@ struct logger_gbl_t { if(lvl<=Level(0)) lvl = Level(1); + decltype (config)::value_type* conf = nullptr; + for(auto& tup : config) { if(tup.first==exp) { // update of existing config - if(tup.second!=lvl) { - tup.second = lvl; - - for(auto& pair : loggers) { - if(epicsStrGlobMatch(pair.first.c_str(), tup.first.c_str())) { - pair.second->lvl.store(lvl, std::memory_order_relaxed); - } - } - } - return; + conf = &tup; + break; } } // new config - config.emplace_back(exp, lvl); + if(!conf) { + config.emplace_back(exp, Level(-1)); + conf = &config.back(); + } + + if(conf->second!=lvl) { + conf->second = lvl; + + for(auto& pair : loggers) { + if(epicsStrGlobMatch(pair.first.c_str(), conf->first.c_str())) { + pair.second->lvl.store(lvl, std::memory_order_relaxed); + } + } + } } } *logger_gbl; diff --git a/test/Makefile b/test/Makefile index 4c4622d..62c32a5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -22,6 +22,10 @@ TESTPROD_HOST += testev testev_SRCS += testev.cpp TESTS += testev +TESTPROD_HOST += testlog +testlog_SRCS += testlog.cpp +TESTS += testlog + TESTPROD_HOST += testudp testudp_SRCS += testudp.cpp TESTS += testudp diff --git a/test/testlog.cpp b/test/testlog.cpp new file mode 100644 index 0000000..eaaecfa --- /dev/null +++ b/test/testlog.cpp @@ -0,0 +1,85 @@ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * pvxs is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ + +#include + +#include + +#include + +#include +#include + +namespace pvxs { + +std::ostream& operator<<(std::ostream& strm, Level lvl) +{ + switch (lvl) { +#define CASE(NAME) case Level::NAME: strm<<#NAME; break + CASE(Crit); + CASE(Err); + CASE(Warn); + CASE(Info); + CASE(Debug); +#undef CASE + } + return strm; +} + +} // namespace pvxs + +using namespace pvxs; + +namespace { + +DEFINE_LOGGER(loggera, "test.a"); +DEFINE_LOGGER(loggerb, "test.b"); + +void testLog() +{ + testDiag("%s", __func__); + + testTrue(loggera.test(Level::Warn)); + testEq(loggera.lvl.load(), Level::Warn); + + logger_level_set("test.*", Level::Err); + + testEq(loggera.lvl.load(), Level::Err); + + testTrue(loggerb.test(Level::Err)); + testEq(loggerb.lvl.load(), Level::Err); + + logger_level_set("test.*", Level::Info); + + testEq(loggera.lvl.load(), Level::Info); + testEq(loggerb.lvl.load(), Level::Info); + + logger_level_set("test.a", Level::Err); + + testEq(loggera.lvl.load(), Level::Err); + testEq(loggerb.lvl.load(), Level::Info); + + logger_level_set("test.*", Level::Warn); + + testEq(loggera.lvl.load(), Level::Warn); + testEq(loggerb.lvl.load(), Level::Warn); + + logger_level_clear(); + logger_level_set("test.*", Level::Err); + + testEq(loggera.lvl.load(), Level::Err); + testEq(loggerb.lvl.load(), Level::Err); +} + +} // namespace + +MAIN(testlog) +{ + testPlan(13); + testSetup(); + testLog(); + return testDone(); +}