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 #include <cstdio>
00029 #include "ros/file_log.h"
00030 #include "ros/this_node.h"
00031
00032 #include "log4cxx/rollingfileappender.h"
00033 #include "log4cxx/patternlayout.h"
00034
00035 #include <ros/console.h>
00036
00037 #include <boost/filesystem.hpp>
00038
00039 namespace fs = boost::filesystem;
00040
00041 namespace ros
00042 {
00043
00044 namespace file_log
00045 {
00046
00047 std::string g_log_filename;
00048 std::string g_log_directory;
00049 log4cxx::LoggerPtr g_file_only_logger;
00050
00051 const std::string& getLogFilename()
00052 {
00053 return g_log_filename;
00054 }
00055
00056 const std::string& getLogDirectory()
00057 {
00058 return g_log_directory;
00059 }
00060
00061 log4cxx::LoggerPtr& getFileOnlyLogger()
00062 {
00063 return g_file_only_logger;
00064 }
00065
00066 log4cxx::LevelPtr getDebugLevel()
00067 {
00068 return log4cxx::Level::getDebug();
00069 }
00070
00071 void init(const M_string& remappings)
00072 {
00073 std::string log_file_name;
00074 M_string::const_iterator it = remappings.find("__log");
00075 if (it != remappings.end())
00076 {
00077 log_file_name = it->second;
00078 }
00079
00080 {
00081
00082
00083 if (log_file_name.empty())
00084 {
00085
00086
00087 pid_t pid = getpid();
00088 char* ros_log_env = getenv("ROS_LOG_DIR");
00089 if (ros_log_env)
00090 {
00091 log_file_name = ros_log_env + std::string("/");
00092 }
00093 else
00094 {
00095 ros_log_env = getenv("ROS_HOME");
00096
00097 if (ros_log_env)
00098 {
00099 log_file_name = ros_log_env + std::string("/log/");
00100 }
00101 else
00102 {
00103
00104 ros_log_env = getenv("HOME");
00105 if (ros_log_env)
00106 {
00107 std::string dotros = ros_log_env + std::string("/.ros/");
00108 fs::create_directory(dotros);
00109 log_file_name = dotros + "log/";
00110 fs::create_directory(log_file_name);
00111 }
00112 }
00113 }
00114
00115
00116 for (size_t i = 1; i < this_node::getName().length(); i++)
00117 {
00118 if (!isalnum(this_node::getName()[i]))
00119 {
00120 log_file_name += '_';
00121 }
00122 else
00123 {
00124 log_file_name += this_node::getName()[i];
00125 }
00126 }
00127
00128 char pid_str[100];
00129 snprintf(pid_str, sizeof(pid_str), "%d", pid);
00130 log_file_name += std::string("_") + std::string(pid_str) + std::string(".log");
00131 }
00132
00133 log_file_name = fs::system_complete(log_file_name).string();
00134 g_log_directory = fs::path(log_file_name).parent_path().string();
00135 g_log_filename = log_file_name;
00136
00137 const log4cxx::LoggerPtr& logger = log4cxx::Logger::getLogger(ROSCONSOLE_ROOT_LOGGER_NAME);
00138 log4cxx::LayoutPtr layout = new log4cxx::PatternLayout("[%c] [%d] [thread %t]: [%p] %m\n");
00139 log4cxx::RollingFileAppenderPtr appender = new log4cxx::RollingFileAppender(layout, log_file_name, false);
00140 appender->setMaximumFileSize(100*1024*1024);
00141 appender->setMaxBackupIndex(10);
00142 log4cxx::helpers::Pool pool;
00143 appender->activateOptions(pool);
00144 logger->addAppender(appender);
00145
00146 g_file_only_logger = log4cxx::Logger::getLogger("roscpp_internal");
00147 g_file_only_logger->addAppender(appender);
00148 g_file_only_logger->setLevel(log4cxx::Level::getDebug());
00149 }
00150 }
00151
00152 }
00153
00154 }