Skip to content

Commit 3445a56

Browse files
authored
Logger (#431)
1 parent b970990 commit 3445a56

38 files changed

Lines changed: 770 additions & 206 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ set(ODR_SOURCE_FILES
8282
"src/odr/global_params.cpp"
8383
"src/odr/html.cpp"
8484
"src/odr/http_server.cpp"
85+
"src/odr/logger.cpp"
8586
"src/odr/odr.cpp"
8687
"src/odr/quantity.cpp"
8788
"src/odr/style.cpp"

cli/src/server.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using namespace odr;
1010

1111
int main(int argc, char **argv) {
12+
auto logger = Logger::create_stdio("odr-server", LogLevel::verbose);
13+
1214
std::string input{argv[1]};
1315

1416
std::optional<std::string> password;
@@ -20,17 +22,17 @@ int main(int argc, char **argv) {
2022
decode_preference.engine_priority = {
2123
DecoderEngine::poppler, DecoderEngine::wvware, DecoderEngine::odr};
2224

23-
DecodedFile decoded_file{input, decode_preference};
25+
DecodedFile decoded_file{input, decode_preference, *logger};
2426

2527
if (decoded_file.password_encrypted() && !password) {
26-
std::cerr << "document encrypted but no password given" << std::endl;
28+
ODR_FATAL(*logger, "document encrypted but no password given");
2729
return 2;
2830
}
2931
if (decoded_file.password_encrypted()) {
3032
try {
3133
decoded_file = decoded_file.decrypt(*password);
3234
} catch (const WrongPasswordError &) {
33-
std::cerr << "wrong password" << std::endl;
35+
ODR_FATAL(*logger, "wrong password");
3436
return 1;
3537
}
3638
}
@@ -47,10 +49,10 @@ int main(int argc, char **argv) {
4749
{
4850
std::string prefix = "one_file";
4951
HtmlViews views = server.serve_file(decoded_file, prefix, html_config);
50-
std::cout << "hosted decoded file with id: " << prefix << std::endl;
52+
ODR_INFO(*logger, "hosted decoded file with id: " << prefix);
5153
for (const auto &view : views) {
52-
std::cout << "http://localhost:8080/file/" << prefix << "/" << view.path()
53-
<< std::endl;
54+
ODR_INFO(*logger,
55+
"http://localhost:8080/file/" << prefix << "/" << view.path());
5456
}
5557
}
5658

src/odr/exceptions.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <odr/exceptions.hpp>
22

33
#include <odr/file.hpp>
4+
#include <odr/odr.hpp>
45

56
namespace odr {
67

@@ -18,11 +19,19 @@ FileNotFound::FileNotFound(const std::string &path)
1819
UnknownFileType::UnknownFileType() : std::runtime_error("unknown file type") {}
1920

2021
UnsupportedFileType::UnsupportedFileType(const FileType file_type)
21-
: std::runtime_error("unknown file type"), file_type{file_type} {}
22+
: std::runtime_error("unknown file type: " +
23+
file_type_to_string(file_type)),
24+
file_type{file_type} {}
2225

2326
UnknownDecoderEngine::UnknownDecoderEngine()
2427
: std::runtime_error("unknown decoder engine") {}
2528

29+
UnsupportedDecoderEngine::UnsupportedDecoderEngine(
30+
const DecoderEngine decoder_engine)
31+
: std::runtime_error("unsupported decoder engine: " +
32+
decoder_engine_to_string(decoder_engine)),
33+
decoder_engine{decoder_engine} {}
34+
2635
FileReadError::FileReadError() : std::runtime_error("file read error") {}
2736

2837
FileWriteError::FileWriteError(const std::string &path)

src/odr/exceptions.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace odr {
66
enum class FileType;
7+
enum class DecoderEngine;
78

89
/// @brief Unsupported operation exception
910
struct UnsupportedOperation final : public std::runtime_error {
@@ -34,6 +35,13 @@ struct UnknownDecoderEngine final : public std::runtime_error {
3435
UnknownDecoderEngine();
3536
};
3637

38+
/// @brief Unsupported decoder engine exception
39+
struct UnsupportedDecoderEngine final : public std::runtime_error {
40+
DecoderEngine decoder_engine;
41+
42+
explicit UnsupportedDecoderEngine(DecoderEngine decoder_engine);
43+
};
44+
3745
/// @brief File read error
3846
struct FileReadError final : public std::runtime_error {
3947
FileReadError();

src/odr/file.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ void File::copy(const std::string &path) const {
6060

6161
std::shared_ptr<internal::abstract::File> File::impl() const { return m_impl; }
6262

63-
std::vector<FileType> DecodedFile::list_file_types(const std::string &path) {
63+
std::vector<FileType> DecodedFile::list_file_types(const std::string &path,
64+
Logger &logger) {
6465
return internal::open_strategy::list_file_types(
65-
std::make_shared<internal::common::DiskFile>(path));
66+
std::make_shared<internal::common::DiskFile>(path), logger);
6667
}
6768

68-
std::vector<DecoderEngine>
69-
DecodedFile::list_decoder_engines(const std::string &path, FileType as) {
70-
return internal::open_strategy::list_decoder_engines(
71-
std::make_shared<internal::common::DiskFile>(path), as);
69+
std::vector<DecoderEngine> DecodedFile::list_decoder_engines(FileType as) {
70+
return internal::open_strategy::list_decoder_engines(as);
7271
}
7372

7473
DecodedFile::DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl)
@@ -78,24 +77,26 @@ DecodedFile::DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl)
7877
}
7978
}
8079

81-
DecodedFile::DecodedFile(const File &file)
82-
: DecodedFile(internal::open_strategy::open_file(file.impl())) {}
80+
DecodedFile::DecodedFile(const File &file, Logger &logger)
81+
: DecodedFile(internal::open_strategy::open_file(file.impl(), logger)) {}
8382

84-
DecodedFile::DecodedFile(const File &file, FileType as)
85-
: DecodedFile(internal::open_strategy::open_file(file.impl(), as)) {}
83+
DecodedFile::DecodedFile(const File &file, FileType as, Logger &logger)
84+
: DecodedFile(internal::open_strategy::open_file(file.impl(), as, logger)) {
85+
}
8686

87-
DecodedFile::DecodedFile(const std::string &path)
87+
DecodedFile::DecodedFile(const std::string &path, Logger &logger)
8888
: DecodedFile(internal::open_strategy::open_file(
89-
std::make_shared<internal::common::DiskFile>(path))) {}
89+
std::make_shared<internal::common::DiskFile>(path), logger)) {}
9090

91-
DecodedFile::DecodedFile(const std::string &path, FileType as)
91+
DecodedFile::DecodedFile(const std::string &path, FileType as, Logger &logger)
9292
: DecodedFile(internal::open_strategy::open_file(
93-
std::make_shared<internal::common::DiskFile>(path), as)) {}
93+
std::make_shared<internal::common::DiskFile>(path), as, logger)) {}
9494

9595
DecodedFile::DecodedFile(const std::string &path,
96-
const DecodePreference &preference)
96+
const DecodePreference &preference, Logger &logger)
9797
: DecodedFile(internal::open_strategy::open_file(
98-
std::make_shared<internal::common::DiskFile>(path), preference)) {}
98+
std::make_shared<internal::common::DiskFile>(path), preference,
99+
logger)) {}
99100

100101
File DecodedFile::file() const { return File(m_impl->file()); }
101102

@@ -227,9 +228,9 @@ DocumentFile::DocumentFile(
227228
std::shared_ptr<internal::abstract::DocumentFile> impl)
228229
: DecodedFile(impl), m_impl{std::move(impl)} {}
229230

230-
DocumentFile::DocumentFile(const std::string &path)
231+
DocumentFile::DocumentFile(const std::string &path, Logger &logger)
231232
: DocumentFile(internal::open_strategy::open_document_file(
232-
std::make_shared<internal::common::DiskFile>(path))) {}
233+
std::make_shared<internal::common::DiskFile>(path), logger)) {}
233234

234235
DocumentType DocumentFile::document_type() const {
235236
return m_impl->document_type();

src/odr/file.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <odr/logger.hpp>
4+
35
#include <cstdint>
46
#include <memory>
57
#include <optional>
@@ -174,16 +176,19 @@ class File final {
174176
/// @brief Represents a decoded file.
175177
class DecodedFile {
176178
public:
177-
static std::vector<FileType> list_file_types(const std::string &path);
178-
static std::vector<DecoderEngine>
179-
list_decoder_engines(const std::string &path, FileType as);
179+
static std::vector<FileType> list_file_types(const std::string &path,
180+
Logger &logger = Logger::null());
181+
static std::vector<DecoderEngine> list_decoder_engines(FileType as);
180182

181183
explicit DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl);
182-
explicit DecodedFile(const File &file);
183-
DecodedFile(const File &file, FileType as);
184-
explicit DecodedFile(const std::string &path);
185-
DecodedFile(const std::string &path, FileType as);
186-
DecodedFile(const std::string &path, const DecodePreference &preference);
184+
explicit DecodedFile(const File &file, Logger &logger = Logger::null());
185+
DecodedFile(const File &file, FileType as, Logger &logger = Logger::null());
186+
explicit DecodedFile(const std::string &path,
187+
Logger &logger = Logger::null());
188+
DecodedFile(const std::string &path, FileType as,
189+
Logger &logger = Logger::null());
190+
DecodedFile(const std::string &path, const DecodePreference &preference,
191+
Logger &logger = Logger::null());
187192

188193
[[nodiscard]] File file() const;
189194

@@ -255,7 +260,8 @@ class DocumentFile final : public DecodedFile {
255260
static FileMeta meta(const std::string &path);
256261

257262
explicit DocumentFile(std::shared_ptr<internal::abstract::DocumentFile>);
258-
explicit DocumentFile(const std::string &path);
263+
explicit DocumentFile(const std::string &path,
264+
Logger &logger = Logger::null());
259265

260266
[[nodiscard]] DocumentType document_type() const;
261267
[[nodiscard]] DocumentMeta document_meta() const;

src/odr/html.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,23 @@ void HtmlResource::write_resource(std::ostream &os) const {
214214

215215
HtmlService html::translate(const DecodedFile &decoded_file,
216216
const std::string &output_path,
217-
const HtmlConfig &config) {
217+
const HtmlConfig &config,
218+
std::shared_ptr<Logger> logger) {
218219
if (decoded_file.is_text_file()) {
219-
return translate(decoded_file.as_text_file(), output_path, config);
220+
return translate(decoded_file.as_text_file(), output_path, config,
221+
std::move(logger));
220222
} else if (decoded_file.is_image_file()) {
221-
return translate(decoded_file.as_image_file(), output_path, config);
223+
return translate(decoded_file.as_image_file(), output_path, config,
224+
std::move(logger));
222225
} else if (decoded_file.is_archive_file()) {
223-
return translate(decoded_file.as_archive_file(), output_path, config);
226+
return translate(decoded_file.as_archive_file(), output_path, config,
227+
std::move(logger));
224228
} else if (decoded_file.is_document_file()) {
225-
return translate(decoded_file.as_document_file(), output_path, config);
229+
return translate(decoded_file.as_document_file(), output_path, config,
230+
std::move(logger));
226231
} else if (decoded_file.is_pdf_file()) {
227-
return translate(decoded_file.as_pdf_file(), output_path, config);
232+
return translate(decoded_file.as_pdf_file(), output_path, config,
233+
std::move(logger));
228234
}
229235

230236
throw UnsupportedFileType(decoded_file.file_type());
@@ -257,75 +263,89 @@ HtmlResourceLocator html::standard_resource_locator() {
257263

258264
HtmlService html::translate(const TextFile &text_file,
259265
const std::string &output_path,
260-
const HtmlConfig &config) {
266+
const HtmlConfig &config,
267+
std::shared_ptr<Logger> logger) {
261268
std::filesystem::create_directories(output_path);
262-
return internal::html::create_text_service(text_file, output_path, config);
269+
return internal::html::create_text_service(text_file, output_path, config,
270+
std::move(logger));
263271
}
264272

265273
HtmlService html::translate(const ImageFile &image_file,
266274
const std::string &output_path,
267-
const HtmlConfig &config) {
275+
const HtmlConfig &config,
276+
std::shared_ptr<Logger> logger) {
268277
std::filesystem::create_directories(output_path);
269-
return internal::html::create_image_service(image_file, output_path, config);
278+
return internal::html::create_image_service(image_file, output_path, config,
279+
std::move(logger));
270280
}
271281

272282
HtmlService html::translate(const ArchiveFile &archive_file,
273283
const std::string &output_path,
274-
const HtmlConfig &config) {
275-
return translate(archive_file.archive(), output_path, config);
284+
const HtmlConfig &config,
285+
std::shared_ptr<Logger> logger) {
286+
return translate(archive_file.archive(), output_path, config,
287+
std::move(logger));
276288
}
277289

278290
HtmlService html::translate(const DocumentFile &document_file,
279291
const std::string &output_path,
280-
const HtmlConfig &config) {
292+
const HtmlConfig &config,
293+
std::shared_ptr<Logger> logger) {
281294
auto document_file_impl = document_file.impl();
282295

283296
#ifdef ODR_WITH_WVWARE
284297
if (auto wv_document_file =
285298
std::dynamic_pointer_cast<internal::WvWareLegacyMicrosoftFile>(
286299
document_file_impl)) {
287300
std::filesystem::create_directories(output_path);
288-
return internal::html::create_wvware_oldms_service(*wv_document_file,
289-
output_path, config);
301+
return internal::html::create_wvware_oldms_service(
302+
*wv_document_file, output_path, config, std::move(logger));
290303
}
291304
#endif
292305

293-
return translate(document_file.document(), output_path, config);
306+
return translate(document_file.document(), output_path, config,
307+
std::move(logger));
294308
}
295309

296310
HtmlService html::translate(const PdfFile &pdf_file,
297311
const std::string &output_path,
298-
const HtmlConfig &config) {
312+
const HtmlConfig &config,
313+
std::shared_ptr<Logger> logger) {
299314
auto pdf_file_impl = pdf_file.impl();
300315

301316
#ifdef ODR_WITH_PDF2HTMLEX
302317
if (auto poppler_pdf_file =
303318
std::dynamic_pointer_cast<internal::PopplerPdfFile>(pdf_file_impl)) {
304319
std::filesystem::create_directories(output_path);
305-
return internal::html::create_poppler_pdf_service(*poppler_pdf_file,
306-
output_path, config);
320+
return internal::html::create_poppler_pdf_service(
321+
*poppler_pdf_file, output_path, config, std::move(logger));
307322
}
308323
#endif
309324

310-
return internal::html::create_pdf_service(pdf_file, output_path, config);
325+
return internal::html::create_pdf_service(pdf_file, output_path, config,
326+
std::move(logger));
311327
}
312328

313329
HtmlService html::translate(const Archive &archive,
314330
const std::string &output_path,
315-
const HtmlConfig &config) {
331+
const HtmlConfig &config,
332+
std::shared_ptr<Logger> logger) {
316333
std::filesystem::create_directories(output_path);
317-
return internal::html::create_filesystem_service(archive.as_filesystem(),
318-
output_path, config);
334+
return internal::html::create_filesystem_service(
335+
archive.as_filesystem(), output_path, config, std::move(logger));
319336
}
320337

321338
HtmlService html::translate(const Document &document,
322339
const std::string &output_path,
323-
const HtmlConfig &config) {
340+
const HtmlConfig &config,
341+
std::shared_ptr<Logger> logger) {
324342
std::filesystem::create_directories(output_path);
325-
return internal::html::create_document_service(document, output_path, config);
343+
return internal::html::create_document_service(document, output_path, config,
344+
std::move(logger));
326345
}
327346

328-
void html::edit(const Document &document, const char *diff) {
347+
void html::edit(const Document &document, const char *diff,
348+
Logger & /*logger*/) {
329349
auto json = nlohmann::json::parse(diff);
330350
for (const auto &[key, value] : json["modifiedText"].items()) {
331351
auto element =

0 commit comments

Comments
 (0)