herald  2.0.0
logging_analysis_delegate.h
1 // Copyright 2021 Herald Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #ifndef HERALD_LOGGING_ANALYSIS_DELEGATE_H
6 #define HERALD_LOGGING_ANALYSIS_DELEGATE_H
7 
8 #include "sampling.h"
9 #include "../data/sensor_logger.h"
10 #include "../context.h"
11 
12 #include <memory>
13 #include <optional>
14 #include <functional>
15 
16 namespace herald {
17 namespace analysis {
18 
19 using namespace sampling;
20 
21 template <typename ContextT>
23  OptionalSensorLogger(ContextT& ctx)
24  : m_context(ctx)
25  HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
26  {
27  ;
28  }
29 
31  : m_context(other.m_context)
32  HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
33  {
34  ;
35  }
36 
38  : m_context(other.m_context)
39  HLOGGERINIT(m_context,"herald","LoggingAnalysisDelegate")
40  {
41  ;
42  }
43 
44  OptionalSensorLogger& operator=(OptionalSensorLogger&& other)
45  {
46  m_context = other.m_context;
47 #ifdef HERALD_LOG_LEVEL
48 #if HERALD_LOG_LEVEL != 0
49  logger = other.logger;
50 #endif
51 #endif
52  return *this;
53  }
54 
55  OptionalSensorLogger& operator=(const OptionalSensorLogger& other)
56  {
57  m_context = other.m_context;
58 #ifdef HERALD_LOG_LEVEL
59 #if HERALD_LOG_LEVEL != 0
60  logger = other.logger;
61 #endif
62 #endif
63  return *this;
64  }
65 
66  void debug(std::string toLog,SampledID sampled,double value)
67  {
68  HTDBG(toLog);
69  // HTDBG(std::to_string(sampled));
70  // HTDBG(std::to_string(value));
71  }
72 
73  void debug(std::string toLog)
74  {
75  HTDBG(toLog);
76  }
77 
78 private:
79  ContextT& m_context;
80  HLOGGER(ContextT);
81 };
82 
84 template <typename ContextT, typename ValT>
86  using value_type = ValT;
87 
89  : ctx(),
90  logger()
91  {
92  ;
93  }
94 
95  LoggingAnalysisDelegate(ContextT& context)
96  : ctx(context),
97  logger(ctx)
98  {
99  ;
100  }
101 
102  LoggingAnalysisDelegate(const LoggingAnalysisDelegate& other) noexcept
103  : ctx(other.ctx),
104  logger(ctx)
105  {
106  ;
107  }
108 
110  : ctx(other.ctx),
111  logger(ctx)
112  {
113  ;
114  }
115 
116  ~LoggingAnalysisDelegate() = default;
117 
118  LoggingAnalysisDelegate& operator=(LoggingAnalysisDelegate&& other) noexcept {
119  ctx = other.ctx;
120  logger = other.logger;
121  return *this;
122  }
123 
124  void assignContext(ContextT& newContext) noexcept {
125  ctx = newContext;
126  logger.emplace(ctx);
127  }
128 
129  // specific override of template
130  void newSample(SampledID sampled, Sample<ValT> sample) {
131  // Log the read distance as debug
132  if (!logger.has_value()) { // GUARD
133  return;
134  }
135  logger.value().debug("New Sample Recorded.");
136  // logger.value().debug("New Sample Recorded. SampledID: {}, Value: {}",sampled,(double)sample);
137  }
138 
139 private:
140  std::optional<std::reference_wrapper<ContextT>> ctx;
141  std::optional<OptionalSensorLogger<ContextT>> logger;
142 };
143 
144 }
145 }
146 
147 #endif
std::size_t SampledID
Definition: sampling.h:24
Acts as a non-global memory arena for arbitrary classes.
Definition: aggregates.h:15
Logs any given type of sample to the Herald logging subsystem as a Debug message.
Definition: logging_analysis_delegate.h:85
Definition: logging_analysis_delegate.h:22
The Sample taken from an object with ID of type SampledID.
Definition: sampling.h:28