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 201 of file Mqtt.cpp.

201 {
202 std::ostringstream formatAsLogStringStream;
203
204 for (const std::string& line : myformat(prefix, headLine, message, 34)) {
205 formatAsLogStringStream << (formatAsLogStringStream.view().empty() ? "" : " ") << line << "\n";
206 }
207
208 std::string formatStr = formatAsLogStringStream.str();
209
210 formatStr.pop_back();
211
212 return formatStr;
213}
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:119

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 73 of file Mqtt.cpp.

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

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 119 of file Mqtt.cpp.

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

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 85 of file Mqtt.cpp.

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

Referenced by myformat().

Here is the caller graph for this function: