32 #if defined(__APPLE__) && defined(__GNUC__) && defined(__llvm__) && !defined(__clang__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 2)
33 #error This code is known to provoke a compiler crash with llvm-gcc 4.2. You will have better luck with clang++. See code.ros.org/trac/ros/ticket/3626
39 #define ROSCONSOLE_CONSOLE_IMPL_EXPORTS
42 #include "log4cxx/appenderskeleton.h"
43 #include "log4cxx/spi/loggingevent.h"
44 #include "log4cxx/level.h"
45 #include "log4cxx/propertyconfigurator.h"
48 #include "log4cxx/helpers/transcoder.h"
51 #include <boost/thread.hpp>
52 #include <boost/shared_array.hpp>
53 #include <boost/regex.hpp>
71 log4cxx::Level::getDebug(),
72 log4cxx::Level::getInfo(),
73 log4cxx::Level::getWarn(),
74 log4cxx::Level::getError(),
75 log4cxx::Level::getFatal(),
87 virtual void append(
const log4cxx::spi::LoggingEventPtr& event,
88 log4cxx::helpers::Pool&)
91 if (event->getLevel()->toInt() == log4cxx::Level::DEBUG_INT)
95 else if (event->getLevel()->toInt() == log4cxx::Level::INFO_INT)
99 else if (event->getLevel()->toInt() == log4cxx::Level::WARN_INT)
103 else if (event->getLevel()->toInt() == log4cxx::Level::ERROR_INT)
107 else if (event->getLevel()->toInt() == log4cxx::Level::FATAL_INT)
112 LOG4CXX_ENCODE_CHAR(tmpstr, event->getMessage());
113 std::string msg = tmpstr;
115 std::string msg =
event->getMessage();
117 const log4cxx::spi::LocationInfo& location_info =
event->getLocationInformation();
118 ::ros::console::backend::print(event.operator->(), level, msg.c_str(), location_info.getFileName(), location_info.getMethodName().c_str(), location_info.getLineNumber());
134 ros_logger->setLevel(log4cxx::Level::getInfo());
136 log4cxx::LoggerPtr roscpp_superdebug = log4cxx::Logger::getLogger(
"ros.roscpp.superdebug");
137 roscpp_superdebug->setLevel(log4cxx::Level::getWarn());
140 char* ros_root_cstr = NULL;
142 _dupenv_s(&ros_root_cstr, NULL,
"ROS_ROOT");
144 ros_root_cstr = getenv(
"ROS_ROOT");
148 std::string config_file = std::string(ros_root_cstr) +
"/config/rosconsole.config";
149 FILE* config_file_ptr = fopen( config_file.c_str(),
"r" );
150 if( config_file_ptr )
152 fclose( config_file_ptr );
153 log4cxx::PropertyConfigurator::configure(config_file);
156 char* config_file_cstr = NULL;
158 _dupenv_s(&config_file_cstr, NULL,
"ROSCONSOLE_CONFIG_FILE");
160 config_file_cstr = getenv(
"ROSCONSOLE_CONFIG_FILE");
162 if ( config_file_cstr )
164 std::string config_file = config_file_cstr;
165 log4cxx::PropertyConfigurator::configure(config_file);
169 logger->addAppender(
new ROSConsoleStdioAppender);
171 if ( ros_root_cstr != NULL ) {
174 if ( config_file_cstr != NULL ) {
175 free(config_file_cstr);
182 void print(
void* handle, ::
ros::console::Level level,
const char* str,
const char* file,
const char*
function,
int line)
184 log4cxx::Logger* logger = (log4cxx::Logger*)handle;
187 logger->forcedLog(
g_level_lookup[level], str, log4cxx::spi::LocationInfo(file,
function, line));
189 catch (std::exception& e)
191 fprintf(stderr,
"Caught exception while logging: [%s]\n", e.what());
197 log4cxx::Logger* logger = (log4cxx::Logger*)handle;
203 return log4cxx::Logger::getLogger(name);
206 std::string
getName(
void* handle)
208 const log4cxx::spi::LoggingEvent*
event = (
const log4cxx::spi::LoggingEvent*)handle;
210 LOG4CXX_ENCODE_CHAR(tmpstr, event->getLoggerName());
213 return event->getLoggerName();
217 bool get_loggers(std::map<std::string, levels::Level>& loggers)
221 log4cxx::LoggerList current_loggers = repo->getCurrentLoggers();
222 log4cxx::LoggerList::iterator it = current_loggers.begin();
223 log4cxx::LoggerList::iterator end = current_loggers.end();
224 for (; it != end; ++it)
227 LOG4CXX_ENCODE_CHAR(tmpstr, (*it)->getName());
228 std::string name = tmpstr;
230 std::string name = (*it)->getName();
233 int log4cxx_level = (*it)->getEffectiveLevel()->toInt();
235 if (log4cxx_level == log4cxx::Level::DEBUG_INT)
239 else if (log4cxx_level == log4cxx::Level::INFO_INT)
243 else if (log4cxx_level == log4cxx::Level::WARN_INT)
247 else if (log4cxx_level == log4cxx::Level::ERROR_INT)
251 else if (log4cxx_level == log4cxx::Level::FATAL_INT)
259 loggers[name] = level;
267 log4cxx::LevelPtr log4cxx_level;
270 log4cxx_level = log4cxx::Level::getDebug();
274 log4cxx_level = log4cxx::Level::getInfo();
278 log4cxx_level = log4cxx::Level::getWarn();
282 log4cxx_level = log4cxx::Level::getError();
286 log4cxx_level = log4cxx::Level::getFatal();
293 log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(name);
294 logger->setLevel(log4cxx_level);
310 virtual void append(
const log4cxx::spi::LoggingEventPtr& event, log4cxx::helpers::Pool& pool)
314 if (event->getLevel()->toInt() == log4cxx::Level::FATAL_INT)
318 else if (event->getLevel()->toInt() == log4cxx::Level::ERROR_INT)
322 else if (event->getLevel()->toInt() == log4cxx::Level::WARN_INT)
326 else if (event->getLevel()->toInt() == log4cxx::Level::INFO_INT)
330 else if (event->getLevel()->toInt() == log4cxx::Level::DEBUG_INT)
340 LOG4CXX_ENCODE_CHAR(tmpstr, event->getMessage());
341 std::string msg = tmpstr;
343 std::string msg =
event->getMessage();
346 const log4cxx::spi::LocationInfo& info =
event->getLocationInformation();
347 appender_->
log(level, msg.c_str(), info.getFileName(), info.getMethodName().c_str(), info.getLineNumber());
386 log4cxx::Logger::getRootLogger()->getLoggerRepository()->shutdown();