From 11674a7cb01b47669bce8efb4fc397f8d3685fae Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Tue, 7 May 2024 00:14:07 -0400 Subject: [PATCH] Inject logger into JFactory --- src/libraries/JANA/JFactoryGenerator.h | 1 + .../unit_tests/Components/JFactoryTests.cc | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/libraries/JANA/JFactoryGenerator.h b/src/libraries/JANA/JFactoryGenerator.h index e0de26916..b15c859eb 100644 --- a/src/libraries/JANA/JFactoryGenerator.h +++ b/src/libraries/JANA/JFactoryGenerator.h @@ -62,6 +62,7 @@ class JFactoryGeneratorT : public JFactoryGenerator { factory->SetFactoryName(JTypeInfo::demangle()); factory->SetPluginName(GetPluginName()); factory->SetApplication(GetApplication()); + factory->SetLogger(GetApplication()->template GetService()->get_logger(factory->GetPrefix())); factory_set->Add(factory); } }; diff --git a/src/programs/unit_tests/Components/JFactoryTests.cc b/src/programs/unit_tests/Components/JFactoryTests.cc index 2c7501e72..436edbc79 100644 --- a/src/programs/unit_tests/Components/JFactoryTests.cc +++ b/src/programs/unit_tests/Components/JFactoryTests.cc @@ -249,3 +249,22 @@ TEST_CASE("JFactory_Exception") { REQUIRE(found_throw == true); } +struct MyLoggedFactory : public JFactoryT { + MyLoggedFactory() { + SetPrefix("myfac"); + } + void Process(const std::shared_ptr&) override { + LOG_INFO(GetLogger()) << "Process ran!" << LOG_END; + REQUIRE(GetLogger().level == JLogger::Level::DEBUG); + } +}; +TEST_CASE("JFactory_Logger") { + JApplication app; + app.Add(new JEventSource); + app.Add(new JFactoryGeneratorT()); + app.SetParameterValue("log:debug", "myfac"); + app.SetParameterValue("jana:nevents", "1"); + app.SetParameterValue("autoactivate", "JFactoryTestDummyObject"); + app.Run(); +} +