EMANE  1.2.1
loggerrecordmessage.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "loggerlevelconvert.h"
34 #include "loggerrecordmessage.h"
35 #include "loggermessage.pb.h"
36 
37 class EMANE::Messages::LoggerRecordMessage::Implementation
38 {
39 public:
40  Implementation(std::string sLogRecord, LogLevel logLevel, uint32_t u32LogSequenceNumber) :
41  sLogRecord_{sLogRecord},
42  logLevel_{logLevel},
43  u32LogSequenceNumber_{u32LogSequenceNumber}{}
44 
45  LogLevel getLogLevel() const
46  {
47  return logLevel_;
48  }
49 
50  std::uint32_t getLogSequenceNumber() const
51  {
52  return u32LogSequenceNumber_;
53  }
54 
55  const std::string & getLogRecord() const
56  {
57  return sLogRecord_;
58  }
59 
60 private:
61  const std::string sLogRecord_;
62  const LogLevel logLevel_;
63  const std::uint32_t u32LogSequenceNumber_;
64 };
65 
67  size_t len,
68  LogLevel logLevel,
69  std::uint32_t u32LogSequenceNumber):
70  pImpl_{new Implementation{std::string{p, len}, logLevel, u32LogSequenceNumber}}
71 { }
72 
73 
75 {
76  EMANEMessage::LoggerMessage loggerMessage;
77 
78  if(!loggerMessage.ParseFromArray(p, len))
79  {
80  throw SerializationException("unable to deserialize LoggerRecordMessage");
81  }
82 
83  // check for record rype
84  if(loggerMessage.type() == EMANEMessage::LoggerMessage_Type_RECORD)
85  {
86  // check for record
87  if(loggerMessage.has_record ())
88  {
89  pImpl_.reset(new Implementation(loggerMessage.record().record(),
90  convertLogLevel(loggerMessage.record().level()),
91  loggerMessage.record().sequencenumber()));
92  }
93  else
94  {
95  throw SerializationException("LoggerRecordMessage does NOT have record");
96  }
97  }
98  else
99  {
100  throw SerializationException("LoggerRecordMessage type is not LoggerMessage_Type_RECORD");
101  }
102 }
103 
104 
106 { }
107 
108 
110 {
111  return pImpl_->getLogLevel();
112 }
113 
114 
116 {
117  return pImpl_->getLogRecord();
118 }
119 
120 
122 {
123  return pImpl_->getLogSequenceNumber();
124 }
125 
126 
128 {
129  Serialization serialization;
130 
131  // logger message
132  EMANEMessage::LoggerMessage loggerMessage;
133 
134  // set type log record
135  loggerMessage.set_type(EMANEMessage::LoggerMessage_Type_RECORD);
136 
137  // get ref to log record msg
138  EMANEMessage::LoggerMessage_RecordMessage * pRecordMessage = loggerMessage.mutable_record();
139 
140  // set the log record fields
141  pRecordMessage->set_level(convertLogLevel(pImpl_->getLogLevel()));
142 
143  pRecordMessage->set_record(pImpl_->getLogRecord());
144 
145  pRecordMessage->set_sequencenumber(pImpl_->getLogSequenceNumber());
146 
147  if(!loggerMessage.SerializeToString(&serialization))
148  {
149  throw SerializationException("unable to serialize LoggerRecordMessage");
150  }
151 
152  return serialization;
153 }
std::string Serialization
Definition: serializable.h:42
SerializationException is thrown when an exception occurs during serialization or deserialization of ...
EMANEMessage::LoggerMessage_LogLevel convertLogLevel(EMANE::LogLevel level)
const std::string & getLogRecord() const
Serialization serialize() const override
LoggerRecordMessage(const char *p, size_t len, LogLevel level, std::uint32_t u32SequenceNumber)