The Gaudi Framework  v36r9p1 (5c15b2bb)
JSONSink.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2022 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
12 #include "GaudiKernel/Service.h"
13 
14 #include <Gaudi/MonitoringHub.h>
15 
16 #include <fstream>
17 
18 #include <deque>
19 #include <string>
20 
21 namespace Gaudi::Monitoring {
22 
23  class JSONSink : public Service, public Hub::Sink {
24 
25  public:
26  using Service::Service;
27 
28  StatusCode initialize() override {
29  return Service::initialize().andThen( [&] { serviceLocator()->monitoringHub().addSink( this ); } );
30  }
31 
32  StatusCode stop() override {
33  auto ok = Service::stop();
34  if ( !ok ) return ok;
35  if ( m_fileName.empty() ) { return ok; }
36 
39  output.emplace_back( nlohmann::json{
40  { "name", ent.name },
41  { "component", ent.component },
42  { "entity", ent.toJSON() },
43  } );
44  } );
45  info() << "Writing JSON file " << m_fileName.value() << endmsg;
47  os << output.dump( 4 );
48  os.close();
49  return ok;
50  }
51 
52  void registerEntity( Hub::Entity ent ) override {
53  if ( wanted( ent.type, m_typesToSave ) && wanted( ent.name, m_namesToSave ) &&
56  }
57  }
58 
59  void removeEntity( Hub::Entity const& ent ) override {
61  if ( it != m_monitoringEntities.end() ) { m_monitoringEntities.erase( it ); }
62  }
63 
64  private:
66  if ( searchNames.empty() ) { return true; }
67  for ( const auto& searchName : searchNames ) {
68  const std::regex regex( searchName );
69  if ( std::regex_match( name, regex ) ) { return true; }
70  }
71  return false;
72  }
73 
75  Gaudi::Property<std::string> m_fileName{ this, "FileName", "json_output.json", "Name of output json file" };
76  Gaudi::Property<std::vector<std::string>> m_namesToSave{ this, "NamesToSave", {}, "Names of entities to save" };
78  this, "ComponentsToSave", {}, "Component names of entities to save" };
80  this, "TypesToSave", {}, "Type names of entities to save" };
81  };
82 
83  DECLARE_COMPONENT( JSONSink )
84 
85 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:156
std::for_each
T for_each(T... args)
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:107
Gaudi::Monitoring::JSONSink::removeEntity
void removeEntity(Hub::Entity const &ent) override
Definition: JSONSink.cpp:59
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
Gaudi::Monitoring::JSONSink
Definition: JSONSink.cpp:23
std::string
STL class.
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
std::move
T move(T... args)
MonitoringHub.h
std::vector< std::string >
std::find
T find(T... args)
std::regex_match
T regex_match(T... args)
Gaudi::Monitoring
Definition: JSONSink.cpp:21
gaudirun.output
output
Definition: gaudirun.py:521
jsonFromLHCbLog.json
dictionary json
Definition: jsonFromLHCbLog.py:87
Gaudi::Monitoring::JSONSink::registerEntity
void registerEntity(Hub::Entity ent) override
Definition: JSONSink.cpp:52
Service
Definition: Service.h:46
Gaudi::Monitoring::JSONSink::initialize
StatusCode initialize() override
Definition: JSONSink.cpp:28
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
std::ofstream
STL class.
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Monitoring::JSONSink::m_typesToSave
Gaudi::Property< std::vector< std::string > > m_typesToSave
Definition: JSONSink.cpp:79
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:240
std::ofstream::close
T close(T... args)
Gaudi::Monitoring::JSONSink::m_namesToSave
Gaudi::Property< std::vector< std::string > > m_namesToSave
Definition: JSONSink.cpp:76
std::deque::erase
T erase(T... args)
std::deque< Gaudi::Monitoring::Hub::Entity >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Gaudi::Monitoring::JSONSink::stop
StatusCode stop() override
Definition: JSONSink.cpp:32
std::regex
IOTest.end
def end
Definition: IOTest.py:128
Service.h
std::deque::emplace_back
T emplace_back(T... args)
Gaudi::Monitoring::JSONSink::m_monitoringEntities
std::deque< Gaudi::Monitoring::Hub::Entity > m_monitoringEntities
Definition: JSONSink.cpp:74
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor
Definition: Service.cpp:339
std::vector::empty
T empty(T... args)
std::deque::end
T end(T... args)
Gaudi::Monitoring::JSONSink::m_fileName
Gaudi::Property< std::string > m_fileName
Definition: JSONSink.cpp:75
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:89
Gaudi::Monitoring::JSONSink::wanted
bool wanted(std::string name, std::vector< std::string > searchNames)
Definition: JSONSink.cpp:65
Gaudi::Monitoring::JSONSink::m_componentsToSave
Gaudi::Property< std::vector< std::string > > m_componentsToSave
Definition: JSONSink.cpp:77
Gaudi::Property< std::string >
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:109
PrepareBase.out
out
Definition: PrepareBase.py:20
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:105