Skip to content

Commit 454e5a4

Browse files
committed
Creating a logging configure function that can be used to select which output handlers you want to use and setting the framework to allow for multiple output handlers in the logging code.
cr https://code.amazon.com/reviews/CR-3734291
1 parent c35774e commit 454e5a4

11 files changed

Lines changed: 321 additions & 67 deletions

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ if(NOT CMAKE_CXX_STANDARD)
1111
set(CMAKE_CXX_STANDARD 14)
1212
endif()
1313

14+
include(cmake/get_default_rc_logging_implementation.cmake)
15+
16+
1417
find_package(ament_cmake_python REQUIRED)
1518
find_package(ament_cmake_ros REQUIRED)
1619

20+
get_default_rc_logging_implementation(RC_LOGGING_IMPL)
21+
1722
ament_python_install_package(${PROJECT_NAME})
1823

1924
include_directories(include)
@@ -91,6 +96,8 @@ add_library(
9196
${PROJECT_NAME}
9297
${rcutils_sources})
9398

99+
target_link_libraries(${PROJECT_NAME} ${${RC_LOGGING_IMPL}_LIBRARIES})
100+
94101
# Causes the visibility macros to use dllexport rather than dllimport,
95102
# which is appropriate when building the dll but not consuming it.
96103
target_compile_definitions(${PROJECT_NAME} PRIVATE "RCUTILS_BUILDING_DLL")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2018 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# Get the package name of the default logging implementation.
17+
#
18+
# Either selecting it using the variable RC_LOGGING_IMPLEMENTATION or
19+
# choosing a default from the available implementations.
20+
#
21+
# :param var: the output variable name containing the package name
22+
# :type var: string
23+
#
24+
macro(get_default_rc_logging_implementation var)
25+
26+
# if logging implementation already specified or RC_LOGGING_IMPLEMENTATION environment variable is set then use that,
27+
# otherwise default to using rc_logging_log4cxx
28+
if(NOT "${RC_LOGGING_IMPLEMENTATION}" STREQUAL "")
29+
set(_logging_implementation "${RC_LOGGING_IMPLEMENTATION}")
30+
elseif(NOT "$ENV{RC_LOGGING_IMPLEMENTATION}" STREQUAL "")
31+
set(_logging_implementation "$ENV{RC_LOGGING_IMPLEMENTATION}")
32+
else()
33+
set(_logging_implementation rc_logging_log4cxx)
34+
endif()
35+
36+
# persist implementation decision in cache
37+
# if it was not determined dynamically
38+
set(
39+
RC_LOGGING_IMPLEMENTATION "${_logging_implementation}"
40+
CACHE STRING "Select ROS middleware implementation to link against" FORCE
41+
)
42+
43+
find_package("${_logging_implementation}" REQUIRED)
44+
45+
set(${var} ${_logging_implementation})
46+
endmacro()

include/rcutils/logging.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ RCUTILS_PUBLIC
142142
RCUTILS_WARN_UNUSED
143143
rcutils_ret_t rcutils_logging_shutdown(void);
144144

145+
/// Configures the logging system.
146+
/**
147+
* This function should be called during the ROS initialization process. It will
148+
* add the enabled log output appenders to the root logger.
149+
*
150+
* <hr>
151+
* Attribute | Adherence
152+
* ------------------ | -------------
153+
* Allocates Memory | No
154+
* Thread-Safe | No
155+
* Uses Atomics | No
156+
* Lock-Free | Yes
157+
*
158+
* \param default_level The default severity level to log at
159+
* \param config_file The configuration file for the external logging library to use. Should be a null terminated string.
160+
* If NULL or an empty string the default configuration will be used
161+
* \param enable_stdout Should the stdout output appender be enabled
162+
* \param enable_rosout Should the rosout output appender be enabled
163+
* \param enable_ext_lib Should the external logger library output appender be enabled
164+
* \return `RCUTILS_RET_OK` if successful.
165+
* \return `RCUTILS_RET_ERR` if a general error occurs
166+
*/
167+
RCUTILS_PUBLIC
168+
rcutils_ret_t rcutils_logging_configure(int default_level, const char * config_file, bool enable_stdout, bool enable_rosout, bool enable_ext_lib);
169+
145170
/// The structure identifying the caller location in the source code.
146171
typedef struct rcutils_log_location_t
147172
{
@@ -196,16 +221,14 @@ rcutils_logging_severity_level_from_string(
196221
* \param severity The severity level
197222
* \param name The name of the logger
198223
* \param timestamp The timestamp
199-
* \param format The format string
200-
* \param args The variable argument list
224+
* \param log_str The string to be logged
201225
*/
202226
typedef void (* rcutils_logging_output_handler_t)(
203227
const rcutils_log_location_t *, // location
204228
int, // severity
205229
const char *, // name
206230
rcutils_time_point_value_t, // timestamp
207-
const char *, // format
208-
va_list * // args
231+
const char * // log_str
209232
);
210233

211234
/// The function pointer of the current output handler.
@@ -418,7 +441,8 @@ int rcutils_logging_get_logger_effective_level(const char * name);
418441
* <hr>
419442
* Attribute | Adherence
420443
* ------------------ | -------------
421-
* Allocates Memory | No
444+
* Allocates Memory | No, for formatted outputs <= 1023 characters
445+
* | Yes, for formatted outputs >= 1024 characters
422446
* Thread-Safe | No
423447
* Uses Atomics | No
424448
* Lock-Free | Yes
@@ -449,8 +473,7 @@ void rcutils_log(
449473
* <hr>
450474
* Attribute | Adherence
451475
* ------------------ | -------------
452-
* Allocates Memory | No, for formatted outputs <= 1023 characters
453-
* | Yes, for formatted outputs >= 1024 characters
476+
* Allocates Memory | No
454477
* Thread-Safe | Yes, if the underlying *printf functions are
455478
* Uses Atomics | No
456479
* Lock-Free | Yes
@@ -459,14 +482,13 @@ void rcutils_log(
459482
* \param severity The severity level
460483
* \param name The name of the logger, must be null terminated c string
461484
* \param timestamp The timestamp for when the log message was made
462-
* \param format The format string for the message contents
463-
* \param args The variable argument list for the message format string
485+
* \param log_str The string to be logged
464486
*/
465487
RCUTILS_PUBLIC
466488
void rcutils_logging_console_output_handler(
467489
const rcutils_log_location_t * location,
468490
int severity, const char * name, rcutils_time_point_value_t timestamp,
469-
const char * format, va_list * args);
491+
const char * log_str);
470492

471493
// Provide the compiler with branch prediction information
472494
#ifndef _WIN32
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2018 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCUTILS_LOGGING_EXTERNAL_INTERFACE_H_
16+
#define RCUTILS_LOGGING_EXTERNAL_INTERFACE_H_
17+
18+
#include <stdarg.h>
19+
#include "rcutils/types/rcutils_ret.h"
20+
#include "rcutils/visibility_control.h"
21+
22+
23+
/**
24+
* \brief Initializes the external logging library.
25+
*
26+
* \param config_file The location of a config file that the external logging library should use to configure itself.
27+
* If no config file is provided this will be set to an empty string. Must be a NULL terminated c string.
28+
* \return RC_EXTERNAL_LOGGING_RET_OK if initialized successfully or an error code if not.
29+
*/
30+
RCUTILS_PUBLIC
31+
rcutils_ret_t rcutils_logging_external_initialize(const char * config_file);
32+
33+
/**
34+
* \brief Free the resources allocated for the external logging system.
35+
* This puts the system into a state equivalent to being uninitialized
36+
*
37+
* \return RC_EXTERNAL_LOGGING_RET_OK if successfully shutdown or an error code if not.
38+
*/
39+
RCUTILS_PUBLIC
40+
rcutils_ret_t rcutils_logging_external_shutdown();
41+
42+
/**
43+
* \brief Logs a message
44+
*
45+
* \param severity The severity level of the message being logged
46+
* \param name The name of the logger, must be a null terminated c string or NULL. If NULL or empty the root logger will
47+
* be used.
48+
* \param msg The message to be logged. Must be a null terminated c string.
49+
*/
50+
RCUTILS_PUBLIC
51+
void rcutils_logging_external_log(int severity, const char * name, const char * msg);
52+
53+
54+
/**
55+
* \brief Set the severity level for a logger.
56+
* Sets the severity level for the specified logger. If the name provided is an empty string or NULL it will change the
57+
* level of the root logger.
58+
*
59+
* \param name The name of the logger. Must be a NULL terminated c string or NULL.
60+
* \param level - The severity level to be used for the specified logger
61+
* \return RC_EXTERNAL_LOGGING_RET_OK if set successfully or an error code if not.
62+
*/
63+
RCUTILS_PUBLIC
64+
rcutils_ret_t rcutils_logging_external_set_logger_level(const char * name, int level);
65+
66+
#endif

include/rcutils/macros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extern "C"
6262

6363
#define RCUTILS_STRINGIFY_IMPL(x) #x
6464
#define RCUTILS_STRINGIFY(x) RCUTILS_STRINGIFY_IMPL(x)
65+
#define RCUTILS_UNUSED(x) (void)(x)
6566

6667
#ifdef __cplusplus
6768
}

package.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3-
<package format="2">
3+
<package format="3">
44
<name>rcutils</name>
55
<version>0.6.0</version>
66
<description>Package containing various utility types and functions for C</description>
@@ -10,6 +10,9 @@
1010
<buildtool_depend>ament_cmake_ros</buildtool_depend>
1111
<buildtool_depend>python3-empy</buildtool_depend>
1212

13+
<depend>rc_logging_log4cxx</depend>
14+
<group_depend>rc_logging_packages</group_depend>
15+
1316
<test_depend>ament_cmake_gmock</test_depend>
1417
<test_depend>ament_cmake_gtest</test_depend>
1518
<test_depend>ament_cmake_pytest</test_depend>

src/allocator.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@
3131
static void *
3232
__default_allocate(size_t size, void * state)
3333
{
34-
(void)state; // unused
34+
RCUTILS_UNUSED(state);
3535
return malloc(size);
3636
}
3737

3838
static void
3939
__default_deallocate(void * pointer, void * state)
4040
{
41-
(void)state; // unused
41+
RCUTILS_UNUSED(state);
4242
free(pointer);
4343
}
4444

4545
static void *
4646
__default_reallocate(void * pointer, size_t size, void * state)
4747
{
48-
(void)state; // unused
48+
RCUTILS_UNUSED(state);
4949
return realloc(pointer, size);
5050
}
5151

5252
static void *
5353
__default_zero_allocate(size_t number_of_elements, size_t size_of_element, void * state)
5454
{
55-
(void)state; // unused
55+
RCUTILS_UNUSED(state);
5656
return calloc(number_of_elements, size_of_element);
5757
}
5858

0 commit comments

Comments
 (0)