简体   繁体   English

fmt std::string 显示为数字

[英]fmt std::string displayed as numbers

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.其中 m_Info 是 std::string,持续时间很长很长,ms 是双倍。 The resoult looks like this: -9891888000000.0 => Duration: 49172 micro, 412316861 ms结果如下所示: -9891888000000.0 => Duration: 49172 micro, 412316861 ms

The std::string was displayed as random numbers and ms was supposed to be 49,172. std::string 显示为随机数,ms 应该是 49,172。

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

but that resoulted in fmt::format_errror .但这导致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我在代码的同一部分使用了其他一些 fmt 函数

    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.在所有这些中std::stringdouble都显示为一些随机数。 The last function printed the unformatted std::string correctly, same happened when I tried it with long long and double .最后一个函数正确打印了未格式化的std::string ,当我用long longdouble尝试它时也发生了同样的情况。

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.您没有提供足够的信息来调试此问题,但可能是您的 fmtlib 副本和 spdlog 中的副本彼此不喜欢。 spdlog offers a cmake configuration variable to prevent this from happening, namely (from my cmake file) spdlog 提供了一个 cmake 配置变量来防止这种情况发生,即(来自我的 cmake 文件)

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

You might want to try this.你可能想试试这个。

I found the reason.我找到了原因。 It was because I included spdlog before fmt那是因为我在fmt之前包含了spdlog

BEFORE

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

AFTER

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM