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 
49  console_bridge::restorePreviousOutputHandler();
50 }
51 
52 void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
53 {
54  std::string prefix;
55 
56  // Check for fake subprefix name defined as text output "package.prefix: message"
57  // Rejects something like "foo bar: message" because of the space
58  static const std::string NEEDLE_STRING = ": ";
59  size_t sub_index = text.find(NEEDLE_STRING);
60  size_t space_index = text.find(" ");
61 
62  if (sub_index != std::string::npos && // needle is found
63  sub_index > 0 && // does not start with needle
64  space_index > sub_index && // no spaces before needle (": ")
65  text.length() > space_index + 1) // remaining text is not empty
66  {
67  prefix = std::string(ROSCONSOLE_NAME_PREFIX) + "." + text.substr(0, sub_index);
68  // update the sub_index to allow us to later remove the prefix from the message
69  sub_index += NEEDLE_STRING.length();
70  }
71  else
72  {
73  prefix = std::string(ROSCONSOLE_NAME_PREFIX) + ".console_bridge";
74  sub_index = 0;
75  }
76 
77  // Handle different logging levels
78  switch (level)
79  {
80  case console_bridge::CONSOLE_BRIDGE_LOG_INFO:
81  {
83  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
84  {
85  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
86  text.substr(sub_index, std::string::npos).c_str());
87  }
88  }
89  break;
90  case console_bridge::CONSOLE_BRIDGE_LOG_WARN:
91  {
93  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
94  {
95  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
96  text.substr(sub_index, std::string::npos).c_str());
97  }
98  }
99  break;
100  case console_bridge::CONSOLE_BRIDGE_LOG_ERROR:
101  {
103  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
104  {
105  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
106  text.substr(sub_index, std::string::npos).c_str());
107  }
108  }
109  break;
110  default:
111  // debug
112  {
114  if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
115  {
116  ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
117  text.substr(sub_index, std::string::npos).c_str());
118  }
119  }
120  break;
121  }
122 }
123 
125 {
126  static OutputHandlerROS oh_ros;
127  console_bridge::useOutputHandler(&oh_ros);
128 
129  // we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
130  console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
131 }
132 
133 }
#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:52
#define ROSCONSOLE_DEFINE_LOCATION(cond, level, name)


rosconsole_bridge
Author(s): Ioan Sucan
autogenerated on Thu Oct 26 2017 02:30:29