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 void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
00049 {
00050   std::string prefix;
00051 
00052   // Check for fake subprefix name defined as text output "package.prefix: message"
00053   // Rejects something like "foo bar: message" because of the space
00054   static const std::string NEEDLE_STRING = ": ";
00055   size_t sub_index = text.find(NEEDLE_STRING);
00056   size_t space_index = text.find(" ");
00057 
00058   if (sub_index != std::string::npos && // needle is found
00059       sub_index > 0 && // does not start with needle
00060       space_index > sub_index && // no spaces before needle (": ")
00061       text.length() > space_index + 1) // remaining text is not empty
00062   {
00063     prefix = std::string(ROSCONSOLE_NAME_PREFIX) + "." + text.substr(0, sub_index);
00064     // update the sub_index to allow us to later remove the prefix from the message
00065     sub_index += NEEDLE_STRING.length();
00066   }
00067   else
00068   {
00069     prefix = std::string(ROSCONSOLE_NAME_PREFIX) + ".console_bridge";
00070     sub_index = 0;
00071   }
00072 
00073   // Handle different logging levels
00074   switch (level)
00075   {
00076   case console_bridge::CONSOLE_BRIDGE_LOG_INFO:
00077     {
00078       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Info, prefix);
00079       if (ROS_UNLIKELY(enabled))
00080       {
00081         ::ros::console::print(NULL, loc.logger_, loc.level_, filename, line, "", "%s",
00082           text.substr(sub_index, std::string::npos).c_str());
00083       }
00084     }
00085     break;
00086   case console_bridge::CONSOLE_BRIDGE_LOG_WARN:
00087     {
00088       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Warn, prefix);
00089       if (ROS_UNLIKELY(enabled))
00090       {
00091         ::ros::console::print(NULL, loc.logger_, loc.level_, filename, line, "", "%s",
00092           text.substr(sub_index, std::string::npos).c_str());
00093       }
00094     }
00095     break;
00096   case console_bridge::CONSOLE_BRIDGE_LOG_ERROR:
00097     {
00098       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Error, prefix);
00099       if (ROS_UNLIKELY(enabled))
00100       {
00101         ::ros::console::print(NULL, loc.logger_, loc.level_, filename, line, "", "%s",
00102           text.substr(sub_index, std::string::npos).c_str());
00103       }
00104     }
00105     break;
00106   default:
00107     // debug
00108     {
00109       ROSCONSOLE_DEFINE_LOCATION(true, ::ros::console::levels::Debug, prefix);
00110       if (ROS_UNLIKELY(enabled))
00111       {
00112         ::ros::console::print(NULL, loc.logger_, loc.level_, filename, line, "", "%s",
00113           text.substr(sub_index, std::string::npos).c_str());
00114       }
00115     }
00116     break;
00117   }
00118 }
00119 
00120 RegisterOutputHandlerProxy::RegisterOutputHandlerProxy(void)
00121 {
00122   static OutputHandlerROS oh_ros;
00123   console_bridge::useOutputHandler(&oh_ros);
00124 
00125   // we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
00126   console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
00127 }
00128 
00129 }


rosconsole_bridge
Author(s): Ioan Sucan
autogenerated on Thu Aug 27 2015 14:50:31