|
| 1 | +// -- BEGIN LICENSE BLOCK ---------------------------------------------- |
| 2 | +// Copyright 2021 Universal Robots A/S |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// All source code contained in and/or linked to in this message (the “Source Code”) is subject to the copyright of |
| 17 | +// Universal Robots A/S and/or its licensors. THE SOURCE CODE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | +// OR IMPLIED, INCLUDING – BUT NOT LIMITED TO – WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
| 19 | +// NONINFRINGEMENT. USE OF THE SOURCE CODE IS AT YOUR OWN RISK AND UNIVERSAL ROBOTS A/S AND ITS LICENSORS SHALL, TO THE |
| 20 | +// MAXIMUM EXTENT PERMITTED BY LAW, NOT BE LIABLE FOR ANY ERRORS OR MALICIOUS CODE IN THE SOURCE CODE, ANY THIRD-PARTY |
| 21 | +// CLAIMS, OR ANY OTHER CLAIMS AND DAMAGES, INCLUDING INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES, |
| 22 | +// OR ANY LOSS OF PROFITS, EXPECTED SAVINGS, OR REVENUES, WHETHER INCURRED DIRECTLY OR INDIRECTLY, OR ANY LOSS OF DATA, |
| 23 | +// USE, GOODWILL, OR OTHER INTANGIBLE LOSSES, RESULTING FROM YOUR USE OF THE SOURCE CODE. You may make copies of the |
| 24 | +// Source Code for use in connection with a Universal Robots or UR+ product, provided that you include (i) an |
| 25 | +// appropriate copyright notice (“© [the year in which you received the Source Code or the Source Code was first |
| 26 | +// published, e.g. “2021”] Universal Robots A/S and/or its licensors”) along with the capitalized section of this notice |
| 27 | +// in all copies of the Source Code. By using the Source Code, you agree to the above terms. For more information, |
| 28 | +// please contact [email protected]. |
| 29 | +// -- END LICENSE BLOCK ------------------------------------------------ |
| 30 | + |
| 31 | +#include <ros/console.h> |
| 32 | +#include <ur_robot_driver/urcl_log_handler.h> |
| 33 | + |
| 34 | +namespace ur_driver |
| 35 | +{ |
| 36 | +bool g_registered = false; |
| 37 | +std::unique_ptr<UrclLogHandler> g_log_handler(new UrclLogHandler); |
| 38 | + |
| 39 | +UrclLogHandler::UrclLogHandler() : log_name_(std::string(ROSCONSOLE_NAME_PREFIX) + ".ur_client_library") |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +void UrclLogHandler::log(const char* file, int line, urcl::LogLevel loglevel, const char* message) |
| 44 | +{ |
| 45 | + switch (loglevel) |
| 46 | + { |
| 47 | + case urcl::LogLevel::DEBUG: |
| 48 | + logMessage(file, line, ros::console::levels::Debug, message); |
| 49 | + break; |
| 50 | + case urcl::LogLevel::INFO: |
| 51 | + logMessage(file, line, ros::console::levels::Info, message); |
| 52 | + break; |
| 53 | + case urcl::LogLevel::WARN: |
| 54 | + logMessage(file, line, ros::console::levels::Warn, message); |
| 55 | + break; |
| 56 | + case urcl::LogLevel::ERROR: |
| 57 | + logMessage(file, line, ros::console::levels::Error, message); |
| 58 | + break; |
| 59 | + case urcl::LogLevel::FATAL: |
| 60 | + logMessage(file, line, ros::console::levels::Fatal, message); |
| 61 | + break; |
| 62 | + default: |
| 63 | + break; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void UrclLogHandler::logMessage(const char* file, int line, ros::console::Level level, const char* message) |
| 68 | +{ |
| 69 | + ROSCONSOLE_DEFINE_LOCATION(true, level, log_name_); |
| 70 | + if (ROS_UNLIKELY(__rosconsole_define_location__enabled)) |
| 71 | + { |
| 72 | + ros::console::print(NULL, __rosconsole_define_location__loc.logger_, level, file, line, "", "%s", message); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void registerUrclLogHandler() |
| 77 | +{ |
| 78 | + if (g_registered == false) |
| 79 | + { |
| 80 | + // Log level is decided by ROS log level |
| 81 | + urcl::setLogLevel(urcl::LogLevel::DEBUG); |
| 82 | + urcl::registerLogHandler(std::move(g_log_handler)); |
| 83 | + g_registered = true; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void unregisterUrclLogHandler() |
| 88 | +{ |
| 89 | + if (g_registered == true) |
| 90 | + { |
| 91 | + urcl::unregisterLogHandler(); |
| 92 | + g_registered = false; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +} // namespace ur_driver |
0 commit comments