Belle II Software  release-05-01-25
ZMQLogger.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <framework/pcore/zmq/utils/ZMQLogger.h>
11 #include <boost/property_tree/json_parser.hpp>
12 #include <ostream>
13 #include <stdexcept>
14 
15 using namespace Belle2;
16 
17 std::string ZMQLogger::getMonitoringJSON() const
18 {
19  std::stringstream buffer;
20  buffer << "{";
21  bool first = true;
22  for (const auto& keyValue : m_monitoring) {
23  if (not first) {
24  buffer << ", ";
25  }
26  first = false;
27  buffer << "\"" << keyValue.first << "\": ";
28  buffer << std::visit(toJSON{}, keyValue.second);
29  }
30  buffer << "}" << std::endl;
31  return buffer.str();
32 }
33 
34 void ZMQLogger::increment(const std::string& key)
35 {
36  std::visit(Incrementor{}, m_monitoring[key]);
37 }
38 
39 void ZMQLogger::decrement(const std::string& key)
40 {
41  std::visit(Decrementor{}, m_monitoring[key]);
42 }
43 
44 void ZMQLogger::logTime(const std::string& key)
45 {
46  auto current = std::chrono::system_clock::now();
47  auto displayTime = std::chrono::system_clock::to_time_t(current);
48  log(key, std::ctime(&displayTime));
49 }
50 
51 std::string ZMQLogger::toJSON::operator()(long value)
52 {
53  return std::to_string(value);
54 }
55 
56 std::string ZMQLogger::toJSON::operator()(double value)
57 {
58  return std::to_string(value);
59 }
60 
61 std::string ZMQLogger::toJSON::operator()(const std::string& value)
62 {
63  return "\"" + boost::property_tree::json_parser::create_escapes(value) + "\"";
64 }
65 
67 {
68  value += 1;
69 }
70 
72 {
73  value += 1;
74 }
75 
77 {
78  throw std::domain_error("Can not increment a string type");
79 }
80 
82 {
83  value -= 1;
84 }
85 
87 {
88  value -= 1;
89 }
90 
92 {
93  throw std::domain_error("Can not decrement a string type");
94 }
Belle2::ZMQLogger::toJSON::operator()
std::string operator()(long value)
just stringify longs
Definition: ZMQLogger.cc:51
Belle2::ZMQLogger::Decrementor
Visitor Helper for decrementing a numerical value.
Definition: ZMQLogger.h:95
Belle2::ZMQLogger::logTime
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:44
Belle2::ZMQLogger::Decrementor::operator()
void operator()(long &value)
-1 for longs
Definition: ZMQLogger.cc:81
Belle2::ZMQLogger::decrement
void decrement(const std::string &key)
Decrement the value with the given key (only numerical values). If not present, set to -1.
Definition: ZMQLogger.cc:39
Belle2::ZMQLogger::log
void log(const std::string &key, const AClass &value)
Store a value under a certain key. Different types of values can be stored, namely long,...
Definition: ZMQLogger.h:106
Belle2::ZMQLogger::increment
void increment(const std::string &key)
Increment the value with the given key (only numerical values). If not present, set to 1.
Definition: ZMQLogger.cc:34
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQLogger::Incrementor::operator()
void operator()(long &value)
+1 for longs
Definition: ZMQLogger.cc:66
Belle2::ZMQLogger::toJSON
Visitor Helper for converting a variant value into a JSON string.
Definition: ZMQLogger.h:75
Belle2::ZMQLogger::Incrementor
Visitor Helper for incrementing a numerical value.
Definition: ZMQLogger.h:85
Belle2::ZMQLogger::getMonitoringJSON
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:17
Belle2::ZMQLogger::m_monitoring
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:68