2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
16 constexpr int kLogError = 1;
17 constexpr int kLogInfo = 2;
18 constexpr int kLogDebug = 3;
19 constexpr int kLogVerbose = 4;
26 using Clock = std::chrono::system_clock;
27 Clock::time_point lastUpdate_;
28 std::chrono::milliseconds refreshRate_;
31 explicit Logger(int level, FILE* out = stderr)
32 : out_(out), level_(level), lastUpdate_(Clock::now()),
36 bool logsAt(int level) {
37 return level <= level_;
40 template <typename... Args>
41 void operator()(int level, const char *fmt, Args... args) {
45 std::lock_guard<std::mutex> lock(mutex_);
46 std::fprintf(out_, fmt, args...);
49 template <typename... Args>
50 void update(int level, const char *fmt, Args... args) {
54 std::lock_guard<std::mutex> lock(mutex_);
55 auto now = Clock::now();
56 if (now - lastUpdate_ > refreshRate_) {
58 std::fprintf(out_, "\r");
59 std::fprintf(out_, fmt, args...);
63 void clear(int level) {
67 std::lock_guard<std::mutex> lock(mutex_);
68 std::fprintf(out_, "\r%79s\r", "");