$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * $Id: print.cpp 30706 2010-07-09 18:20:31Z rusu $ 00035 * 00036 */ 00037 #include "terminal_tools/print.h" 00038 00040 00046 void 00047 terminal_tools::change_text_color (FILE *stream, int attribute, int fg, int bg) 00048 { 00049 char command[13]; 00050 // Command is the control command to the terminal 00051 sprintf (command, "%c[%d;%d;%dm", 0x1B, attribute, fg + 30, bg + 40); 00052 fprintf (stream, "%s", command); 00053 } 00054 00056 00061 void 00062 terminal_tools::change_text_color (FILE *stream, int attribute, int fg) 00063 { 00064 char command[13]; 00065 // Command is the control command to the terminal 00066 sprintf (command, "%c[%d;%dm", 0x1B, attribute, fg + 30); 00067 fprintf (stream, "%s", command); 00068 } 00069 00071 00074 void 00075 terminal_tools::reset_text_color (FILE *stream) 00076 { 00077 char command[13]; 00078 // Command is the control command to the terminal 00079 sprintf (command, "%c[0;m", 0x1B); 00080 fprintf (stream, "%s", command); 00081 } 00082 00084 00090 void 00091 terminal_tools::print_color (FILE *stream, int attr, int fg, const char *format, ...) 00092 { 00093 change_text_color (stream, attr, fg); 00094 va_list ap; 00095 00096 va_start (ap, format); 00097 vfprintf (stream, format, ap); 00098 va_end (ap); 00099 00100 reset_text_color (stream); 00101 } 00102 00104 00107 void 00108 terminal_tools::print_info (const char *format, ...) 00109 { 00110 reset_text_color (stdout); 00111 00112 va_list ap; 00113 00114 va_start (ap, format); 00115 vfprintf (stdout, format, ap); 00116 va_end (ap); 00117 } 00118 00120 00124 void 00125 terminal_tools::print_info (FILE *stream, const char *format, ...) 00126 { 00127 reset_text_color (stream); 00128 00129 va_list ap; 00130 00131 va_start (ap, format); 00132 vfprintf (stream, format, ap); 00133 va_end (ap); 00134 } 00135 00137 00140 void 00141 terminal_tools::print_highlight (const char *format, ...) 00142 { 00143 change_text_color (stdout, TT_BRIGHT, TT_GREEN); 00144 fprintf (stdout, "> "); 00145 reset_text_color (stdout); 00146 00147 va_list ap; 00148 00149 va_start (ap, format); 00150 vfprintf (stdout, format, ap); 00151 va_end (ap); 00152 } 00153 00155 00159 void 00160 terminal_tools::print_highlight (FILE *stream, const char *format, ...) 00161 { 00162 change_text_color (stream, TT_BRIGHT, TT_GREEN); 00163 fprintf (stream, "> "); 00164 reset_text_color (stream); 00165 00166 va_list ap; 00167 00168 va_start (ap, format); 00169 vfprintf (stream, format, ap); 00170 va_end (ap); 00171 } 00172 00174 00177 void 00178 terminal_tools::print_error (const char *format, ...) 00179 { 00180 change_text_color (stderr, TT_RESET, TT_RED); 00181 va_list ap; 00182 00183 va_start (ap, format); 00184 vfprintf (stderr, format, ap); 00185 va_end (ap); 00186 00187 reset_text_color (stderr); 00188 } 00189 00191 00195 void 00196 terminal_tools::print_error (FILE *stream, const char *format, ...) 00197 { 00198 change_text_color (stream, TT_RESET, TT_RED); 00199 va_list ap; 00200 00201 va_start (ap, format); 00202 vfprintf (stream, format, ap); 00203 va_end (ap); 00204 00205 reset_text_color (stream); 00206 } 00207 00209 00212 void 00213 terminal_tools::print_warn (const char *format, ...) 00214 { 00215 change_text_color (stderr, TT_RESET, TT_YELLOW); 00216 va_list ap; 00217 00218 va_start (ap, format); 00219 vfprintf (stderr, format, ap); 00220 va_end (ap); 00221 00222 reset_text_color (stderr); 00223 } 00224 00226 00230 void 00231 terminal_tools::print_warn (FILE *stream, const char *format, ...) 00232 { 00233 change_text_color (stream, TT_RESET, TT_YELLOW); 00234 va_list ap; 00235 00236 va_start (ap, format); 00237 vfprintf (stream, format, ap); 00238 va_end (ap); 00239 00240 reset_text_color (stream); 00241 } 00242 00244 00247 void 00248 terminal_tools::print_value (const char *format, ...) 00249 { 00250 change_text_color (stdout, TT_RESET, TT_CYAN); 00251 va_list ap; 00252 00253 va_start (ap, format); 00254 vfprintf (stdout, format, ap); 00255 va_end (ap); 00256 00257 reset_text_color (stdout); 00258 } 00259 00261 00265 void 00266 terminal_tools::print_value (FILE *stream, const char *format, ...) 00267 { 00268 change_text_color (stream, TT_RESET, TT_CYAN); 00269 va_list ap; 00270 00271 va_start (ap, format); 00272 vfprintf (stream, format, ap); 00273 va_end (ap); 00274 00275 reset_text_color (stream); 00276 } 00277 00279 00282 void 00283 terminal_tools::print_debug (const char *format, ...) 00284 { 00285 change_text_color (stdout, TT_RESET, TT_GREEN); 00286 va_list ap; 00287 00288 va_start (ap, format); 00289 vfprintf (stdout, format, ap); 00290 va_end (ap); 00291 00292 reset_text_color (stdout); 00293 } 00294 00296 00300 void 00301 terminal_tools::print_debug (FILE *stream, const char *format, ...) 00302 { 00303 change_text_color (stream, TT_RESET, TT_GREEN); 00304 va_list ap; 00305 00306 va_start (ap, format); 00307 vfprintf (stream, format, ap); 00308 va_end (ap); 00309 00310 reset_text_color (stream); 00311 } 00312