API
logMeta.cpp
Go to the documentation of this file.
1 /** \file logMeta.cpp
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  */
8 
9 
10 #include "logMeta.hpp"
11 
12 #include "generated/logTypes.hpp"
13 
14 
15 namespace MagAOX
16 {
17 namespace logger
18 {
19 
21  const std::string & memberName
22  )
23 {
24  switch(ec)
25  {
27  return telem_stdcam::getAccessor(memberName);
29  return telem_telcat::getAccessor(memberName);
31  return telem_teldata::getAccessor(memberName);
33  return telem_telpos::getAccessor(memberName);
35  return telem_stage::getAccessor(memberName);
37  return telem_zaber::getAccessor(memberName);
39  return telem_dmspeck::getAccessor(memberName);
41  return telem_observer::getAccessor(memberName);
43  return telem_fxngen::getAccessor(memberName);
45  return telem_loopgain::getAccessor(memberName);
46  default:
47  std::cerr << "Missing logMemberAccessor case entry for " << ec << ":" << memberName << "\n";
48  return logMetaDetail();
49  }
50 }
51 
52 
54 {
55  setLog(lms);
56 }
57 
58 std::string logMeta::keyword()
59 {
60  return m_spec.keyword;
61 }
62 
63 std::string logMeta::comment()
64 {
65  return m_spec.comment;
66 }
67 
68 int logMeta::setLog( const logMetaSpec & lms )
69 {
70  m_spec = lms;
72 
75  if(m_spec.format == "")
76  {
77  switch(m_detail.valType)
78  {
79  case valTypes::String:
80  m_spec.format = "%s";
81  break;
82  case valTypes::Bool:
83  m_spec.format = "%d";
84  break;
85  case valTypes::Char:
86  m_spec.format = "%d";
87  break;
88  case valTypes::UChar:
89  m_spec.format = "%u";
90  break;
91  case valTypes::Short:
92  m_spec.format = "%d";
93  break;
94  case valTypes::UShort:
95  m_spec.format = "%u";
96  break;
97  case valTypes::Int:
98  m_spec.format = "%d";
99  break;
100  case valTypes::UInt:
101  m_spec.format = "%u";
102  break;
103  case valTypes::Long:
104  m_spec.format = "%ld";
105  break;
106  case valTypes::ULong:
107  m_spec.format = "%lu";
108  break;
109  case valTypes::Float:
110  m_spec.format = "%g";
111  break;
112  case valTypes::Double:
113  m_spec.format = "%g";
114  break;
115  case valTypes::Vector_Bool:
116  m_spec.format = "%d";
117  break;
118  case valTypes::Vector_Float:
119  m_spec.format = "%g";
120  break;
121  default:
122  std::cerr << "Unrecognised value type for " + m_spec.device + " " + m_spec.keyword + ". Using format %d/\n";
123  m_spec.format = "%d";
124 
125  }
126 
127  }
128 
129 
131 
132  return 0;
133 }
134 
135 
136 std::string logMeta::value( logMap & lm,
137  const flatlogs::timespecX & stime,
138  const flatlogs::timespecX & atime
139  )
140 {
141  if(m_detail.accessor == nullptr) return "";
142 
143  if(m_detail.valType == valTypes::String)
144  {
145  std::string vs = valueString( lm, stime, atime);
146  if(vs == m_invalidValue)
147  {
148  std::cerr << __FILE__ << " " << __LINE__ << " valueString returned invalid value\n";
149  }
150  return vs;
151  }
152  else
153  {
154  std::string vn = valueNumber( lm, stime, atime);
155  if(vn == m_invalidValue)
156  {
157  std::cerr << __FILE__ << " " << __LINE__ << " valueNumber returned invalid value\n";
158  }
159  return vn;
160  }
161 }
162 
163 std::string logMeta::valueNumber( logMap & lm,
164  const flatlogs::timespecX & stime,
165  const flatlogs::timespecX & atime
166  )
167 {
168  char str[64];
169 
170  if(m_detail.metaType == metaTypes::State)
171  {
172  switch(m_detail.valType)
173  {
174  case valTypes::Bool:
175  {
176  bool val;
177  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<bool(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
178  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
179  return std::string(str);
180  }
181  case valTypes::Char:
182  {
183  char val;
184  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<char(*)(void*)>(m_detail.accessor), &m_hint) != 0)
185  {
186  std::cerr << "getLogStateVal returned error: " << __FILE__ << " " << __LINE__ << "\n";
187  return m_invalidValue;
188  }
189  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
190  return std::string(str);
191  }
192  case valTypes::UChar:
193  {
194  unsigned char val;
195  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned char(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
196  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
197  return std::string(str);
198  }
199  case valTypes::Short:
200  {
201  short val;
202  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<short(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
203  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
204  return std::string(str);
205  }
206  case valTypes::UShort:
207  {
208  unsigned short val;
209  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned short(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
210  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
211  return std::string(str);
212  }
213  case valTypes::Int:
214  {
215  int val;
216  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<int(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
217  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
218  return std::string(str);
219  }
220  case valTypes::UInt:
221  {
222  unsigned int val;
223  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned int(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
224  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
225  return std::string(str);
226  }
227  case valTypes::Long:
228  {
229  long val;
230  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
231  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
232  return std::string(str);
233  }
234  case valTypes::ULong:
235  {
236  unsigned long val;
237  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
238  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
239  return std::string(str);
240  }
241  case valTypes::LongLong:
242  {
243  long long val;
244  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<long long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
245  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
246  return std::string(str);
247  }
248  case valTypes::ULongLong:
249  {
250  unsigned long long val;
251  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned long long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
252  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
253  return std::string(str);
254  }
255  case valTypes::Float:
256  {
257  float val;
258  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<float(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
259  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
260  return std::string(str);
261  }
262  case valTypes::Double:
263  {
264  double val;
265  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<double(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
266  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
267  return std::string(str);
268  }
269  case valTypes::Vector_Bool:
270  {
271  std::vector<bool> val;
272  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<std::vector<bool>(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
273 
274  if(val.size() == 0) return "";
275 
276  std::string res;
277 
278  for(size_t n = 0; n < val.size()-1; ++n)
279  {
280  snprintf(str, sizeof(str), m_spec.format.c_str(), (int) val[n]);
281  res += str;
282  res += ',';
283  }
284 
285  snprintf(str, sizeof(str), m_spec.format.c_str(), (int) val.back());
286  res += str;
287 
288  return res;
289  }
290  case valTypes::Vector_Float:
291  {
292  std::vector<float> val;
293  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<std::vector<float>(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
294 
295  if(val.size() == 0) return "";
296 
297  std::string res;
298 
299  for(size_t n = 0; n < val.size()-1; ++n)
300  {
301  snprintf(str, sizeof(str), m_spec.format.c_str(), val[n]);
302  res += str;
303  res += ',';
304  }
305 
306  snprintf(str, sizeof(str), m_spec.format.c_str(), val.back());
307  res += str;
308 
309  return res;
310  }
311  default:
312  return m_invalidValue;
313  }
314  }
315  else if(m_detail.metaType == metaTypes::Continuous)
316  {
317  switch(m_detail.valType)
318  {
319  case valTypes::Bool:
320  {
321  bool val;
322  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<bool(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
323  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
324  return std::string(str);
325  }
326  case valTypes::Char:
327  {
328  char val;
329  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<char(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
330  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
331  return std::string(str);
332  }
333  case valTypes::UChar:
334  {
335  unsigned char val;
336  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned char(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
337  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
338  return std::string(str);
339  }
340  case valTypes::Short:
341  {
342  short val;
343  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<short(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
344  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
345  return std::string(str);
346  }
347  case valTypes::UShort:
348  {
349  unsigned short val;
350  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned short(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
351  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
352  return std::string(str);
353  }
354  case valTypes::Int:
355  {
356  int val;
357  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<int(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
358  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
359  return std::string(str);
360  }
361  case valTypes::UInt:
362  {
363  unsigned int val;
364  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned int(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
365  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
366  return std::string(str);
367  }
368  case valTypes::Long:
369  {
370  long val;
371  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
372  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
373  return std::string(str);
374  }
375  case valTypes::ULong:
376  {
377  unsigned long val;
378  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
379  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
380  return std::string(str);
381  }
382  case valTypes::LongLong:
383  {
384  long long val;
385  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<long long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
386  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
387  return std::string(str);
388  }
389  case valTypes::ULongLong:
390  {
391  unsigned long long val;
392  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<unsigned long long(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
393  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
394  return std::string(str);
395  }
396  case valTypes::Float:
397  {
398  float val;
399  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<float(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
400  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
401  return std::string(str);
402  }
403  case valTypes::Double:
404  {
405  double val;
406  if( getLogContVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<double(*)(void*)>(m_detail.accessor), &m_hint) != 0) return m_invalidValue;
407  snprintf(str, sizeof(str), m_spec.format.c_str(), val);
408  return std::string(str);
409  }
410  default:
411  return m_invalidValue;
412  }
413  }
414 
415  return m_invalidValue;
416 
417 }
418 
419 std::string logMeta::valueString( logMap & lm,
420  const flatlogs::timespecX & stime,
421  const flatlogs::timespecX & atime
422  )
423 {
424  std::string val;
425  if(m_detail.metaType == metaTypes::State)
426  {
427  if( getLogStateVal(val,lm, m_spec.device,m_spec.eventCode,stime,atime,reinterpret_cast<std::string(*)(void*)>(m_detail.accessor), &m_hint) != 0)
428  {
429  std::cerr << "getLogStateVal returned error " << __FILE__ << " " << __LINE__ << "\n";
430 
431  #ifdef HARD_EXIT
432  std::cerr << __FILE__ << " " << __LINE__ << "\n";
433 
434  exit(-1);
435  #endif
436  val = m_invalidValue;
437  }
438  }
439  else
440  {
441  std::cerr << "String type specified as something other than state\n";
442  }
443  return val;
444 }
445 
446 mx::fits::fitsHeaderCard logMeta::card( logMap &lm,
447  const flatlogs::timespecX & stime,
448  const flatlogs::timespecX & atime
449  )
450 {
451  #ifdef DEBUG
452  std::cerr << __FILE__ << " " << __LINE__ << "\n";
453  #endif
454 
455  std::string vstr = value(lm, stime, atime);
456 
457  #ifdef DEBUG
458  std::cerr << __FILE__ << " " << __LINE__ << "\n";
459  #endif
460 
461  std::string keyw;
462  if(m_detail.hierarch)
463  {
464  //Add spaces to make sure hierarch is invoked
465  keyw = m_spec.device + " " + m_spec.keyword;
466  if(keyw.size() < 9)
467  {
468  keyw += std::string(9-keyw.size(), ' ');
469  }
470  }
471  else
472  {
473  keyw = m_spec.keyword;
474  }
475 
476  if(vstr == m_invalidValue)
477  {
478  std::cerr << "got invalid value: " << __FILE__ << " " << __LINE__ << "\n";
479  // always a string sentinel value, so return here to skip the valType conditional
480  return mx::fits::fitsHeaderCard(keyw, vstr, m_spec.comment);
481  }
482 
483  if(m_detail.valType == valTypes::String)
484  {
485  return mx::fits::fitsHeaderCard(keyw, vstr, m_spec.comment);
486  }
487  else
488  {
489  return mx::fits::fitsHeaderCard(keyw, vstr.c_str(), m_detail.valType, m_spec.comment);
490  }
491 }
492 
493 } // logger
494 } // MagAOX
495 
496 
497 
uint16_t eventCodeT
The type of an event code (16-bit unsigned int).
Definition: logDefs.hpp:40
Declares and defines the logMeta 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
Map of log entries by application name, mapping both to files and to loaded buffers.
Definition: logMap.hpp:44
flatlogs::eventCodeT eventCode
Definition: logMeta.hpp:27
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
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get pointer to the accessor for a member by name.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
Definition: telem_stage.hpp:28
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
static const flatlogs::eventCodeT eventCode
The event code.
static const flatlogs::eventCodeT eventCode
The event code.
Definition: telem_zaber.hpp:28
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
A fixed-width timespec structure.
Definition: timespecX.hpp:35