Skip to content
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
62 changes: 61 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(rcutils_sources
src/find.c
src/format_string.c
src/get_env.c
src/logging.c
src/repl_str.c
src/split.c
src/strdup.c
Expand All @@ -39,6 +40,27 @@ set_source_files_properties(
${rcutils_sources}
PROPERTIES language "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/rcutils/logging_macros.h', '${CMAKE_CURRENT_SOURCE_DIR}/resource/logging_macros.h.em'])")
string(REPLACE ";" "$<SEMICOLON>" python_code "${python_code}")
add_custom_command(OUTPUT include/rcutils/logging_macros.h
COMMAND ${CMAKE_COMMAND} -E make_directory "include/rcutils"
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 rcutils_sources
include/rcutils/logging_macros.h)
include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")

add_library(
${PROJECT_NAME}
SHARED
Expand Down Expand Up @@ -69,13 +91,51 @@ if(BUILD_TESTING)

find_package(ament_cmake_gmock REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_cmake_nose REQUIRED)
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/rcutils/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/rcutils/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/rcutils/logging_macros.h")
endif()

set(extra_test_libraries)
set(extra_test_env)
set(extra_lib_dirs)

ament_add_gtest(test_logging test/test_logging.cpp
APPEND_LIBRARY_DIRS ${extra_lib_dirs})
target_link_libraries(test_logging ${PROJECT_NAME})

add_executable(test_logging_long_messages test/test_logging_long_messages.cpp)
target_link_libraries(test_logging_long_messages ${PROJECT_NAME})
ament_add_nose_test(test_logging_long_messages
"test/test_logging_long_messages.py"
APPEND_LIBRARY_DIRS ${extra_lib_dirs}
WORKING_DIRECTORY "$<TARGET_FILE_DIR:test_logging_long_messages>"
TIMEOUT 10)

ament_add_gmock(test_logging_macros test/test_logging_macros.cpp
APPEND_LIBRARY_DIRS ${extra_lib_dirs})
target_link_libraries(test_logging_macros ${PROJECT_NAME})

# This subdirectory extends both extra_test_libraries, extra_test_env, and extra_lib_dirs.
add_subdirectory(test/memory_tools)

Expand Down Expand Up @@ -211,5 +271,5 @@ ament_export_libraries(${PROJECT_NAME})
ament_package()

install(
DIRECTORY include/
DIRECTORY include/ ${CMAKE_CURRENT_BINARY_DIR}/include/
DESTINATION include)
218 changes: 218 additions & 0 deletions include/rcutils/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 RCUTILS__LOGGING_H_
#define RCUTILS__LOGGING_H_

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

#include "rcutils/visibility_control.h"

#if __cplusplus
extern "C"
{
#endif

/// The flag if the logging system has been initialized.
RCUTILS_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
*/
RCUTILS_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 RCUTILS_LOG_SEVERITY
{
RCUTILS_LOG_SEVERITY_DEBUG = 0, ///< The debug log level
RCUTILS_LOG_SEVERITY_INFO = 1, ///< The info log level
RCUTILS_LOG_SEVERITY_WARN = 2, ///< The warn log level
RCUTILS_LOG_SEVERITY_ERROR = 3, ///< The error log level
RCUTILS_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.
RCUTILS_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.
*/
RCUTILS_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.
*/
RCUTILS_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.
*/
RCUTILS_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.
*/
RCUTILS_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.
*/
RCUTILS_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
*/
RCUTILS_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
*/
RCUTILS_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 // RCUTILS__LOGGING_H_
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>python3-empy</buildtool_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_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>launch_testing</test_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down
Loading