bridge.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #include <ros/console.h>
38 #include <console_bridge/console.h>
40 
41 namespace rosconsole_bridge
42 {
43 
44 OutputHandlerROS::OutputHandlerROS(void) : OutputHandler()
45 {
46 }
47 
48 void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
49 {
50  std::string prefix;
51 
52  // Check for fake subprefix name defined as text output "package.prefix: message"
53  // Rejects something like "foo bar: message" because of the space
54  static const std::string NEEDLE_STRING = ": ";
55  size_t sub_index = text.find(NEEDLE_STRING);
56  size_t space_index = text.find(" ");
57 
58  if (sub_index != std::string::npos && // needle is found
59  sub_index > 0 && // does not start with needle
60  space_index > sub_index && // no spaces before needle (": ")
61  text.length() > space_index + 1) // remaining text is not empty
62  {
63  prefix = std::string(ROSCONSOLE_NAME_PREFIX) + "." + text.substr(0, sub_index);
64  // update the sub_index to allow us to later remove the prefix from the message
65  sub_index += NEEDLE_STRING.length();
66  }
67  else
68  {
69  prefix = std::string(ROSCONSOLE_NAME_PREFIX) + ".console_bridge";
70  sub_index = 0;
71  }
72 
73  // Handle different logging levels
74  switch (level)
75  {
76  case console_bridge::CONSOLE_BRIDGE_LOG_INFO:
77  {
79  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
80  {
81  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
82  text.substr(sub_index, std::string::npos).c_str());
83  }
84  }
85  break;
86  case console_bridge::CONSOLE_BRIDGE_LOG_WARN:
87  {
89  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
90  {
91  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
92  text.substr(sub_index, std::string::npos).c_str());
93  }
94  }
95  break;
96  case console_bridge::CONSOLE_BRIDGE_LOG_ERROR:
97  {
99  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
100  {
101  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
102  text.substr(sub_index, std::string::npos).c_str());
103  }
104  }
105  break;
106  default:
107  // debug
108  {
110  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
111  {
112  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
113  text.substr(sub_index, std::string::npos).c_str());
114  }
115  }
116  break;
117  }
118 }
119 
121 {
122  static OutputHandlerROS oh_ros;
123  console_bridge::useOutputHandler(&oh_ros);
124 
125  // we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
126  console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
127 }
128 
130 {
131  console_bridge::restorePreviousOutputHandler();
132 }
133 
134 }
#define ROS_UNLIKELY(x)
#define ROSCONSOLE_NAME_PREFIX
virtual void log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
Definition: bridge.cpp:48
#define ROSCONSOLE_DEFINE_LOCATION(cond, level, name)


rosconsole_bridge
Author(s): Ioan Sucan , Dirk Thomas
autogenerated on Sun Oct 11 2020 03:22:37