API
 
Loading...
Searching...
No Matches
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
18namespace MagAOX
19{
20namespace logger
21{
22
23
24///Message type for resolving log messages with a f.b. builder.
25/**
26 * \ingroup logger_types_basic
27 */
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
36 * eventCode or defaultLevel,
37 * so this can not be used as a log type directly.
38 *
39 *
40 * \ingroup logger_types_basic
41 */
43{
44
45 ///Get the length of the message.
46 static flatlogs::msgLenT length( const fbMessage & msg /**< [in] the fbMessage type holding a FlatBufferBuilder */)
47 {
48 return msg.builder.GetSize();
49 }
50
51 ///Format the buffer given the input message.
52 /** \todo this is an unneccesary memcpy from the FlatBufferBuilder, we need to figure out how to not do this.
53 */
54 static int format( void * msgBuffer, ///< [out] the buffer, must be pre-allocated to size length(msg)
55 const fbMessage & msg ///< [in] the message which contains a flatbuffer builder, from which the data are memcpy-ed.
56 )
57 {
58 uint8_t * cbuff = reinterpret_cast<uint8_t *>(msgBuffer);
59
60 memcpy(cbuff, msg.builder.GetBufferPointer(), msg.builder.GetSize());
61
62 return 0;
63 }
64
65 static std::string msgJSON( void * msgBuffer, /**< [in] Buffer containing the flatbuffer serialized message.*/
66 flatlogs::msgLenT len, /**< [in] [unused] length of msgBuffer.*/
67 const uint8_t * binarySchema, /**< [in] flatbuffers binary schema for this log type */
68 const unsigned int binarySchemaLength /**< [in] flatbuffers binary schema length */
69 )
70 {
71 static_cast<void>(len);
72 flatbuffers::Parser parser;
73 parser.opts.output_default_scalars_in_json = true;
74 parser.opts.output_enum_identifiers = true;
75 parser.opts.strict_json = true;
76 parser.opts.indent_step = -1; // also disables line breaking within record
77 bool ok = parser.Deserialize(binarySchema, binarySchemaLength);
78 if(!ok) {
79 std::cerr << __FILE__ << ":" << __LINE__ << " Failed to deserialize binary schema\n";
80 }
81 std::string output;
82 flatbuffers::GenText(parser, msgBuffer, &output);
83 return output;
84 }
85
86};
87
88
89
90} //namespace logger
91} //namespace MagAOX
92
93#endif //logger_types_flatbuffer_log_hpp
msgLen2T msgLenT
The type used to refer to the message length, regardless of length.
Definition logDefs.hpp:69
Definition dm.hpp:19
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.