Skip to content

Commit

Permalink
feat: Silence DD4HEP if requested (acts-project#3263)
Browse files Browse the repository at this point in the history
For testing purposes it is useful to disable DD4HEP logging if requested. While most of it can be disabled already some internal ROOT/DD4HEP code uses either ROOT logging or pure `std::cout`. This PR tries to silence also that.

Note that `std::cout` part is a bit hacky so I'm fine if this is not merged, but maybe somebody else would be interested in this.
  • Loading branch information
ntadej authored and Matthew Harris committed Jun 18, 2024
1 parent 7457dd7 commit b34ac69
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Examples/Detectors/DD4hepDetector/src/DD4hepGeometryService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <DD4hep/Handle.h>
#include <DD4hep/Volumes.h>
#include <Parsers/Printout.h>
#include <TError.h>

class TGeoNode;

Expand All @@ -41,6 +42,7 @@ ActsExamples::DD4hep::DD4hepGeometryService::~DD4hepGeometryService() {

ActsExamples::ProcessCode
ActsExamples::DD4hep::DD4hepGeometryService::buildDD4hepGeometry() {
const int old_gErrorIgnoreLevel = gErrorIgnoreLevel;
switch (m_cfg.dd4hepLogLevel) {
case Acts::Logging::Level::VERBOSE:
dd4hep::setPrintLevel(dd4hep::PrintLevel::VERBOSE);
Expand All @@ -53,17 +55,25 @@ ActsExamples::DD4hep::DD4hepGeometryService::buildDD4hepGeometry() {
break;
case Acts::Logging::Level::WARNING:
dd4hep::setPrintLevel(dd4hep::PrintLevel::WARNING);
gErrorIgnoreLevel = kWarning;
break;
case Acts::Logging::Level::ERROR:
dd4hep::setPrintLevel(dd4hep::PrintLevel::ERROR);
gErrorIgnoreLevel = kError;
break;
case Acts::Logging::Level::FATAL:
dd4hep::setPrintLevel(dd4hep::PrintLevel::FATAL);
gErrorIgnoreLevel = kFatal;
break;
case Acts::Logging::Level::MAX:
dd4hep::setPrintLevel(dd4hep::PrintLevel::ALWAYS);
break;
}
// completely silence std::cout as DD4HEP is using it for logging
if (m_cfg.dd4hepLogLevel >= Acts::Logging::Level::WARNING) {
std::cout.setstate(std::ios_base::failbit);
}

m_detector = &dd4hep::Detector::getInstance();
for (auto& file : m_cfg.xmlFileNames) {
m_detector->fromCompact(file.c_str());
Expand All @@ -72,6 +82,10 @@ ActsExamples::DD4hep::DD4hepGeometryService::buildDD4hepGeometry() {
m_detector->apply("DD4hepVolumeManager", 0, nullptr);
m_geometry = m_detector->world();

// restore the logging
gErrorIgnoreLevel = old_gErrorIgnoreLevel;
std::cout.clear();

return ActsExamples::ProcessCode::SUCCESS;
}

Expand Down

0 comments on commit b34ac69

Please sign in to comment.