Skip to content

Commit efd1cd3

Browse files
committed
wire logger to html service
1 parent 515abaf commit efd1cd3

20 files changed

Lines changed: 173 additions & 100 deletions

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 =

src/odr/html.hpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include <odr/document.hpp>
44
#include <odr/file.hpp>
5+
#include <odr/logger.hpp>
56
#include <odr/style.hpp>
67

78
#include <functional>
9+
#include <memory>
810
#include <optional>
911
#include <string>
1012
#include <vector>
@@ -189,75 +191,93 @@ HtmlResourceLocator standard_resource_locator();
189191
/// @param file Decoded file to translate.
190192
/// @param cache_path Directory path for temporary output.
191193
/// @param config Configuration for the HTML output.
194+
/// @param logger Logger to use for logging.
192195
/// @return HTML output.
193196
HtmlService translate(const DecodedFile &file, const std::string &cache_path,
194-
const HtmlConfig &config);
197+
const HtmlConfig &config,
198+
std::shared_ptr<Logger> logger = Logger::create_null());
195199

196200
/// @brief Translates a text file to HTML.
197201
///
198202
/// @param text_file Text file to translate.
199203
/// @param cache_path Directory path for temporary output.
200204
/// @param config Configuration for the HTML output.
205+
/// @param logger Logger to use for logging.
201206
/// @return HTML output.
202207
HtmlService translate(const TextFile &text_file, const std::string &cache_path,
203-
const HtmlConfig &config);
208+
const HtmlConfig &config,
209+
std::shared_ptr<Logger> logger = Logger::create_null());
204210
/// @brief Translates an image file to HTML.
205211
///
206212
/// @param image_file Image file to translate.
207213
/// @param cache_path Directory path for temporary output.
208214
/// @param config Configuration for the HTML output.
215+
/// @param logger Logger to use for logging.
209216
/// @return HTML output.
210217
HtmlService translate(const ImageFile &image_file,
211-
const std::string &cache_path, const HtmlConfig &config);
218+
const std::string &cache_path, const HtmlConfig &config,
219+
std::shared_ptr<Logger> logger = Logger::create_null());
212220
/// @brief Translates an archive to HTML.
213221
///
214222
/// @param archive Archive file to translate.
215223
/// @param cache_path Directory path for temporary output.
216224
/// @param config Configuration for the HTML output.
225+
/// @param logger Logger to use for logging.
217226
/// @return HTML output.
218227
HtmlService translate(const ArchiveFile &archive_file,
219-
const std::string &cache_path, const HtmlConfig &config);
228+
const std::string &cache_path, const HtmlConfig &config,
229+
std::shared_ptr<Logger> logger = Logger::create_null());
220230
/// @brief Translates a document to HTML.
221231
///
222232
/// @param document_file Document file to translate.
223233
/// @param cache_path Directory path for temporary output.
224234
/// @param config Configuration for the HTML output.
235+
/// @param logger Logger to use for logging.
225236
/// @return HTML output.
226237
HtmlService translate(const DocumentFile &document_file,
227-
const std::string &cache_path, const HtmlConfig &config);
238+
const std::string &cache_path, const HtmlConfig &config,
239+
std::shared_ptr<Logger> logger = Logger::create_null());
228240
/// @brief Translates a PDF file to HTML.
229241
///
230242
/// @param pdf_file PDF file to translate.
231243
/// @param cache_path Directory path for temporary output.
232244
/// @param config Configuration for the HTML output.
245+
/// @param logger Logger to use for logging.
233246
/// @return HTML output.
234247
HtmlService translate(const PdfFile &pdf_file, const std::string &cache_path,
235-
const HtmlConfig &config);
248+
const HtmlConfig &config,
249+
std::shared_ptr<Logger> logger = Logger::create_null());
236250

237251
/// @brief Translates an archive to HTML.
238252
///
239253
/// @param archive Archive to translate.
240254
/// @param cache_path Directory path for temporary output.
241255
/// @param config Configuration for the HTML output.
256+
/// @param logger Logger to use for logging.
242257
/// @return HTML output.
243258
HtmlService translate(const Archive &archive, const std::string &cache_path,
244-
const HtmlConfig &config);
259+
const HtmlConfig &config,
260+
std::shared_ptr<Logger> logger = Logger::create_null());
245261
/// @brief Translates a document to HTML.
246262
///
247263
/// @param document Document to translate.
248264
/// @param cache_path Directory path for temporary output.
249265
/// @param config Configuration for the HTML output.
266+
/// @param logger Logger to use for logging.
250267
/// @return HTML output.
251268
HtmlService translate(const Document &document, const std::string &cache_path,
252-
const HtmlConfig &config);
269+
const HtmlConfig &config,
270+
std::shared_ptr<Logger> logger = Logger::create_null());
253271

254272
/// @brief Edits a document with a diff.
255273
///
256274
/// @note The diff is generated by our JavaScript code in the browser.
257275
///
258276
/// @param document Document to edit.
259277
/// @param diff Diff to apply.
260-
void edit(const Document &document, const char *diff);
278+
/// @param logger Logger to use for logging.
279+
void edit(const Document &document, const char *diff,
280+
Logger &logger = Logger::null());
261281

262282
} // namespace html
263283

src/odr/internal/html/document.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ class HtmlServiceImpl : public HtmlService {
194194
public:
195195
HtmlServiceImpl(Document document,
196196
std::vector<std::shared_ptr<HtmlFragmentBase>> fragments,
197-
HtmlConfig config)
198-
: HtmlService(std::move(config)), m_document{std::move(document)},
199-
m_fragments{std::move(fragments)} {
197+
HtmlConfig config, std::shared_ptr<Logger> logger)
198+
: HtmlService(std::move(config), std::move(logger)),
199+
m_document{std::move(document)}, m_fragments{std::move(fragments)} {
200200
m_views.emplace_back(
201201
std::make_shared<HtmlView>(*this, "document", "document.html"));
202202
for (const auto &fragment : m_fragments) {
@@ -413,11 +413,9 @@ class PageHtmlFragment final : public HtmlFragmentBase {
413413

414414
namespace odr::internal {
415415

416-
odr::HtmlService html::create_document_service(const Document &document,
417-
const std::string &cache_path,
418-
HtmlConfig config) {
419-
(void)cache_path;
420-
416+
odr::HtmlService html::create_document_service(
417+
const Document &document, const std::string & /*cache_path*/,
418+
HtmlConfig config, std::shared_ptr<Logger> logger) {
421419
std::vector<std::shared_ptr<HtmlFragmentBase>> fragments;
422420

423421
if (document.document_type() == DocumentType::text) {
@@ -454,8 +452,8 @@ odr::HtmlService html::create_document_service(const Document &document,
454452
throw UnknownDocumentType();
455453
}
456454

457-
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(document, fragments,
458-
std::move(config)));
455+
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
456+
document, fragments, std::move(config), std::move(logger)));
459457
}
460458

461459
} // namespace odr::internal

src/odr/internal/html/document.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
namespace odr {
67
class Document;
78
struct HtmlConfig;
89
class HtmlService;
10+
class Logger;
911
} // namespace odr
1012

1113
namespace odr::internal::html {
1214

1315
odr::HtmlService create_document_service(const Document &document,
1416
const std::string &cache_path,
15-
HtmlConfig config);
17+
HtmlConfig config,
18+
std::shared_ptr<Logger> logger);
1619

1720
} // namespace odr::internal::html

src/odr/internal/html/filesystem.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ namespace {
1515

1616
class HtmlServiceImpl : public HtmlService {
1717
public:
18-
HtmlServiceImpl(Filesystem filesystem, HtmlConfig config)
19-
: HtmlService(std::move(config)), m_filesystem{std::move(filesystem)} {
18+
HtmlServiceImpl(Filesystem filesystem, HtmlConfig config,
19+
std::shared_ptr<Logger> logger)
20+
: HtmlService(std::move(config), std::move(logger)),
21+
m_filesystem{std::move(filesystem)} {
2022
m_views.emplace_back(
2123
std::make_shared<HtmlView>(*this, "files", "files.html"));
2224
}
@@ -147,13 +149,11 @@ class HtmlServiceImpl : public HtmlService {
147149

148150
namespace odr::internal {
149151

150-
odr::HtmlService html::create_filesystem_service(const Filesystem &filesystem,
151-
const std::string &cache_path,
152-
HtmlConfig config) {
153-
(void)cache_path;
154-
155-
return odr::HtmlService(
156-
std::make_unique<HtmlServiceImpl>(filesystem, std::move(config)));
152+
odr::HtmlService html::create_filesystem_service(
153+
const Filesystem &filesystem, const std::string & /*cache_path*/,
154+
HtmlConfig config, std::shared_ptr<Logger> logger) {
155+
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
156+
filesystem, std::move(config), std::move(logger)));
157157
}
158158

159159
} // namespace odr::internal
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
namespace odr {
67
enum class FileType;
78
struct HtmlConfig;
89
class HtmlService;
910
class Filesystem;
11+
class Logger;
1012
} // namespace odr
1113

1214
namespace odr::internal::html {
1315

1416
odr::HtmlService create_filesystem_service(const Filesystem &filesystem,
1517
const std::string &cache_path,
16-
HtmlConfig config);
18+
HtmlConfig config,
19+
std::shared_ptr<Logger> logger);
1720

1821
}

src/odr/internal/html/html_service.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
namespace odr::internal::html {
1010

11-
HtmlService::HtmlService(HtmlConfig config) : m_config{std::move(config)} {}
11+
HtmlService::HtmlService(HtmlConfig config, std::shared_ptr<Logger> logger)
12+
: m_config{std::move(config)}, m_logger{std::move(logger)} {}
1213

1314
const HtmlConfig &HtmlService::config() const { return m_config; }
1415

src/odr/internal/html/html_service.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ namespace odr::internal::html {
99

1010
class HtmlService : public abstract::HtmlService {
1111
public:
12-
explicit HtmlService(HtmlConfig config);
12+
HtmlService(HtmlConfig config, std::shared_ptr<Logger> logger);
1313

1414
[[nodiscard]] const HtmlConfig &config() const override;
1515

16+
protected:
17+
std::shared_ptr<Logger> m_logger;
18+
1619
private:
1720
HtmlConfig m_config;
1821
};

0 commit comments

Comments
 (0)