API
logMeta.hpp
Go to the documentation of this file.
1 /** \file logMeta.hpp
2  * \brief Declares and defines the logMeta class and related classes.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * \ingroup logger_files
6  *
7  * History:
8  * - 2020-01-02 created by JRM
9  */
10 
11 #ifndef logger_logMeta_hpp
12 #define logger_logMeta_hpp
13 
14 #include <mx/ioutils/fits/fitsHeaderCard.hpp>
15 //#define HARD_EXIT
16 #include "logMap.hpp"
17 
18 namespace MagAOX
19 {
20 namespace logger
21 {
22 
23 //This is how the user specifies an item of log meta data (i.e. via a config file)
25 {
26  std::string device;
28  std::string member;
29  std::string keyword; //overrides the default
30  std::string format; //overrides the default
31  std::string comment; //overrides the default
32 
33 
35  {
36  }
37 
38  logMetaSpec( const std::string & dev,
39  const flatlogs::eventCodeT ec,
40  const std::string & memb,
41  const std::string & k,
42  const std::string & f,
43  const std::string & c
44  ) : device(dev), eventCode(ec), member(memb), keyword(k), format(f), comment(c)
45  {
46  }
47 
48  logMetaSpec( const std::string & dev,
49  const flatlogs::eventCodeT ec,
50  const std::string & memb
51  ) : device(dev), eventCode(ec), member(memb)
52  {
53  }
54 
55 };
56 
57 //This is the data returned by the member accessor.
59 {
60  std::string keyword;
61  std::string comment;
62  std::string format;
63  int valType {-1};
64  int metaType {-1};
65  void * accessor {nullptr};
66  bool hierarch {true}; // if false the device name is not included.
67 
69  {
70  }
71 
72  logMetaDetail( const std::string & k,
73  const std::string & c,
74  const std::string & f,
75  int vt,
76  int mt,
77  void *acc
78  ) : keyword(k), comment(c), format(f), valType(vt), metaType(mt), accessor(acc)
79  {
80  }
81 
82  logMetaDetail( const std::string & k,
83  const std::string & c,
84  const std::string & f,
85  int vt,
86  int mt,
87  void *acc,
88  bool h
89  ) : keyword(k), comment(c), format(f), valType(vt), metaType(mt), accessor(acc), hierarch(h)
90  {
91  }
92 
93  logMetaDetail( const std::string & k,
94  const std::string & c,
95  int vt,
96  int mt,
97  void *acc,
98  bool h
99  ) : keyword(k), comment(c), valType(vt), metaType(mt), accessor(acc), hierarch(h)
100  {
101  }
102 
103  logMetaDetail( const std::string & k,
104  int vt,
105  int mt,
106  void *acc
107  ) : keyword(k), valType(vt), metaType(mt), accessor(acc)
108  {
109  }
110 
111  logMetaDetail( const std::string & k,
112  int vt,
113  int mt,
114  void *acc,
115  bool h
116  ) : keyword(k), valType(vt), metaType(mt), accessor(acc), hierarch(h)
117  {
118  }
119 
120 };
121 
122 logMetaDetail logMemberAccessor( flatlogs::eventCodeT ec,
123  const std::string & memberName
124  );
125 
126 template<typename valT>
127 int getLogStateVal( valT & val,
128  logMap & lm,
129  const std::string & appName,
131  const flatlogs::timespecX & stime,
132  const flatlogs::timespecX & atime,
133  valT (*getter)(void *),
134  char ** hint = 0
135  )
136 {
137  char * atprior = nullptr;
138  char * stprior = nullptr;
139 
140  char * _hint = nullptr;
141 
142  if(hint) _hint = *hint;
143  else _hint = 0;
144 
145  #ifdef DEBUG
146  std::cerr << __FILE__ << " " << __LINE__ << "\n";
147  #endif
148 
149  if(lm.getPriorLog(stprior, appName, ev, stime, _hint) != 0)
150  {
151  std::cerr << __FILE__ << " " << __LINE__ << " getPriorLog returned error for " << appName << ":" << ev << "\n";
152  return -1;
153  }
154  valT stprV = getter(flatlogs::logHeader::messageBuffer(stprior));
155 
156  valT atprV;
157 
158  #ifdef DEBUG
159  std::cerr << __FILE__ << " " << __LINE__ << "\n";
160  #endif
161 
162  if(lm.getNextLog(atprior, stprior, appName) != 0)
163  {
164  std::cerr << __FILE__ << " " << __LINE__ << " getNextLog returned error for " << appName << ":" << ev << "\n";
165  return -1;
166  }
167 
168  #ifdef DEBUG
169  std::cerr << __FILE__ << " " << __LINE__ << "\n";
170  #endif
171 
172  while( flatlogs::logHeader::timespec(atprior) < atime )
173  {
174  atprV = getter(flatlogs::logHeader::messageBuffer(atprior));
175  if(atprV != stprV)
176  {
177  val = atprV;
178  if(hint) *hint = stprior;
179  return 0;
180  }
181  stprior = atprior;
182  if(lm.getNextLog(atprior, stprior, appName) != 0)
183  {
184  std::cerr << __FILE__ << " " << __LINE__ << " getNextLog returned error for " << appName << ":" << ev << "\n";
185  return -1;
186  }
187  }
188 
189  val = stprV;
190 
191  if(hint) *hint = stprior;
192  return 0;
193 }
194 
195 template<typename valT>
196 int getLogContVal( valT & val,
197  logMap & lm,
198  const std::string & appName,
200  const flatlogs::timespecX & stime,
201  const flatlogs::timespecX & atime,
202  valT (*getter)(void *),
203  char ** hint = 0
204  )
205 {
206  char * atafter;
207  char * stprior;
208 
209  char * _hint;
210  if(hint) _hint = *hint;
211  else _hint = 0;
212 
213  flatlogs::timespecX midexp = meanTimespecX(atime, stime);
214 
215  //Get log entry before midexp
216  if(lm.getPriorLog(stprior, appName, ev, midexp, _hint)!=0)
217  {
218  std::cerr << __FILE__ << " " << __LINE__ << " getPriorLog returned error for " << appName << ":" << ev << "\n";
219  return 1;
220  }
221  valT stprV = getter(flatlogs::logHeader::messageBuffer(stprior));
222 
223  //Get log entry after.
224  if(lm.getNextLog(atafter, stprior, appName)!=0)
225  {
226  std::cerr << __FILE__ << " " << __LINE__ << " getNextLog returned error for " << appName << ":" << ev << "\n";
227  #ifdef HARD_EXIT
228  exit(-1);
229  #endif
230  return 1;
231  }
232  valT atprV = getter(flatlogs::logHeader::messageBuffer(atafter));
233 
234  double st = flatlogs::logHeader::timespec(stprior).asDouble();
235  double it = midexp.asDouble();
236  double et = flatlogs::logHeader::timespec(atafter).asDouble();
237 
238  val = stprV + (atprV-stprV)/(et-st)*(it-st);
239 
240  if(hint) *hint = stprior;
241 
242  return 0;
243 }
244 
245 
246 /// Manage meta data for a log entry
247 /** Handles cases where log is a state, i.e. has one of a finite number of values, or is a
248  * continuous variable, e.g. a temperature.
249  *
250  * Contains the information to construct a FITS header card.
251  */
252 struct logMeta
253 {
254 public:
255  enum valTypes
256  {
257  String = mx::fits::fitsType<std::string>(),
258  Bool = mx::fits::fitsType<bool>(),
259  Char = mx::fits::fitsType<char>(),
260  UChar = mx::fits::fitsType<unsigned char>(),
261  Short = mx::fits::fitsType<short>(),
262  UShort = mx::fits::fitsType<unsigned short>(),
263  Int = mx::fits::fitsType<int>(),
264  UInt = mx::fits::fitsType<unsigned int>(),
265  Long = mx::fits::fitsType<long>(),
266  ULong = mx::fits::fitsType<unsigned long>(),
267  LongLong = mx::fits::fitsType<long long>(),
268  ULongLong = mx::fits::fitsType<unsigned long long>(),
269  Float = mx::fits::fitsType<float>(),
270  Double = mx::fits::fitsType<double>(),
271  Vector_String = 10000,
272  Vector_Bool = 10002,
273  Vector_Char = 10004,
274  Vector_UChar = 10006,
275  Vector_Short = 10008,
276  Vector_UShort = 10010,
277  Vector_Int = 10012,
278  Vector_UInt = 10014,
279  Vector_Long = 10016,
280  Vector_ULong = 10018,
283  Vector_Float = 10024,
284  Vector_Double = 10026
285  };
286 
288  {
290  Continuous
291  };
292 
293 protected:
294 
297 
298  bool m_isValid {false};
299  std::string m_invalidValue {"invalid"};
300 
301  char * m_hint {nullptr};
302 
303 public:
304 
305  logMeta( const logMetaSpec & lms /**< [in] the specification of this meta data entry */ );
306 
307  std::string keyword();
308 
309  std::string comment();
310 
311  int setLog( const logMetaSpec &);
312 
313  std::string value( logMap & lm,
314  const flatlogs::timespecX & stime,
315  const flatlogs::timespecX & atime
316  );
317 
318  std::string valueNumber( logMap & lm,
319  const flatlogs::timespecX & stime,
320  const flatlogs::timespecX & atime
321  );
322 
323  std::string valueString( logMap & lm,
324  const flatlogs::timespecX & stime,
325  const flatlogs::timespecX & atime
326  );
327 
328  mx::fits::fitsHeaderCard card( logMap &lm,
329  const flatlogs::timespecX & stime,
330  const flatlogs::timespecX & atime
331  );
332 
333 };
334 
335 
336 }
337 }
338 
339 
340 
341 
342 
343 
344 
345 #endif //logger_logMeta_hpp
uint16_t eventCodeT
The type of an event code (16-bit unsigned int).
Definition: logDefs.hpp:40
static void * messageBuffer(bufferPtrT &logBuffer)
Get the message buffer address.
Definition: logHeader.hpp:621
static int timespec(bufferPtrT &logBuffer, const timespecX &ts)
Set the timespec of a log entry.
Definition: logHeader.hpp:435
Declares and defines the logMap class and related classes.
std::ostream & cerr()
int getLogStateVal(valT &val, logMap &lm, const std::string &appName, flatlogs::eventCodeT ev, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime, valT(*getter)(void *), char **hint=0)
Definition: logMeta.hpp:127
logMetaDetail logMemberAccessor(flatlogs::eventCodeT ec, const std::string &memberName)
Definition: logMeta.cpp:20
int getLogContVal(valT &val, logMap &lm, const std::string &appName, flatlogs::eventCodeT ev, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime, valT(*getter)(void *), char **hint=0)
Definition: logMeta.hpp:196
Definition: dm.hpp:24
timespecX meanTimespecX(timespecX ts1, timespecX ts2)
Definition: timespecX.hpp:331
Map of log entries by application name, mapping both to files and to loaded buffers.
Definition: logMap.hpp:44
int getPriorLog(char *&logBefore, const std::string &appName, const flatlogs::eventCodeT &ev, const flatlogs::timespecX &ts, char *hint=0)
Get the log for an event code which is the first prior to the supplied time.
Definition: logMap.cpp:135
int getNextLog(char *&logAfter, char *logCurrent, const std::string &appName)
Get the next log with the same event code which is after the supplied time.
Definition: logMap.cpp:252
logMetaDetail(const std::string &k, int vt, int mt, void *acc, bool h)
Definition: logMeta.hpp:111
logMetaDetail(const std::string &k, int vt, int mt, void *acc)
Definition: logMeta.hpp:103
logMetaDetail(const std::string &k, const std::string &c, const std::string &f, int vt, int mt, void *acc, bool h)
Definition: logMeta.hpp:82
logMetaDetail(const std::string &k, const std::string &c, int vt, int mt, void *acc, bool h)
Definition: logMeta.hpp:93
logMetaDetail(const std::string &k, const std::string &c, const std::string &f, int vt, int mt, void *acc)
Definition: logMeta.hpp:72
flatlogs::eventCodeT eventCode
Definition: logMeta.hpp:27
logMetaSpec(const std::string &dev, const flatlogs::eventCodeT ec, const std::string &memb, const std::string &k, const std::string &f, const std::string &c)
Definition: logMeta.hpp:38
logMetaSpec(const std::string &dev, const flatlogs::eventCodeT ec, const std::string &memb)
Definition: logMeta.hpp:48
Manage meta data for a log entry.
Definition: logMeta.hpp:253
std::string keyword()
Definition: logMeta.cpp:58
std::string value(logMap &lm, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime)
Definition: logMeta.cpp:136
mx::fits::fitsHeaderCard card(logMap &lm, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime)
Definition: logMeta.cpp:446
logMetaDetail m_detail
Definition: logMeta.hpp:296
std::string valueString(logMap &lm, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime)
Definition: logMeta.cpp:419
std::string valueNumber(logMap &lm, const flatlogs::timespecX &stime, const flatlogs::timespecX &atime)
Definition: logMeta.cpp:163
std::string m_invalidValue
Definition: logMeta.hpp:299
logMeta(const logMetaSpec &lms)
Definition: logMeta.cpp:53
int setLog(const logMetaSpec &)
Definition: logMeta.cpp:68
std::string comment()
Definition: logMeta.cpp:63
A fixed-width timespec structure.
Definition: timespecX.hpp:35
double asDouble()
Get the time as a double from a timespecX.
Definition: timespecX.hpp:231