Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
Logging.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
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).
8 */
9#pragma once
10
11#include <cstdio>
12#include <mutex>
13
14namespace pzstd {
15
16constexpr int kLogError = 1;
17constexpr int kLogInfo = 2;
18constexpr int kLogDebug = 3;
19constexpr int kLogVerbose = 4;
20
21class Logger {
22 std::mutex mutex_;
23 FILE* out_;
24 const int level_;
25
26 using Clock = std::chrono::system_clock;
27 Clock::time_point lastUpdate_;
28 std::chrono::milliseconds refreshRate_;
29
30 public:
31 explicit Logger(int level, FILE* out = stderr)
32 : out_(out), level_(level), lastUpdate_(Clock::now()),
33 refreshRate_(150) {}
34
35
36 bool logsAt(int level) {
37 return level <= level_;
38 }
39
40 template <typename... Args>
41 void operator()(int level, const char *fmt, Args... args) {
42 if (level > level_) {
43 return;
44 }
45 std::lock_guard<std::mutex> lock(mutex_);
46 std::fprintf(out_, fmt, args...);
47 }
48
49 template <typename... Args>
50 void update(int level, const char *fmt, Args... args) {
51 if (level > level_) {
52 return;
53 }
54 std::lock_guard<std::mutex> lock(mutex_);
55 auto now = Clock::now();
56 if (now - lastUpdate_ > refreshRate_) {
57 lastUpdate_ = now;
58 std::fprintf(out_, "\r");
59 std::fprintf(out_, fmt, args...);
60 }
61 }
62
63 void clear(int level) {
64 if (level > level_) {
65 return;
66 }
67 std::lock_guard<std::mutex> lock(mutex_);
68 std::fprintf(out_, "\r%79s\r", "");
69 }
70};
71
72}
Definition Logging.h:21
void update(int level, const char *fmt, Args... args)
Definition Logging.h:50
Logger(int level, FILE *out=stderr)
Definition Logging.h:31
bool logsAt(int level)
Definition Logging.h:36
void operator()(int level, const char *fmt, Args... args)
Definition Logging.h:41
void clear(int level)
Definition Logging.h:63
Definition ErrorHolder.h:16
constexpr int kLogVerbose
Definition Logging.h:19
constexpr int kLogDebug
Definition Logging.h:18
constexpr int kLogError
Definition Logging.h:16
constexpr int kLogInfo
Definition Logging.h:17
const lzma_allocator const uint8_t size_t uint8_t * out
Definition block.h:528
const char * fmt
Definition message.h:50