file_log.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <cstdio>
29 #include "ros/file_log.h"
30 #include "ros/this_node.h"
31 
32 #include <ros/io.h>
33 #include <ros/console.h>
34 
35 #include <boost/filesystem.hpp>
36 
37 #ifdef _MSC_VER
38  #ifdef snprintf
39  #undef snprintf
40  #endif
41  #define snprintf _snprintf_s
42 #endif
43 
44 
45 namespace fs = boost::filesystem;
46 
47 namespace ros
48 {
49 
50 namespace file_log
51 {
52 
53 std::string g_log_directory;
54 
55 const std::string& getLogDirectory()
56 {
57  return g_log_directory;
58 }
59 
60 void init(const M_string& remappings)
61 {
62  std::string log_file_name;
63  M_string::const_iterator it = remappings.find("__log");
64  if (it != remappings.end())
65  {
66  log_file_name = it->second;
67  }
68 
69  {
70  // Log filename can be specified on the command line through __log
71  // If it's been set, don't create our own name
72  if (log_file_name.empty())
73  {
74  // Setup the logfile appender
75  // Can't do this in rosconsole because the node name is not known
76  pid_t pid = getpid();
77  std::string ros_log_env;
78  if ( get_environment_variable(ros_log_env, "ROS_LOG_DIR"))
79  {
80  log_file_name = ros_log_env + std::string("/");
81  }
82  else
83  {
84  if ( get_environment_variable(ros_log_env, "ROS_HOME"))
85  {
86  log_file_name = ros_log_env + std::string("/log/");
87  }
88  else
89  {
90  // Not cross-platform?
91  if( get_environment_variable(ros_log_env, "HOME") )
92  {
93  std::string dotros = ros_log_env + std::string("/.ros/");
94  fs::create_directory(dotros);
95  log_file_name = dotros + "log/";
96  fs::create_directory(log_file_name);
97  }
98  }
99  }
100 
101  // sanitize the node name and tack it to the filename
102  for (size_t i = 1; i < this_node::getName().length(); i++)
103  {
104  if (!isalnum(this_node::getName()[i]))
105  {
106  log_file_name += '_';
107  }
108  else
109  {
110  log_file_name += this_node::getName()[i];
111  }
112  }
113 
114  char pid_str[100];
115  snprintf(pid_str, sizeof(pid_str), "%d", pid);
116  log_file_name += std::string("_") + std::string(pid_str) + std::string(".log");
117  }
118 
119  log_file_name = fs::system_complete(log_file_name).string();
120  g_log_directory = fs::path(log_file_name).parent_path().string();
121  }
122 }
123 
124 } // namespace file_log
125 
126 } // namespace ros
void init(const M_string &remappings)
Definition: file_log.cpp:60
ROSCPP_DECL const std::string & getName()
Returns the name of the current node.
Definition: this_node.cpp:81
std::map< std::string, std::string > M_string
bool get_environment_variable(std::string &str, const char *environment_variable)
ROSCPP_DECL const std::string & getLogDirectory()
Definition: file_log.cpp:55
std::string g_log_directory
Definition: file_log.cpp:53


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:29:54