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 namespace fs = boost::filesystem;
38 
39 namespace ros
40 {
41 
42 namespace file_log
43 {
44 
45 std::string g_log_directory;
46 
47 const std::string& getLogDirectory()
48 {
49  return g_log_directory;
50 }
51 
52 void init(const M_string& remappings)
53 {
54  std::string log_file_name;
55  M_string::const_iterator it = remappings.find("__log");
56  if (it != remappings.end())
57  {
58  log_file_name = it->second;
59  }
60 
61  {
62  // Log filename can be specified on the command line through __log
63  // If it's been set, don't create our own name
64  if (log_file_name.empty())
65  {
66  // Setup the logfile appender
67  // Can't do this in rosconsole because the node name is not known
68  pid_t pid = getpid();
69  std::string ros_log_env;
70  if ( get_environment_variable(ros_log_env, "ROS_LOG_DIR"))
71  {
72  log_file_name = ros_log_env + std::string("/");
73  }
74  else
75  {
76  if ( get_environment_variable(ros_log_env, "ROS_HOME"))
77  {
78  log_file_name = ros_log_env + std::string("/log/");
79  }
80  else
81  {
82  // Not cross-platform?
83  if( get_environment_variable(ros_log_env, "HOME") )
84  {
85  std::string dotros = ros_log_env + std::string("/.ros/");
86  fs::create_directory(dotros);
87  log_file_name = dotros + "log/";
88  fs::create_directory(log_file_name);
89  }
90  }
91  }
92 
93  // sanitize the node name and tack it to the filename
94  for (size_t i = 1; i < this_node::getName().length(); i++)
95  {
96  if (!isalnum(this_node::getName()[i]))
97  {
98  log_file_name += '_';
99  }
100  else
101  {
102  log_file_name += this_node::getName()[i];
103  }
104  }
105 
106  char pid_str[100];
107  std::snprintf(pid_str, sizeof(pid_str), "%d", pid);
108  log_file_name += std::string("_") + std::string(pid_str) + std::string(".log");
109  }
110 
111  log_file_name = fs::system_complete(log_file_name).string();
112  g_log_directory = fs::path(log_file_name).parent_path().string();
113  }
114 }
115 
116 } // namespace file_log
117 
118 } // namespace ros
void init(const M_string &remappings)
Definition: file_log.cpp:52
ROSCPP_DECL const std::string & getName()
Returns the name of the current node.
Definition: this_node.cpp:74
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:47
std::string g_log_directory
Definition: file_log.cpp:45


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:27