bridge.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2011, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include <ros/console.h>
00038 #include <console_bridge/console.h>
00039 #include "rosconsole_bridge/bridge.h"
00040 
00041 namespace rosconsole_bridge
00042 {
00043 
00044 OutputHandlerROS::OutputHandlerROS(void) : OutputHandler()
00045 {
00046 }
00047 
00048 OutputHandlerROS::~OutputHandlerROS(void) {
00049   console_bridge::restorePreviousOutputHandler();
00050 }
00051 
00052 void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
00053 {
00054   std::string prefix;
00055 
00056   // Check for fake subprefix name defined as text output "package.prefix: message"
00057   // Rejects something like "foo bar: message" because of the space
00058   static const std::string NEEDLE_STRING = ": ";
00059   size_t sub_index = text.find(NEEDLE_STRING);
00060   size_t space_index = text.find(" ");
00061 
00062   if (sub_index != std::string::npos && // needle is found
00063       sub_index > 0 && // does not start with needle
00064       space_index > sub_index && // no spaces before needle (": ")
00065       text.length() > space_index + 1) // remaining text is not empty
00066   {
00067     prefix = std::string(ROSCONSOLE_NAME_PREFIX) + "." + text.substr(0, sub_index);
00068     // update the sub_index to allow us to later remove the prefix from the message
00069     sub_index += NEEDLE_STRING.length();
00070   }
00071   else
00072   {
00073     prefix = std::string(ROSCONSOLE_NAME_PREFIX) + ".console_bridge";
00074     sub_index = 0;
00075   }
00076 
00077   // Handle different logging levels
00078   switch (level)
00079   {
00080   case console_bridge::CONSOLE_BRIDGE_LOG_INFO:
00081     {
00082       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Info, prefix);
00083       if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
00084       {
00085         ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
00086           text.substr(sub_index, std::string::npos).c_str());
00087       }
00088     }
00089     break;
00090   case console_bridge::CONSOLE_BRIDGE_LOG_WARN:
00091     {
00092       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Warn, prefix);
00093       if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
00094       {
00095         ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
00096           text.substr(sub_index, std::string::npos).c_str());
00097       }
00098     }
00099     break;
00100   case console_bridge::CONSOLE_BRIDGE_LOG_ERROR:
00101     {
00102       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Error, prefix);
00103       if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
00104       {
00105         ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
00106           text.substr(sub_index, std::string::npos).c_str());
00107       }
00108     }
00109     break;
00110   default:
00111     // debug
00112     {
00113       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Debug, prefix);
00114       if (ROS_UNLIKELY(__rosconsole_define_location__enabled))
00115       {
00116         ::ros::console::print(NULL, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, filename, line, "", "%s",
00117           text.substr(sub_index, std::string::npos).c_str());
00118       }
00119     }
00120     break;
00121   }
00122 }
00123 
00124 RegisterOutputHandlerProxy::RegisterOutputHandlerProxy(void)
00125 {
00126   static OutputHandlerROS oh_ros;
00127   console_bridge::useOutputHandler(&oh_ros);
00128 
00129   // we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
00130   console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
00131 }
00132 
00133 }


rosconsole_bridge
Author(s): Ioan Sucan
autogenerated on Wed Apr 20 2016 06:05:01