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 <cstdio>
00041 #include <cstdarg>
00042
00044
00045 struct DefaultOutputHandler
00046 {
00047 DefaultOutputHandler(void)
00048 {
00049 output_handler_ = static_cast<ompl::msg::OutputHandler*>(&std_output_handler_);
00050 previous_output_handler_ = output_handler_;
00051 logLevel_ = ompl::msg::LOG_DEBUG;
00052 }
00053
00054 ompl::msg::OutputHandlerSTD std_output_handler_;
00055 ompl::msg::OutputHandler *output_handler_;
00056 ompl::msg::OutputHandler *previous_output_handler_;
00057 ompl::msg::LogLevel logLevel_;
00058 boost::mutex lock_;
00059 };
00060
00061
00062
00063
00064
00065 static DefaultOutputHandler* getDOH(void)
00066 {
00067 static DefaultOutputHandler DOH;
00068 return &DOH;
00069 }
00070
00071 #define USE_DOH \
00072 DefaultOutputHandler *doh = getDOH(); \
00073 boost::mutex::scoped_lock slock(doh->lock_)
00074
00075 #define MAX_BUFFER_SIZE 1024
00076
00078
00079 void ompl::msg::noOutputHandler(void)
00080 {
00081 USE_DOH;
00082 doh->previous_output_handler_ = doh->output_handler_;
00083 doh->output_handler_ = NULL;
00084 }
00085
00086 void ompl::msg::restorePreviousOutputHandler(void)
00087 {
00088 USE_DOH;
00089 std::swap(doh->previous_output_handler_, doh->output_handler_);
00090 }
00091
00092 void ompl::msg::useOutputHandler(OutputHandler *oh)
00093 {
00094 USE_DOH;
00095 doh->previous_output_handler_ = doh->output_handler_;
00096 doh->output_handler_ = oh;
00097 }
00098
00099 ompl::msg::OutputHandler* ompl::msg::getOutputHandler(void)
00100 {
00101 return getDOH()->output_handler_;
00102 }
00103
00104 void ompl::msg::log(const char *file, int line, LogLevel level, const char* m, ...)
00105 {
00106 USE_DOH;
00107 if (doh->output_handler_ && level >= doh->logLevel_)
00108 {
00109 va_list __ap;
00110 va_start(__ap, m);
00111 char buf[MAX_BUFFER_SIZE];
00112 vsnprintf(buf, sizeof(buf), m, __ap);
00113 va_end(__ap);
00114 buf[MAX_BUFFER_SIZE - 1] = '\0';
00115
00116 doh->output_handler_->log(buf, level, file, line);
00117 }
00118 }
00119
00120 void ompl::msg::setLogLevel(LogLevel level)
00121 {
00122 USE_DOH;
00123 doh->logLevel_ = level;
00124 }
00125
00126 ompl::msg::LogLevel ompl::msg::getLogLevel(void)
00127 {
00128 USE_DOH;
00129 return doh->logLevel_;
00130 }
00131
00132 static const char* LogLevelString[4] = {"Debug: ", "Info: ", "Warning: ", "Error: "};
00133
00134 void ompl::msg::OutputHandlerSTD::log(const std::string &text, LogLevel level, const char *filename, int line)
00135 {
00136 if (level >= LOG_WARN)
00137 {
00138 std::cerr << LogLevelString[level] << text << std::endl;
00139 std::cerr << " at line " << line << " in " << filename << std::endl;
00140 std::cerr.flush();
00141 }
00142 else
00143 {
00144 std::cout << LogLevelString[level] << text << std::endl;
00145 std::cout.flush();
00146 }
00147 }
00148
00149 ompl::msg::OutputHandlerFile::OutputHandlerFile(const char *filename) : OutputHandler()
00150 {
00151 file_ = fopen(filename, "a");
00152 if (!file_)
00153 std::cerr << "Unable to open log file: '" << filename << "'" << std::endl;
00154 }
00155
00156 ompl::msg::OutputHandlerFile::~OutputHandlerFile(void)
00157 {
00158 if (file_)
00159 if (fclose(file_) != 0)
00160 std::cerr << "Error closing logfile" << std::endl;
00161 }
00162
00163 void ompl::msg::OutputHandlerFile::log(const std::string &text, LogLevel level, const char *filename, int line)
00164 {
00165 if (file_)
00166 {
00167 fprintf(file_, "%s%s\n", LogLevelString[level], text.c_str());
00168 if(level >= LOG_WARN)
00169 fprintf(file_, " at line %d in %s\n", line, filename);
00170 fflush(file_);
00171 }
00172 }