diff --git a/config/config.cpp b/config/config.cpp index 8ef0014..1265b95 100644 --- a/config/config.cpp +++ b/config/config.cpp @@ -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" }; diff --git a/config/config_reader.cpp b/config/config_reader.cpp index 0ffbd51..64a166d 100644 --- a/config/config_reader.cpp +++ b/config/config_reader.cpp @@ -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) { @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/iaa.cpp b/iaa.cpp index 7dbb10c..0d1ac00 100644 --- a/iaa.cpp +++ b/iaa.cpp @@ -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. @@ -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; } @@ -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 @@ -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; @@ -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; @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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; } @@ -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 diff --git a/logging.h b/logging.h index ceb1620..911c473 100644 --- a/logging.h +++ b/logging.h @@ -3,90 +3,89 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #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 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(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 +inline void Log(LogLevel level, Args&&... args) { + std::lock_guard lock(log_mutex); if (static_cast(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 +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 +inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, uint32_t len, + int window_bits, Args&&... args) { if (static_cast(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(data[header_length] & 0b00000001), + ", btype = ", static_cast((data[header_length] & 0b00000110) >> 1), + "\n", std::forward(args)...); } } #else diff --git a/qat.cpp b/qat.cpp index bf2c17c..ba35405 100644 --- a/qat.cpp +++ b/qat.cpp @@ -6,7 +6,9 @@ #include "config/config.h" #include "logging.h" #include "utils.h" + using namespace config; + #ifdef USE_QAT #include @@ -79,16 +81,14 @@ void QATJob::Init(QzSession_T **qzSession, CompressedFormat format, // Initialize QAT hardware int status = qzInit(*qzSession, 0); if (status != QZ_OK && status != QZ_DUPLICATE) { - Log(LogLevel::LOG_ERROR, - "qzInit() failure Line %d session %p returned %d\n", __LINE__, - *qzSession, status); + Log(LogLevel::LOG_ERROR, "qzInit() failure Line ", __LINE__, " session ", + static_cast(*qzSession), " returned ", status, "\n"); delete *qzSession; *qzSession = nullptr; return; } else { - Log(LogLevel::LOG_INFO, - "qzInit() success Line %d session %p returned %d\n", __LINE__, - *qzSession, status); + Log(LogLevel::LOG_INFO, "qzInit() success Line ", __LINE__, " session ", + static_cast(*qzSession), " returned ", status, "\n"); } QzSessionParamsDeflateExt_T deflateExt = {{}, 0, 0}; @@ -136,9 +136,9 @@ void QATJob::Init(QzSession_T **qzSession, CompressedFormat format, } status = qzSetupSessionDeflateExt(*qzSession, &deflateExt); if (status != QZ_OK) { - Log(LogLevel::LOG_ERROR, - "qzSetupSessionDeflateExt() Line %d session %p returned %d\n", __LINE__, - *qzSession, status); + Log(LogLevel::LOG_ERROR, "qzSetupSessionDeflateExt() Line ", __LINE__, + " session ", static_cast(*qzSession), " returned ", status, + "\n"); Close(); return; } @@ -162,16 +162,15 @@ void QATJob::Close(QzSession_T *qzSession) { int rc = qzTeardownSession(qzSession); if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, - "qzTeardownSession() Line %d session %p returned %d\n", __LINE__, - qzSession, rc); + Log(LogLevel::LOG_ERROR, "qzTeardownSession() Line ", __LINE__, " session ", + static_cast(qzSession), " returned ", rc, "\n"); } // Attempt to close the session rc = qzClose(qzSession); if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, "qzClose() Line %d session %p returned %d\n", - __LINE__, qzSession, rc); + Log(LogLevel::LOG_ERROR, "qzClose() Line ", __LINE__, " session ", + static_cast(qzSession), " returned ", rc, "\n"); } delete qzSession; @@ -181,12 +180,12 @@ static thread_local QATJob qat_job_; int CompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, uint32_t *output_length, int window_bits, bool gzip_ext) { - Log(LogLevel::LOG_INFO, "CompressQAT() Line %d input_length %d \n", __LINE__, - *input_length); + Log(LogLevel::LOG_INFO, "CompressQAT() Line ", __LINE__, " input_length ", + *input_length, " \n"); QzSession_T *qzSessObj = qat_job_.GetQATSession(window_bits, gzip_ext); if (qzSessObj == nullptr) { - Log(LogLevel::LOG_ERROR, "CompressQAT() Line %d Error qzSessObj null \n", - __LINE__); + Log(LogLevel::LOG_ERROR, "CompressQAT() Line ", __LINE__, + " Error qzSessObj null \n"); return 1; } unsigned int src_buf_size = static_cast(*input_length); @@ -194,22 +193,22 @@ int CompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, int rc = qzCompress(qzSessObj, (unsigned char *)input, &src_buf_size, (unsigned char *)output, &dst_buf_size, 1); if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, - "CompressQAT() Line %d qzCompress returns status %d \n", __LINE__, rc); + Log(LogLevel::LOG_ERROR, "CompressQAT() Line ", __LINE__, + " qzCompress returns status ", rc, " \n"); return rc; } *input_length = src_buf_size; *output_length = dst_buf_size; - Log(LogLevel::LOG_INFO, "CompressQAT() Line %d compressed_size %d \n", - __LINE__, *output_length); + Log(LogLevel::LOG_INFO, "CompressQAT() Line ", __LINE__, " compressed_size ", + *output_length, " \n"); return 0; } int UncompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, uint32_t *output_length, int window_bits, bool *end_of_stream, bool detect_gzip_ext) { - Log(LogLevel::LOG_INFO, "UncompressQAT() Line %d input_length %d \n", - __LINE__, *input_length); + Log(LogLevel::LOG_INFO, "UncompressQAT() Line ", __LINE__, " input_length ", + *input_length, " \n"); bool gzip_ext = false; uint32_t gzip_ext_src_size = 0; @@ -221,8 +220,8 @@ int UncompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, QzSession_T *qzSessObj = qat_job_.GetQATSession(window_bits, gzip_ext); if (qzSessObj == nullptr) { - Log(LogLevel::LOG_ERROR, "UncompressQAT() Line %d Error qzSessObj null \n", - __LINE__); + Log(LogLevel::LOG_ERROR, "UncompressQAT() Line ", __LINE__, + " Error qzSessObj null \n"); return 1; } @@ -234,8 +233,8 @@ int UncompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, int rc = qzDecompress(qzSessObj, (unsigned char *)input, &src_buf_size, (unsigned char *)output, &dst_buf_size); if (rc != QZ_OK) { - Log(LogLevel::LOG_ERROR, - "UncompressQAT() Line %d qzDecompress status %d \n", __LINE__, rc); + Log(LogLevel::LOG_ERROR, "UncompressQAT() Line ", __LINE__, + " qzDecompress status ", rc, " \n"); return 1; } *input_length = src_buf_size; @@ -257,9 +256,8 @@ int UncompressQAT(uint8_t *input, uint32_t *input_length, uint8_t *output, *end_of_stream = true; } - Log(LogLevel::LOG_INFO, - "UncompressQAT() Line %d output size %d end_of_stream %d \n", __LINE__, - dst_buf_size, *end_of_stream); + Log(LogLevel::LOG_INFO, "UncompressQAT() Line ", __LINE__, " output size ", + dst_buf_size, " end_of_stream ", *end_of_stream, " \n"); return 0; } @@ -268,19 +266,16 @@ bool SupportedOptionsQAT(int window_bits, uint32_t input_length) { (window_bits >= 8 && window_bits <= 15) || (window_bits >= 24 && window_bits <= 31)) { if (input_length < QZ_COMP_THRESHOLD_DEFAULT) { - Log(LogLevel::LOG_INFO, - "SupportedOptionsQAT() Line %d input length %d is less than " - "QAT HW threshold\n", - __LINE__, input_length); + Log(LogLevel::LOG_INFO, "SupportedOptionsQAT() Line ", __LINE__, + " input length ", input_length, " is less than QAT HW threshold\n"); return false; } if (GetCompressedFormat(window_bits) != CompressedFormat::DEFLATE_RAW && !configs[QAT_COMPRESSION_ALLOW_CHUNKING] && input_length > QAT_HW_BUFF_SZ) { - Log(LogLevel::LOG_INFO, - "SupportedOptionsQAT() Line %d input length %d is greater than " - "QAT HW buffer and chunking is not allowed\n", - __LINE__, input_length); + Log(LogLevel::LOG_INFO, "SupportedOptionsQAT() Line ", __LINE__, + " input length ", input_length, + " is greater than QAT HW buffer and chunking is not allowed\n"); return false; } return true; diff --git a/statistics.cpp b/statistics.cpp index 4430aaf..e0a93ff 100644 --- a/statistics.cpp +++ b/statistics.cpp @@ -2,8 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 #include "statistics.h" - #ifdef ENABLE_STATISTICS +#include +#include #include #include @@ -11,68 +12,25 @@ #include "logging.h" using namespace config; -// clang-format off -const std::string stat_names[STATS_COUNT] { - "deflate_count", - "deflate_error_count", - "deflate_qat_count", - "deflate_qat_error_count", - "deflate_iaa_count", - "deflate_iaa_error_count", - "deflate_zlib_count", - - "inflate_count", - "inflate_error_count", - "inflate_qat_count", - "inflate_qat_error_count", - "inflate_iaa_count", - "inflate_iaa_error_count", - "inflate_zlib_count" -}; -// clang-format on +const std::array stat_names{ + {"deflate_count", "deflate_error_count", "deflate_qat_count", + "deflate_qat_error_count", "deflate_iaa_count", "deflate_iaa_error_count", + "deflate_zlib_count", "inflate_count", "inflate_error_count", + "inflate_qat_count", "inflate_qat_error_count", "inflate_iaa_count", + "inflate_iaa_error_count", "inflate_zlib_count"}}; -thread_local uint64_t stats[STATS_COUNT]; -#endif - -bool AreStatsEnabled() { -#ifdef ENABLE_STATISTICS - return true; -#else - return false; -#endif -} +thread_local std::array stats{}; -void ResetStats() { -#ifdef ENABLE_STATISTICS - for (int i = 0; i < STATS_COUNT; i++) { - stats[i] = 0; - } -#endif -} - -uint64_t GetStat(Statistic stat) { -#ifdef ENABLE_STATISTICS - return stats[stat]; -#else - (void)stat; - return 0; -#endif -} - -#ifdef ENABLE_STATISTICS void PrintStats() { - if ((stats[DEFLATE_COUNT] + stats[INFLATE_COUNT]) % - configs[LOG_STATS_SAMPLES] != - 0) { + auto total_operations = stats[static_cast(Statistic::DEFLATE_COUNT)] + + stats[static_cast(Statistic::INFLATE_COUNT)]; + if (total_operations % configs[LOG_STATS_SAMPLES] != 0) { return; } - - std::stringstream printed_stats; - printed_stats << "Thread: " << std::this_thread::get_id() << std::endl; - for (int i = 0; i < STATS_COUNT; i++) { - printed_stats << stat_names[i] << " = " << stats[i] << std::endl; + LogStats("Thread: ", + (std::ostringstream() << std::this_thread::get_id()).str(), "\n"); + for (size_t i = 0; i < stats.size(); ++i) { + LogStats(stat_names[i], " = ", stats[i], "\n"); } - - LogStats(printed_stats.str().c_str()); } #endif diff --git a/statistics.h b/statistics.h index 387e555..8cee404 100644 --- a/statistics.h +++ b/statistics.h @@ -3,19 +3,14 @@ #pragma once -#define VISIBLE_FOR_TESTING __attribute__((visibility("default"))) -#include +#include +#include +#include +#include -#ifdef ENABLE_STATISTICS -#define INCREMENT_STAT(stat) stats[stat]++ -#define INCREMENT_STAT_COND(cond, stat) \ - if (cond) stats[stat]++ -#else -#define INCREMENT_STAT(stat) -#define INCREMENT_STAT_COND(cond, stat) -#endif +#define VISIBLE_FOR_TESTING __attribute__((visibility("default"))) -enum Statistic { +enum class Statistic : size_t { DEFLATE_COUNT = 0, DEFLATE_ERROR_COUNT, DEFLATE_QAT_COUNT, @@ -23,7 +18,6 @@ enum Statistic { DEFLATE_IAA_COUNT, DEFLATE_IAA_ERROR_COUNT, DEFLATE_ZLIB_COUNT, - INFLATE_COUNT, INFLATE_ERROR_COUNT, INFLATE_QAT_COUNT, @@ -31,17 +25,51 @@ enum Statistic { INFLATE_IAA_COUNT, INFLATE_IAA_ERROR_COUNT, INFLATE_ZLIB_COUNT, - STATS_COUNT }; +constexpr size_t STATS_COUNT = static_cast(Statistic::STATS_COUNT); + +inline std::ostream& operator<<(std::ostream& os, const Statistic& stat) { + return os << static_cast(stat); +} + #ifdef ENABLE_STATISTICS -extern thread_local uint64_t stats[STATS_COUNT]; +#define INCREMENT_STAT(stat) stats[static_cast(Statistic::stat)]++ +#define INCREMENT_STAT_COND(cond, stat) \ + if (cond) stats[static_cast(Statistic::stat)]++ +#else +#define INCREMENT_STAT(stat) +#define INCREMENT_STAT_COND(cond, stat) +#endif + +#ifdef ENABLE_STATISTICS +extern thread_local std::array stats; +extern const std::array stat_names; +#endif + +VISIBLE_FOR_TESTING constexpr bool AreStatsEnabled() { +#ifdef ENABLE_STATISTICS + return true; +#else + return false; +#endif +} + +VISIBLE_FOR_TESTING inline void ResetStats() { +#ifdef ENABLE_STATISTICS + stats.fill(0); #endif +} -VISIBLE_FOR_TESTING bool AreStatsEnabled(); -VISIBLE_FOR_TESTING void ResetStats(); -VISIBLE_FOR_TESTING uint64_t GetStat(Statistic stat); +VISIBLE_FOR_TESTING inline uint64_t GetStat(Statistic stat) { +#ifdef ENABLE_STATISTICS + return stats[static_cast(stat)]; +#else + (void)stat; + return 0; +#endif +} #ifdef ENABLE_STATISTICS void PrintStats(); diff --git a/tests/zlib_accel_test.cpp b/tests/zlib_accel_test.cpp index 8ced603..c8ec0ce 100644 --- a/tests/zlib_accel_test.cpp +++ b/tests/zlib_accel_test.cpp @@ -601,28 +601,28 @@ TEST_P(ZlibTest, CompressDecompress) { int ret = ZlibCompress( input, input_length, &compressed, test_param.window_bits_compress, test_param.flush_compress, &output_upper_bound, &execution_path); - VerifyStatIncremented(DEFLATE_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_COUNT); bool compress_fallback_expected = ZlibCompressExpectFallback(test_param, input_length, output_upper_bound); if (compress_fallback_expected && !test_param.zlib_fallback_compress) { ASSERT_EQ(ret, Z_DATA_ERROR); - VerifyStatIncremented(DEFLATE_ERROR_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_ERROR_COUNT); DestroyBlock(input); return; } else { ASSERT_EQ(ret, Z_STREAM_END); if (compress_fallback_expected) { ASSERT_EQ(execution_path, ZLIB); - VerifyStatIncremented(DEFLATE_ZLIB_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_ZLIB_COUNT); } else { ASSERT_EQ(execution_path, test_param.execution_path_compress); if (test_param.execution_path_compress == QAT) { - VerifyStatIncremented(DEFLATE_QAT_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_QAT_COUNT); } else if (test_param.execution_path_compress == IAA) { - VerifyStatIncremented(DEFLATE_IAA_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_IAA_COUNT); } else if (test_param.execution_path_compress == ZLIB) { - VerifyStatIncremented(DEFLATE_ZLIB_COUNT); + VerifyStatIncremented(Statistic::DEFLATE_ZLIB_COUNT); } } } @@ -643,7 +643,8 @@ TEST_P(ZlibTest, CompressDecompress) { &uncompressed, &uncompressed_length, &input_consumed, window_bits_uncompress, test_param.flush_uncompress, test_param.input_chunks_uncompress, &execution_path); - VerifyStatIncrementedUpTo(INFLATE_COUNT, test_param.input_chunks_uncompress); + VerifyStatIncrementedUpTo(Statistic::INFLATE_COUNT, + test_param.input_chunks_uncompress); bool error_expected = false; bool accelerator_tried = false; @@ -652,12 +653,12 @@ TEST_P(ZlibTest, CompressDecompress) { window_bits_uncompress, compress_fallback_expected, &accelerator_tried); if (uncompress_fallback_expected && !test_param.zlib_fallback_uncompress) { ASSERT_EQ(ret, Z_DATA_ERROR); - VerifyStatIncremented(INFLATE_ERROR_COUNT); + VerifyStatIncremented(Statistic::INFLATE_ERROR_COUNT); if (accelerator_tried) { if (test_param.execution_path_uncompress == QAT) { - VerifyStatIncremented(INFLATE_QAT_ERROR_COUNT); + VerifyStatIncremented(Statistic::INFLATE_QAT_ERROR_COUNT); } else if (test_param.execution_path_uncompress == IAA) { - VerifyStatIncremented(INFLATE_IAA_ERROR_COUNT); + VerifyStatIncremented(Statistic::INFLATE_IAA_ERROR_COUNT); } } error_expected = true; @@ -665,16 +666,16 @@ TEST_P(ZlibTest, CompressDecompress) { ASSERT_EQ(ret, Z_STREAM_END); if (uncompress_fallback_expected) { ASSERT_EQ(execution_path, ZLIB); - VerifyStatIncrementedUpTo(INFLATE_ZLIB_COUNT, + VerifyStatIncrementedUpTo(Statistic::INFLATE_ZLIB_COUNT, test_param.input_chunks_uncompress); } else { ASSERT_EQ(execution_path, test_param.execution_path_uncompress); if (test_param.execution_path_uncompress == QAT) { - VerifyStatIncremented(INFLATE_QAT_COUNT); + VerifyStatIncremented(Statistic::INFLATE_QAT_COUNT); } else if (test_param.execution_path_uncompress == IAA) { - VerifyStatIncremented(INFLATE_IAA_COUNT); + VerifyStatIncremented(Statistic::INFLATE_IAA_COUNT); } else if (test_param.execution_path_uncompress == ZLIB) { - VerifyStatIncrementedUpTo(INFLATE_ZLIB_COUNT, + VerifyStatIncrementedUpTo(Statistic::INFLATE_ZLIB_COUNT, test_param.input_chunks_uncompress); } } diff --git a/zlib_accel.cpp b/zlib_accel.cpp index 7c9f934..4101b88 100644 --- a/zlib_accel.cpp +++ b/zlib_accel.cpp @@ -239,8 +239,8 @@ InflateStreamSettings inflate_stream_settings; int ZEXPORT deflateInit_(z_streamp strm, int level, const char* version, int stream_size) { - Log(LogLevel::LOG_INFO, "deflateInit_ Line %d, strm %p, level %d\n", __LINE__, - strm, level); + Log(LogLevel::LOG_INFO, "deflateInit_ Line ", __LINE__, ", strm ", + static_cast(strm), ", level ", level, "\n"); deflate_stream_settings.Set(strm, level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY); @@ -250,9 +250,9 @@ int ZEXPORT deflateInit_(z_streamp strm, int level, const char* version, int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int window_bits, int mem_level, int strategy, const char* version, int stream_size) { - Log(LogLevel::LOG_INFO, - "deflateInit2_ Line %d, strm %p, level %d, window_bits %d \n", __LINE__, - strm, level, window_bits); + Log(LogLevel::LOG_INFO, "deflateInit2_ Line ", __LINE__, ", strm ", + static_cast(strm), ", level ", level, ", window_bits ", + window_bits, " \n"); deflate_stream_settings.Set(strm, level, method, window_bits, mem_level, strategy); @@ -262,9 +262,8 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method, int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef* dictionary, uInt dictLength) { - Log(LogLevel::LOG_INFO, - "deflateSetDictionary Line %d, strm %p, dictLength %u\n", __LINE__, strm, - dictLength); + Log(LogLevel::LOG_INFO, "deflateSetDictionary Line ", __LINE__, ", strm ", + static_cast(strm), ", dictLength ", dictLength, "\n"); DeflateSettings* deflate_settings = deflate_stream_settings.Get(strm); deflate_settings->path = ZLIB; return orig_deflateSetDictionary(strm, dictionary, dictLength); @@ -275,11 +274,10 @@ int ZEXPORT deflate(z_streamp strm, int flush) { INCREMENT_STAT(DEFLATE_COUNT); PrintStats(); - Log(LogLevel::LOG_INFO, - "deflate Line %d, strm %p, avail_in %d, avail_out %d, flush %d, in_call " - "%d, path %d\n", - __LINE__, strm, strm->avail_in, strm->avail_out, flush, in_call, - deflate_settings->path); + Log(LogLevel::LOG_INFO, "deflate Line ", __LINE__, ", strm ", + static_cast(strm), ", avail_in ", strm->avail_in, ", avail_out ", + strm->avail_out, ", flush ", flush, ", in_call ", in_call, ", path ", + static_cast(deflate_settings->path), "\n"); int ret = 1; bool iaa_available = false; @@ -354,11 +352,10 @@ int ZEXPORT deflate(z_streamp strm, int flush) { ret = Z_BUF_ERROR; } - Log(LogLevel::LOG_INFO, - "deflate Line %d, strm %p, accelerator return code %d, avail_in %d, " - "avail_out %d, path %d\n", - __LINE__, strm, ret, strm->avail_in, strm->avail_out, - deflate_settings->path); + Log(LogLevel::LOG_INFO, "deflate Line ", __LINE__, ", strm ", + static_cast(strm), ", accelerator return code ", ret, + ", avail_in ", strm->avail_in, ", avail_out ", strm->avail_out, + ", path ", static_cast(deflate_settings->path), "\n"); return ret; } } @@ -373,24 +370,25 @@ int ZEXPORT deflate(z_streamp strm, int flush) { ret = Z_DATA_ERROR; } - Log(LogLevel::LOG_INFO, - "deflate Line %d, strm %p, zlib return code %d, avail_in %d, " - "avail_out %d, path %d\n", - __LINE__, strm, ret, strm->avail_in, strm->avail_out, - deflate_settings->path); + Log(LogLevel::LOG_INFO, "deflate Line ", __LINE__, ", strm ", + static_cast(strm), ", zlib return code ", ret, ", avail_in ", + strm->avail_in, ", avail_out ", strm->avail_out, ", path ", + static_cast(deflate_settings->path), "\n"); INCREMENT_STAT_COND(ret < 0, DEFLATE_ERROR_COUNT); return ret; } int ZEXPORT deflateEnd(z_streamp strm) { - Log(LogLevel::LOG_INFO, "deflateEnd Line %d, strm %p\n", __LINE__, strm); + Log(LogLevel::LOG_INFO, "deflateEnd Line ", __LINE__, ", strm ", + static_cast(strm), "\n"); deflate_stream_settings.Unset(strm); return orig_deflateEnd(strm); } int ZEXPORT deflateReset(z_streamp strm) { - Log(LogLevel::LOG_INFO, "deflateReset Line %d, strm %p\n", __LINE__, strm); + Log(LogLevel::LOG_INFO, "deflateReset Line ", __LINE__, ", strm ", + static_cast(strm), "\n"); DeflateSettings* deflate_settings = deflate_stream_settings.Get(strm); if (deflate_settings != nullptr) { deflate_settings->path = UNDEFINED; @@ -401,7 +399,8 @@ int ZEXPORT deflateReset(z_streamp strm) { int ZEXPORT inflateInit_(z_streamp strm, const char* version, int stream_size) { inflate_stream_settings.Set(strm, 15); - Log(LogLevel::LOG_INFO, "inflateInit_ Line %d, strm %p\n", __LINE__, strm); + Log(LogLevel::LOG_INFO, "inflateInit_ Line ", __LINE__, ", strm ", + static_cast(strm), "\n"); return orig_inflateInit_(strm, version, stream_size); } @@ -409,17 +408,16 @@ int ZEXPORT inflateInit_(z_streamp strm, const char* version, int stream_size) { int ZEXPORT inflateInit2_(z_streamp strm, int window_bits, const char* version, int stream_size) { inflate_stream_settings.Set(strm, window_bits); - Log(LogLevel::LOG_INFO, "inflateInit2_ Line %d, strm %p, window_bits %d\n", - __LINE__, strm, window_bits); + Log(LogLevel::LOG_INFO, "inflateInit2_ Line ", __LINE__, ", strm ", + static_cast(strm), ", window_bits ", window_bits, "\n"); return orig_inflateInit2_(strm, window_bits, version, stream_size); } int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef* dictionary, uInt dictLength) { - Log(LogLevel::LOG_INFO, - "inflateSetDictionary Line %d, strm %p, dictLength %u\n", __LINE__, strm, - dictLength); + Log(LogLevel::LOG_INFO, "inflateSetDictionary Line ", __LINE__, ", strm ", + static_cast(strm), ", dictLength ", dictLength, "\n"); InflateSettings* inflate_settings = inflate_stream_settings.Get(strm); inflate_settings->path = ZLIB; return orig_inflateSetDictionary(strm, dictionary, dictLength); @@ -430,11 +428,10 @@ int ZEXPORT inflate(z_streamp strm, int flush) { INCREMENT_STAT(INFLATE_COUNT); PrintStats(); - Log(LogLevel::LOG_INFO, - "inflate Line %d, strm %p, avail_in %d, avail_out %d, flush %d, in_call " - "%d, path %d\n", - __LINE__, strm, strm->avail_in, strm->avail_out, flush, in_call, - inflate_settings->path); + Log(LogLevel::LOG_INFO, "inflate Line ", __LINE__, ", strm ", + static_cast(strm), ", avail_in ", strm->avail_in, ", avail_out ", + strm->avail_out, ", flush ", flush, ", in_call ", in_call, ", path ", + static_cast(inflate_settings->path), "\n"); PrintDeflateBlockHeader(LogLevel::LOG_INFO, strm->next_in, strm->avail_in, inflate_settings->window_bits); @@ -522,11 +519,11 @@ int ZEXPORT inflate(z_streamp strm, int flush) { ret = Z_BUF_ERROR; } - Log(LogLevel::LOG_INFO, - "inflate Line %d, strm %p, accelerator return code %d, avail_in %d, " - "avail_out %d, end_of_stream %d, path %d\n", - __LINE__, strm, ret, strm->avail_in, strm->avail_out, end_of_stream, - inflate_settings->path); + Log(LogLevel::LOG_INFO, "inflate Line ", __LINE__, ", strm ", + static_cast(strm), ", accelerator return code ", ret, + ", avail_in ", strm->avail_in, ", avail_out ", strm->avail_out, + ", end_of_stream ", end_of_stream, ", path ", + static_cast(inflate_settings->path), "\n"); return ret; } } @@ -541,24 +538,25 @@ int ZEXPORT inflate(z_streamp strm, int flush) { ret = Z_DATA_ERROR; } - Log(LogLevel::LOG_INFO, - "inflate Line %d, strm %p, zlib return code %d, avail_in %d, avail_out " - "%d, path %d\n", - __LINE__, strm, ret, strm->avail_in, strm->avail_out, - inflate_settings->path); + Log(LogLevel::LOG_INFO, "inflate Line ", __LINE__, ", strm ", + static_cast(strm), ", zlib return code ", ret, ", avail_in ", + strm->avail_in, ", avail_out ", strm->avail_out, ", path ", + static_cast(inflate_settings->path), "\n"); INCREMENT_STAT_COND(ret < 0, INFLATE_ERROR_COUNT); return ret; } int ZEXPORT inflateEnd(z_streamp strm) { - Log(LogLevel::LOG_INFO, "inflateEnd Line %d, strm %p\n", __LINE__, strm); + Log(LogLevel::LOG_INFO, "inflateEnd Line ", __LINE__, ", strm ", + static_cast(strm), "\n"); inflate_stream_settings.Unset(strm); return orig_inflateEnd(strm); } int ZEXPORT inflateReset(z_streamp strm) { - Log(LogLevel::LOG_INFO, "inflateReset Line %d, strm %p\n", __LINE__, strm); + Log(LogLevel::LOG_INFO, "inflateReset Line ", __LINE__, ", strm ", + static_cast(strm), "\n"); InflateSettings* inflate_settings = inflate_stream_settings.Get(strm); if (inflate_settings != nullptr) { inflate_settings->path = UNDEFINED; @@ -569,8 +567,8 @@ int ZEXPORT inflateReset(z_streamp strm) { int ZEXPORT compress2(Bytef* dest, uLongf* destLen, const Bytef* source, uLong sourceLen, int level) { - Log(LogLevel::LOG_INFO, "compress2 Line %d, sourceLen %lu, destLen %lu\n", - __LINE__, sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "compress2 Line ", __LINE__, ", sourceLen ", + sourceLen, ", destLen ", *destLen, "\n"); int ret = 1; uint32_t input_len = sourceLen; @@ -615,10 +613,9 @@ int ZEXPORT compress2(Bytef* dest, uLongf* destLen, const Bytef* source, *destLen = output_len; ret = Z_OK; - Log(LogLevel::LOG_INFO, - "compress2 Line %d, accelerator return code %d, sourceLen %lu, " - "destLen %lu\n", - __LINE__, ret, sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "compress2 Line ", __LINE__, + ", accelerator return code ", ret, ", sourceLen ", sourceLen, + ", destLen ", *destLen, "\n"); } else if (configs[USE_ZLIB_COMPRESS]) { // compress2 in zlib calls deflate. It was observed that deflate is // sometimes intercepted by the shim. in_call prevents deflate from using @@ -626,10 +623,8 @@ int ZEXPORT compress2(Bytef* dest, uLongf* destLen, const Bytef* source, in_call = true; ret = orig_compress2(dest, destLen, source, sourceLen, level); in_call = false; - Log(LogLevel::LOG_INFO, - "compress2 Line %d, zlib return code %d, sourceLen %lu, " - "destLen %lu\n", - __LINE__, ret, sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "compress2 Line ", __LINE__, ", zlib return code ", + ret, ", sourceLen ", sourceLen, ", destLen ", *destLen, "\n"); } else { ret = Z_DATA_ERROR; } @@ -643,8 +638,8 @@ int ZEXPORT compress(Bytef* dest, uLongf* destLen, const Bytef* source, int ZEXPORT uncompress2(Bytef* dest, uLongf* destLen, const Bytef* source, uLong* sourceLen) { - Log(LogLevel::LOG_INFO, "uncompress2 Line %d, sourceLen %lu, destLen %lu\n", - __LINE__, *sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "uncompress2 Line ", __LINE__, ", sourceLen ", + *sourceLen, ", destLen ", *destLen, "\n"); int ret = 1; bool end_of_stream = true; @@ -693,19 +688,17 @@ int ZEXPORT uncompress2(Bytef* dest, uLongf* destLen, const Bytef* source, *destLen = output_len; ret = Z_OK; - Log(LogLevel::LOG_INFO, - "uncompress2 Line %d, accelerator return code %d, sourceLen %lu, " - "destLen %lu\n", - __LINE__, ret, *sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "uncompress2 Line ", __LINE__, + ", accelerator return code ", ret, ", sourceLen ", *sourceLen, + ", destLen ", *destLen, "\n"); } else if (configs[USE_ZLIB_UNCOMPRESS]) { // refer to comment in compress2 in_call = true; ret = orig_uncompress2(dest, destLen, source, sourceLen); in_call = false; - Log(LogLevel::LOG_INFO, - "uncompress2 Line %d, zlib return code %d, sourceLen %lu, " - "destLen %lu\n", - __LINE__, ret, *sourceLen, *destLen); + Log(LogLevel::LOG_INFO, "uncompress2 Line ", __LINE__, + ", zlib return code ", ret, ", sourceLen ", *sourceLen, ", destLen ", + *destLen, "\n"); } else { ret = Z_DATA_ERROR; } @@ -896,8 +889,8 @@ gzFile ZEXPORT gzopen(const char* path, const char* mode) { gzFile file = orig_gzdopen(fd, mode); // TODO in case of error fall back to zlib and set execution path. - Log(LogLevel::LOG_INFO, "gzopen Line %d, file %p, path %s, mode %s\n", - __LINE__, file, path, mode); + Log(LogLevel::LOG_INFO, "gzopen Line ", __LINE__, ", file ", + static_cast(file), ", path ", path, ", mode ", mode, "\n"); gzip_files.Set(file, fd, file_mode); return file; @@ -906,8 +899,8 @@ gzFile ZEXPORT gzopen(const char* path, const char* mode) { gzFile ZEXPORT gzdopen(int fd, const char* mode) { gzFile file = orig_gzdopen(fd, mode); - Log(LogLevel::LOG_INFO, "gzdopen Line %d, file %d, fd %p, mode %s\n", - __LINE__, fd, file, mode); + Log(LogLevel::LOG_INFO, "gzdopen Line ", __LINE__, ", file ", fd, ", fd ", + static_cast(file), ", mode ", mode, "\n"); FileMode file_mode = FileMode::NONE; GetOpenFlags(mode, &file_mode); @@ -1045,11 +1038,9 @@ static int CompressAndWrite(gzFile file, GzipFile* gz) { // TODO loop in case not all data compressed int ret = GzwriteAcceleratorCompress(gz, input, &input_len, output, &output_len); - Log(LogLevel::LOG_INFO, - "CompressAndWrite Line %d, file %p, accelerator return code %d, input " - "%d, " - "output %d\n", - __LINE__, file, ret, input_len, output_len); + Log(LogLevel::LOG_INFO, "CompressAndWrite Line ", __LINE__, ", file ", + static_cast(file), ", accelerator return code ", ret, ", input ", + input_len, ", output ", output_len, "\n"); if (ret == 0) { gz->data_buf_pos = input_len; @@ -1060,11 +1051,11 @@ static int CompressAndWrite(gzFile file, GzipFile* gz) { gz->deflate_stream.next_out = (Bytef*)(gz->io_buf); gz->deflate_stream.avail_out = static_cast(gz->io_buf_size); ret = orig_deflate(&gz->deflate_stream, Z_FINISH); - Log(LogLevel::LOG_INFO, - "CompressAndWrite Line %d, file %p, zlib return code %d, input %d, " - "output %d, avail_in %d, avail_out %d\n", - __LINE__, file, ret, input_len, output_len, gz->deflate_stream.avail_in, - gz->deflate_stream.avail_out); + Log(LogLevel::LOG_INFO, "CompressAndWrite Line ", __LINE__, ", file ", + static_cast(file), ", zlib return code ", ret, ", input ", + input_len, ", output ", output_len, ", avail_in ", + gz->deflate_stream.avail_in, ", avail_out ", + gz->deflate_stream.avail_out, "\n"); if (ret == Z_STREAM_END) { gz->data_buf_pos = gz->data_buf_content - gz->deflate_stream.avail_in; output_len = gz->io_buf_size - gz->deflate_stream.avail_out; @@ -1077,9 +1068,8 @@ static int CompressAndWrite(gzFile file, GzipFile* gz) { int write_ret = 0; do { write_ret = write(gz->fd, gz->io_buf, output_len); - Log(LogLevel::LOG_INFO, - "CompressAndWrite Line %d, file %p, written to file %d\n", __LINE__, - file, write_ret); + Log(LogLevel::LOG_INFO, "CompressAndWrite Line ", __LINE__, ", file ", + static_cast(file), ", written to file ", write_ret, "\n"); if (write_ret >= 0) { output_len -= write_ret; } @@ -1094,8 +1084,8 @@ static int CompressAndWrite(gzFile file, GzipFile* gz) { int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) { GzipFile* gz = gzip_files.Get(file); - Log(LogLevel::LOG_INFO, "gzwrite Line %d, file %p, buf %p, len %u\n", - __LINE__, file, buf, len); + Log(LogLevel::LOG_INFO, "gzwrite Line ", __LINE__, ", file ", + static_cast(file), ", buf ", buf, ", len ", len, "\n"); unsigned int written_bytes = 0; bool accelerator_selected = @@ -1116,9 +1106,9 @@ int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) { data_to_copy); gz->data_buf_content += data_to_copy; written_bytes += data_to_copy; - Log(LogLevel::LOG_INFO, - "gzwrite Line %d, file %p, remaining %u, to copy %u, written %u\n", - __LINE__, file, data_buf_remaining, data_to_copy, written_bytes); + Log(LogLevel::LOG_INFO, "gzwrite Line ", __LINE__, ", file ", + static_cast(file), ", remaining ", data_buf_remaining, + ", to copy ", data_to_copy, ", written ", written_bytes, "\n"); // Compress and write the buffer if (written_bytes < len) { @@ -1142,10 +1132,9 @@ int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) { } gzwrite_end: - Log(LogLevel::LOG_INFO, - "gzwrite Line %d, file %p, " - "written %d, buffered %d, path %d\n", - __LINE__, file, written_bytes, gz->data_buf_pos, gz->path); + Log(LogLevel::LOG_INFO, "gzwrite Line ", __LINE__, ", file ", + static_cast(file), ", written ", written_bytes, ", buffered ", + gz->data_buf_pos, ", path ", static_cast(gz->path), "\n"); return written_bytes; } @@ -1153,8 +1142,8 @@ int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) { int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { GzipFile* gz = gzip_files.Get(file); - Log(LogLevel::LOG_INFO, "gzread Line %d, file %p, buf %p, len %u\n", __LINE__, - file, buf, len); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", buf ", buf, ", len ", len, "\n"); int ret = 1; uint32_t read_bytes = 0; @@ -1176,9 +1165,9 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { data_to_copy); gz->data_buf_pos += data_to_copy; read_bytes += data_to_copy; - Log(LogLevel::LOG_INFO, - "gzread Line %d, file %p, remaining %u, to copy %u, read %u\n", - __LINE__, file, data_remaining, data_to_copy, read_bytes); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", remaining ", data_remaining, + ", to copy ", data_to_copy, ", read ", read_bytes, "\n"); // If not enough uncompressed data in data_buf, read and decompress more // (if more available) @@ -1199,9 +1188,8 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { if (read_ret > 0) { gz->io_buf_content += read_ret; } - Log(LogLevel::LOG_INFO, - "gzread Line %d, file %p, read from file %d\n", __LINE__, file, - read_ret); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", read from file ", read_ret, "\n"); } while (gz->io_buf_content < gz->io_buf_size && read_ret > 0); // Check for EOF/error @@ -1225,11 +1213,9 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { bool end_of_stream = false; ret = GzreadAcceleratorUncompress(gz, input, &input_len, output, &output_len, &end_of_stream); - Log(LogLevel::LOG_INFO, - "gzread Line %d, file %p, accelerator return code %d, input " - "%d, " - "output %d\n", - __LINE__, file, ret, input_len, output_len); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", accelerator return code ", ret, + ", input ", input_len, ", output ", output_len, "\n"); // If we didn't reach end-of-stream, it means io_buf is not large // enough to hold the entire stream @@ -1252,11 +1238,11 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { gz->inflate_stream.avail_out = static_cast(gz->data_buf_size); ret = orig_inflate(&gz->inflate_stream, Z_SYNC_FLUSH); - Log(LogLevel::LOG_INFO, - "gzread Line %d, file %p, zlib return code %d, input %d, " - "output %d, avail_in %d, avail_out %d\n", - __LINE__, file, ret, input_len, output_len, - gz->inflate_stream.avail_in, gz->inflate_stream.avail_out); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", zlib return code ", ret, + ", input ", input_len, ", output ", output_len, ", avail_in ", + gz->inflate_stream.avail_in, ", avail_out ", + gz->inflate_stream.avail_out, "\n"); if (ret == Z_STREAM_END || ret == Z_OK) { gz->io_buf_pos += (gz->io_buf_content - gz->inflate_stream.avail_in); @@ -1288,19 +1274,20 @@ int ZEXPORT gzread(gzFile file, voidp buf, unsigned len) { } gzread_end: - Log(LogLevel::LOG_INFO, - "gzread Line %d, file %p, return code %d, " - "read %d, buffered compressed %d, buffered uncompressed %d, path %d\n", - __LINE__, file, ret, read_bytes, gz->io_buf_content, - gz->data_buf_content - gz->data_buf_pos, gz->path); + Log(LogLevel::LOG_INFO, "gzread Line ", __LINE__, ", file ", + static_cast(file), ", return code ", ret, ", read ", read_bytes, + ", buffered compressed ", gz->io_buf_content, ", buffered uncompressed ", + gz->data_buf_content - gz->data_buf_pos, ", path ", + static_cast(gz->path), "\n"); return read_bytes; } int ZEXPORT gzclose(gzFile file) { GzipFile* gz = gzip_files.Get(file); - Log(LogLevel::LOG_INFO, "gzclose Line %d, file %p, buffered %d, path %d\n", - __LINE__, file, gz->data_buf_content, gz->path); + Log(LogLevel::LOG_INFO, "gzclose Line ", __LINE__, ", file ", + static_cast(file), ", buffered ", gz->data_buf_content, ", path ", + static_cast(gz->path), "\n"); int ret = 0; if (gz->path != ZLIB && @@ -1320,8 +1307,8 @@ int ZEXPORT gzclose(gzFile file) { if (readlink_ret == -1) { ret = orig_gzclose(file); gzip_files.Unset(file); - Log(LogLevel::LOG_ERROR, "gzclose Line %d, readlink_ret return error \n", - __LINE__); + Log(LogLevel::LOG_ERROR, "gzclose Line ", __LINE__, + ", readlink_ret return error \n"); return ret; } file_path[readlink_ret] = '\0'; @@ -1345,9 +1332,9 @@ int ZEXPORT gzclose(gzFile file) { ret = orig_gzclose(file); } - Log(LogLevel::LOG_INFO, - "gzclose Line %d, file %p, return code %d, buffered processed %d\n", - __LINE__, file, ret, gz->data_buf_pos); + Log(LogLevel::LOG_INFO, "gzclose Line ", __LINE__, ", file ", + static_cast(file), ", return code ", ret, ", buffered processed ", + gz->data_buf_pos, "\n"); gzip_files.Unset(file); return ret; }