-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogging.h
More file actions
134 lines (108 loc) · 2.92 KB
/
Copy pathlogging.h
File metadata and controls
134 lines (108 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) 2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <utility>
#include "config/config.h"
#include "utils.h"
using namespace config;
// Log verbosity levels. 0 = silent; higher values = more verbose.
// Matches QATzip's QzLogLevel_T convention.
enum class LogLevel {
LOG_NONE = 0,
LOG_ERROR = 1,
LOG_INFO = 2,
LOG_DEBUG = 3
};
#if defined(DEBUG_LOG) || defined(ENABLE_STATISTICS)
inline std::unique_ptr<std::ofstream>& LogFileStream() {
static std::unique_ptr<std::ofstream> stream;
return stream;
}
inline void CreateLogFile(const char* file_name) {
auto& s = LogFileStream();
s = std::make_unique<std::ofstream>(file_name, std::ios::app);
}
inline void CloseLogFile() {
auto& s = LogFileStream();
s.reset();
}
inline std::ostream& GetLogStream() {
auto& s = LogFileStream();
if (s && s->is_open()) {
return *s;
}
return std::cout;
}
#endif // DEBUG_LOG || ENABLE_STATISTICS
#ifdef DEBUG_LOG
static std::mutex log_mutex;
template <typename... Args>
inline void Log(LogLevel level, Args&&... args) {
std::lock_guard<std::mutex> lock(log_mutex);
uint32_t current_level = config::GetConfig(config::LOG_LEVEL);
if (current_level == static_cast<uint32_t>(LogLevel::LOG_NONE)) {
return;
}
if (static_cast<uint32_t>(level) > current_level) {
return;
}
std::ostream& stream = GetLogStream();
stream << std::dec;
switch (level) {
case LogLevel::LOG_ERROR:
stream << "Error: ";
break;
case LogLevel::LOG_INFO:
stream << "Info: ";
break;
case LogLevel::LOG_DEBUG:
stream << "Debug: ";
break;
case LogLevel::LOG_NONE:
return;
}
(..., (stream << args));
stream << std::flush;
}
#else
#define Log(...)
#endif
#ifdef ENABLE_STATISTICS
template <typename... Args>
inline void LogStats(Args&&... args) {
std::ostream& stream = GetLogStream();
stream << "Stats:\n";
(..., (stream << args));
stream << std::flush;
}
#else
#define LogStats(...)
#endif
#ifdef DEBUG_LOG
template <typename... Args>
inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, uint32_t len,
int window_bits, Args&&... args) {
uint32_t current_level = config::GetConfig(config::LOG_LEVEL);
if (current_level == static_cast<uint32_t>(LogLevel::LOG_NONE)) {
return;
}
if (static_cast<uint32_t>(level) > current_level) {
return;
}
CompressedFormat format = GetCompressedFormat(window_bits);
uint32_t header_length = GetHeaderLength(format);
if (len >= (header_length + 1)) {
Log(level, "Deflate block header bfinal = ",
static_cast<int>(data[header_length] & 0b00000001),
", btype = ", static_cast<int>((data[header_length] & 0b00000110) >> 1),
"\n", std::forward<Args>(args)...);
}
}
#else
#define PrintDeflateBlockHeader(...)
#endif