0

When I use glog to print an Eigen matrix in console, I found the output format has difference when using std::cout against using LOG(INFO). Here is the source code, in which 'sophus_R' is a 3x3 matrix.

LOG(INFO) << "using glog, sophus_R is:\n" << sophus_R.matrix();
std::cout << "using std::cout, sophus_R is:\n" << sophus_R.matrix() << std::endl;

And here is the output:

I0606 16:07:06.307516 122123 so3_test.cc:30] using glog, sophus_R is:
00000.696364 000-0.707107 00000.122788
00000.696364 00000.707107 00000.122788
000-0.173648 -2.08167e-17 00000.984808
using std::cout, sophus_R is:
    0.696364    -0.707107     0.122788
    0.696364     0.707107     0.122788
   -0.173648 -2.08167e-17     0.984808

It seems glog will automatically fill '0' to the empty place before number. How can I make glog output format be same with std::cout(not fill 0)?

  • 1
    Have you tried `LOG(INFO) << std::setfill(' ') << ...;`? – Ted Lyngmo Jun 06 '22 at 08:17
  • I tried but failed when compiling, error is: ‘setfill’ is not a member of ‘std’; did you mean ‘fill’?, then I tried std::fill(' '), but it also doesn't work. – Zhang Zheng Jun 06 '22 at 08:23
  • You need to `#include ` to use `std::setfill`. – Ted Lyngmo Jun 06 '22 at 08:24
  • I tested setfill() after include, unfortunately, it also doesn't work: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘std::_Setfill’) 31 | LOG(INFO) << "using glog, sophus_R is:\n" << std::setfill(" ") << sophus_R.matrix(); | ^~ ~~~~~~~~~~~~~~~~~ | | | std::_Setfill – Zhang Zheng Jun 06 '22 at 08:28
  • 1
    Please copy what I wrote exactly as I wrote it: `std::setfill(' ')` - as you can see at [`std::setfill`](https://en.cppreference.com/w/cpp/io/manip/setfill), it takes a `char`, not a `const char*`. – Ted Lyngmo Jun 06 '22 at 09:04
  • 1
    It success, thanks for your patient help! – Zhang Zheng Jun 06 '22 at 11:22

1 Answers1

1

It seems the glog stream's fill character has been set to 0. I suggest explicitly setting it back to a space, ' ' using std::setfill:

#include <iomanip>

LOG(INFO) << "using glog, sophus_R is:\n" << std::setfill(' ') << sophus_R.matrix();
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108