API
flatbuffer_log.hpp
Go to the documentation of this file.
1 /** \file flatbuffer_log.hpp
2  * \brief The MagAO-X logger flatbuffer log base type.
3  * \author Jared R. Males (jaredmales@gmail.com)
4  *
5  * \ingroup logger_types_files
6  *
7  * History:
8  * - 2018-08-18 created by JRM
9  */
10 #ifndef logger_types_flatbuffer_log_hpp
11 #define logger_types_flatbuffer_log_hpp
12 
13 #include "flatbuffers/flatbuffers.h"
14 #include "flatbuffers/idl.h"
15 
16 #include "../logMeta.hpp"
17 
18 namespace MagAOX
19 {
20 namespace logger
21 {
22 
23 
24 ///Message type for resolving log messages with a f.b. builder.
25 /**
26  * \ingroup logger_types_basic
27  */
28 struct fbMessage
29 {
30  flatbuffers::FlatBufferBuilder builder;
31 };
32 
33 
34 ///Base class for logs consisting of a flatbuffer message.
35 /** Such logs are used to log arbitrary data structures using the flatbuffer protocol. Does not have eventCode or defaultLevel,
36  * so this can not be used as a log type directly.
37  *
38  *
39  * \ingroup logger_types_basic
40  */
42 {
43 
44  ///Get the length of the message.
45  static flatlogs::msgLenT length( const fbMessage & msg /**< [in] the fbMessage type holding a FlatBufferBuilder */)
46  {
47  return msg.builder.GetSize();
48  }
49 
50  ///Format the buffer given the input message.
51  /** \todo this is an unneccesary memcpy from the FlatBufferBuilder, we need to figure out how to not do this.
52  */
53  static int format( void * msgBuffer, ///< [out] the buffer, must be pre-allocated to size length(msg)
54  const fbMessage & msg ///< [in] the message which contains a flatbuffer builder, from which the data are memcpy-ed.
55  )
56  {
57  uint8_t * cbuff = reinterpret_cast<uint8_t *>(msgBuffer);
58 
59  memcpy(cbuff, msg.builder.GetBufferPointer(), msg.builder.GetSize());
60 
61  return 0;
62  }
63 
64  static std::string msgJSON( void * msgBuffer, /**< [in] Buffer containing the flatbuffer serialized message.*/
65  flatlogs::msgLenT len, /**< [in] [unused] length of msgBuffer.*/
66  const uint8_t * binarySchema, /**< [in] flatbuffers binary schema for this log type */
67  const unsigned int binarySchemaLength /**< [in] flatbuffers binary schema length */
68  )
69  {
70  static_cast<void>(len);
71  flatbuffers::Parser parser;
72  parser.opts.output_default_scalars_in_json = true;
73  parser.opts.output_enum_identifiers = true;
74  parser.opts.strict_json = true;
75  parser.opts.indent_step = -1; // also disables line breaking within record
76  bool ok = parser.Deserialize(binarySchema, binarySchemaLength);
77  if(!ok) {
78  std::cerr << __FILE__ << ":" << __LINE__ << " Failed to deserialize binary schema\n";
79  }
80  std::string output;
81  flatbuffers::GenText(parser, msgBuffer, &output);
82  return output;
83  }
84 
85 };
86 
87 
88 
89 } //namespace logger
90 } //namespace MagAOX
91 
92 #endif //logger_types_flatbuffer_log_hpp
msgLen2T msgLenT
The type used to refer to the message length, regardless of length.
Definition: logDefs.hpp:69
std::ostream & cerr()
std::stringstream msg
Definition: dm.hpp:24
Message type for resolving log messages with a f.b. builder.
flatbuffers::FlatBufferBuilder builder
Base class for logs consisting of a flatbuffer message.
static int format(void *msgBuffer, const fbMessage &msg)
Format the buffer given the input message.
static std::string msgJSON(void *msgBuffer, flatlogs::msgLenT len, const uint8_t *binarySchema, const unsigned int binarySchemaLength)
static flatlogs::msgLenT length(const fbMessage &msg)
Get the length of the message.