Skip to content

Commit

Permalink
fixup for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
nburek committed Jan 4, 2019
1 parent 1f48bf6 commit bf0b0a5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
27 changes: 15 additions & 12 deletions rcl/include/rcl/logging_rosout.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
// Copyright 2018-2019 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.
Expand Down Expand Up @@ -73,14 +73,14 @@ RCL_WARN_UNUSED
rcl_ret_t
rcl_logging_rosout_fini();

/// Creates a rosout publisher for a node and registers to be used by the logging system
/// Creates a rosout publisher for a node and registers it to be used by the logging system
/**
* Calling this for an rcl_node_t will create a new publisher on that node that will be
* used by the logging system to publish all log messages from that Node's logger.
*
* If a publisher already exists for this node then a new publisher will NOT be created.
*
* It is expected that after creating a rosout publisher with this function that
* It is expected that after creating a rosout publisher with this function
* rcl_logging_destroy_rosout_publisher_for_node() will be called for the node to cleanup
* the publisher while the Node is still valid.
*
Expand All @@ -96,7 +96,7 @@ rcl_logging_rosout_fini();
* \param[in] node a valid rcl_node_t that the publisher will be created on
* \return `RCL_RET_OK` if the publisher was created successfully, or
* \return `RCL_RET_ALREADY_INIT` if the publisher has already exists, or
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RCL_RET_NODE_INVALID` if any arguments are invalid, or
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
* \return `RCL_RET_ERROR` if an unspecified error occurs.
*/
Expand All @@ -123,7 +123,7 @@ rcl_logging_rosout_init_publisher_for_node(
* \param[in] node a valid rcl_node_t that the publisher will be created on
* \return `RCL_RET_OK` if the publisher was created successfully, or
* \return `RCL_RET_NOT_INIT` if the publisher does not exist for the provided node, or
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RCL_RET_NODE_INVALID` if any arguments are invalid, or
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
* \return `RCL_RET_ERROR` if an unspecified error occurs.
*/
Expand All @@ -150,17 +150,20 @@ rcl_logging_rosout_fini_publisher_for_node(
* Uses Atomics | No
* Lock-Free | Yes
*
* \param location The pointer to the location struct or NULL
* \param severity The severity level
* \param name The name of the logger, must be null terminated c string
* \param timestamp The timestamp for when the log message was made
* \param log_str The string to be logged
* \param[in] location The pointer to the location struct or NULL
* \param[in] severity The severity level
* \param[in] name The name of the logger, must be null terminated c string
* \param[in] timestamp The timestamp for when the log message was made
* \param[in] log_str The string to be logged
*/
RCL_PUBLIC
void rcl_logging_rosout_output_handler(
const rcutils_log_location_t * location,
int severity, const char * name, rcutils_time_point_value_t timestamp,
const char * format, va_list * args);
int severity,
const char * name,
rcutils_time_point_value_t timestamp,
const char * format,
va_list * args);

#ifdef __cplusplus
}
Expand Down
54 changes: 30 additions & 24 deletions rcl/src/rcl/logging_rosout.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Open Source Robotics Foundation, Inc.
// Copyright 2018-2019 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.
Expand All @@ -12,14 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCL__LOGGING_H_
#define RCL__LOGGING_H_


#include "rcl/allocator.h"
#include "rcl/error_handling.h"
#include "rcl/node.h"
#include "rcl/publisher.h"
#include "rcl/time.h"
#include "rcl/types.h"
#include "rcl/visibility_control.h"
#include "rcl_interfaces/msg/log.h"
Expand Down Expand Up @@ -111,7 +108,7 @@ rcl_ret_t rcl_logging_rosout_init_publisher_for_node(
rcl_ret_t status = RCL_RET_OK;

// Verify input and make sure it's not already initialized
RCL_CHECK_ARGUMENT_FOR_NULL(node, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(node, RCL_RET_NODE_INVALID);
logger_name = rcl_node_get_logger_name(node);
if (NULL == logger_name) {
RCL_SET_ERROR_MSG("Logger name was null.");
Expand Down Expand Up @@ -155,7 +152,7 @@ rcl_ret_t rcl_logging_rosout_fini_publisher_for_node(
rcl_ret_t status = RCL_RET_OK;

// Verify input and make sure it's initialized
RCL_CHECK_ARGUMENT_FOR_NULL(node, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(node, RCL_RET_NODE_INVALID);
logger_name = rcl_node_get_logger_name(node);
if (NULL == logger_name) {
return RCL_RET_ERROR;
Expand Down Expand Up @@ -201,21 +198,32 @@ void rcl_logging_rosout_output_handler(
va_copy(args_clone, *args);
status = rcutils_char_array_vsprintf(&msg_array, format, args_clone);
va_end(args_clone);


rcl_interfaces__msg__Log * log_message = rcl_interfaces__msg__Log__create();
if (NULL != log_message) {
log_message->stamp.sec = (timestamp / 1000000000);
log_message->stamp.nanosec = (timestamp % 1000000000);
log_message->level = severity;
log_message->line = location->line_number;
rosidl_generator_c__String__assign(&log_message->name, name);
rosidl_generator_c__String__assign(&log_message->msg, msg_array.buffer);
rosidl_generator_c__String__assign(&log_message->file, location->file_name);
rosidl_generator_c__String__assign(&log_message->function, location->function_name);
status = rcl_publish(&entry.publisher, log_message);

rcl_interfaces__msg__Log__destroy(log_message);
if (RCL_RET_OK != status) {
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to format log string: ");
RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
rcl_reset_error();
RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
} else {
rcl_interfaces__msg__Log * log_message = rcl_interfaces__msg__Log__create();
if (NULL != log_message) {
log_message->stamp.sec = RCL_NS_TO_S(timestamp);
log_message->stamp.nanosec = (timestamp % RCL_S_TO_NS(1));
log_message->level = severity;
log_message->line = location->line_number;
rosidl_generator_c__String__assign(&log_message->name, name);
rosidl_generator_c__String__assign(&log_message->msg, msg_array.buffer);
rosidl_generator_c__String__assign(&log_message->file, location->file_name);
rosidl_generator_c__String__assign(&log_message->function, location->function_name);
status = rcl_publish(&entry.publisher, log_message);
if (RCL_RET_OK != status) {
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to publish log message to rosout: ");
RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
rcl_reset_error();
RCUTILS_SAFE_FWRITE_TO_STDERR("\n");
}

rcl_interfaces__msg__Log__destroy(log_message);
}
}

status = rcutils_char_array_fini(&msg_array);
Expand All @@ -232,5 +240,3 @@ void rcl_logging_rosout_output_handler(
#ifdef __cplusplus
}
#endif

#endif // RCL__LOGGING_H_

0 comments on commit bf0b0a5

Please sign in to comment.