file_log.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009, Willow Garage, Inc.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include <cstdio>
00029 #include "ros/file_log.h"
00030 #include "ros/this_node.h"
00031 
00032 #include <ros/io.h>
00033 #include <ros/console.h>
00034 
00035 #include <boost/filesystem.hpp>
00036 
00037 #ifdef _MSC_VER
00038   #ifdef snprintf
00039     #undef snprintf
00040   #endif
00041   #define snprintf _snprintf_s
00042 #endif
00043 
00044 
00045 namespace fs = boost::filesystem;
00046 
00047 namespace ros
00048 {
00049 
00050 namespace file_log
00051 {
00052 
00053 std::string g_log_directory;
00054 
00055 const std::string& getLogDirectory()
00056 {
00057   return g_log_directory;
00058 }
00059 
00060 void init(const M_string& remappings)
00061 {
00062   std::string log_file_name;
00063   M_string::const_iterator it = remappings.find("__log");
00064   if (it != remappings.end())
00065   {
00066     log_file_name = it->second;
00067   }
00068 
00069   {
00070     // Log filename can be specified on the command line through __log
00071     // If it's been set, don't create our own name
00072     if (log_file_name.empty())
00073     {
00074       // Setup the logfile appender
00075       // Can't do this in rosconsole because the node name is not known
00076       pid_t pid = getpid();
00077       std::string ros_log_env;
00078       if ( get_environment_variable(ros_log_env, "ROS_LOG_DIR"))
00079       {
00080         log_file_name = ros_log_env + std::string("/");
00081       }
00082       else
00083       {
00084         if ( get_environment_variable(ros_log_env, "ROS_HOME"))
00085         {
00086           log_file_name = ros_log_env + std::string("/log/");
00087         }
00088         else
00089         {
00090           // Not cross-platform?
00091           if( get_environment_variable(ros_log_env, "HOME") )
00092           {
00093             std::string dotros = ros_log_env + std::string("/.ros/");
00094             fs::create_directory(dotros);
00095             log_file_name = dotros + "log/";
00096             fs::create_directory(log_file_name);
00097           }
00098         }
00099       }
00100 
00101       // sanitize the node name and tack it to the filename
00102       for (size_t i = 1; i < this_node::getName().length(); i++)
00103       {
00104         if (!isalnum(this_node::getName()[i]))
00105         {
00106           log_file_name += '_';
00107         }
00108         else
00109         {
00110           log_file_name += this_node::getName()[i];
00111         }
00112       }
00113 
00114       char pid_str[100];
00115       snprintf(pid_str, sizeof(pid_str), "%d", pid);
00116       log_file_name += std::string("_") + std::string(pid_str) + std::string(".log");
00117     }
00118 
00119     log_file_name = fs::system_complete(log_file_name).string();
00120     g_log_directory = fs::path(log_file_name).parent_path().string();
00121   }
00122 }
00123 
00124 } // namespace file_log
00125 
00126 } // namespace ros


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Tue Mar 7 2017 03:44:47