Skip to content

provide logging macro signature that accepts std::string #570

@wjwwood

Description

@wjwwood

Feature request

Feature description

I'd like to have std::string (and possibly other common string containter) friendly logging macros.

For example, right now you must make logging calls like this:

std::string msg = "some error message from elsewhere";
RCLCPP_INFO(logger, ("some suffix: " + msg).c_str());

But I'd like to write:

std::string msg = "some error message from elsewhere";
RCLCPP_INFO(logger, "some suffix: " + msg);
// or just...
RCLCPP_INFO(logger, msg);

Implementation considerations

I think a helper function could make the macros simple, something like:

#include <string>

using namespace std::string_literals;

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();
}

#define LOG_INFO(msg) printf("%s\n", get_c_string(msg));

int main()
{
  LOG_INFO("plain c string");
  std::string a_str("a local std::string");
  LOG_INFO(a_str);
  LOG_INFO(std::string("a temporary std::string"));
  LOG_INFO("a std::string literal"s);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is neededin reviewWaiting for review (Kanban column)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions