Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool LoadConfigFile(std::string& file_content, const char* file_path) {
"iaa_prepend_empty_block",
"qat_periodical_polling",
"qat_compression_level",
"qat_compression_allow_chunking",
"qat_compression_allow_chunking",
"log_level",
"log_stats_samples"
};
Expand Down
25 changes: 10 additions & 15 deletions config/config_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "../logging.h"

constexpr int PATH_MAX = 4096;
constexpr int CUSTOM_PATH_MAX = 4096;

bool ConfigReader::GetValue(const std::string& tag, uint32_t& value,
uint32_t max_value, uint32_t min_value) {
Expand All @@ -25,18 +25,15 @@ bool ConfigReader::GetValue(const std::string& tag, uint32_t& value,
unsigned long temp = std::stoul(it->second, &pos);

if (temp > UINT32_MAX) {
Log(LogLevel::LOG_ERROR,
"ConfigReader::GetValue Line %d value exceeds uint32_t range for tag "
"%s\n",
__LINE__, tag.c_str());
Log(LogLevel::LOG_ERROR, "ConfigReader::GetValue Line ", __LINE__,
" value exceeds uint32_t range for tag ", tag.c_str(), "\n");
value = 0;
return false;
}

if (pos != it->second.length() || temp < min_value || temp > max_value) {
Log(LogLevel::LOG_ERROR,
"ConfigReader::GetValue Line %d invalid input value for tag %s\n",
__LINE__, tag.c_str());
Log(LogLevel::LOG_ERROR, "ConfigReader::GetValue Line ", __LINE__,
" invalid input value for tag ", tag.c_str(), "\n");
value = 0;
return false;
}
Expand All @@ -45,9 +42,8 @@ bool ConfigReader::GetValue(const std::string& tag, uint32_t& value,
return true;

} catch (const std::exception&) {
Log(LogLevel::LOG_ERROR,
"ConfigReader::GetValue Line %d invalid input value for tag %s\n",
__LINE__, tag.c_str());
Log(LogLevel::LOG_ERROR, "ConfigReader::GetValue Line ", __LINE__,
" invalid input value for tag ", tag.c_str(), "\n");
value = 0;
return false;
}
Expand All @@ -62,9 +58,8 @@ bool ConfigReader::GetValue(const std::string& tag, std::string& value) {
value = it->second;

if (tag == "log_file" && !IsValidFileNameOrPath(value)) {
Log(LogLevel::LOG_ERROR,
"ConfigReader::GetValue Line %d invalid log_file value %s\n", __LINE__,
value.c_str());
Log(LogLevel::LOG_ERROR, "ConfigReader::GetValue Line ", __LINE__,
" invalid log_file value ", value.c_str(), "\n");
value.clear();
return false;
}
Expand Down Expand Up @@ -167,7 +162,7 @@ bool ConfigReader::IsValidFileNameOrPath(const std::string& input) {
}

// Check for length constraints
if (input.length() > PATH_MAX) {
if (input.length() > CUSTOM_PATH_MAX) {
return false;
}

Expand Down
49 changes: 23 additions & 26 deletions iaa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ uint32_t GetFormatFlag(int window_bits) {
int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
uint32_t* output_length, qpl_path_t execution_path,
int window_bits, uint32_t max_compressed_size, bool gzip_ext) {
Log(LogLevel::LOG_INFO, "CompressIAA() Line %d input_length %d\n", __LINE__,
*input_length);
Log(LogLevel::LOG_INFO, "CompressIAA() Line ", __LINE__, " input_length ",
*input_length, "\n");

// State from previous job execution not ignored/reset correctly for zlib
// format. Force job reinitialization.
Expand All @@ -69,8 +69,8 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,

qpl_job* job = job_.GetJob(execution_path);
if (job == nullptr) {
Log(LogLevel::LOG_ERROR, "CompressIAA() Line %d Error qpl_job is null\n",
__LINE__);
Log(LogLevel::LOG_ERROR, "CompressIAA() Line ", __LINE__,
" Error qpl_job is null\n");
return 1;
}

Expand Down Expand Up @@ -114,8 +114,8 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,

qpl_status status = qpl_execute_job(job);
if (status != QPL_STS_OK) {
Log(LogLevel::LOG_ERROR, "CompressIAA() Line %d status %d\n", __LINE__,
status);
Log(LogLevel::LOG_ERROR, "CompressIAA() Line ", __LINE__, " status ",
status, "\n");
return 1;
}
// In some cases, QPL compressed data size is larger than the upper bound
Expand All @@ -128,8 +128,8 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
*input_length = job->total_in;
*output_length = job->total_out;

Log(LogLevel::LOG_INFO, "CompressIAA() Line %d compressed_size %d\n",
__LINE__, *output_length);
Log(LogLevel::LOG_INFO, "CompressIAA() Line ", __LINE__, " compressed_size ",
*output_length, "\n");

if (output_shift > 0) {
uint32_t pos = 0;
Expand Down Expand Up @@ -181,8 +181,8 @@ int CompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
uint32_t* output_length, qpl_path_t execution_path,
int window_bits, bool* end_of_stream, bool detect_gzip_ext) {
Log(LogLevel::LOG_INFO, "UncompressIAA() Line %d input_length %d\n", __LINE__,
*input_length);
Log(LogLevel::LOG_INFO, "UncompressIAA() Line ", __LINE__, " input_length ",
*input_length, "\n");

bool gzip_ext = false;
uint32_t gzip_ext_src_size = 0;
Expand All @@ -198,8 +198,8 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,

qpl_job* job = job_.GetJob(execution_path);
if (job == nullptr) {
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line %d Error qpl_job is null\n",
__LINE__);
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line ", __LINE__,
" Error qpl_job is null\n");
return 1;
}

Expand All @@ -218,9 +218,8 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,

qpl_status status = qpl_execute_job(job);
if (status != QPL_STS_OK) {
Log(LogLevel::LOG_ERROR,
"UncompressIAA() Line %d qpl_execute_job status %d\n", __LINE__,
status);
Log(LogLevel::LOG_ERROR, "UncompressIAA() Line ", __LINE__,
" qpl_execute_job status ", status, "\n");
return 1;
}

Expand All @@ -231,8 +230,8 @@ int UncompressIAA(uint8_t* input, uint32_t* input_length, uint8_t* output,
*input_length = gzip_ext_dest_size + GZIP_EXT_HDRFTR_SIZE;
}
*end_of_stream = true;
Log(LogLevel::LOG_INFO, "UncompressIAA() Line %d output size %d\n", __LINE__,
job->total_out);
Log(LogLevel::LOG_INFO, "UncompressIAA() Line ", __LINE__, " output size ",
job->total_out, "\n");
return 0;
}

Expand All @@ -242,10 +241,9 @@ bool SupportedOptionsIAA(int window_bits, uint32_t input_length,
(window_bits >= 8 && window_bits <= 15) ||
(window_bits >= 24 && window_bits <= 31)) {
if (input_length > MAX_BUFFER_SIZE || output_length > MAX_BUFFER_SIZE) {
Log(LogLevel::LOG_INFO,
"SupportedOptionsIAA() Line %d input length %d or output length %d "
"is more than 2MB\n",
__LINE__, input_length, output_length);
Log(LogLevel::LOG_INFO, "SupportedOptionsIAA() Line ", __LINE__,
" input length ", input_length, " or output length ", output_length,
" is more than 2MB\n");
return false;
}
return true;
Expand All @@ -263,9 +261,8 @@ bool PrependedEmptyBlockPresent(uint8_t* input, uint32_t input_length,
if (input[header_length] == 0 && input[header_length + 1] == 0 &&
input[header_length + 2] == 0 && input[header_length + 3] == 0xFF &&
input[header_length + 4] == 0xFF) {
Log(LogLevel::LOG_INFO,
"PrependedEmptyBlockPresent() Line %d Empty block detected\n",
__LINE__);
Log(LogLevel::LOG_INFO, "PrependedEmptyBlockPresent() Line ", __LINE__,
" Empty block detected\n");
return true;
}

Expand All @@ -277,8 +274,8 @@ bool IsIAADecompressible(uint8_t* input, uint32_t input_length,
CompressedFormat format = GetCompressedFormat(window_bits);
if (format == CompressedFormat::ZLIB) {
int window = GetWindowSizeFromZlibHeader(input, input_length);
Log(LogLevel::LOG_INFO, "IsIAADecompressible() Line %d window %d\n",
__LINE__, window);
Log(LogLevel::LOG_INFO, "IsIAADecompressible() Line ", __LINE__, " window ",
window, "\n");
return window <= 12;
} else {
// if no empty block markers selected, we cannot tell for sure it's
Expand Down
79 changes: 39 additions & 40 deletions logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,89 @@

#pragma once

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <utility>

#include "config/config.h"
#include "utils.h"

using namespace config;

enum class LogLevel { LOG_NONE = 0, LOG_INFO = 1, LOG_ERROR = 2 };

#if defined(DEBUG_LOG) || defined(ENABLE_STATISTICS)
inline FILE* log_file_stream = nullptr;
inline std::unique_ptr<std::ofstream> log_file_stream = nullptr;

inline static void CreateLogFile(const char* file_name) {
log_file_stream = fopen(file_name, "a");
inline void CreateLogFile(const char* file_name) {
log_file_stream = std::make_unique<std::ofstream>(file_name, std::ios::app);
}

inline static void CloseLogFile() {
if (log_file_stream != nullptr) {
fclose(log_file_stream);
inline void CloseLogFile() { log_file_stream.reset(); }

inline std::ostream& GetLogStream() {
if (log_file_stream && log_file_stream->is_open()) {
return *log_file_stream;
}
return std::cout;
}
#endif

#ifdef DEBUG_LOG
static inline void Log(LogLevel level, const char* format, ...) {
static std::mutex log_mutex;

template <typename... Args>
inline void Log(LogLevel level, Args&&... args) {
std::lock_guard<std::mutex> lock(log_mutex);
if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) {
return;
}

FILE* stream = stdout;
if (log_file_stream != nullptr) {
stream = log_file_stream;
}

auto& stream = GetLogStream();
switch (level) {
case LogLevel::LOG_ERROR:
fprintf(stream, "Error: ");
stream << "Error: ";
break;
case LogLevel::LOG_INFO:
fprintf(stream, "Info: ");
stream << "Info: ";
break;
case LogLevel::LOG_NONE:
return;
}
va_list args;
va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
(..., (stream << args));
stream << std::flush;
}
#else
#define Log(...)
#endif

#ifdef ENABLE_STATISTICS
static inline void LogStats(const char* stats_str) {
FILE* stream = stdout;
if (log_file_stream != nullptr) {
stream = log_file_stream;
}

fprintf(stream, "Stats:\n");
fprintf(stream, "%s", stats_str);
template <typename... Args>
inline void LogStats(Args&&... args) {
auto& stream = GetLogStream();
stream << "Stats:\n";
(..., (stream << args));
stream << std::flush;
}
#else
#define LogStats(...)
#endif

#ifdef DEBUG_LOG
static inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data,
uint32_t len, int window_bits) {
template <typename... Args>
inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, uint32_t len,
int window_bits, Args&&... args) {
if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) {
return;
}

CompressedFormat format = GetCompressedFormat(window_bits);
uint32_t header_length = GetHeaderLength(format);
if (len >= (header_length + 1)) {
Log(level, "Deflate block header bfinal=%d, btype=%d\n",
data[header_length] & 0b00000001,
(data[header_length] & 0b00000110) >> 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
Expand Down
Loading