Skip to content

Commit

Permalink
Moving the external logger library from rcutils into rcl. Also automa…
Browse files Browse the repository at this point in the history
…tic uncrustify.
  • Loading branch information
nburek committed Nov 28, 2018
1 parent cf4d247 commit 2914917
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 36 deletions.
6 changes: 6 additions & 0 deletions rcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ find_package(rosidl_generator_c REQUIRED)

include_directories(include)

include(cmake/get_default_rc_logging_implementation.cmake)
get_default_rc_logging_implementation(RC_LOGGING_IMPL)

# Default to C11
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
Expand All @@ -35,6 +38,7 @@ set(${PROJECT_NAME}_sources
src/rcl/guard_condition.c
src/rcl/lexer.c
src/rcl/lexer_lookahead.c
src/rcl/logging.c
src/rcl/node.c
src/rcl/publisher.c
src/rcl/rcl.c
Expand All @@ -56,6 +60,7 @@ ament_target_dependencies(${PROJECT_NAME}
"rmw"
"rcutils"
"rosidl_generator_c"
${RC_LOGGING_IMPL}
)

# Causes the visibility macros to use dllexport rather than dllimport,
Expand Down Expand Up @@ -84,6 +89,7 @@ ament_export_dependencies(rmw_implementation)
ament_export_dependencies(rmw)
ament_export_dependencies(rcutils)
ament_export_dependencies(rosidl_generator_c)
ament_export_dependencies(${RC_LOGGING_IMPL})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
Expand Down
46 changes: 46 additions & 0 deletions rcl/cmake/get_default_rc_logging_implementation.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2018 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.

#
# Get the package name of the default logging implementation.
#
# Either selecting it using the variable RC_LOGGING_IMPLEMENTATION or
# choosing a default from the available implementations.
#
# :param var: the output variable name containing the package name
# :type var: string
#
macro(get_default_rc_logging_implementation var)

# if logging implementation already specified or RC_LOGGING_IMPLEMENTATION environment variable
# is set then use that, otherwise default to using rc_logging_log4cxx
if(NOT "${RC_LOGGING_IMPLEMENTATION}" STREQUAL "")
set(_logging_implementation "${RC_LOGGING_IMPLEMENTATION}")
elseif(NOT "$ENV{RC_LOGGING_IMPLEMENTATION}" STREQUAL "")
set(_logging_implementation "$ENV{RC_LOGGING_IMPLEMENTATION}")
else()
set(_logging_implementation rc_logging_log4cxx)
endif()

# persist implementation decision in cache
# if it was not determined dynamically
set(
RC_LOGGING_IMPLEMENTATION "${_logging_implementation}"
CACHE STRING "Select ROS middleware implementation to link against" FORCE
)

find_package("${_logging_implementation}" REQUIRED)

set(${var} ${_logging_implementation})
endmacro()
12 changes: 6 additions & 6 deletions rcl/include/rcl/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ typedef struct rcl_arguments_t
struct rcl_arguments_impl_t * impl;
} rcl_arguments_t;

#define RCL_LOG_LEVEL_ARG_RULE "__log_level:="
#define RCL_EXTERNAL_LOG_CONFIG_ARG_RULE "__log_config_file:="
#define RCL_LOG_DISABLE_STDOUT_ARG_RULE "__log_disable_stdout:="
#define RCL_LOG_DISABLE_ROSOUT_ARG_RULE "__log_disable_rosout:="
#define RCL_LOG_DISABLE_EXT_LIB_ARG_RULE "__log_disable_external_lib:="
#define RCL_PARAM_FILE_ARG_RULE "__params:="
#define RCL_LOG_LEVEL_ARG_RULE "__log_level:="
#define RCL_EXTERNAL_LOG_CONFIG_ARG_RULE "__log_config_file:="
#define RCL_LOG_DISABLE_STDOUT_ARG_RULE "__log_disable_stdout:="
#define RCL_LOG_DISABLE_ROSOUT_ARG_RULE "__log_disable_rosout:="
#define RCL_LOG_DISABLE_EXT_LIB_ARG_RULE "__log_disable_external_lib:="
#define RCL_PARAM_FILE_ARG_RULE "__params:="

/// Return a rcl_node_t struct with members initialized to `NULL`.
RCL_PUBLIC
Expand Down
58 changes: 58 additions & 0 deletions rcl/include/rcl/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 RCL__LOGGING_H_
#define RCL__LOGGING_H_

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>

#include "rcl/allocator.h"
#include "rcl/arguments.h"
#include "rcl/error_handling.h"
#include "rcl/types.h"
#include "rcl/visibility_control.h"

#ifdef __cplusplus
extern "C"
{
#endif


/// Configures the logging system.
/**
* This function should be called during the ROS initialization process. It will
* add the enabled log output appenders to the root logger.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param global_args The global arguments for the system
* \return `RCL_RET_OK` if successful.
* \return `RCL_RET_ERR` if a general error occurs
*/
RCL_PUBLIC
rcl_ret_t rcl_logging_configure(const rcl_arguments_t * global_args);

#ifdef __cplusplus
}
#endif

#endif // RCL__LOGGING_H_
66 changes: 66 additions & 0 deletions rcl/include/rcl/logging_external_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2018 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 RCL__LOGGING_EXTERNAL_INTERFACE_H_
#define RCL__LOGGING_EXTERNAL_INTERFACE_H_

#include <stdarg.h>
#include "rcl/types.h"
#include "rcl/visibility_control.h"


/**
* \brief Initializes the external logging library.
*
* \param config_file The location of a config file that the external logging library should use to configure itself.
* If no config file is provided this will be set to an empty string. Must be a NULL terminated c string.
* \return RC_EXTERNAL_LOGGING_RET_OK if initialized successfully or an error code if not.
*/
RCL_PUBLIC
rcl_ret_t rcl_logging_external_initialize(const char * config_file);

/**
* \brief Free the resources allocated for the external logging system.
* This puts the system into a state equivalent to being uninitialized
*
* \return RC_EXTERNAL_LOGGING_RET_OK if successfully shutdown or an error code if not.
*/
RCL_PUBLIC
rcl_ret_t rcl_logging_external_shutdown();

/**
* \brief Logs a message
*
* \param severity The severity level of the message being logged
* \param name The name of the logger, must be a null terminated c string or NULL. If NULL or empty the root logger will
* be used.
* \param msg The message to be logged. Must be a null terminated c string.
*/
RCL_PUBLIC
void rcl_logging_external_log(int severity, const char * name, const char * msg);


/**
* \brief Set the severity level for a logger.
* Sets the severity level for the specified logger. If the name provided is an empty string or NULL it will change the
* level of the root logger.
*
* \param name The name of the logger. Must be a NULL terminated c string or NULL.
* \param level - The severity level to be used for the specified logger
* \return RC_EXTERNAL_LOGGING_RET_OK if set successfully or an error code if not.
*/
RCL_PUBLIC
rcl_ret_t rcl_logging_external_set_logger_level(const char * name, int level);

#endif // RCL__LOGGING_EXTERNAL_INTERFACE_H_
2 changes: 2 additions & 0 deletions rcl/include/rcl/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extern "C"
# define RCL_WARN_UNUSED _Check_return_
#endif

#define RCL_UNUSED(x) (void)(x)

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion rcl/package.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<package format="3">
<name>rcl</name>
<version>0.6.0</version>
<description>The ROS client library common implementation.
Expand All @@ -27,6 +27,8 @@

<depend>rmw_implementation</depend>

<group_depend>rc_logging_packages</group_depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_pytest</test_depend>
<test_depend>ament_lint_auto</test_depend>
Expand Down
42 changes: 27 additions & 15 deletions rcl/src/rcl/arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ rcl_parse_arguments(
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as parameter file rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as remap rule
Expand All @@ -272,7 +272,8 @@ rcl_parse_arguments(
continue;
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as remap rule. Error: %s", i, argv[i], rcl_get_error_string());
"Couldn't parse arg %d (%s) as remap rule. Error: %s", i, argv[i],
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as log level configuration
Expand All @@ -283,43 +284,54 @@ rcl_parse_arguments(
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as log level rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as log configuration file
if (RCL_RET_OK == _rcl_parse_external_log_config_file(argv[i], allocator, &args_impl->external_log_config_file)) {
rcl_ret_t ret = _rcl_parse_external_log_config_file(argv[i], allocator,
&args_impl->external_log_config_file);
if (RCL_RET_OK == ret) {
continue;
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as log config rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as log_stdout_disabled
if (RCL_RET_OK == _rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_STDOUT_ARG_RULE, &args_impl->log_stdout_disabled)) {
if (RCL_RET_OK ==
_rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_STDOUT_ARG_RULE,
&args_impl->log_stdout_disabled))
{
continue;
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as log_stdout_disabled rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as log_rosout_disabled
if (RCL_RET_OK == _rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_ROSOUT_ARG_RULE, &args_impl->log_rosout_disabled)) {
if (RCL_RET_OK ==
_rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_ROSOUT_ARG_RULE,
&args_impl->log_rosout_disabled))
{
continue;
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as log_rosout_disabled rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();

// Attempt to parse argument as log_ext_lib_disabled
if (RCL_RET_OK == _rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_EXT_LIB_ARG_RULE, &args_impl->log_ext_lib_disabled)) {
if (RCL_RET_OK ==
_rcl_parse_bool_arg(argv[i], RCL_LOG_DISABLE_EXT_LIB_ARG_RULE,
&args_impl->log_ext_lib_disabled))
{
continue;
}
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Couldn't parse arg %d (%s) as log_ext_lib_disabled rule. Error: %s", i, argv[i],
rcl_get_error_string());
rcl_get_error_string().str);
rcl_reset_error();


Expand Down Expand Up @@ -1170,10 +1182,10 @@ _rcl_parse_external_log_config_file(
{
RCL_CHECK_ARGUMENT_FOR_NULL(arg, RCL_RET_INVALID_ARGUMENT);

const size_t param_prefix_len = sizeof(RCL_EXTERNAL_LOG_CONFIG_ARG_RULE);
const size_t param_prefix_len = sizeof(RCL_EXTERNAL_LOG_CONFIG_ARG_RULE) - 1;
if (strncmp(RCL_EXTERNAL_LOG_CONFIG_ARG_RULE, arg, param_prefix_len) == 0) {
size_t outlen = strlen(arg) - param_prefix_len;
log_config_file = allocator.allocate(sizeof(char) * (outlen + 1), allocator.state);
*log_config_file = allocator.allocate(sizeof(char) * (outlen + 1), allocator.state);
if (NULL == log_config_file) {
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to allocate memory for parameters file path\n");
return RCL_RET_BAD_ALLOC;
Expand Down Expand Up @@ -1213,8 +1225,8 @@ _atob(
{
RCL_CHECK_ARGUMENT_FOR_NULL(str, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(val, RCL_RET_INVALID_ARGUMENT);
const char * true_values[] = {"y", "Y", "yes", "Yes", "t", "T", "true", "True", "1" };
const char * false_values[] = {"n", "N", "no", "No", "f", "F", "false", "False", "0" };
const char * true_values[] = {"y", "Y", "yes", "Yes", "t", "T", "true", "True", "1"};
const char * false_values[] = {"n", "N", "no", "No", "f", "F", "false", "False", "0"};

for (size_t idx = 0; idx < sizeof(true_values) / sizeof(char *); idx++) {
if (0 == strncmp(true_values[idx], str, strlen(true_values[idx]))) {
Expand Down
3 changes: 3 additions & 0 deletions rcl/src/rcl/arguments_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ typedef struct rcl_arguments_impl_t
int log_level;
/// A file used to configure the external logging library
char * external_log_config_file;
/// A boolean value indicating if the standard out handler should be used for log output
bool log_stdout_disabled;
/// A boolean value indicating if the rosout topic handler should be used for log output
bool log_rosout_disabled;
/// A boolean value indicating if the external lib handler should be used for log output
bool log_ext_lib_disabled;

/// Allocator used to allocate objects in this struct
Expand Down
6 changes: 3 additions & 3 deletions rcl/src/rcl/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ rcl_get_topic_names_and_types(
&rcutils_allocator,
no_demangle,
topic_names_and_types
);
);
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
}

Expand All @@ -76,7 +76,7 @@ rcl_get_service_names_and_types(
rcl_node_get_rmw_handle(node),
&rcutils_allocator,
service_names_and_types
);
);
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ rcl_service_server_is_available(
rcl_node_get_rmw_handle(node),
rcl_client_get_rmw_handle(client),
is_available
);
);
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
}

Expand Down
Loading

0 comments on commit 2914917

Please sign in to comment.