Belle II Software development
ZMQLogger Class Reference

Base class for the ZMQ connection to help monitor and log certain values. More...

#include <ZMQLogger.h>

Inheritance diagram for ZMQLogger:
ZMQConnection ZMQConnectionOverSocket ZMQDataAndROIOutput ZMQHistoServerToFileOutput ZMQHistoServerToRawOutput ZMQHistoServerToZMQOutput ZMQNullConnection ZMQROIOutput ZMQConfirmedInput ZMQConfirmedOutput ZMQLoadBalancedInput ZMQLoadBalancedOutput ZMQRawInput ZMQRawOutput ZMQSimpleConnection

Classes

struct  Decrementor
 Visitor Helper for decrementing a numerical value. More...
 
struct  Incrementor
 Visitor Helper for incrementing a numerical value. More...
 
struct  toJSON
 Visitor Helper for converting a variant value into a JSON string. More...
 

Public Member Functions

virtual std::string getMonitoringJSON () const
 Convert the stored monitoring values to a JSON string ready for sending out via a message.
 
template<class AClass >
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, double or string. Mixtures are not allowed for a given key.
 
void increment (const std::string &key)
 Increment the value with the given key (only numerical values). If not present, set to 1.
 
void decrement (const std::string &key)
 Decrement the value with the given key (only numerical values). If not present, set to -1.
 
template<size_t MAX_SIZE = 100>
void average (const std::string &key, double value)
 Instead of storeing the double value directly under the given key, store the average of the last MAX_SIZE values.
 
template<size_t AVERAGE_SIZE = 2000>
void timeit (const std::string &key)
 Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time AVERAGE_SIZE was reached under <key>_last_measurement)
 
void logTime (const std::string &key)
 Store the current time as a string under the given key.
 

Private Attributes

std::map< std::string, std::variant< long, double, std::string > > m_monitoring
 Internal storage of all stored values.
 
std::unordered_map< std::string, std::tuple< std::vector< double >, size_t > > m_averages
 Internal storage of the previous values when calculating averages.
 
std::unordered_map< std::string, std::tuple< unsigned long, std::chrono::system_clock::time_point > > m_timeCounters
 Internal storage how often the timeit function for a given key was called and when it has last reached MAX_SIZE.
 

Detailed Description

Base class for the ZMQ connection to help monitor and log certain values.

Which and when to monitor/log must be defined by the derived classes, this base class only gives helpers to store/add/average/measure time and most importantly convert all stored monitoring values to a JSON string on request (useful e.g. for monitoring purposes).

Please note, that in all cases you need to call the corresponding monitor function by yourself. There is no "registering" of monitor values, values will only be stored on actively calling a monitor function (e.g. log).

Definition at line 35 of file ZMQLogger.h.

Member Function Documentation

◆ decrement()

void decrement ( const std::string &  key)

Decrement the value with the given key (only numerical values). If not present, set to -1.

Definition at line 37 of file ZMQLogger.cc.

38{
39 std::visit(Decrementor{}, m_monitoring[key]);
40}
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:58

◆ getMonitoringJSON()

std::string getMonitoringJSON ( ) const
virtual

Convert the stored monitoring values to a JSON string ready for sending out via a message.

Reimplemented in ZMQHistoServerToZMQOutput, ZMQHistoServerToRawOutput, ZMQROIOutput, and ZMQDataAndROIOutput.

Definition at line 15 of file ZMQLogger.cc.

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}

◆ increment()

void increment ( const std::string &  key)

Increment the value with the given key (only numerical values). If not present, set to 1.

Definition at line 32 of file ZMQLogger.cc.

33{
34 std::visit(Incrementor{}, m_monitoring[key]);
35}

◆ logTime()

void logTime ( const std::string &  key)

Store the current time as a string under the given key.

Definition at line 42 of file ZMQLogger.cc.

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}
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

Member Data Documentation

◆ m_averages

std::unordered_map<std::string, std::tuple<std::vector<double>, size_t> > m_averages
private

Internal storage of the previous values when calculating averages.

Definition at line 60 of file ZMQLogger.h.

◆ m_monitoring

std::map<std::string, std::variant<long, double, std::string> > m_monitoring
private

Internal storage of all stored values.

Definition at line 58 of file ZMQLogger.h.

◆ m_timeCounters

std::unordered_map<std::string, std::tuple<unsigned long, std::chrono::system_clock::time_point> > m_timeCounters
private

Internal storage how often the timeit function for a given key was called and when it has last reached MAX_SIZE.

Definition at line 62 of file ZMQLogger.h.


The documentation for this class was generated from the following files: