All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
Console.cpp
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, 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 the Willow Garage 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 
00035 /* Author: Ioan Sucan */
00036 
00037 #include "ompl/util/Console.h"
00038 #include <boost/thread/mutex.hpp>
00039 #include <iostream>
00040 #include <cstdlib>
00041 #include <cstdio>
00042 #include <cstdarg>
00043 
00044 static ompl::msg::OutputHandlerSTD _defaultOutputHandler;
00045 static ompl::msg::OutputHandler   *OUTPUT_HANDLER = static_cast<ompl::msg::OutputHandler*>(&_defaultOutputHandler);
00046 static ompl::msg::OutputHandler   *PREVIOUS_OH = OUTPUT_HANDLER;
00047 static boost::mutex                _lock; // it is likely the outputhandler does some I/O, so we serialize it
00048 
00049 void ompl::msg::noOutputHandler(void)
00050 {
00051     _lock.lock();
00052     PREVIOUS_OH = OUTPUT_HANDLER;
00053     OUTPUT_HANDLER = NULL;
00054     _lock.unlock();
00055 }
00056 
00057 void ompl::msg::restorePreviousOutputHandler(void)
00058 {
00059     _lock.lock();
00060     std::swap(PREVIOUS_OH, OUTPUT_HANDLER);
00061     _lock.unlock();
00062 }
00063 
00064 void ompl::msg::useOutputHandler(OutputHandler *oh)
00065 {
00066     _lock.lock();
00067     PREVIOUS_OH = OUTPUT_HANDLER;
00068     OUTPUT_HANDLER = oh;
00069     _lock.unlock();
00070 }
00071 
00072 ompl::msg::OutputHandler* ompl::msg::getOutputHandler(void)
00073 {
00074     return OUTPUT_HANDLER;
00075 }
00076 
00077 ompl::msg::Interface::Interface(const std::string &prefix)
00078 {
00079     setPrefix(prefix);
00080 }
00081 
00082 ompl::msg::Interface::~Interface(void)
00083 {
00084 }
00085 
00086 const std::string& ompl::msg::Interface::getPrefix(void) const
00087 {
00088     return prefix_;
00089 }
00090 
00091 void ompl::msg::Interface::setPrefix(const std::string &prefix)
00092 {
00093     prefix_ = prefix;
00094     if (!prefix_.empty())
00095         prefix_ += ": ";
00096 }
00097 
00098 // the maximum buffer size to use when printing a message
00099 #define MAX_BUFFER_SIZE 1024
00100 
00101 // macro that combines the set of arguments into a single string
00102 #define COMBINE(m, buf, size)                                                \
00103     va_list __ap;                                                        \
00104     va_start(__ap, m);                                                        \
00105     char buf##_chr[size];                                                \
00106     vsnprintf(buf##_chr, sizeof(buf##_chr), m, __ap);                        \
00107     va_end(__ap);                                                        \
00108     std::string buf(buf##_chr)
00109 
00110 void ompl::msg::Interface::debug(const std::string &text) const
00111 {
00112     if (OUTPUT_HANDLER)
00113     {
00114         _lock.lock();
00115         OUTPUT_HANDLER->debug(prefix_ + text);
00116         _lock.unlock();
00117     }
00118 }
00119 
00120 void ompl::msg::Interface::debug(const char *msg, ...) const
00121 {
00122     COMBINE(msg, buf, MAX_BUFFER_SIZE);
00123     debug(buf);
00124 }
00125 
00126 void ompl::msg::Interface::inform(const std::string &text) const
00127 {
00128     if (OUTPUT_HANDLER)
00129     {
00130         _lock.lock();
00131         OUTPUT_HANDLER->inform(prefix_ + text);
00132         _lock.unlock();
00133     }
00134 }
00135 
00136 void ompl::msg::Interface::inform(const char *msg, ...) const
00137 {
00138     COMBINE(msg, buf, MAX_BUFFER_SIZE);
00139     inform(buf);
00140 }
00141 
00142 void ompl::msg::Interface::warn(const std::string &text) const
00143 {
00144     if (OUTPUT_HANDLER)
00145     {
00146         _lock.lock();
00147         OUTPUT_HANDLER->warn(prefix_ + text);
00148         _lock.unlock();
00149     }
00150 }
00151 
00152 void ompl::msg::Interface::warn(const char *msg, ...) const
00153 {
00154     COMBINE(msg, buf, MAX_BUFFER_SIZE);
00155     warn(buf);
00156 }
00157 
00158 void ompl::msg::Interface::error(const std::string &text) const
00159 {
00160     if (OUTPUT_HANDLER)
00161     {
00162         _lock.lock();
00163         OUTPUT_HANDLER->error(prefix_ + text);
00164         _lock.unlock();
00165     }
00166 }
00167 
00168 void ompl::msg::Interface::error(const char *msg, ...) const
00169 {
00170     COMBINE(msg, buf, MAX_BUFFER_SIZE);
00171     error(buf);
00172 }
00173 
00174 void ompl::msg::OutputHandlerSTD::error(const std::string &text)
00175 {
00176     std::cerr << "Error:   " << text << std::endl;
00177     std::cerr.flush();
00178 }
00179 
00180 void ompl::msg::OutputHandlerSTD::warn(const std::string &text)
00181 {
00182     std::cerr << "Warning: " << text << std::endl;
00183     std::cerr.flush();
00184 }
00185 
00186 void ompl::msg::OutputHandlerSTD::inform(const std::string &text)
00187 {
00188     std::cout << "Info:    " << text << std::endl;
00189     std::cout.flush();
00190 }
00191 
00192 void ompl::msg::OutputHandlerSTD::debug(const std::string &text)
00193 {
00194     std::cout << "Debug:   " << text << std::endl;
00195     std::cout.flush();
00196 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator