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 
42 #include <pcl/pcl_exports.h>
43 #include <pcl/pcl_config.h>
44 
45 // Use e.g. like this:
46 // PCL_INFO_STREAM("Info: this is a point: " << pcl::PointXYZ(1.0, 2.0, 3.0) << std::endl);
47 // PCL_ERROR_STREAM("Error: an Eigen vector: " << std::endl << Eigen::Vector3f(1.0, 2.0, 3.0) << std::endl);
48 #define PCL_LOG_STREAM(LEVEL, STREAM, CSTR, ATTR, FG, ARGS) if(pcl::console::isVerbosityLevelEnabled(pcl::console::LEVEL)) { fflush(stdout); pcl::console::change_text_color(CSTR, pcl::console::ATTR, pcl::console::FG); STREAM << ARGS; pcl::console::reset_text_color(CSTR); }
49 #define PCL_ALWAYS_STREAM(ARGS)  PCL_LOG_STREAM(L_ALWAYS,  std::cout, stdout, TT_RESET,  TT_WHITE,  ARGS)
50 #define PCL_ERROR_STREAM(ARGS)   PCL_LOG_STREAM(L_ERROR,   std::cerr, stderr, TT_BRIGHT, TT_RED,    ARGS)
51 #define PCL_WARN_STREAM(ARGS)    PCL_LOG_STREAM(L_WARN,    std::cerr, stderr, TT_BRIGHT, TT_YELLOW, ARGS)
52 #define PCL_INFO_STREAM(ARGS)    PCL_LOG_STREAM(L_INFO,    std::cout, stdout, TT_RESET,  TT_WHITE,  ARGS)
53 #define PCL_DEBUG_STREAM(ARGS)   PCL_LOG_STREAM(L_DEBUG,   std::cout, stdout, TT_RESET,  TT_GREEN,  ARGS)
54 #define PCL_VERBOSE_STREAM(ARGS) PCL_LOG_STREAM(L_VERBOSE, std::cout, stdout, TT_RESET,  TT_WHITE,  ARGS)
55 
56 
57 #define PCL_ALWAYS(...)  pcl::console::print (pcl::console::L_ALWAYS, __VA_ARGS__)
58 #define PCL_ERROR(...)   pcl::console::print (pcl::console::L_ERROR, __VA_ARGS__)
59 #define PCL_WARN(...)    pcl::console::print (pcl::console::L_WARN, __VA_ARGS__)
60 #define PCL_INFO(...)    pcl::console::print (pcl::console::L_INFO, __VA_ARGS__)
61 #define PCL_DEBUG(...)   pcl::console::print (pcl::console::L_DEBUG, __VA_ARGS__)
62 #define PCL_VERBOSE(...) pcl::console::print (pcl::console::L_VERBOSE, __VA_ARGS__)
63 
64 #define PCL_ASSERT_ERROR_PRINT_CHECK(pred, msg) \
65     do \
66     { \
67         if (!(pred)) \
68         { \
69             PCL_ERROR(msg); \
70             PCL_ERROR("In File %s, in line %d\n" __FILE__, __LINE__); \
71         } \
72     } while (0)
73 
74 #define PCL_ASSERT_ERROR_PRINT_RETURN(pred, msg, err) \
75     do \
76     { \
77         PCL_ASSERT_ERROR_PRINT_CHECK(pred, msg); \
78         if (!(pred)) return err; \
79     } while (0)
80 
81 namespace pcl
82 {
83   namespace console
84   {
85     enum TT_ATTIBUTES
86     {
87       TT_RESET     = 0,
88       TT_BRIGHT    = 1,
89       TT_DIM       = 2,
90       TT_UNDERLINE = 3,
91       TT_BLINK     = 4,
92       TT_REVERSE   = 7,
93       TT_HIDDEN    = 8
94     };
95 
96     enum TT_COLORS
97     {
98       TT_BLACK,
99       TT_RED,
100       TT_GREEN,
101       TT_YELLOW,
102       TT_BLUE,
103       TT_MAGENTA,
104       TT_CYAN,
105       TT_WHITE
106     };
107 
108     enum VERBOSITY_LEVEL
109     {
110       L_ALWAYS,
111       L_ERROR,
112       L_WARN,
113       L_INFO,
114       L_DEBUG,
115       L_VERBOSE
116     };
117 
118     /** set the verbosity level */
119     PCL_EXPORTS void
120     setVerbosityLevel (VERBOSITY_LEVEL level);
121 
122     /** get the verbosity level. */
123     PCL_EXPORTS VERBOSITY_LEVEL
124     getVerbosityLevel ();
125 
126     /** initialize verbosity level. */
127     PCL_EXPORTS bool
128     initVerbosityLevel ();
129 
130     /** is verbosity level enabled? */
131     PCL_EXPORTS bool
132     isVerbosityLevelEnabled (VERBOSITY_LEVEL severity);
133 
134     /** \brief Enable or disable colored text output, overriding the default behavior.
135       *
136       * By default, colored output is enabled for interactive terminals or when the environment
137       * variable PCL_CLICOLOR_FORCE is set.
138       *
139       * \param stream the output stream (stdout, stderr, etc)
140       * \param enable whether to emit color codes when calling any of the color related methods
141       */
142     PCL_EXPORTS void
143     enableColoredOutput (FILE *stream, bool enable);
144 
145     /** \brief Change the text color (on either stdout or stderr) with an attr:fg:bg
146       * \param stream the output stream (stdout, stderr, etc)
147       * \param attribute the text attribute
148       * \param fg the foreground color
149       * \param bg the background color
150       */
151     PCL_EXPORTS void
152     change_text_color (FILE *stream, int attribute, int fg, int bg);
153 
154     /** \brief Change the text color (on either stdout or stderr) with an attr:fg
155       * \param stream the output stream (stdout, stderr, etc)
156       * \param attribute the text attribute
157       * \param fg the foreground color
158       */
159     PCL_EXPORTS void
160     change_text_color (FILE *stream, int attribute, int fg);
161 
162     /** \brief Reset the text color (on either stdout or stderr) to its original state
163       * \param stream the output stream (stdout, stderr, etc)
164       */
165     PCL_EXPORTS void
166     reset_text_color (FILE *stream);
167 
168     /** \brief Print a message on stream with colors
169       * \param stream the output stream (stdout, stderr, etc)
170       * \param attr the text attribute
171       * \param fg the foreground color
172       * \param format the message
173       */
174     PCL_EXPORTS void
175     print_color (FILE *stream, int attr, int fg, const char *format, ...);
176 
177     /** \brief Print an info message on stream with colors
178       * \param format the message
179       */
180     PCL_EXPORTS void
181     print_info  (const char *format, ...);
182 
183     /** \brief Print an info message on stream with colors
184       * \param stream the output stream (stdout, stderr, etc)
185       * \param format the message
186       */
187     PCL_EXPORTS void
188     print_info  (FILE *stream, const char *format, ...);
189 
190     /** \brief Print a highlighted info message on stream with colors
191       * \param format the message
192       */
193     PCL_EXPORTS void
194     print_highlight  (const char *format, ...);
195 
196     /** \brief Print a highlighted info message on stream with colors
197       * \param stream the output stream (stdout, stderr, etc)
198       * \param format the message
199       */
200     PCL_EXPORTS void
201     print_highlight  (FILE *stream, const char *format, ...);
202 
203     /** \brief Print an error message on stream with colors
204       * \param format the message
205       */
206     PCL_EXPORTS void
207     print_error (const char *format, ...);
208 
209     /** \brief Print an error message on stream with colors
210       * \param stream the output stream (stdout, stderr, etc)
211       * \param format the message
212       */
213     PCL_EXPORTS void
214     print_error (FILE *stream, const char *format, ...);
215 
216     /** \brief Print a warning message on stream with colors
217       * \param format the message
218       */
219     PCL_EXPORTS void
220     print_warn (const char *format, ...);
221 
222     /** \brief Print a warning message on stream with colors
223       * \param stream the output stream (stdout, stderr, etc)
224       * \param format the message
225       */
226     PCL_EXPORTS void
227     print_warn (FILE *stream, const char *format, ...);
228 
229     /** \brief Print a debug message on stream with colors
230       * \param format the message
231       */
232     PCL_EXPORTS void
233     print_debug (const char *format, ...);
234 
235     /** \brief Print a debug message on stream with colors
236       * \param stream the output stream (stdout, stderr, etc)
237       * \param format the message
238       */
239     PCL_EXPORTS void
240     print_debug (FILE *stream, const char *format, ...);
241 
242 
243     /** \brief Print a value message on stream with colors
244       * \param format the message
245       */
246     PCL_EXPORTS void
247     print_value (const char *format, ...);
248 
249     /** \brief Print a value message on stream with colors
250       * \param stream the output stream (stdout, stderr, etc)
251       * \param format the message
252       */
253     PCL_EXPORTS void
254     print_value (FILE *stream, const char *format, ...);
255 
256     /** \brief Print a message on stream
257       * \param level the verbosity level
258       * \param stream the output stream (stdout, stderr, etc)
259       * \param format the message
260       */
261     PCL_EXPORTS void
262     print (VERBOSITY_LEVEL level, FILE *stream, const char *format, ...);
263 
264     /** \brief Print a message
265       * \param level the verbosity level
266       * \param format the message
267       */
268     PCL_EXPORTS void
269     print (VERBOSITY_LEVEL level, const char *format, ...);
270   }
271 }
272