Skip to content

Instantly share code, notes, and snippets.

@clalancette
Created November 5, 2020 19:41
Show Gist options
  • Save clalancette/3f71fa3d0e47971bc86db3c47bf812d0 to your computer and use it in GitHub Desktop.
Save clalancette/3f71fa3d0e47971bc86db3c47bf812d0 to your computer and use it in GitHub Desktop.
format-literal cut-down example
#include <iostream>
#include <string>
typedef struct rcutils_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;
} rcutils_log_location_t;
enum RCUTILS_LOG_SEVERITY
{
RCUTILS_LOG_SEVERITY_UNSET = 0, ///< The unset log level
RCUTILS_LOG_SEVERITY_DEBUG = 10, ///< The debug log level
RCUTILS_LOG_SEVERITY_INFO = 20, ///< The info log level
RCUTILS_LOG_SEVERITY_WARN = 30, ///< The warn log level
RCUTILS_LOG_SEVERITY_ERROR = 40, ///< The error log level
RCUTILS_LOG_SEVERITY_FATAL = 50, ///< The fatal log level
};
void rcutils_log(
const rcutils_log_location_t * location,
int severity,
const char * name,
const char * format,
...)
__attribute__ ((format(printf, 4, 5)));
void rcutils_log(
const rcutils_log_location_t * location,
int severity,
const char * name,
const char * format,
...)
{
(void)location;
(void)severity;
(void)name;
(void)format;
}
namespace rclcpp {
const char *
get_c_string(const char * string_in)
{
return string_in;
}
const char *
get_c_string(const std::string & string_in)
{
return string_in.c_str();
}
} // namespace rclcpp
int main()
{
static rcutils_log_location_t __rcutils_logging_location = {
__func__,
"/root/ros2_ws/src/ros2/rclcpp/rclcpp_components/src/component_manager.cpp",
54
};
// This is the original line
//rcutils_log( & __rcutils_logging_location, RCUTILS_LOG_SEVERITY_DEBUG, get_logger().get_name(), rclcpp::get_c_string("Removing components from executor"), "");
// this statement causes the compile warning with -Wformat-literal on
//rcutils_log( & __rcutils_logging_location, RCUTILS_LOG_SEVERITY_DEBUG, "name", rclcpp::get_c_string("Removing components from executor"), "");
// this statement doesn't
rcutils_log( & __rcutils_logging_location, RCUTILS_LOG_SEVERITY_DEBUG, "name", "Removing components from executor");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment