|
21 | 21 | #include "CDoc2Writer.h" |
22 | 22 | #include "CDoc2Reader.h" |
23 | 23 | #include "Configuration.h" |
24 | | -#include "ILogger.h" |
25 | 24 | #include "Io.h" |
26 | 25 | #include "NetworkBackend.h" |
| 26 | +#include "Utils.h" |
| 27 | +#include "Logger.h" |
| 28 | + |
| 29 | +#include <iostream> |
| 30 | +#include <filesystem> |
27 | 31 |
|
28 | 32 | namespace libcdoc { |
29 | 33 |
|
@@ -71,6 +75,58 @@ getVersion() |
71 | 75 | return VERSION_STR; |
72 | 76 | } |
73 | 77 |
|
| 78 | +/** |
| 79 | + * @brief Console logger |
| 80 | + * |
| 81 | + * An ILogger subclass that logs text to console. |
| 82 | + * |
| 83 | + * Info messages are logged to cout, all others to cerr. |
| 84 | + */ |
| 85 | + |
| 86 | +class ConsoleLogger : public Logger |
| 87 | +{ |
| 88 | +public: |
| 89 | + virtual void logMessage(LogLevel level, std::string_view file, int line, std::string_view message) override |
| 90 | + { |
| 91 | + // We ignore by default the file name and line number, and call LogMessage with the level and message. |
| 92 | + std::ostream& ofs = (level == LEVEL_INFO) ? std::cout : std::cerr; |
| 93 | + if (!file.empty()) { |
| 94 | + ofs << std::filesystem::path(file).filename().string() << ':' << line << " " << message << '\n'; |
| 95 | + } else { |
| 96 | + ofs << message << '\n'; |
| 97 | + } |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +static Logger * |
| 102 | +getDefaultLogger() |
| 103 | +{ |
| 104 | + static ConsoleLogger clogger; |
| 105 | + return &clogger; |
| 106 | +} |
| 107 | + |
| 108 | +static Logger *sys_logger = nullptr; |
| 109 | + |
| 110 | +void |
| 111 | +setLogger(Logger *logger) |
| 112 | +{ |
| 113 | + sys_logger = logger; |
| 114 | +} |
| 115 | + |
| 116 | +void |
| 117 | +setLogLevel(LogLevel level) |
| 118 | +{ |
| 119 | + Logger *logger = (sys_logger) ? sys_logger : getDefaultLogger(); |
| 120 | + logger->setMinLogLevel(level); |
| 121 | +} |
| 122 | + |
| 123 | +void |
| 124 | +log(LogLevel level, std::string_view file, int line, std::string_view msg) |
| 125 | +{ |
| 126 | + Logger *logger = (sys_logger) ? sys_logger : getDefaultLogger(); |
| 127 | + logger->log(level, file, line, msg); |
| 128 | +} |
| 129 | + |
74 | 130 | int |
75 | 131 | libcdoc::CDocReader::getCDocFileVersion(DataSource *src) |
76 | 132 | { |
@@ -139,31 +195,6 @@ libcdoc::CDocReader::createReader(std::istream& ifs, Configuration *conf, Crypto |
139 | 195 | return createReader(isrc.release(), true, conf, crypto, network); |
140 | 196 | } |
141 | 197 |
|
142 | | -#if LIBCDOC_TESTING |
143 | | -int64_t |
144 | | -libcdoc::CDocReader::testConfig(std::vector<uint8_t>& dst) |
145 | | -{ |
146 | | - LOG_TRACE("CDocReader::testConfig::Native superclass"); |
147 | | - if (conf) { |
148 | | - LOG_DBG("CDocReader::testConfig this={} conf={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(conf)); |
149 | | - } |
150 | | - LOG_ERROR("CDocReader::testConfig::conf is null"); |
151 | | - return WORKFLOW_ERROR; |
152 | | -} |
153 | | - |
154 | | -int64_t |
155 | | -libcdoc::CDocReader::testNetwork(std::vector<std::vector<uint8_t>>& dst) |
156 | | -{ |
157 | | - LOG_TRACE("CDocReader::testNetwork::Native superclass"); |
158 | | - if (network) { |
159 | | - LOG_DBG("CDocReader::testNetwork this={} network={}", reinterpret_cast<void*>(this), reinterpret_cast<void*>(network)); |
160 | | - return network->test(dst); |
161 | | - } |
162 | | - LOG_ERROR("CDocReader::testNetwork::network is null"); |
163 | | - return WORKFLOW_ERROR; |
164 | | -} |
165 | | -#endif |
166 | | - |
167 | 198 | libcdoc::CDocWriter::CDocWriter(int _version, DataConsumer *_dst, bool take_ownership) |
168 | 199 | : version(_version), dst(_dst), owned(take_ownership) |
169 | 200 | { |
|
0 commit comments