Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/logger/console_logger.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 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 Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived 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 // @author Brett Ponsler (bponsler (at) wpi (dot) edu) 00035 #include "console_logger.h" 00036 00037 #include <ros/ros.h> 00038 #include <sstream> 00039 #include <stdio.h> 00040 00041 #include "../lib/engagement_params.h" 00042 00043 std::string ConsoleLogger::start_color = "\033["; 00044 std::string ConsoleLogger::end_color = "\033[0m"; 00045 std::string ConsoleLogger::DEFAULT = "0m"; 00046 std::string ConsoleLogger::RED = "31m"; 00047 std::string ConsoleLogger::GREEN = "32m"; 00048 std::string ConsoleLogger::YELLOW = "33m"; 00049 std::string ConsoleLogger::BLUE = "34m"; 00050 std::string ConsoleLogger::PURPLE = "35m"; 00051 std::string ConsoleLogger::CYAN = "36m"; 00052 std::string ConsoleLogger::BRIGHT_RED = "91m"; 00053 std::string ConsoleLogger::BRIGHT_GREEN = "92m"; 00054 std::string ConsoleLogger::BRIGHT_YELLOW = "93m"; 00055 std::string ConsoleLogger::BRIGHT_BLUE = "94m"; 00056 std::string ConsoleLogger::BRIGHT_PURPLE = "95m"; 00057 std::string ConsoleLogger::BRIGHT_CYAN = "96m"; 00058 00059 ConsoleLogger::ConsoleLogger() 00060 { 00061 this->verbosity_ = EngagementParams::getVerbosity(); 00062 this->prefix_ = std::string(""); 00063 } 00064 00065 ConsoleLogger::ConsoleLogger(std::string prefix) 00066 { 00067 this->verbosity_ = EngagementParams::getVerbosity(); 00068 this->prefix_ = std::string(prefix); 00069 } 00070 00071 ConsoleLogger::ConsoleLogger(ConsoleLogger *logger, std::string prefix) 00072 { 00073 this->verbosity_ = EngagementParams::getVerbosity(); 00074 this->prefix_ = std::string(prefix); 00075 00076 // If the logger exists, then append its prefix before ours 00077 if (logger != NULL) 00078 this->prefix_ = std::string(logger->prefix_ + this->prefix_); 00079 } 00080 00081 ConsoleLogger::ConsoleLogger(std::string prefix, int color) 00082 { 00083 this->verbosity_ = EngagementParams::getVerbosity(); 00084 // Create the prefix for this logger 00085 this->createPrefix(prefix, color); 00086 } 00087 00088 ConsoleLogger::ConsoleLogger(ConsoleLogger *logger, std::string prefix, 00089 int color) 00090 { 00091 this->verbosity_ = EngagementParams::getVerbosity(); 00092 // Create the prefix for this logger 00093 this->createPrefix(prefix, color); 00094 00095 // Now append the base logger's prefix before our prefix 00096 // so long as the console logger exists 00097 if (logger != NULL) 00098 this->prefix_ = std::string(logger->prefix_ + this->prefix_); 00099 } 00100 00101 ConsoleLogger::ConsoleLogger(ConsoleLogger *logger) 00102 { 00103 this->verbosity_ = EngagementParams::getVerbosity(); 00104 // Make sure the given logger exists 00105 if (logger != NULL) 00106 { 00107 // Our prefix is the same as the logger's prefix 00108 this->prefix_ = std::string(logger->prefix_); 00109 } 00110 else 00111 this->prefix_ = std::string(""); 00112 } 00113 00114 void ConsoleLogger::createPrefix(std::string prefix, int color) 00115 { 00116 // Make sure the prefix exists 00117 if (prefix.size() > 0) 00118 { 00119 // Convert the color into a string 00120 std::stringstream ss; 00121 ss << color; 00122 00123 // Create the prefix using the color 00124 this->prefix_ = ConsoleLogger::start_color + ss.str() + 00125 std::string(prefix) + ConsoleLogger::end_color; 00126 } 00127 else 00128 this->prefix_ = std::string(""); 00129 } 00130 00131 const char *ConsoleLogger::getPrefix() 00132 { 00133 return this->prefix_.c_str(); 00134 } 00135 00136 void ConsoleLogger::debug(int level, const char *str, va_list args) 00137 { 00138 // Determine if the verbosity level is high enough 00139 if (level <= this->verbosity_) 00140 { 00141 // Create a buffer to hold the formatted string 00142 char buff[4096]; 00143 00144 // Print all the arguments to the buffer in the formatted string 00145 vsnprintf(buff, 4096, str, args); 00146 00147 pthread_t thread = pthread_self(); 00148 00149 // Append the correct color around the string 00150 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00151 ConsoleLogger::GREEN + buff + ConsoleLogger::end_color; 00152 00153 ROS_DEBUG("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00154 } 00155 } 00156 00157 void ConsoleLogger::info(const char *str, va_list args) 00158 { 00159 // Create a buffer to hold the formatted string 00160 char buff[4096]; 00161 00162 // Print all the arguments to the buffer in the formatted string 00163 vsnprintf(buff, 4096, str, args); 00164 00165 pthread_t thread = pthread_self(); 00166 00167 // Append the correct color around the string 00168 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00169 ConsoleLogger::DEFAULT + buff + ConsoleLogger::end_color; 00170 00171 ROS_INFO("[THREAD %d] %s", (unsigned int)thread, temp.c_str()); 00172 } 00173 00174 void ConsoleLogger::warn(const char *str, va_list args) 00175 { 00176 // Create a buffer to hold the formatted string 00177 char buff[4096]; 00178 00179 // Print all the arguments to the buffer in the formatted string 00180 vsnprintf(buff, 4096, str, args); 00181 00182 pthread_t thread = pthread_self(); 00183 00184 // Append the correct color around the string 00185 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00186 ConsoleLogger::YELLOW + buff + ConsoleLogger::end_color; 00187 00188 ROS_WARN("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00189 } 00190 00191 void ConsoleLogger::error(const char *str, va_list args) 00192 { 00193 // Create a buffer to hold the formatted string 00194 char buff[4096]; 00195 00196 // Print all the arguments to the buffer in the formatted string 00197 vsnprintf(buff, 4096, str, args); 00198 00199 pthread_t thread = pthread_self(); 00200 00201 // Append the correct color around the string 00202 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00203 ConsoleLogger::RED + buff + ConsoleLogger::end_color; 00204 00205 ROS_ERROR("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00206 } 00207 00208 void ConsoleLogger::fatal(const char *str, va_list args) 00209 { 00210 // Create a buffer to hold the formatted string 00211 char buff[4096]; 00212 00213 // Print all the arguments to the buffer in the formatted string 00214 vsnprintf(buff, 4096, str, args); 00215 00216 pthread_t thread = pthread_self(); 00217 00218 // Append the correct color around the string 00219 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00220 ConsoleLogger::RED + buff + ConsoleLogger::end_color; 00221 00222 ROS_FATAL("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00223 } 00224 00225 void ConsoleLogger::red(const char *str, va_list args) 00226 { 00227 // Create a buffer to hold the formatted string 00228 char buff[4096]; 00229 00230 // Print all the arguments to the buffer in the formatted string 00231 vsnprintf(buff, 4096, str, args); 00232 00233 pthread_t thread = pthread_self(); 00234 00235 // Append the correct color around the string 00236 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00237 ConsoleLogger::RED + buff + ConsoleLogger::end_color; 00238 00239 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00240 } 00241 00242 void ConsoleLogger::green(const char *str, va_list args) 00243 { 00244 // Create a buffer to hold the formatted string 00245 char buff[4096]; 00246 00247 // Print all the arguments to the buffer in the formatted string 00248 vsnprintf(buff, 4096, str, args); 00249 00250 pthread_t thread = pthread_self(); 00251 00252 // Append the correct color around the string 00253 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00254 ConsoleLogger::GREEN + buff + ConsoleLogger::end_color; 00255 00256 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00257 } 00258 00259 void ConsoleLogger::yellow(const char *str, va_list args) 00260 { 00261 // Create a buffer to hold the formatted string 00262 char buff[4096]; 00263 00264 // Print all the arguments to the buffer in the formatted string 00265 vsnprintf(buff, 4096, str, args); 00266 00267 pthread_t thread = pthread_self(); 00268 00269 // Append the correct color around the string 00270 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00271 ConsoleLogger::YELLOW + buff + ConsoleLogger::end_color; 00272 00273 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00274 } 00275 00276 void ConsoleLogger::blue(const char *str, va_list args) 00277 { 00278 // Create a buffer to hold the formatted string 00279 char buff[4096]; 00280 00281 // Print all the arguments to the buffer in the formatted string 00282 vsnprintf(buff, 4096, str, args); 00283 00284 pthread_t thread = pthread_self(); 00285 00286 // Append the correct color around the string 00287 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00288 ConsoleLogger::BLUE + buff + ConsoleLogger::end_color; 00289 00290 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00291 } 00292 00293 void ConsoleLogger::purple(const char *str, va_list args) 00294 { 00295 // Create a buffer to hold the formatted string 00296 char buff[4096]; 00297 00298 // Print all the arguments to the buffer in the formatted string 00299 vsnprintf(buff, 4096, str, args); 00300 00301 pthread_t thread = pthread_self(); 00302 00303 // Append the correct color around the string 00304 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00305 ConsoleLogger::PURPLE + buff + ConsoleLogger::end_color; 00306 00307 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00308 } 00309 00310 void ConsoleLogger::cyan(const char *str, va_list args) 00311 { 00312 // Create a buffer to hold the formatted string 00313 char buff[4096]; 00314 00315 // Print all the arguments to the buffer in the formatted string 00316 vsnprintf(buff, 4096, str, args); 00317 00318 pthread_t thread = pthread_self(); 00319 00320 // Append the correct color around the string 00321 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00322 ConsoleLogger::CYAN + buff + ConsoleLogger::end_color; 00323 00324 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00325 } 00326 00327 void ConsoleLogger::brightRed(const char *str, va_list args) 00328 { 00329 // Create a buffer to hold the formatted string 00330 char buff[4096]; 00331 00332 // Print all the arguments to the buffer in the formatted string 00333 vsnprintf(buff, 4096, str, args); 00334 00335 pthread_t thread = pthread_self(); 00336 00337 // Append the correct color around the string 00338 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00339 ConsoleLogger::BRIGHT_RED + buff + ConsoleLogger::end_color; 00340 00341 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00342 } 00343 00344 void ConsoleLogger::brightGreen(const char *str, va_list args) 00345 { 00346 // Create a buffer to hold the formatted string 00347 char buff[4096]; 00348 00349 // Print all the arguments to the buffer in the formatted string 00350 vsnprintf(buff, 4096, str, args); 00351 00352 pthread_t thread = pthread_self(); 00353 00354 // Append the correct color around the string 00355 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00356 ConsoleLogger::BRIGHT_GREEN + buff + ConsoleLogger::end_color; 00357 00358 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00359 } 00360 00361 void ConsoleLogger::brightYellow(const char *str, va_list args) 00362 { 00363 // Create a buffer to hold the formatted string 00364 char buff[4096]; 00365 00366 // Print all the arguments to the buffer in the formatted string 00367 vsnprintf(buff, 4096, str, args); 00368 00369 pthread_t thread = pthread_self(); 00370 00371 // Append the correct color around the string 00372 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00373 ConsoleLogger::BRIGHT_YELLOW + buff + ConsoleLogger::end_color; 00374 00375 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00376 } 00377 00378 void ConsoleLogger::brightBlue(const char *str, va_list args) 00379 { 00380 // Create a buffer to hold the formatted string 00381 char buff[4096]; 00382 00383 // Print all the arguments to the buffer in the formatted string 00384 vsnprintf(buff, 4096, str, args); 00385 00386 pthread_t thread = pthread_self(); 00387 00388 // Append the correct color around the string 00389 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00390 ConsoleLogger::BRIGHT_BLUE + buff + ConsoleLogger::end_color; 00391 00392 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00393 } 00394 00395 void ConsoleLogger::brightPurple(const char *str, va_list args) 00396 { 00397 // Create a buffer to hold the formatted string 00398 char buff[4096]; 00399 00400 // Print all the arguments to the buffer in the formatted string 00401 vsnprintf(buff, 4096, str, args); 00402 00403 pthread_t thread = pthread_self(); 00404 00405 // Append the correct color around the string 00406 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00407 ConsoleLogger::BRIGHT_PURPLE + buff + ConsoleLogger::end_color; 00408 00409 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00410 } 00411 00412 void ConsoleLogger::brightCyan(const char *str, va_list args) 00413 { 00414 // Create a buffer to hold the formatted string 00415 char buff[4096]; 00416 00417 // Print all the arguments to the buffer in the formatted string 00418 vsnprintf(buff, 4096, str, args); 00419 00420 pthread_t thread = pthread_self(); 00421 00422 // Append the correct color around the string 00423 std::string temp = this->prefix_ + ConsoleLogger::start_color + 00424 ConsoleLogger::BRIGHT_CYAN + buff + ConsoleLogger::end_color; 00425 00426 ROS_INFO("[THREAD: %u] %s", (unsigned int)thread, temp.c_str()); 00427 } |