API
user_log.hpp
Go to the documentation of this file.
1 /** \file user_log.hpp
2  * \brief The MagAO-X logger user_log log 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  * - 2025-05-02 revised with added email field by JRM
10  */
11 #ifndef logger_types_user_log_hpp
12 #define logger_types_user_log_hpp
13 
14 #include "generated/user_log_generated.h"
15 #include "flatbuffer_log.hpp"
16 
17 namespace MagAOX
18 {
19 namespace logger
20 {
21 
22 /// User entered log
23 /** \ingroup logger_types
24  */
25 struct user_log : public flatbuffer_log
26 {
27  /// The event code
28  static const flatlogs::eventCodeT eventCode = eventCodes::USER_LOG;
29 
30  /// The default level
32 
33  ///The type of the message
34  struct messageT : public fbMessage
35  {
36  messageT( const std::string & email,
37  const std::string & message
38  )
39  {
40  auto _eml = builder.CreateString(email);
41  auto _msg = builder.CreateString(message);
42 
43  auto gs = CreateUser_log_fb(builder, _eml, _msg);
44  builder.Finish(gs);
45  }
46  };
47 
48  static bool verify( flatlogs::bufferPtrT & logBuff, ///< [in] Buffer containing the flatbuffer serialized message.
49  flatlogs::msgLenT len ///< [in] length of msgBuffer.
50  )
51  {
52  auto verifier = flatbuffers::Verifier( static_cast<uint8_t*>(flatlogs::logHeader::messageBuffer(logBuff)), static_cast<size_t>(len));
53  return VerifyString_log_fbBuffer(verifier);
54  }
55 
56  ///Get the message formatte for human consumption.
57  static std::string msgString( void * msgBuffer, /**< [in] Buffer containing the flatbuffer serialized message.*/
58  flatlogs::msgLenT len /**< [in] [unused] length of msgBuffer.*/
59  )
60  {
61  static_cast<void>(len);
62 
63  auto fbs = GetUser_log_fb(msgBuffer);
64 
65  std::string msg = "[USER] ";
66 
67  if(fbs->email())
68  {
69  msg += fbs->email()->c_str();
70  msg += ": ";
71  }
72  else
73  {
74  msg += "unknown: ";
75  }
76 
77  if(fbs->message())
78  {
79  msg += fbs->message()->c_str();
80  }
81 
82  return msg;
83  }
84 
85  ///Get the user email from a user_log
86  static std::string email( void * msgBuffer /**< [in] Buffer containing the flatbuffer serialized message.*/)
87  {
88  auto fbs = GetUser_log_fb(msgBuffer);
89  if(fbs->email() != nullptr)
90  {
91  return std::string(fbs->email()->c_str());
92  }
93  else return "";
94  }
95 
96  ///Get the message from a user_log
97  static std::string message( void * msgBuffer /**< [in] Buffer containing the flatbuffer serialized message.*/)
98  {
99  auto fbs = GetUser_log_fb(msgBuffer);
100  if(fbs->message() != nullptr)
101  {
102  return std::string(fbs->message()->c_str());
103  }
104  else return "";
105  }
106 
107  /// Get the logMetaDetail for a member by name
108  /**
109  * \returns the a logMetaDetail filled in with the appropriate details
110  * \returns an empty logmegaDetail if member not recognized
111  */
112  static logMetaDetail getAccessor( const std::string & member /**< [in] the name of the member */ )
113  {
114  if( member == "email") return logMetaDetail({"USER", logMeta::valTypes::String, logMeta::metaTypes::State, reinterpret_cast<void*>(&email), false});
115  else if(member == "message") return logMetaDetail({"MESSAGE", logMeta::valTypes::String, logMeta::metaTypes::State, reinterpret_cast<void*>(&message), false});
116  else
117  {
118  std::cerr << "No string member " << member << " in user_log\n";
119  return logMetaDetail();
120  }
121  }
122 
123 };
124 
125 
126 } //namespace logger
127 } //namespace MagAOX
128 
129 #endif //logger_types_user_log_hpp
The MagAO-X logger flatbuffer log base type.
uint16_t eventCodeT
The type of an event code (16-bit unsigned int).
Definition: logDefs.hpp:40
msgLen2T msgLenT
The type used to refer to the message length, regardless of length.
Definition: logDefs.hpp:69
int8_t logPrioT
The type of the log priority code.
Definition: logDefs.hpp:21
static void * messageBuffer(bufferPtrT &logBuffer)
Get the message buffer address.
Definition: logHeader.hpp:621
std::shared_ptr< char > bufferPtrT
The log entry buffer smart pointer.
Definition: logHeader.hpp:58
std::ostream & cerr()
std::stringstream msg
Definition: dm.hpp:24
constexpr static logPrioT LOG_INFO
Informational. The info log level is the lowest level recorded during normal operations.
Definition: logPriority.hpp:49
Message type for resolving log messages with a f.b. builder.
flatbuffers::FlatBufferBuilder builder
Base class for logs consisting of a flatbuffer message.
The type of the message.
Definition: user_log.hpp:35
messageT(const std::string &email, const std::string &message)
Definition: user_log.hpp:36
User entered log.
Definition: user_log.hpp:26
static std::string msgString(void *msgBuffer, flatlogs::msgLenT len)
Get the message formatte for human consumption.
Definition: user_log.hpp:57
static const flatlogs::eventCodeT eventCode
The event code.
Definition: user_log.hpp:28
static bool verify(flatlogs::bufferPtrT &logBuff, flatlogs::msgLenT len)
Definition: user_log.hpp:48
static const flatlogs::logPrioT defaultLevel
The default level.
Definition: user_log.hpp:31
static std::string email(void *msgBuffer)
Get the user email from a user_log.
Definition: user_log.hpp:86
static std::string message(void *msgBuffer)
Get the message from a user_log.
Definition: user_log.hpp:97
static logMetaDetail getAccessor(const std::string &member)
Get the logMetaDetail for a member by name.
Definition: user_log.hpp:112