gabime / spdlog

Fast C++ logging library.
Other
23.92k stars 4.49k forks source link

[Qustion] How can I output Eigen Vector in spdlog #1638

Closed Rullec closed 4 years ago

Rullec commented 4 years ago

Hi all,

Thanks for your attention.

According to lots of guys mentioned before, If "<<" is supported by one type, it can certainly be printed out by spdlog.

1. Question

Current I'm using Eigen this linear algebra library in my project. And I am eager to print out some Eigen::Vector3d in spdlog.

For example:

std::shared_ptr<spdlog::logger> logger = spdlog::stdout_color_mt("test");
Eigen::Vector3d vec = Eigen::Vector3d::Random();
logger->info("test output {}", vec);

But I got the following compilation error:

/usr/local/include/spdlog/fmt/bundled/core.h:1016:5: error: static_assert failed due to requirement
      'formattable' "Cannot format argument. To make type T formattable provide a formatter<T>
      specialization: https://fmt.dev/latest/api.html#formatting-user-defined-types"
    static_assert()

2. Possible Solution?

  1. "<<" has been overloaded by Eigen, why our spdlog can not prints these variables out?
  2. Do I need to follow the instructions indicated by this error message?

Any suggestion is appreciated! ;-)

tt4g commented 4 years ago

spdlog uses the fmt library to format its log messages.

The fmt library supports opereator<<(std::ostream &, T&), but it is an extension. Please add #include "spdlog/fmt/ostr.h" to enable this feature.

Rullec commented 4 years ago

spdlog uses the fmt library to format its log messages.

The fmt library supports opereator<<(std::ostream &, T&), but it is an extension. Please add #include "spdlog/fmt/ostr.h" to enable this feature.

Thanks for your quick reply, I will try it quickly now in otrder to give a feedback.

Rullec commented 4 years ago

It works, thanks!

whw-create commented 6 months ago

It works, thanks!

Hi, How can i set the precision of each element in a Matrix? Looking forward to your reply.

wspri commented 1 month ago

spdlog uses the fmt library to format its log messages.

The fmt library supports opereator<<(std::ostream &, T&), but it is an extension. Please add #include "spdlog/fmt/ostr.h" to enable this feature.

Hi, I came across the same problem, the method above works well in version 1.10.0, but faild in version 1.14.1

Orwlit commented 2 weeks ago

including the same header seems to fail format Eigen Matrix4d in spdlog v1.14.1. same issue here!! #include "spdlog/fmt/ostr.h"

tt4g commented 2 weeks ago

including the same header seems to fail format Eigen Matrix4d in spdlog v1.14.1. same issue here!! #include "spdlog/fmt/ostr.h"

@Orwlit Should you define fmt::ostream_formatter. See: https://github.com/gabime/spdlog/issues/3097#issuecomment-2132446276