ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
eventLog.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_common_libdaq
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains declaration for EventLog, ObservableEventLog and related events.
7  */
8 #ifndef OCM_DAQ_DAQ_EVENT_HPP_
9 #define OCM_DAQ_DAQ_EVENT_HPP_
10 
11 #include <chrono>
12 #include <string>
13 #include <vector>
14 #include <variant>
15 #include <iosfwd>
16 
17 #include <boost/signals2/signal.hpp>
18 
19 #include "status.hpp"
20 
21 
22 namespace daq {
23 
24 /**
25  * Represents a generic event if a more specific event is not usable.
26  *
27  * @ingroup daq_common_libdaq
28  */
29 struct GenericEvent {
30  using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
31 
32  GenericEvent(std::string id, std::string description, std::optional<Status> status) noexcept;
33 
34  GenericEvent(GenericEvent&&) = default;
35  GenericEvent(GenericEvent const&) = default;
37  GenericEvent& operator=(GenericEvent const&) = default;
38 
39  bool operator==(GenericEvent const& rhs) const noexcept;
40  bool operator!=(GenericEvent const& rhs) const noexcept;
41 
43  std::string id;
44  std::string description;
45  std::optional<Status> status;
46 };
47 
48 std::ostream& operator<<(std::ostream& os, GenericEvent const& s);
49 
50 
51 /**
52  * Event related to an action being requested or performed.
53  *
54  * @ingroup daq_common_libdaq
55  */
58 };
59 
60 /**
61  * Event directly related to user action, such as a command to do something.
62  *
63  * @ingroup daq_common_libdaq
64  */
66  using ActionEvent::ActionEvent;
67 };
68 
69 struct ErrorEvent final : GenericEvent {
70  ErrorEvent(std::string id,
71  std::string description,
72  std::optional<Status> status,
73  std::string origin) noexcept;
74  bool operator==(ErrorEvent const& rhs) const noexcept;
75  bool operator!=(ErrorEvent const& rhs) const noexcept;
76  /**
77  * Error origin.
78  */
79  std::string origin;
80 };
81 std::ostream& operator<<(std::ostream& os, ErrorEvent const& s);
82 
83 
84 /**
85  * Represents a collection of events.
86  *
87  * @ingroup daq_common_libdaq
88  */
89 struct EventLog {
90  using EventType = std::variant<ActionEvent, UserActionEvent, GenericEvent, ErrorEvent>;
91  using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
92 
93  bool operator==(EventLog const& rhs) const noexcept;
94  bool operator!=(EventLog const& rhs) const noexcept;
95 
96  std::vector<EventType> events;
97 };
98 
99 
100 /**
101  * Stores data acquisition status and allows subscription to status changes.
102  *
103  * DaqController and other objects will update ObservableEvent as changes occur.
104  *
105  * @ingroup daq_common_libdaq
106  */
108  public:
109  using Signal = boost::signals2::signal<void(EventLog::EventType const&)>;
110 
111  /**
112  * Construct a new object
113  *
114  * @param id Data acquisition identifier.
115  */
116  explicit ObservableEventLog() = default;
119 
120  /** @name Modifiers
121  * @{ */
122  /**
123  * Records that a file has been produced for this data acquisition.
124  *
125  * @param files Files to add/record.
126  *
127  * @post Connected observers have been signalled.
128  */
129  void AddEvent(EventLog::EventType event);
130  /**
131  * Like AddEvent but emplaces the specified event type.
132  */
133  template<class T, class... Args>
134  void EmplaceEvent(Args&&... args) {
135  m_event_log.events.emplace_back(std::in_place_type<T>, std::forward<Args>(args)...);
136  m_signal(m_event_log.events.back());
137  }
138 
139  /**
140  * Connect observer that is invoked when state is modified.
141  *
142  * @param o Observer callable invoked on status changes (state or file changes)
143  * Observer must be invocable with signature `void(ObservableEvent const&)`.
144  *
145  * @return signal connection object that can be used to disconnect observer:
146  *
147  * @code
148  * auto c = status.ConnectObserver([](ObservableEvent const& s){});
149  * // later the connection object can be used to disconnect
150  * c.disconnect();
151  * @endcode
152  */
153  template <class Observer>
154  boost::signals2::connection ConnectObserver(Observer o) {
155  return m_signal.connect(std::move(o));
156  }
157  /** @} */
158 
159  /** @name Accessors
160  * @{ */
161  /**
162  * Allow implicit conversion to non-observable status.
163  */
164  inline operator EventLog const&() {
165  return m_event_log;
166  }
167 
168  inline EventLog const& GetEventLog() const noexcept {
169  return m_event_log;
170  }
171 
172  inline std::vector<EventLog::EventType> const& GetEventContainer() const noexcept {
173  return m_event_log.events;
174  }
175  /** @} */
176 
177  private:
178  EventLog m_event_log;
179  Signal m_signal;
180 };
181 
182 } // namespace daq
183 #endif // #ifndef OCM_DAQ_DAQ_EVENT_HPP_
Stores data acquisition status and allows subscription to status changes.
Definition: eventLog.hpp:107
void EmplaceEvent(Args &&... args)
Like AddEvent but emplaces the specified event type.
Definition: eventLog.hpp:134
boost::signals2::connection ConnectObserver(Observer o)
Connect observer that is invoked when state is modified.
Definition: eventLog.hpp:154
ObservableEventLog(ObservableEventLog const &)=delete
ObservableEventLog & operator=(ObservableEventLog const &)=delete
void AddEvent(EventLog::EventType event)
Records that a file has been produced for this data acquisition.
Definition: eventLog.cpp:56
EventLog const & GetEventLog() const noexcept
Allow implicit conversion to non-observable status.
Definition: eventLog.hpp:168
std::vector< EventLog::EventType > const & GetEventContainer() const noexcept
Allow implicit conversion to non-observable status.
Definition: eventLog.hpp:172
ObservableEventLog()=default
Construct a new object.
boost::signals2::signal< void(EventLog::EventType const &)> Signal
Definition: eventLog.hpp:109
daqif::DaqStatus & operator<<(daqif::DaqStatus &status, daq::Status const &rhs)
Convert daq::Status -> daqif::DaqStatus by populating from rhs.
Definition: conversion.cpp:18
Contains declaration for Status and ObservableStatus.
Event related to an action being requested or performed.
Definition: eventLog.hpp:56
ErrorEvent(std::string id, std::string description, std::optional< Status > status, std::string origin) noexcept
Definition: eventLog.cpp:28
bool operator!=(ErrorEvent const &rhs) const noexcept
Definition: eventLog.cpp:40
std::string origin
Error origin.
Definition: eventLog.hpp:79
bool operator==(ErrorEvent const &rhs) const noexcept
Definition: eventLog.cpp:36
Represents a collection of events.
Definition: eventLog.hpp:89
std::variant< ActionEvent, UserActionEvent, GenericEvent, ErrorEvent > EventType
Definition: eventLog.hpp:90
bool operator!=(EventLog const &rhs) const noexcept
std::vector< EventType > events
Definition: eventLog.hpp:96
bool operator==(EventLog const &rhs) const noexcept
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: eventLog.hpp:91
Represents a generic event if a more specific event is not usable.
Definition: eventLog.hpp:29
bool operator!=(GenericEvent const &rhs) const noexcept
Definition: eventLog.cpp:24
GenericEvent(GenericEvent &&)=default
GenericEvent & operator=(GenericEvent const &)=default
TimePoint timestamp
Definition: eventLog.hpp:42
GenericEvent & operator=(GenericEvent &&)=default
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: eventLog.hpp:30
std::string description
Definition: eventLog.hpp:44
GenericEvent(GenericEvent const &)=default
std::optional< Status > status
Definition: eventLog.hpp:45
GenericEvent(std::string id, std::string description, std::optional< Status > status) noexcept
Definition: eventLog.cpp:14
bool operator==(GenericEvent const &rhs) const noexcept
Definition: eventLog.cpp:20
std::string id
Definition: eventLog.hpp:43
Event directly related to user action, such as a command to do something.
Definition: eventLog.hpp:65