Skip to content
Closed
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
45 changes: 44 additions & 1 deletion rcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(${PROJECT_NAME}_sources
src/rcl/expand_topic_name.c
src/rcl/graph.c
src/rcl/guard_condition.c
src/rcl/logging.c
src/rcl/node.c
src/rcl/publisher.c
src/rcl/rcl.c
Expand All @@ -42,6 +43,27 @@ set(${PROJECT_NAME}_sources
src/rcl/wait.c
)

# "watch" template for changes
configure_file(
"resource/logging_macros.h.em"
"logging_macros.h.em.watch"
COPYONLY)
# generate header with logging macros
set(python_code
"import em"
"em.invoke(['-o', 'include/rcl/logging_macros.h', '${CMAKE_CURRENT_SOURCE_DIR}/resource/logging_macros.h.em'])")
string(REPLACE ";" "$<SEMICOLON>" python_code "${python_code}")
add_custom_command(OUTPUT include/rcl/logging_macros.h
COMMAND ${CMAKE_COMMAND} -E make_directory "include/rcl"
COMMAND ${PYTHON_EXECUTABLE} ARGS -c "${python_code}"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/logging_macros.h.em.watch"
COMMENT "Expanding logging_macros.h.em"
VERBATIM
)
list(APPEND ${PROJECT_NAME}_sources
include/rcl/logging_macros.h)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be part of the sources?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the file is generated this makes sure the generation happens before the library is being compiled.

include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")

add_library(${PROJECT_NAME} ${${PROJECT_NAME}_sources})
ament_target_dependencies(${PROJECT_NAME}
"rcl_interfaces"
Expand Down Expand Up @@ -82,12 +104,33 @@ if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()

if(ament_cmake_cppcheck_FOUND)
ament_cppcheck(
TESTNAME "cppcheck_logging_macros"
"${CMAKE_CURRENT_BINARY_DIR}/include/rcl/logging_macros.h")
endif()
if(ament_cmake_cpplint_FOUND)
ament_cpplint(
TESTNAME "cpplint_logging_macros"
# the generated code might contain longer lines for templated types
MAX_LINE_LENGTH 999
ROOT "${CMAKE_CURRENT_BINARY_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}/include/rcl/logging_macros.h")
endif()
if(ament_cmake_uncrustify_FOUND)
ament_uncrustify(
TESTNAME "uncrustify_logging_macros"
# the generated code might contain longer lines for templated types
MAX_LINE_LENGTH 999
"${CMAKE_CURRENT_BINARY_DIR}/include/rcl/logging_macros.h")
endif()

add_subdirectory(test)
endif()

ament_package()

install(
DIRECTORY include/
DIRECTORY include/ ${CMAKE_CURRENT_BINARY_DIR}/include/
DESTINATION include
)
218 changes: 218 additions & 0 deletions rcl/include/rcl/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// 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/visibility_control.h"

#if __cplusplus
extern "C"
{
#endif

/// The flag if the logging system has been initialized.
RCL_PUBLIC
extern bool g_rcl_logging_initialized;

/// Initialize the logging system.
/**
* The function is called automatically when using the logging macros.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*/
RCL_PUBLIC
void rcl_logging_initialize();

/// The structure identifying the caller location in the source code.
typedef struct rcl_log_location_t
{
/// The name of the function containing the log call.
const char * function_name;
/// The name of the source file containing the log call.
const char * file_name;
/// The line number containing the log call.
size_t line_number;
} rcl_log_location_t;

/// The severity levels of log message.
enum RCL_LOG_SEVERITY
{
RCL_LOG_SEVERITY_DEBUG = 0, ///< The debug log level
RCL_LOG_SEVERITY_INFO = 1, ///< The info log level
RCL_LOG_SEVERITY_WARN = 2, ///< The warn log level
RCL_LOG_SEVERITY_ERROR = 3, ///< The error log level
RCL_LOG_SEVERITY_FATAL = 4, ///< The fatal log level
};

/// The function signature to log messages.
/**
* \param The pointer to the location struct
* \param The severity level
* \param The name of the logger
* \param The format string
* \param The variable argument list
*/
typedef void (* rcl_logging_output_handler_t)(
rcl_log_location_t *, // location
int, // severity
const char *, // name
const char *, // format
va_list * // args
);

/// The function pointer of the current output handler.
RCL_PUBLIC
extern rcl_logging_output_handler_t g_rcl_logging_output_handler;

/// Get the current output handler.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \return The function pointer of the current output handler.
*/
RCL_PUBLIC
rcl_logging_output_handler_t rcl_logging_get_output_handler();

/// Set the current output handler.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param function The function pointer of the output handler to be used.
*/
RCL_PUBLIC
void rcl_logging_set_output_handler(rcl_logging_output_handler_t function);

/// The global severity threshold before calling the output handler.
/**
* The global severity threshold is being checked after the conditions when
* using the various logging macros.
*
* \param severity The global severity threshold to be used.
*/
RCL_PUBLIC
extern int g_rcl_logging_severity_threshold;

/// Get the global severity threshold.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \return The severity threshold.
*/
RCL_PUBLIC
int rcl_logging_get_severity_threshold();

/// Set the global severity threshold.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param severity The severity threshold to be used.
*/
RCL_PUBLIC
void rcl_logging_set_severity_threshold(int severity);

/// Log a message.
/**
* The attributes of this function are also being influenced by the currently
* set output handler.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param location The pointer to the location struct
* \param severity The severity level
* \param name The name of the logger
* \param format The format string
* \param ... The variable arguments
*/
RCL_PUBLIC
void rcl_log(
rcl_log_location_t * location,
int severity,
const char * name,
const char * format,
...);

/// The default output handler outputs log messages to the standard streams.
/**
* The messages with a severity `DEBUG` and `INFO` are written to `stdout`.
* The messages with a severity `WARN`, `ERROR`, and `FATAL` are written to
* `stderr`.
* For each message the severity and name is prepended and the location
* information is appended when available.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No, for formatted messages <= 1023 characters
* | Yes, for formatted messages >= 1024 characters
* Thread-Safe | Yes, if the underlying *printf functions are
* Uses Atomics | No
* Lock-Free | Yes
*
* \param location The pointer to the location struct
* \param severity The severity level
* \param name The name of the logger
* \param format The format string
* \param args The variable argument list
*/
RCL_PUBLIC
void rcl_logging_console_output_handler(
rcl_log_location_t * location,
int severity, const char * name, const char * format, va_list * args);

#if __cplusplus
}
#endif

#endif // RCL__LOGGING_H_
4 changes: 4 additions & 0 deletions rcl/include/rcl/rcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
* - rcl/time.h
* - Error handling functionality (C style)
* - rcl/error_handling.h
* - Types and functions for the logging subsystem
* - rcl/logging.h
* - Macros to log messages with a specific severity level and optional conditions
* - rcl/logging_macros.h
* - Macros
* - rcl/macros.h
* - Return code types
Expand Down
3 changes: 3 additions & 0 deletions rcl/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_ros</buildtool_depend>
<buildtool_depend>python3-empy</buildtool_depend>

<build_export_depend>rmw</build_export_depend>

Expand All @@ -27,13 +28,15 @@

<depend>rmw_implementation</depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_nose</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>rmw</test_depend>
<test_depend>rmw_implementation_cmake</test_depend>
<test_depend>launch</test_depend>
<test_depend>launch_testing</test_depend>
<test_depend>example_interfaces</test_depend>
<test_depend>std_msgs</test_depend>

Expand Down
Loading