|
3 | 3 |
|
4 | 4 | #pragma once |
5 | 5 |
|
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 <fstream> |
| 7 | +#include <iostream> |
| 8 | +#include <memory> |
| 9 | +#include <sstream> |
| 10 | +#include <utility> |
13 | 11 |
|
14 | 12 | #include "config/config.h" |
15 | 13 | #include "utils.h" |
| 14 | + |
16 | 15 | using namespace config; |
| 16 | + |
17 | 17 | enum class LogLevel { LOG_NONE = 0, LOG_INFO = 1, LOG_ERROR = 2 }; |
18 | 18 |
|
19 | 19 | #if defined(DEBUG_LOG) || defined(ENABLE_STATISTICS) |
20 | | -inline FILE* log_file_stream = nullptr; |
| 20 | +inline std::unique_ptr<std::ofstream> log_file_stream = nullptr; |
21 | 21 |
|
22 | | -inline static void CreateLogFile(const char* file_name) { |
23 | | - log_file_stream = fopen(file_name, "a"); |
| 22 | +inline void CreateLogFile(const char* file_name) { |
| 23 | + log_file_stream = std::make_unique<std::ofstream>(file_name, std::ios::app); |
24 | 24 | } |
25 | 25 |
|
26 | | -inline static void CloseLogFile() { |
27 | | - if (log_file_stream != nullptr) { |
28 | | - fclose(log_file_stream); |
| 26 | +inline void CloseLogFile() { log_file_stream.reset(); } |
| 27 | + |
| 28 | +inline std::ostream& GetLogStream() { |
| 29 | + if (log_file_stream && log_file_stream->is_open()) { |
| 30 | + return *log_file_stream; |
29 | 31 | } |
| 32 | + return std::cout; |
| 33 | +} |
| 34 | + |
| 35 | +inline void LogImpl(std::ostream& stream) { stream << std::endl; } |
| 36 | + |
| 37 | +template <typename T, typename... Args> |
| 38 | +inline void LogImpl(std::ostream& stream, T&& first, Args&&... rest) { |
| 39 | + stream << std::forward<T>(first); |
| 40 | + LogImpl(stream, std::forward<Args>(rest)...); |
30 | 41 | } |
31 | 42 | #endif |
32 | 43 |
|
33 | 44 | #ifdef DEBUG_LOG |
34 | | -static inline void Log(LogLevel level, const char* format, ...) { |
| 45 | +template <typename... Args> |
| 46 | +inline void Log(LogLevel level, Args&&... args) { |
35 | 47 | if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) { |
36 | 48 | return; |
37 | 49 | } |
38 | | - |
39 | | - FILE* stream = stdout; |
40 | | - if (log_file_stream != nullptr) { |
41 | | - stream = log_file_stream; |
42 | | - } |
43 | | - |
| 50 | + auto& stream = GetLogStream(); |
44 | 51 | switch (level) { |
45 | 52 | case LogLevel::LOG_ERROR: |
46 | | - fprintf(stream, "Error: "); |
| 53 | + stream << "Error: "; |
47 | 54 | break; |
48 | 55 | case LogLevel::LOG_INFO: |
49 | | - fprintf(stream, "Info: "); |
| 56 | + stream << "Info: "; |
50 | 57 | break; |
51 | 58 | case LogLevel::LOG_NONE: |
52 | 59 | return; |
53 | 60 | } |
54 | | - va_list args; |
55 | | - va_start(args, format); |
56 | | - vfprintf(stream, format, args); |
57 | | - va_end(args); |
| 61 | + LogImpl(stream, std::forward<Args>(args)...); |
58 | 62 | } |
59 | 63 | #else |
60 | | -#define Log(...) |
| 64 | +template <typename... Args> |
| 65 | +inline void Log(LogLevel, Args&&...) {} |
61 | 66 | #endif |
62 | 67 |
|
63 | 68 | #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); |
| 69 | +template <typename... Args> |
| 70 | +inline void LogStats(Args&&... args) { |
| 71 | + auto& stream = GetLogStream(); |
| 72 | + stream << "Stats:\n"; |
| 73 | + LogImpl(stream, std::forward<Args>(args)...); |
72 | 74 | } |
73 | 75 | #else |
74 | | -#define LogStats(...) |
| 76 | +template <typename... Args> |
| 77 | +inline void LogStats(Args&&...) {} |
75 | 78 | #endif |
76 | 79 |
|
77 | 80 | #ifdef DEBUG_LOG |
78 | | -static inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, |
79 | | - uint32_t len, int window_bits) { |
| 81 | +template <typename... Args> |
| 82 | +inline void PrintDeflateBlockHeader(LogLevel level, uint8_t* data, uint32_t len, |
| 83 | + int window_bits, Args&&... args) { |
80 | 84 | if (static_cast<uint32_t>(level) < configs[LOG_LEVEL]) { |
81 | 85 | return; |
82 | 86 | } |
83 | | - |
84 | 87 | CompressedFormat format = GetCompressedFormat(window_bits); |
85 | 88 | uint32_t header_length = GetHeaderLength(format); |
86 | 89 | 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); |
| 90 | + Log(level, "Deflate block header bfinal=", |
| 91 | + static_cast<int>(data[header_length] & 0b00000001), |
| 92 | + ", btype=", static_cast<int>((data[header_length] & 0b00000110) >> 1), |
| 93 | + "\n", std::forward<Args>(args)...); |
90 | 94 | } |
91 | 95 | } |
92 | 96 | #else |
93 | | -#define PrintDeflateBlockHeader(...) |
| 97 | +template <typename... Args> |
| 98 | +inline void PrintDeflateBlockHeader(LogLevel, uint8_t*, uint32_t, int, |
| 99 | + Args&&...) {} |
94 | 100 | #endif |
0 commit comments