MengRao / fmtlog

fmtlog is a performant fmtlib-style logging library with latency in nanoseconds.
MIT License
807 stars 124 forks source link

我把fmtlog改了一下,可以写普通文件了. #9

Closed JaydenFish closed 3 years ago

JaydenFish commented 3 years ago

写普通文件就不需要TSCNS了. 前端延迟只需要1cycle, 我都惊呆了!

ps: TSCNS应该提炼出来, 再完善一下, 和fmt结合, 提供时间日期格式化功能.

MengRao commented 3 years ago

TSCNS本来就是一个独立的项目:https://github.com/MengRao/tscns 另外用fmt格式化ns可以自己实现:

static const int64_t SEC = 1000000000;
extern int64_t tzoffset_ns;

struct FmtNs
{
  FmtNs(int64_t ns_)
    : ns(ns_) {}
  int64_t ns;
};

template<>
struct fmt::formatter<FmtNs> : fmt::formatter<std::string_view>
{
  template<typename FormatContext>
  auto format(FmtNs ts, FormatContext& ctx) {
    uint64_t t = ts.ns + tzoffset_ns;
    Str<18> str;
    ((Str<9>*)&str[9])->fromi(t % SEC); // ns
    t /= SEC;
    str[8] = '.';
    ((Str<2>*)&str[6])->fromi(t % 60); // sec
    t /= 60;
    str[5] = ':';
    ((Str<2>*)&str[3])->fromi(t % 60); // min
    t /= 60;
    str[2] = ':';
    ((Str<2>*)&str[0])->fromi(t % 24); // hour

    return format_to(ctx.out(), "{}", str.tosv());
  }
};
int64_t ns = tscns.rdns();
fmt::print("ns: {}", FmtNs(ns));