00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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;
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
00099 #define MAX_BUFFER_SIZE 1024
00100
00101
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 }