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 }
0 commit comments