Line data Source code
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
36 : * eventCode or defaultLevel,
37 : * so this can not be used as a log type directly.
38 : *
39 : *
40 : * \ingroup logger_types_basic
41 : */
42 : struct flatbuffer_log
43 : {
44 :
45 : ///Get the length of the message.
46 1525 : static flatlogs::msgLenT length( const fbMessage & msg /**< [in] the fbMessage type holding a FlatBufferBuilder */)
47 : {
48 1525 : 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 1473 : 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 1473 : uint8_t * cbuff = reinterpret_cast<uint8_t *>(msgBuffer);
59 :
60 1473 : memcpy(cbuff, msg.builder.GetBufferPointer(), msg.builder.GetSize());
61 :
62 1473 : return 0;
63 : }
64 :
65 0 : 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 0 : flatbuffers::Parser parser;
73 0 : parser.opts.output_default_scalars_in_json = true;
74 0 : parser.opts.output_enum_identifiers = true;
75 0 : parser.opts.strict_json = true;
76 0 : parser.opts.indent_step = -1; // also disables line breaking within record
77 0 : bool ok = parser.Deserialize(binarySchema, binarySchemaLength);
78 0 : if(!ok) {
79 0 : std::cerr << __FILE__ << ":" << __LINE__ << " Failed to deserialize binary schema\n";
80 : }
81 0 : std::string output;
82 0 : flatbuffers::GenText(parser, msgBuffer, &output);
83 0 : return output;
84 0 : }
85 :
86 : };
87 :
88 :
89 :
90 : } //namespace logger
91 : } //namespace MagAOX
92 :
93 : #endif //logger_types_flatbuffer_log_hpp
|