Belle II Software  release-08-01-10
ZMQLogger.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <framework/pcore/zmq/utils/ZMQLogger.h>
9 #include <boost/property_tree/json_parser.hpp>
10 #include <ostream>
11 #include <stdexcept>
12 
13 using namespace Belle2;
14 
15 std::string ZMQLogger::getMonitoringJSON() const
16 {
17  std::stringstream buffer;
18  buffer << "{";
19  bool first = true;
20  for (const auto& keyValue : m_monitoring) {
21  if (not first) {
22  buffer << ", ";
23  }
24  first = false;
25  buffer << "\"" << keyValue.first << "\": ";
26  buffer << std::visit(toJSON{}, keyValue.second);
27  }
28  buffer << "}" << std::endl;
29  return buffer.str();
30 }
31 
32 void ZMQLogger::increment(const std::string& key)
33 {
34  std::visit(Incrementor{}, m_monitoring[key]);
35 }
36 
37 void ZMQLogger::decrement(const std::string& key)
38 {
39  std::visit(Decrementor{}, m_monitoring[key]);
40 }
41 
42 void ZMQLogger::logTime(const std::string& key)
43 {
44  auto current = std::chrono::system_clock::now();
45  auto displayTime = std::chrono::system_clock::to_time_t(current);
46  log(key, std::ctime(&displayTime));
47 }
48 
49 std::string ZMQLogger::toJSON::operator()(long value)
50 {
51  return std::to_string(value);
52 }
53 
54 std::string ZMQLogger::toJSON::operator()(double value)
55 {
56  return std::to_string(value);
57 }
58 
59 std::string ZMQLogger::toJSON::operator()(const std::string& value)
60 {
61  return "\"" + boost::property_tree::json_parser::create_escapes(value) + "\"";
62 }
63 
65 {
66  value += 1;
67 }
68 
70 {
71  value += 1;
72 }
73 
75 {
76  throw std::domain_error("Can not increment a string type");
77 }
78 
80 {
81  value -= 1;
82 }
83 
85 {
86  value -= 1;
87 }
88 
90 {
91  throw std::domain_error("Can not decrement a string type");
92 }
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:37
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:15
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:42
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:32
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:58
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:96
Abstract base class for different kinds of events.
Visitor Helper for decrementing a numerical value.
Definition: ZMQLogger.h:85
void operator()(long &value)
-1 for longs
Definition: ZMQLogger.cc:79
Visitor Helper for incrementing a numerical value.
Definition: ZMQLogger.h:75
void operator()(long &value)
+1 for longs
Definition: ZMQLogger.cc:64
Visitor Helper for converting a variant value into a JSON string.
Definition: ZMQLogger.h:65
std::string operator()(long value)
just stringify longs
Definition: ZMQLogger.cc:49