Skip to content

Commit aeaf218

Browse files
committed
Refactor logging and statistics based on ISO C++ Core Guidelines. More specifically:
- Avoid C variable args, use variadic templates instead. - Avoid raw pointers. - Avoid C arrays. - Use scoped enums Signed-off-by: Mulugeta Mammo <[email protected]>
1 parent 0026971 commit aeaf218

File tree

11 files changed

+263
-251
lines changed

11 files changed

+263
-251
lines changed

common.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ if(TSAN)
7171
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
7272
endif()
7373

74-
set(CMAKE_CXX_STANDARD 17)
74+
set(CMAKE_CXX_STANDARD 20)
7575
set(CMAKE_CXX_STANDARD_REQUIRED True)
7676

7777
if(USE_IAA)

config/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool LoadConfigFile(std::string& file_content, const char* file_path) {
5050
"iaa_prepend_empty_block",
5151
"qat_periodical_polling",
5252
"qat_compression_level",
53-
"qat_compression_allow_chunking",
53+
"qat_compression_allow_chunking",
5454
"log_level",
5555
"log_stats_samples"
5656
};

config/config_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool ConfigReader::GetValue(std::string tag, uint32_t& value,
2626
ret_val = true;
2727
} else {
2828
Log(LogLevel::LOG_INFO,
29-
"ConfigReader::GetValue Line %d invalid input value for tag %s\n",
29+
"ConfigReader::GetValue Line {} invalid input value for tag {}\n",
3030
__LINE__, tag.c_str());
3131
value = 0;
3232
ret_val = false;
@@ -43,7 +43,7 @@ bool ConfigReader::GetValue(std::string tag, std::string& value) {
4343
value = it->second;
4444
if ((tag == "log_file") && !IsValidFileNameOrPath(value)) {
4545
Log(LogLevel::LOG_INFO,
46-
"ConfigReader::GetValue Line %d invalid log_file value %s\n",
46+
"ConfigReader::GetValue Line {} invalid log_file value {}\n",
4747
__LINE__, value.c_str());
4848
value = "";
4949
ret_val = false;

iaa.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ uint32_t GetFormatFlag(int window_bits) {
5656
int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
5757
uint32_t* output_length, qpl_path_t execution_path,
5858
int window_bits, uint32_t max_compressed_size, bool gzip_ext) {
59-
Log(LogLevel::LOG_INFO, "CompressIAA() Line %d input_length %d\n", __LINE__,
59+
Log(LogLevel::LOG_INFO, "CompressIAA() Line {} input_length {}\n", __LINE__,
6060
*input_length);
6161

6262
// State from previous job execution not ignored/reset correctly for zlib
@@ -68,7 +68,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
6868

6969
qpl_job* job = job_.GetJob(execution_path);
7070
if (job == nullptr) {
71-
Log(LogLevel::LOG_ERROR, "CompressIAA() Line %d Error qpl_job is null\n",
71+
Log(LogLevel::LOG_ERROR, "CompressIAA() Line {} Error qpl_job is null\n",
7272
__LINE__);
7373
return 1;
7474
}
@@ -113,7 +113,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
113113

114114
qpl_status status = qpl_execute_job(job);
115115
if (status != QPL_STS_OK) {
116-
Log(LogLevel::LOG_ERROR, "CompressIAA() Line %d status %d\n", __LINE__,
116+
Log(LogLevel::LOG_ERROR, "CompressIAA() Line {} status {}\n", __LINE__,
117117
status);
118118
return 1;
119119
}
@@ -127,7 +127,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
127127
*input_length = job->total_in;
128128
*output_length = job->total_out;
129129

130-
Log(LogLevel::LOG_INFO, "CompressIAA() Line %d compressed_size %d\n",
130+
Log(LogLevel::LOG_INFO, "CompressIAA() Line {} compressed_size {}\n",
131131
__LINE__, *output_length);
132132

133133
if (output_shift > 0) {
@@ -180,7 +180,7 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
180180
int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
181181
uint32_t* output_length, qpl_path_t execution_path,
182182
int window_bits, bool* end_of_stream, bool detect_gzip_ext) {
183-
Log(LogLevel::LOG_INFO, "UncompressIAA() Line %d input_length %d\n", __LINE__,
183+
Log(LogLevel::LOG_INFO, "UncompressIAA() Line {} input_length {}\n", __LINE__,
184184
*input_length);
185185

186186
bool gzip_ext = false;
@@ -197,7 +197,7 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
197197

198198
qpl_job* job = job_.GetJob(execution_path);
199199
if (job == nullptr) {
200-
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line %d Error qpl_job is null\n",
200+
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line {} Error qpl_job is null\n",
201201
__LINE__);
202202
return 1;
203203
}
@@ -218,7 +218,7 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
218218
qpl_status status = qpl_execute_job(job);
219219
if (status != QPL_STS_OK) {
220220
Log(LogLevel::LOG_ERROR,
221-
"UncompressIAA() Line %d qpl_execute_job status %d\n", __LINE__,
221+
"UncompressIAA() Line {} qpl_execute_job status {}\n", __LINE__,
222222
status);
223223
return 1;
224224
}
@@ -230,7 +230,7 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
230230
*input_length = gzip_ext_dest_size + GZIP_EXT_HDRFTR_SIZE;
231231
}
232232
*end_of_stream = true;
233-
Log(LogLevel::LOG_INFO, "UncompressIAA() Line %d output size %d\n", __LINE__,
233+
Log(LogLevel::LOG_INFO, "UncompressIAA() Line {} output size {}\n", __LINE__,
234234
job->total_out);
235235
return 0;
236236
}
@@ -242,7 +242,7 @@ bool SupportedOptionsIAA(int window_bits, uint32_t input_length,
242242
(window_bits >= 24 && window_bits <= 31)) {
243243
if (input_length > MAX_BUFFER_SIZE || output_length > MAX_BUFFER_SIZE) {
244244
Log(LogLevel::LOG_INFO,
245-
"SupportedOptionsIAA() Line %d input length %d or output length %d "
245+
"SupportedOptionsIAA() Line {} input length {} or output length {} "
246246
"is more than 2MB\n",
247247
__LINE__, input_length, output_length);
248248
return false;
@@ -263,7 +263,7 @@ bool PrependedEmptyBlockPresent(uint8_t* input, uint32_t input_length,
263263
input[header_length + 2] == 0 && input[header_length + 3] == 0xFF &&
264264
input[header_length + 4] == 0xFF) {
265265
Log(LogLevel::LOG_INFO,
266-
"PrependedEmptyBlockPresent() Line %d Empty block detected\n",
266+
"PrependedEmptyBlockPresent() Line {} Empty block detected\n",
267267
__LINE__);
268268
return true;
269269
}
@@ -276,7 +276,7 @@ bool IsIAADecompressible(uint8_t* input, uint32_t input_length,
276276
CompressedFormat format = GetCompressedFormat(window_bits);
277277
if (format == CompressedFormat::ZLIB) {
278278
int window = GetWindowSizeFromZlibHeader(input, input_length);
279-
Log(LogLevel::LOG_INFO, "IsIAADecompressible() Line %d window %d\n",
279+
Log(LogLevel::LOG_INFO, "IsIAADecompressible() Line {} window {}\n",
280280
__LINE__, window);
281281
return window <= 12;
282282
} else {

logging.h

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,92 @@
33

44
#pragma once
55

6-
#include <errno.h>
7-
#include <fcntl.h>
8-
#include <stdarg.h>
9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <string.h>
12-
#include <unistd.h>
6+
#include <format>
7+
#include <fstream>
8+
#include <iostream>
9+
#include <memory>
10+
#include <mutex>
11+
#include <sstream>
12+
#include <utility>
1313

1414
#include "config/config.h"
1515
#include "utils.h"
16+
1617
using namespace config;
18+
1719
enum class LogLevel { LOG_NONE = 0, LOG_INFO = 1, LOG_ERROR = 2 };
1820

1921
#if defined(DEBUG_LOG) || defined(ENABLE_STATISTICS)
20-
inline FILE* log_file_stream = nullptr;
22+
inline std::unique_ptr<std::ofstream> log_file_stream = nullptr;
2123

22-
inline static void CreateLogFile(const char* file_name) {
23-
log_file_stream = fopen(file_name, "a");
24+
inline void CreateLogFile(const char* file_name) {
25+
log_file_stream = std::make_unique<std::ofstream>(file_name, std::ios::app);
2426
}
2527

26-
inline static void CloseLogFile() {
27-
if (log_file_stream != nullptr) {
28-
fclose(log_file_stream);
28+
inline void CloseLogFile() { log_file_stream.reset(); }
29+
30+
inline std::ostream& GetLogStream() {
31+
if (log_file_stream && log_file_stream->is_open()) {
32+
return *log_file_stream;
2933
}
34+
return std::cout;
3035
}
36+
3137
#endif
3238

3339
#ifdef DEBUG_LOG
34-
static inline void Log(LogLevel level, const char* format, ...) {
40+
41+
static std::mutex log_mutex;
42+
43+
template <typename... Args>
44+
inline void Log(LogLevel level, const char* fmt, Args&&... args) {
45+
std::lock_guard<std::mutex> lock(log_mutex);
46+
3547
if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) {
3648
return;
3749
}
38-
39-
FILE* stream = stdout;
40-
if (log_file_stream != nullptr) {
41-
stream = log_file_stream;
42-
}
43-
50+
auto& stream = GetLogStream();
4451
switch (level) {
4552
case LogLevel::LOG_ERROR:
46-
fprintf(stream, "Error: ");
53+
stream << "Error: ";
4754
break;
4855
case LogLevel::LOG_INFO:
49-
fprintf(stream, "Info: ");
56+
stream << "Info: ";
5057
break;
5158
case LogLevel::LOG_NONE:
5259
return;
5360
}
54-
va_list args;
55-
va_start(args, format);
56-
vfprintf(stream, format, args);
57-
va_end(args);
61+
stream << std::vformat(fmt, std::make_format_args(args...));
5862
}
5963
#else
6064
#define Log(...)
6165
#endif
6266

6367
#ifdef ENABLE_STATISTICS
64-
static inline void LogStats(const char* stats_str) {
65-
FILE* stream = stdout;
66-
if (log_file_stream != nullptr) {
67-
stream = log_file_stream;
68-
}
69-
70-
fprintf(stream, "Stats:\n");
71-
fprintf(stream, "%s", stats_str);
68+
template <typename... Args>
69+
inline void LogStats(const char* fmt, Args&&... args) {
70+
auto& stream = GetLogStream();
71+
stream << "Stats:\n";
72+
stream << std::vformat(fmt, std::make_format_args(args...));
7273
}
7374
#else
7475
#define LogStats(...)
7576
#endif
7677

7778
#ifdef DEBUG_LOG
78-
static inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data,
79-
uint32_t len, int window_bits) {
79+
template <typename... Args>
80+
inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, uint32_t len,
81+
int window_bits, Args&&... args) {
8082
if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) {
8183
return;
8284
}
83-
8485
CompressedFormat format = GetCompressedFormat(window_bits);
8586
uint32_t header_length = GetHeaderLength(format);
8687
if (len >= (header_length + 1)) {
87-
Log(level, "Deflate block header bfinal=%d, btype=%d\n",
88-
data[header_length] & 0b00000001,
89-
(data[header_length] & 0b00000110) >> 1);
88+
Log(level, "Deflate block header bfinal = {}, btype = {}\n",
89+
static_cast<int>(data[header_length] & 0b00000001),
90+
static_cast<int>((data[header_length] & 0b00000110) >> 1),
91+
std::forward<Args>(args)...);
9092
}
9193
}
9294
#else

0 commit comments

Comments
 (0)