MQTTSuite
Loading...
Searching...
No Matches
Mqtt.cpp File Reference
#include "Mqtt.h"
#include <iot/mqtt/Topic.h>
#include <iot/mqtt/packets/Connack.h>
#include <iot/mqtt/packets/Publish.h>
#include <iot/mqtt/packets/Suback.h>
#include <nlohmann/json.hpp>
Include dependency graph for Mqtt.cpp:

Go to the source code of this file.

Namespaces

namespace  mqtt
namespace  mqtt::mqttcli
namespace  mqtt::mqttcli::lib

Functions

static int getTerminalWidth ()
static std::vector< std::string > wrapParagraph (const std::string &text, std::size_t width)
static std::vector< std::string > myformat (const std::string &prefix, const std::string &headLine, const std::string &message, std::size_t initialPrefixLength=0)
static const std::string formatAsLogString (const std::string &prefix, const std::string &headLine, const std::string &message)
static uint8_t mqtt::mqttcli::lib::getQos (const std::string &qoSString)

Function Documentation

◆ formatAsLogString()

const std::string formatAsLogString ( const std::string & prefix,
const std::string & headLine,
const std::string & message )
static

Definition at line 204 of file Mqtt.cpp.

204 {
205 std::ostringstream formatAsLogStringStream;
206
207 for (const std::string& line : myformat(prefix, headLine, message, 34)) {
208 formatAsLogStringStream << (formatAsLogStringStream.view().empty() ? "" : " ") << line << "\n";
209 }
210
211 std::string formatStr = formatAsLogStringStream.str();
212
213 formatStr.pop_back();
214
215 return formatStr;
216}
static std::vector< std::string > myformat(const std::string &prefix, const std::string &headLine, const std::string &message, std::size_t initialPrefixLength=0)
Definition Mqtt.cpp:122

References myformat().

Referenced by mqtt::mqttcli::lib::Mqtt::onPublish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTerminalWidth()

int getTerminalWidth ( )
static

Definition at line 76 of file Mqtt.cpp.

76 {
77 int termWidth = 80;
78
79 struct winsize w;
80 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_col > 0) {
81 termWidth = w.ws_col;
82 }
83
84 return termWidth;
85}

Referenced by myformat().

Here is the caller graph for this function:

◆ myformat()

std::vector< std::string > myformat ( const std::string & prefix,
const std::string & headLine,
const std::string & message,
std::size_t initialPrefixLength = 0 )
static

Formats: prefix ┬ headLine ├ <first message line> │ <middle lines> └ <last message line>

If message parses as JSON, we pretty‐print it (indent=2). Otherwise we wrap it to the terminal width.

Returns the whole formatted string (with trailing newline on each line).

Definition at line 122 of file Mqtt.cpp.

125 {
126 // how many spaces before the box‐drawing char on subsequent lines?
127 const size_t prefixLen = prefix.size();
128 const size_t indentCount = prefixLen + 1; // +1 for the space before ┬, +33 for easylogging++ prefix format
129 const std::string indent(indentCount, ' ');
130
131 std::vector<std::string> lines;
132
133 const int termWidth = getTerminalWidth();
134
135 size_t avail = (termWidth > int(indentCount + 2)) ? static_cast<std::size_t>(termWidth) - (indentCount + 2) : 20u;
136
137 auto wrapped = wrapParagraph(prefix + " ┬ " + headLine, avail - (prefix.length() + 2));
138
139 if (wrapped.empty()) {
140 wrapped.push_back("");
141 }
142
143 // lines.insert(lines.end(), wrapped.begin(), wrapped.end());
144
145 bool first = true;
146 for (const auto& line : wrapped) {
147 lines.emplace_back((first ? "" : indent + "│ ") + line);
148 first = false;
149 }
150
151 // try parsing as JSON
152 try {
153 auto j = json::parse(message);
154 // pretty‐print with 2-space indent
155 std::string pretty = j.dump(2);
156 // split into lines
157 std::istringstream prettyIStringStream(pretty);
158
159 for (auto [line, lineNumnber] = std::tuple{std::string(""), 0}; std::getline(prettyIStringStream, line); lineNumnber++) {
160 if (lineNumnber == 0 && !prettyIStringStream.eof()) {
161 lines.push_back(indent + "├ " + line);
162 } else if (prettyIStringStream.eof()) {
163 lines.push_back(indent + "└ " + line);
164 } else {
165 lines.push_back(indent + "│ " + line);
166 }
167 }
168 } catch (json::parse_error&) {
169 // not JSON → wrap text
170
171 // break original message on hard newlines and wrap each paragraph
172 std::istringstream messageIStringStream(message);
173 std::vector<std::string> allLines;
174 for (std::string line; std::getline(messageIStringStream, line);) {
175 wrapped = wrapParagraph(line, avail - initialPrefixLength);
176
177 if (wrapped.empty()) {
178 wrapped.push_back("");
179 }
180
181 allLines.insert(allLines.end(), wrapped.begin(), wrapped.end());
182 }
183
184 if (!allLines.empty() && allLines.back().empty()) {
185 allLines.pop_back();
186 }
187
188 // emit with ├, │ and └
189 for (std::size_t lineNumber = 0; lineNumber < allLines.size(); ++lineNumber) {
190 if (lineNumber == 0 && lineNumber + 1 != allLines.size()) {
191 lines.push_back(indent + "├ " + allLines[lineNumber]);
192 } else if (lineNumber + 1 == allLines.size()) {
193 lines.push_back(indent + "└ " + allLines[lineNumber]);
194 } else {
195 lines.push_back(indent + "│ " + allLines[lineNumber]);
196 }
197 }
198 }
199
200 return lines;
201}
static int getTerminalWidth()
Definition Mqtt.cpp:76
static std::vector< std::string > wrapParagraph(const std::string &text, std::size_t width)
Definition Mqtt.cpp:88

References getTerminalWidth(), and wrapParagraph().

Referenced by formatAsLogString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wrapParagraph()

std::vector< std::string > wrapParagraph ( const std::string & text,
std::size_t width )
static

Definition at line 88 of file Mqtt.cpp.

88 {
89 std::istringstream words(text);
90 std::string word, line;
91 std::vector<std::string> lines;
92 while (words >> word) {
93 if (line.empty()) {
94 line = word;
95 } else if (line.size() + 1 + word.size() <= width) {
96 line += ' ' + word;
97 } else {
98 lines.push_back(line);
99 line = word;
100 }
101 }
102
103 if (!line.empty()) {
104 lines.push_back(line);
105 }
106
107 return lines;
108}

Referenced by myformat().

Here is the caller graph for this function: