-1

I tried to format a simple string that will be displayed in the console

    long long duration = end - start;

    double ms = static_cast<double>(duration) * 0.001;

    std::string resoult = fmt::format("{} => Duration: {} micro, {} ms", m_Info.c_str(), duration, ms);
    AE_TRACE(resoult);

where m_Info is std::string, duration is long long and ms is double. The resoult looks like this: -9891888000000.0 => Duration: 49172 micro, 412316861 ms

The std::string was displayed as random numbers and ms was supposed to be 49,172.

I tried "{:s} => Duration: {:d} micro, {:f} ms"

but that resoulted in fmt::format_errror. I used the same library in other file in the same project and didn't get such errors.

EDIT

I used some other fmt functions in the same part of code

    long long duration = end - start;

    double ms = duration * 0.001;

    std::string test = "test";

    fmt::memory_buffer temp;

    fmt::format_to(temp, "{} {} {}", test, duration, ms);

    AE_TRACE(fmt::format("{} {} {}", test, duration, ms));
    AE_TRACE(temp.data());
    AE_TRACE("{} {} {}"_format(test, duration, ms));
    AE_TRACE(test);

In all of them std::string and double were displayed as some random numbers. The last function printed the unformatted std::string correctly, same happened when I tried it with long long and double.

M1loseph
  • 1
  • 1

2 Answers2

1

You are not giving enough information to debug this but it may be the case that your copy of fmtlib and the one in spdlog don't like each other. spdlog offers a cmake configuration variable to prevent this from happening, namely (from my cmake file)

set(SPDLOG_FMT_EXTERNAL ON CACHE BOOL "Use common fmtlib")

You might want to try this.

tobi_s
  • 1,324
  • 13
  • 16
-1

I found the reason. It was because I included spdlog before fmt

BEFORE

#include "../log/Log.h"
#include "fmt/format.h"

AFTER

#include "fmt/format.h"
#include "../log/Log.h"
M1loseph
  • 1
  • 1
  • You shouldn't be mixing two versions of {fmt} in your code. Either define `SPDLOG_FMT_EXTERNAL` as tobi_s suggested or use spdlog's version. – vitaut Dec 06 '19 at 15:56