Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add demo for logging + logger config #194

Merged
merged 38 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
71709a6
First commit of logger config server component
dhood Nov 17, 2017
9425ba0
Use package name for visibility macros
dhood Nov 22, 2017
8798484
Add logger usage component
dhood Nov 22, 2017
8841e43
Add executable that composes th e two nodes
dhood Nov 22, 2017
4215bef
Make the function to evaluate an attribute
dhood Nov 24, 2017
a45f493
Add test
dhood Nov 25, 2017
20b250a
printed -> logged
dhood Nov 27, 2017
f454f22
Add test that debug output gets enabled
dhood Nov 27, 2017
ebdf605
Enable debug automatically after 5s instead of relying on service call
dhood Nov 27, 2017
e451278
Linter fixup
dhood Nov 27, 2017
f20eeed
WIP readme
dhood Nov 27, 2017
16dcf75
Fixup
dhood Nov 28, 2017
e4f4dd4
Fix tests for windows
dhood Nov 28, 2017
6bc815b
Fix windows warning
dhood Nov 28, 2017
426ce10
Add some description to message of log calls with features
dhood Nov 28, 2017
b106373
Readme WIP
dhood Nov 28, 2017
916b226
Break into 2 wiki pages
dhood Nov 28, 2017
dde08f5
Remove unrelated change
dhood Nov 28, 2017
486446b
Readme edits
dhood Nov 28, 2017
f939305
Fix regex matching
dhood Nov 28, 2017
47305c0
swap to logger obj
dhood Nov 29, 2017
450941e
Add comment why pedantic isn't possible
dhood Nov 30, 2017
816cbfc
Add another reason
dhood Dec 1, 2017
c9d468c
various fixups
dhood Dec 1, 2017
6d8df9a
Revert "swap to logger obj"
dhood Dec 1, 2017
7983c11
Check return value from rcutils
dhood Dec 1, 2017
e1da9e9
Declare membership of rosidl_interface_packages group
dhood Dec 1, 2017
0182e22
Fix launch error message
dhood Dec 1, 2017
5bcdec5
Highlight logging_demo runtime config is temporary approach
dhood Dec 1, 2017
9533f7b
readme fixup
dhood Dec 1, 2017
c81ba64
divides_into_twelve -> is_divisor_of_twelve
dhood Dec 1, 2017
a0933e3
Missed some copyright years
dhood Dec 1, 2017
b6bc4d8
typo
wjwwood Dec 4, 2017
3e9e5b1
Rename severity_threshold -> level
dhood Dec 2, 2017
9194721
Use absolute path for generate (cmake 3.10)
dhood Dec 3, 2017
9f27f84
Don't rely on escalated namespaces
dhood Dec 4, 2017
4d75160
Make service handler a member function
dhood Dec 4, 2017
72f6c9b
Remove wiki pages (will go in wiki)
dhood Dec 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions logging_demo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
cmake_minimum_required(VERSION 3.5)

project(logging_demo)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# we dont use add_compile_options with pedantic in packages generating python
# messages because the Python C extensions dont comply with it
# pedantic is not possible here also because of class_loader macros
add_compile_options(-Wall -Wextra)
endif()

find_package(ament_cmake REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(class_loader REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rcutils REQUIRED)
find_package(std_msgs REQUIRED)

find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/ConfigLogger.srv"
)

include_directories(include)

add_library(logger_config_component SHARED
src/logger_config_component.cpp)
target_compile_definitions(logger_config_component
PRIVATE "LOGGING_DEMO_BUILDING_DLL")
ament_target_dependencies(logger_config_component
"class_loader"
"rclcpp")
rosidl_target_interfaces(logger_config_component
${PROJECT_NAME} "rosidl_typesupport_cpp")
rclcpp_register_node_plugins(logger_config_component "logging_demo::LoggerConfig")

add_library(logger_usage_component SHARED
src/logger_usage_component.cpp)
target_compile_definitions(logger_usage_component
PRIVATE "LOGGING_DEMO_BUILDING_DLL")
ament_target_dependencies(logger_usage_component
"class_loader"
"rclcpp"
"std_msgs")
rclcpp_register_node_plugins(logger_usage_component "logging_demo::LoggerUsage")

add_executable(logging_demo_main
src/logging_demo_main.cpp)
target_link_libraries(logging_demo_main
logger_config_component
logger_usage_component)
ament_target_dependencies(logging_demo_main
"rclcpp")

install(TARGETS
logger_config_component
logger_usage_component
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)

install(TARGETS
logging_demo_main
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()

find_package(ament_cmake_pytest REQUIRED)
find_package(rmw_implementation_cmake REQUIRED)

set(generated_python_files)
macro(tests)
set(LOGGING_DEMO_MAIN_EXECUTABLE $<TARGET_FILE:logging_demo_main>)
set(
EXPECTED_OUTPUT_LOGGING_DEMO_MAIN_DEFAULT_SEVERITY
"${CMAKE_CURRENT_SOURCE_DIR}/test/logging_demo_main_default_severity")
set(
EXPECTED_OUTPUT_LOGGING_DEMO_MAIN_DEBUG_SEVERITY
"${CMAKE_CURRENT_SOURCE_DIR}/test/logging_demo_main_debug_severity")

configure_file(
test/test_logging_demo.py.in
test_logging_demo${target_suffix}.py.genexp
@ONLY
)
file(GENERATE
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_logging_demo${target_suffix}_$<CONFIG>.py"
INPUT "${CMAKE_CURRENT_BINARY_DIR}/test_logging_demo${target_suffix}.py.genexp"
)
ament_add_pytest_test(test_logging_demo${target_suffix}
"${CMAKE_CURRENT_BINARY_DIR}/test_logging_demo${target_suffix}_$<CONFIG>.py"
ENV RMW_IMPLEMENTATION=${rmw_implementation}
APPEND_LIBRARY_DIRS "${append_library_dirs}"
TIMEOUT 20)
list(
APPEND generated_python_files
"${CMAKE_CURRENT_BINARY_DIR}/test_logging_demo${target_suffix}_$<CONFIG>.py")
endmacro()

set(append_library_dirs "${CMAKE_CURRENT_BINARY_DIR}")
if(WIN32)
set(append_library_dirs "${append_library_dirs}/$<CONFIG>")
endif()

call_for_each_rmw_implementation(tests)

find_package(ament_cmake_flake8 REQUIRED)
ament_flake8(
TESTNAME "flake8_generated_launch"
# the generated code might contain longer lines if from a template
MAX_LINE_LENGTH 999
${generated_python_files})
endif()

ament_package()
46 changes: 46 additions & 0 deletions logging_demo/include/logging_demo/logger_config_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef LOGGING_DEMO__LOGGER_CONFIG_COMPONENT_HPP_
#define LOGGING_DEMO__LOGGER_CONFIG_COMPONENT_HPP_

#include <memory>

#include "logging_demo/srv/config_logger.hpp"
#include "logging_demo/visibility_control.h"

#include "rclcpp/rclcpp.hpp"

namespace logging_demo
{

class LoggerConfig : public rclcpp::Node
{
public:
LOGGING_DEMO_PUBLIC
LoggerConfig();

LOGGING_DEMO_PUBLIC
void
handle_logger_config_request(
const std::shared_ptr<logging_demo::srv::ConfigLogger::Request> request,
std::shared_ptr<logging_demo::srv::ConfigLogger::Response> response);

private:
rclcpp::Service<logging_demo::srv::ConfigLogger>::SharedPtr srv_;
};

} // namespace logging_demo

#endif // LOGGING_DEMO__LOGGER_CONFIG_COMPONENT_HPP_
46 changes: 46 additions & 0 deletions logging_demo/include/logging_demo/logger_usage_component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef LOGGING_DEMO__LOGGER_USAGE_COMPONENT_HPP_
#define LOGGING_DEMO__LOGGER_USAGE_COMPONENT_HPP_

#include <string>

#include "logging_demo/visibility_control.h"
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

namespace logging_demo
{

class LoggerUsage : public rclcpp::Node
{
public:
LOGGING_DEMO_PUBLIC
LoggerUsage();

protected:
void on_timer();

private:
size_t count_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_;
rclcpp::TimerBase::SharedPtr one_shot_timer_, timer_;
std::function<bool()> debug_function_to_evaluate_;
};

bool is_divisor_of_twelve(size_t val, std::string logger_name);
} // namespace logging_demo

#endif // LOGGING_DEMO__LOGGER_USAGE_COMPONENT_HPP_
58 changes: 58 additions & 0 deletions logging_demo/include/logging_demo/visibility_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef LOGGING_DEMO__VISIBILITY_CONTROL_H_
#define LOGGING_DEMO__VISIBILITY_CONTROL_H_

#if __cplusplus
extern "C"
{
#endif

// This logic was borrowed (then namespaced) from the examples on the gcc wiki:
// https://gcc.gnu.org/wiki/Visibility

#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define LOGGING_DEMO_EXPORT __attribute__ ((dllexport))
#define LOGGING_DEMO_IMPORT __attribute__ ((dllimport))
#else
#define LOGGING_DEMO_EXPORT __declspec(dllexport)
#define LOGGING_DEMO_IMPORT __declspec(dllimport)
#endif
#ifdef LOGGING_DEMO_BUILDING_DLL
#define LOGGING_DEMO_PUBLIC LOGGING_DEMO_EXPORT
#else
#define LOGGING_DEMO_PUBLIC LOGGING_DEMO_IMPORT
#endif
#define LOGGING_DEMO_PUBLIC_TYPE LOGGING_DEMO_PUBLIC
#define LOGGING_DEMO_LOCAL
#else
#define LOGGING_DEMO_EXPORT __attribute__ ((visibility("default")))
#define LOGGING_DEMO_IMPORT
#if __GNUC__ >= 4
#define LOGGING_DEMO_PUBLIC __attribute__ ((visibility("default")))
#define LOGGING_DEMO_LOCAL __attribute__ ((visibility("hidden")))
#else
#define LOGGING_DEMO_PUBLIC
#define LOGGING_DEMO_LOCAL
#endif
#define LOGGING_DEMO_PUBLIC_TYPE
#endif

#if __cplusplus
}
#endif

#endif // LOGGING_DEMO__VISIBILITY_CONTROL_H_
39 changes: 39 additions & 0 deletions logging_demo/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>logging_demo</name>
<version>0.0.3</version>
<description>Examples for using and configuring loggers.</description>
<maintainer email="dhood@osrfoundation.org">D. Hood</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>

<build_depend>ament_index_cpp</build_depend>
<build_depend>class_loader</build_depend>
<build_depend>rclcpp</build_depend>
<build_depend>rcutils</build_depend>
<build_depend>rosidl_cmake</build_depend>
<build_depend>std_msgs</build_depend>

<exec_depend>ament_index_cpp</exec_depend>
<exec_depend>class_loader</exec_depend>
<exec_depend>rclcpp</exec_depend>
<exec_depend>rcutils</exec_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<exec_depend>std_msgs</exec_depend>

<test_depend>ament_cmake_pytest</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>launch</test_depend>
<test_depend>launch_testing</test_depend>
<test_depend>rmw_implementation_cmake</test_depend>

<member_of_group>rosidl_interface_packages</member_of_group>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
81 changes: 81 additions & 0 deletions logging_demo/src/logger_config_component.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "logging_demo/logger_config_component.hpp"

#include <cinttypes>
#include <iostream>
#include <memory>

#include "logging_demo/srv/config_logger.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rcutils/error_handling.h"

namespace logging_demo
{

LoggerConfig::LoggerConfig()
: Node("logger_config")
{
srv_ = create_service<logging_demo::srv::ConfigLogger>(
"config_logger", std::bind(
&LoggerConfig::handle_logger_config_request,
this, std::placeholders::_1, std::placeholders::_2));
}

void
LoggerConfig::handle_logger_config_request(
const std::shared_ptr<logging_demo::srv::ConfigLogger::Request> request,
std::shared_ptr<logging_demo::srv::ConfigLogger::Response> response)
{
const char * severity_string = request->level.c_str();
RCLCPP_INFO(
this->get_name(), "Incoming request: logger '%s', severity '%s'",
request->logger_name.c_str(), severity_string);
std::flush(std::cout);
int severity;
if (strcmp("DEBUG", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_DEBUG;
} else if (strcmp("INFO", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_INFO;
} else if (strcmp("WARN", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_WARN;
} else if (strcmp("ERROR", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_ERROR;
} else if (strcmp("FATAL", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_FATAL;
} else if (strcmp("UNSET", severity_string) == 0) {
severity = RCUTILS_LOG_SEVERITY_UNSET;
} else {
RCLCPP_ERROR(
this->get_name(), "Unknown severity '%s'", severity_string);
response->success = false;
return;
}

// TODO(dhood): allow configuration through rclcpp
auto ret = rcutils_logging_set_logger_level(request->logger_name.c_str(), severity);
if (ret != RCUTILS_RET_OK) {
RCLCPP_ERROR(get_name(), "Error setting severity: %s", rcutils_get_error_string_safe());
rcutils_reset_error();
response->success = false;
}
response->success = true;
}

} // namespace logging_demo

#include "class_loader/class_loader_register_macro.h"

CLASS_LOADER_REGISTER_CLASS(logging_demo::LoggerConfig, rclcpp::Node)
Loading