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

Expose rcl default logging output handler #660

Merged
merged 2 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 53 additions & 0 deletions rcl/include/rcl/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
#include "rcl/types.h"
#include "rcl/visibility_control.h"

#include "rcutils/logging.h"

#ifdef __cplusplus
extern "C"
{
#endif

typedef rcutils_logging_output_handler_t rcl_logging_output_handler_t;

/// Configure the logging system.
/**
Expand Down Expand Up @@ -53,6 +56,34 @@ rcl_logging_configure(
const rcl_arguments_t * global_args,
const rcl_allocator_t * allocator);

/// Configure the logging system with the provided output handler.
/**
* Similar to rcl_logging_configure, but it uses the provided output handler.
* \sa rcl_logging_configure
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param global_args The global arguments for the system
* \param allocator Used to allocate memory used by the logging system
* \param output_handler Output handler to be installed
* \return `RCL_RET_OK` if successful, or
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
* \return `RCL_RET_ERR` if a general error occurs
*/
RCL_PUBLIC
RCL_WARN_UNUSED
rcl_ret_t
rcl_logging_configure_with_output_handler(
const rcl_arguments_t * global_args,
const rcl_allocator_t * allocator,
rcl_logging_output_handler_t output_handler);

/**
* This function should be called to tear down the logging setup by the configure function.
*
Expand Down Expand Up @@ -90,6 +121,28 @@ RCL_PUBLIC
RCL_WARN_UNUSED
bool rcl_logging_rosout_enabled();

/// Default output handler used by rcl.
/**
* This function can be wrapped in a language specific client library,
* adding the necessary mutual exclusion protection there, and then use
* `rcl_logging_configure_with_output_handler` instead of
* `rcl_logging_configure`.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | Yes
* Uses Atomics | No
* Lock-Free | Yes
*/
RCL_PUBLIC
void
rcl_logging_multiple_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);

#ifdef __cplusplus
}
#endif
Expand Down
25 changes: 12 additions & 13 deletions rcl/src/rcl/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ static bool g_rcl_logging_stdout_enabled = false;
static bool g_rcl_logging_rosout_enabled = false;
static bool g_rcl_logging_ext_lib_enabled = false;

/**
* An output function that sends to multiple output appenders.
*/
static
void
rcl_logging_multiple_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);

/**
* An output function that sends to the external logger library.
*/
Expand All @@ -64,7 +54,10 @@ rcl_logging_ext_lib_output_handler(
const char * format, va_list * args);

rcl_ret_t
rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t * allocator)
rcl_logging_configure_with_output_handler(
const rcl_arguments_t * global_args,
const rcl_allocator_t * allocator,
rcl_logging_output_handler_t output_handler)
{
RCL_CHECK_ARGUMENT_FOR_NULL(global_args, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(allocator, RCL_RET_INVALID_ARGUMENT);
Expand Down Expand Up @@ -106,10 +99,17 @@ rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t
rcl_logging_ext_lib_output_handler;
}
}
rcutils_logging_set_output_handler(rcl_logging_multiple_output_handler);
rcutils_logging_set_output_handler(output_handler);
return status;
}

rcl_ret_t
rcl_logging_configure(const rcl_arguments_t * global_args, const rcl_allocator_t * allocator)
{
return rcl_logging_configure_with_output_handler(
global_args, allocator, &rcl_logging_multiple_output_handler);
}

rcl_ret_t rcl_logging_fini()
{
rcl_ret_t status = RCL_RET_OK;
Expand All @@ -130,7 +130,6 @@ bool rcl_logging_rosout_enabled()
return g_rcl_logging_rosout_enabled;
}

static
void
rcl_logging_multiple_output_handler(
const rcutils_log_location_t * location,
Expand Down