2 * Copyright (c) 2016-present, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
16 constexpr int ERROR
= 1;
17 constexpr int INFO
= 2;
18 constexpr int DEBUG
= 3;
19 constexpr int VERBOSE
= 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", "");