Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
print.h
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the copyright holder(s) nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $Id$
35 *
36 */
37
38#pragma once
39
40#include <cstdio>
41#include <cstring> // For std::strcpy
42#include <functional>
43#include <string>
44#include <sstream>
45#include <stdarg.h> // For va_start, etc.
46#include <iostream>
47#include <memory> // For std::unique_ptr
48
49#include <pcl/pcl_exports.h>
50#include <pcl/pcl_config.h>
51
52// Use e.g. like this:
53// PCL_INFO_STREAM("Info: this is a point: " << pcl::PointXYZ(1.0, 2.0, 3.0) << std::endl);
54// PCL_ERROR_STREAM("Error: an Eigen vector: " << std::endl << Eigen::Vector3f(1.0, 2.0, 3.0) << std::endl);
55// NOLINTBEGIN(bugprone-macro-parentheses)
56#define PCL_LOG_STREAM(LEVEL, ARGS) \
57 if (pcl::console::isVerbosityLevelEnabled(pcl::console::LEVEL)) \
58 { \
59 std::stringstream strstream; \
60 strstream << ARGS; \
61 pcl::console::LogRecord entry{pcl::console::LEVEL, strstream.str()}; \
62 pcl::console::Logger::getInstance().print(entry); \
63 }
64// NOLINTEND(bugprone-macro-parentheses)
65#define PCL_ALWAYS_STREAM(ARGS) PCL_LOG_STREAM(L_ALWAYS, ARGS)
66#define PCL_ERROR_STREAM(ARGS) PCL_LOG_STREAM(L_ERROR, ARGS)
67#define PCL_WARN_STREAM(ARGS) PCL_LOG_STREAM(L_WARN, ARGS)
68#define PCL_INFO_STREAM(ARGS) PCL_LOG_STREAM(L_INFO, ARGS)
69#define PCL_DEBUG_STREAM(ARGS) PCL_LOG_STREAM(L_DEBUG, ARGS)
70#define PCL_VERBOSE_STREAM(ARGS) PCL_LOG_STREAM(L_VERBOSE, ARGS)
71
72
73#define PCL_ALWAYS(...) pcl::console::print (pcl::console::L_ALWAYS, __VA_ARGS__)
74#define PCL_ERROR(...) pcl::console::print (pcl::console::L_ERROR, __VA_ARGS__)
75#define PCL_WARN(...) pcl::console::print (pcl::console::L_WARN, __VA_ARGS__)
76#define PCL_INFO(...) pcl::console::print (pcl::console::L_INFO, __VA_ARGS__)
77#define PCL_DEBUG(...) pcl::console::print (pcl::console::L_DEBUG, __VA_ARGS__)
78#define PCL_VERBOSE(...) pcl::console::print (pcl::console::L_VERBOSE, __VA_ARGS__)
79#define PCL_HIGH(...) pcl::console::print_highlight (__VA_ARGS__)
80
81#define PCL_ASSERT_ERROR_PRINT_CHECK(pred, msg) \
82 do \
83 { \
84 if (!(pred)) \
85 { \
86 PCL_ERROR(msg); \
87 PCL_ERROR("In File %s, in line %d\n" __FILE__, __LINE__); \
88 } \
89 } while (0)
90
91#define PCL_ASSERT_ERROR_PRINT_RETURN(pred, msg, err) \
92 do \
93 { \
94 PCL_ASSERT_ERROR_PRINT_CHECK(pred, msg); \
95 if (!(pred)) return err; \
96 } while (0)
97
98namespace pcl
99{
100 namespace console
101 {
112
124
134
135 /** \brief Structure to hold a log record
136 * Used by the Logger class
137 */
138 struct LogRecord {
139 LogRecord() = default;
140
141 LogRecord(VERBOSITY_LEVEL lvl, const std::string& str)
142 : level(lvl), message(str) {};
143
144 LogRecord(VERBOSITY_LEVEL lvl, std::string&& str)
145 : level(lvl), message(std::move(str))
146 {};
147
148 LogRecord(const LogRecord& other) = default;
149
150 ~LogRecord() = default;
151
153 std::string message;
154 };
155
156 /** set the verbosity level */
157 PCL_EXPORTS void
159
160 /** get the verbosity level. */
161 PCL_EXPORTS VERBOSITY_LEVEL
163
164 /** initialize verbosity level. */
165 PCL_EXPORTS bool
167
168 /** is verbosity level enabled? */
169 PCL_EXPORTS bool
171
172 /** \brief Enable or disable colored text output, overriding the default behavior.
173 *
174 * By default, colored output is enabled for interactive terminals or when the environment
175 * variable PCL_CLICOLOR_FORCE is set.
176 *
177 * \param stream the output stream (stdout, stderr, etc)
178 * \param enable whether to emit color codes when calling any of the color related methods
179 */
180 PCL_EXPORTS void
181 enableColoredOutput (FILE *stream, bool enable);
182
183 /** \brief Change the text color (on either stdout or stderr) with an attr:fg:bg
184 * \param stream the output stream (stdout, stderr, etc)
185 * \param attribute the text attribute
186 * \param fg the foreground color
187 * \param bg the background color
188 */
189 PCL_EXPORTS void
190 change_text_color (FILE *stream, int attribute, int fg, int bg);
191
192 /** \brief Change the text color (on either stdout or stderr) with an attr:fg
193 * \param stream the output stream (stdout, stderr, etc)
194 * \param attribute the text attribute
195 * \param fg the foreground color
196 */
197 PCL_EXPORTS void
198 change_text_color (FILE *stream, int attribute, int fg);
199
200 /** \brief Reset the text color (on either stdout or stderr) to its original state
201 * \param stream the output stream (stdout, stderr, etc)
202 */
203 PCL_EXPORTS void
204 reset_text_color (FILE *stream);
205
206 /**
207 * @brief Logger used to log messages with different verbosity levels
208 * Can be used to redirect log messages to custom outputs by setting a callback
209 */
210 class PCL_EXPORTS Logger {
211 public:
212 static Logger&
214
215 template <typename Functor>
216 void
218 {
219 logcallback = std::move(callback);
220 }
221
222 void
223 print(FILE* stream, const LogRecord& logEntry);
224
225 void
226 print(const LogRecord& logEntry);
227
228 void
229 print_highlight(FILE* stream, const LogRecord& logEntry);
230
231 void
232 print_highlight(const LogRecord& logEntry);
233
234 void
235 print_value(FILE* stream, const LogRecord& logEntry);
236
237 void
238 print_value(const LogRecord& logEntry);
239
240 void
241 print_color(FILE* stream, int attr, int fg, const LogRecord& logEntry);
242
243 private:
244 std::function<void(const LogRecord&)> logcallback;
245 };
246
247 /**
248 * @brief Sets a callable in the Logger instance, which is called whenever a printout has occured
249 * @tparam Functor matching the definition: std::function<void(const LogRecord&)>
250 * @param callback the std::function or lambda to be called
251 * @note Its possible to reset the logging to console by setting the callback to a nullptr
252 */
253 template <typename Functor>
254 void
256 {
257 Logger::getInstance().setCallback(std::move(callback));
258 }
259
260 /**
261 * @brief insert values into a formatted string
262 * @param fmt_str string containing the format
263 * @param values to be inserted
264 * @return formatted string of type std::string
265 */
266 template <typename... Args>
267 std::string
268 to_string(const std::string fmt_str, ...)
269 {
270 int final_n, n = (static_cast<int>(fmt_str.size())) * 2; /* Reserve two times as much as the length of the fmt_str */
271 std::unique_ptr<char[]> formatted;
272 va_list ap;
273 while (true) {
274 formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */
275 va_start(ap, fmt_str);
276 final_n = vsnprintf(formatted.get(), n, fmt_str.c_str(), ap);
277 va_end(ap);
278 if (final_n < 0 || final_n >= n)
279 n += abs(final_n - n + 1);
280 else
281 break;
282 }
283 return {formatted.get()};
284 }
285
286 /** \brief Print an info message on stream with colors
287 * \param stream the output stream (stdout, stderr, etc)
288 * \param format the message
289 */
290 template <typename... Args>
291 PCL_EXPORTS void
292 print_info(FILE* stream, const std::string format, Args&&... args)
293 {
294 print(L_INFO, stream, format, std::forward<Args>(args)...);
295 }
296
297 /** \brief Print an info message on stream with colors
298 * \param format the message
299 */
300 template <typename... Args>
301 PCL_EXPORTS void
302 print_info(const std::string format, Args&&... args)
303 {
304 print_info(stdout, format, std::forward<Args>(args)...);
305 }
306
307 /** \brief Print an error message on stream with colors
308 * \param stream the output stream (stdout, stderr, etc)
309 * \param format the message
310 */
311 template <typename... Args>
312 PCL_EXPORTS void
313 print_error(FILE* stream, const std::string format, Args&&... args)
314 {
315 print(L_ERROR, stream, format, std::forward<Args>(args)...);
316 }
317
318 /** \brief Print an error message on stream with colors
319 * \param format the message
320 */
321 template <typename... Args>
322 PCL_EXPORTS void
323 print_error(std::string format, Args&&... args)
324 {
325 print_error(stderr, format, std::forward<Args>(args)...);
326 }
327
328 /** \brief Print a warning message on stream with colors
329 * \param stream the output stream (stdout, stderr, etc)
330 * \param format the message
331 */
332 template <typename... Args>
333 PCL_EXPORTS void
334 print_warn(FILE* stream, const std::string format, Args&&... args)
335 {
336 print(L_WARN, stream, format, std::forward<Args>(args)...);
337 }
338
339 /** \brief Print a warning message on stream with colors
340 * \param format the message
341 */
342 template <typename... Args>
343 PCL_EXPORTS void
344 print_warn(std::string format, Args&&... args)
345 {
346 print_warn(stderr, format, std::forward<Args>(args)...);
347 }
348
349 /** \brief Print a debug message on stream with colors
350 * \param stream the output stream (stdout, stderr, etc)
351 * \param format the message
352 */
353 template <typename... Args>
354 PCL_EXPORTS void
355 print_debug(FILE* stream, const std::string format, Args&&... args)
356 {
357 print(L_DEBUG, stream, format, std::forward<Args>(args)...);
358 }
359
360 /** \brief Print a debug message on stream with colors
361 * \param format the message
362 */
363 template <typename... Args>
364 PCL_EXPORTS void
365 print_debug(const std::string format, Args&&... args)
366 {
367 print_debug(stdout, format, std::forward<Args>(args)...);
368 }
369
370 /** \brief Print a message on stream with colors
371 * \param stream the output stream (stdout, stderr, etc)
372 * \param attr the text attribute
373 * \param fg the foreground color
374 * \param format the message
375 */
376 template <typename... Args>
377 PCL_EXPORTS void
378 print_color(FILE* stream, int attr, int fg, const std::string format, Args&&... args)
379 {
380 const auto str = to_string(format, std::forward<Args>(args)...);
381 LogRecord logEntry{L_ALWAYS, str};
382 Logger::getInstance().print_color(stream, attr, fg, logEntry);
383 }
384
385 /** \brief Print a value message on stream with colors
386 * \param stream the output stream (stdout, stderr, etc)
387 * \param format the message
388 */
389 template <typename... Args>
390 PCL_EXPORTS void
391 print_value(FILE* stream, const std::string format, Args&&... args)
392 {
393 const auto str = to_string(format, std::forward<Args>(args)...);
394 LogRecord logEntry{L_ALWAYS, str};
395 Logger::getInstance().print_value(stream, logEntry);
396 }
397
398 /** \brief Print a value message on stream with colors
399 * \param format the message
400 */
401 template <typename... Args>
402 PCL_EXPORTS void
403 print_value(const std::string format, Args&&... args)
404 {
405 print_value(stdout, format, std::forward<Args>(args)...);
406 }
407
408 /** \brief Print a message on stream
409 * \param level the verbosity level
410 * \param stream the output stream (stdout, stderr, etc)
411 * \param format the message
412 */
413 template <typename... Args>
414 PCL_EXPORTS void
415 print(VERBOSITY_LEVEL level, FILE* stream, const std::string format, Args&&...args)
416 {
418 return;
419
420 const auto str = to_string(format, std::forward<Args>(args)...);
421 LogRecord logEntry{level, str};
422 Logger::getInstance().print(stream, logEntry);
423 }
424
425 /**
426 * @brief Print a message
427 * @param level Verbosity level
428 * @param fmt_str the string containing the formatting
429 * @param ...args values to be inserted into the fmt_str
430 */
431 template <typename... Args>
432 void
433 print(VERBOSITY_LEVEL level, const std::string format, Args&&... args)
434 {
436 return;
437
438 const auto str = to_string(format, std::forward<Args>(args)...);
439 LogRecord logEntry{level, str};
440 Logger::getInstance().print(logEntry);
441 }
442
443 /** \brief Print a highlighted info message on stream with colors
444 * \param stream the output stream (stdout, stderr, etc)
445 * \param format the message
446 */
447 template <typename... Args>
448 PCL_EXPORTS void
449 print_highlight(FILE* stream, const std::string format, Args... args)
450 {
451 const auto str = to_string(format, std::forward<Args>(args)...);
452 LogRecord logEntry{L_INFO, str};
453 Logger::getInstance().print_highlight(stream,logEntry);
454 }
455
456
457 /** \brief Print a highlighted info message on stream with colors
458 * \param format the message
459 */
460 template <typename... Args>
461 PCL_EXPORTS void
462 print_highlight(const std::string format, Args&&...args)
463 {
464 print_highlight(stdout, format, std::forward<Args>(args)...);
465 }
466 }
467}
Logger used to log messages with different verbosity levels Can be used to redirect log messages to c...
Definition print.h:210
void print(const LogRecord &logEntry)
void setCallback(Functor &&callback)
Definition print.h:217
void print(FILE *stream, const LogRecord &logEntry)
void print_value(FILE *stream, const LogRecord &logEntry)
void print_value(const LogRecord &logEntry)
void print_highlight(FILE *stream, const LogRecord &logEntry)
void print_color(FILE *stream, int attr, int fg, const LogRecord &logEntry)
void print_highlight(const LogRecord &logEntry)
static Logger & getInstance()
PCL_EXPORTS void enableColoredOutput(FILE *stream, bool enable)
Enable or disable colored text output, overriding the default behavior.
void setCallback(Functor &&callback)
Sets a callable in the Logger instance, which is called whenever a printout has occured.
Definition print.h:255
PCL_EXPORTS void setVerbosityLevel(VERBOSITY_LEVEL level)
set the verbosity level
PCL_EXPORTS void print_color(FILE *stream, int attr, int fg, const std::string format, Args &&... args)
Print a message on stream with colors.
Definition print.h:378
PCL_EXPORTS void print_value(FILE *stream, const std::string format, Args &&... args)
Print a value message on stream with colors.
Definition print.h:391
PCL_EXPORTS void print_warn(FILE *stream, const std::string format, Args &&... args)
Print a warning message on stream with colors.
Definition print.h:334
PCL_EXPORTS bool isVerbosityLevelEnabled(VERBOSITY_LEVEL severity)
is verbosity level enabled?
PCL_EXPORTS void reset_text_color(FILE *stream)
Reset the text color (on either stdout or stderr) to its original state.
@ TT_UNDERLINE
Definition print.h:107
PCL_EXPORTS void print_debug(FILE *stream, const std::string format, Args &&... args)
Print a debug message on stream with colors.
Definition print.h:355
PCL_EXPORTS void change_text_color(FILE *stream, int attribute, int fg, int bg)
Change the text color (on either stdout or stderr) with an attr:fg:bg.
PCL_EXPORTS void print(VERBOSITY_LEVEL level, FILE *stream, const std::string format, Args &&...args)
Print a message on stream.
Definition print.h:415
std::string to_string(const std::string fmt_str,...)
insert values into a formatted string
Definition print.h:268
PCL_EXPORTS void print_error(FILE *stream, const std::string format, Args &&... args)
Print an error message on stream with colors.
Definition print.h:313
PCL_EXPORTS VERBOSITY_LEVEL getVerbosityLevel()
get the verbosity level.
PCL_EXPORTS void print_highlight(FILE *stream, const std::string format, Args... args)
Print a highlighted info message on stream with colors.
Definition print.h:449
PCL_EXPORTS bool initVerbosityLevel()
initialize verbosity level.
PCL_EXPORTS void print_info(FILE *stream, const std::string format, Args &&... args)
Print an info message on stream with colors.
Definition print.h:292
Base functor all the models that need non linear optimization must define their own one and implement...
Definition sac_model.h:680
Structure to hold a log record Used by the Logger class.
Definition print.h:138
LogRecord(VERBOSITY_LEVEL lvl, const std::string &str)
Definition print.h:141
VERBOSITY_LEVEL level
Definition print.h:152
LogRecord(const LogRecord &other)=default
std::string message
Definition print.h:153
LogRecord(VERBOSITY_LEVEL lvl, std::string &&str)
Definition print.h:144