Skip to content

Commit 62c5c72

Browse files
committed
Added checks if verbosity level is enabled before to_string.
Replaced Logrecorder with stringstream directly and added as a member to Logger, to reuse it fore the entire lifetime of the application. Added doc strings. Added constructors to LogRecord.
1 parent 1186310 commit 62c5c72

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

common/include/pcl/console/print.h

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@
5454
// PCL_ERROR_STREAM("Error: an Eigen vector: " << std::endl << Eigen::Vector3f(1.0, 2.0, 3.0) << std::endl);
5555
// NOLINTBEGIN(bugprone-macro-parentheses)
5656
#define PCL_LOG_STREAM(LEVEL, ARGS) \
57-
if(pcl::console::isVerbosityLevelEnabled(pcl::console::LEVEL)) \
57+
if (pcl::console::isVerbosityLevelEnabled(pcl::console::LEVEL)) \
5858
{ \
59-
pcl::console::LogRecord entry{pcl::console::LEVEL, ""}; \
60-
pcl::console::LogRecorder rec(entry.message); \
61-
rec << ARGS; \
59+
auto& strstream = pcl::console::Logger::getInstance().getStringStream(); \
60+
strstream.clear(); \
61+
strstream << ARGS; \
62+
pcl::console::LogRecord entry{pcl::console::LEVEL, strstream.str()}; \
6263
pcl::console::Logger::getInstance().print(entry); \
6364
}
6465
// NOLINTEND(bugprone-macro-parentheses)
@@ -132,41 +133,27 @@ namespace pcl
132133
L_VERBOSE
133134
};
134135

136+
/** \brief Structure to hold a log record
137+
* Used by the Logger class
138+
*/
135139
struct LogRecord {
136-
VERBOSITY_LEVEL level;
137-
std::string message;
138-
};
140+
LogRecord() = default;
139141

140-
class LogRecorder {
141-
public:
142-
LogRecorder(std::string& string) : strstream(string) { }
142+
LogRecord(VERBOSITY_LEVEL lvl, const std::string& str)
143+
: level(lvl), message(str) {};
143144

144-
template <class T>
145-
LogRecorder&
146-
operator<<(const T& x)
147-
{
148-
strstream << x;
149-
150-
return *this;
151-
}
145+
LogRecord(VERBOSITY_LEVEL lvl, std::string&& str)
146+
: level(lvl), message(std::move(str))
147+
{};
152148

153-
LogRecorder&
154-
operator<<(std::ostream& (std::ostream&))
155-
{
156-
strstream << std::endl;
157-
return *this;
158-
}
149+
LogRecord(const LogRecord& other) = default;
159150

160-
std::string
161-
to_string() const
162-
{
163-
return strstream.str();
164-
}
151+
~LogRecord() = default;
165152

166-
std::stringstream strstream;
153+
VERBOSITY_LEVEL level;
154+
std::string message;
167155
};
168156

169-
170157
/** set the verbosity level */
171158
PCL_EXPORTS void
172159
setVerbosityLevel (VERBOSITY_LEVEL level);
@@ -217,12 +204,21 @@ namespace pcl
217204
PCL_EXPORTS void
218205
reset_text_color (FILE *stream);
219206

220-
207+
/**
208+
* @brief Logger used to log messages with different verbosity levels
209+
* Can be used to redirect log messages to custom outputs by setting a callback
210+
*/
221211
class PCL_EXPORTS Logger {
222212
public:
223213
static Logger&
224214
getInstance();
225215

216+
std::stringstream&
217+
getStringStream()
218+
{
219+
return strstream;
220+
}
221+
226222
template <typename Functor>
227223
void
228224
setCallback(Functor&& callback)
@@ -249,13 +245,10 @@ namespace pcl
249245
print_value(const LogRecord& logEntry);
250246

251247
void
252-
print_color(FILE* stream,
253-
int attr,
254-
int fg,
255-
const LogRecord& logEntry);
256-
248+
print_color(FILE* stream, int attr, int fg, const LogRecord& logEntry);
257249

258250
private:
251+
std::stringstream strstream;
259252
std::function<void(const LogRecord&)> logcallback;
260253
};
261254

@@ -288,7 +281,7 @@ namespace pcl
288281
while (true) {
289282
formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */
290283
va_start(ap, fmt_str);
291-
final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
284+
final_n = vsnprintf(formatted.get(), n, fmt_str.c_str(), ap);
292285
va_end(ap);
293286
if (final_n < 0 || final_n >= n)
294287
n += abs(final_n - n + 1);
@@ -429,8 +422,10 @@ namespace pcl
429422
PCL_EXPORTS void
430423
print(VERBOSITY_LEVEL level, FILE* stream, const std::string format, Args&&...args)
431424
{
432-
const auto str = to_string(format, std::forward<Args>(args)...);
425+
if (!pcl::console::isVerbosityLevelEnabled(level))
426+
return;
433427

428+
const auto str = to_string(format, std::forward<Args>(args)...);
434429
LogRecord logEntry{level, str};
435430
Logger::getInstance().print(stream, logEntry);
436431
}
@@ -445,8 +440,10 @@ namespace pcl
445440
void
446441
print(VERBOSITY_LEVEL level, const std::string format, Args&&... args)
447442
{
448-
const auto str = to_string(format, std::forward<Args>(args)...);
443+
if (!pcl::console::isVerbosityLevelEnabled(level))
444+
return;
449445

446+
const auto str = to_string(format, std::forward<Args>(args)...);
450447
LogRecord logEntry{level, str};
451448
Logger::getInstance().print(logEntry);
452449
}

common/src/print.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ pcl::console::Logger::print(const LogRecord& logEntry)
300300
void
301301
pcl::console::Logger::print_highlight(FILE* stream, const LogRecord& logEntry)
302302
{
303+
if (!pcl::console::isVerbosityLevelEnabled(logEntry.level))
304+
return;
305+
303306
if (logcallback)
304307
logcallback(logEntry);
305308
else {
@@ -320,6 +323,9 @@ pcl::console::Logger::print_highlight(const LogRecord& logEntry)
320323
void
321324
pcl::console::Logger::print_value(FILE* stream, const LogRecord& logEntry)
322325
{
326+
if (!pcl::console::isVerbosityLevelEnabled(logEntry.level))
327+
return;
328+
323329
if (logcallback)
324330
logcallback(logEntry);
325331
else {
@@ -341,6 +347,9 @@ void
341347
pcl::console::Logger::print_color(
342348
FILE* stream, int attr, int fg, const LogRecord& logEntry)
343349
{
350+
if (!pcl::console::isVerbosityLevelEnabled(logEntry.level))
351+
return;
352+
344353
if (logcallback)
345354
logcallback(logEntry);
346355
else {

0 commit comments

Comments
 (0)