diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-09-09 14:03:21 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-09-09 16:49:25 +0200 |
commit | 3dbab589e32d3ed5bd98e0634255ba7dfab4c892 (patch) | |
tree | 132391fe89db1b2fbf50929721e0eb8bb167e47d /qpdf | |
parent | 0ad4e190ffbb85ea2db5a05d43cd4f81d98cfe63 (diff) | |
download | qpdf-3dbab589e32d3ed5bd98e0634255ba7dfab4c892.tar.zst |
Add C API functions for using custom loggers
Expose functions to the C API to create new loggers and to setLogger
and getLogger for QPDF and QPDFJob.
Diffstat (limited to 'qpdf')
-rw-r--r-- | qpdf/qpdf-ctest.c | 23 | ||||
-rw-r--r-- | qpdf/qpdfjob-ctest.c | 32 | ||||
-rw-r--r-- | qpdf/qtest/qpdf/c-check-warn.out | 6 | ||||
-rw-r--r-- | qpdf/qtest/qpdf/qpdfjob-ctest.out | 3 |
4 files changed, 58 insertions, 6 deletions
diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index b562ed8b..60986611 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -140,6 +140,15 @@ write_to_file(char const* data, size_t size, void* udata) return fwrite(data, 1, size, f) != size; } +static int +custom_log(char const* data, size_t size, void* udata) +{ + fprintf(stderr, "|custom|"); + fwrite(data, 1, size, stderr); + fflush(stderr); + return 0; +} + static void test01( char const* infile, @@ -583,6 +592,20 @@ test23( char const* outfile, char const* xarg) { + /* Test check and also exercise custom logger */ + qpdflogger_handle l1 = qpdf_get_logger(qpdf); + qpdflogger_handle l2 = qpdflogger_default_logger(); + assert(qpdflogger_equal(l1, l2)); + qpdflogger_cleanup(&l1); + qpdflogger_cleanup(&l2); + qpdflogger_handle l = qpdflogger_create(); + qpdflogger_set_warn(l, qpdf_log_dest_custom, custom_log, NULL); + qpdf_set_logger(qpdf, l); + qpdflogger_handle l3 = qpdf_get_logger(qpdf); + assert(qpdflogger_equal(l, l3)); + qpdflogger_cleanup(&l); + qpdflogger_cleanup(&l3); + QPDF_ERROR_CODE status = 0; qpdf_read(qpdf, infile, password); status = qpdf_check_pdf(qpdf); diff --git a/qpdf/qpdfjob-ctest.c b/qpdf/qpdfjob-ctest.c index f3272aef..708d5fa6 100644 --- a/qpdf/qpdfjob-ctest.c +++ b/qpdf/qpdfjob-ctest.c @@ -26,6 +26,15 @@ custom_progress(int progress, void* data) printf("%s: write progress: %d%%\n", (char const*)data, progress); } +static int +custom_log(char const* data, size_t size, void* udata) +{ + fprintf(stderr, "|custom|"); + fwrite(data, 1, size, stderr); + fflush(stderr); + return 0; +} + static void run_tests() { @@ -55,6 +64,7 @@ run_tests() \"objectStreams\": \"generate\"\n\ }") == 0); printf("json test passed\n"); + fflush(stdout); assert(qpdfjob_run_from_json("{\n\ \"inputFile\": \"xref-with-short-size.pdf\",\n\ @@ -64,10 +74,28 @@ run_tests() \"objectStreams\": \"generate\"\n\ }") == 3); printf("json warn test passed\n"); + fflush(stdout); - assert(qpdfjob_run_from_json("{\n\ + /* Also exercise custom logger */ + j = qpdfjob_init(); + qpdflogger_handle l1 = qpdfjob_get_logger(j); + qpdflogger_handle l2 = qpdflogger_default_logger(); + assert(qpdflogger_equal(l1, l2)); + qpdflogger_cleanup(&l1); + qpdflogger_cleanup(&l2); + qpdflogger_handle l = qpdflogger_create(); + qpdflogger_set_error(l, qpdf_log_dest_custom, custom_log, NULL); + qpdfjob_set_logger(j, l); + qpdflogger_handle l3 = qpdfjob_get_logger(j); + assert(qpdflogger_equal(l, l3)); + qpdflogger_cleanup(&l); + qpdflogger_cleanup(&l3); + + qpdfjob_initialize_from_json(j, "{\n\ \"inputFile\": \"nothing-there.pdf\"\n\ -}") == 2); +}"); + assert(qpdfjob_run(j) == 2); + qpdfjob_cleanup(&j); printf("json error test passed\n"); } diff --git a/qpdf/qtest/qpdf/c-check-warn.out b/qpdf/qtest/qpdf/c-check-warn.out index 65602762..c733f15d 100644 --- a/qpdf/qtest/qpdf/c-check-warn.out +++ b/qpdf/qtest/qpdf/c-check-warn.out @@ -1,6 +1,6 @@ -WARNING: c-check-warn-in.pdf: file is damaged -WARNING: c-check-warn-in.pdf (offset 1556): xref not found -WARNING: c-check-warn-in.pdf: Attempting to reconstruct cross-reference table +|custom|WARNING: |custom|c-check-warn-in.pdf: file is damaged|custom| +|custom|WARNING: |custom|c-check-warn-in.pdf (offset 1556): xref not found|custom| +|custom|WARNING: |custom|c-check-warn-in.pdf: Attempting to reconstruct cross-reference table|custom| status: 1 warning: c-check-warn-in.pdf: file is damaged code: 5 diff --git a/qpdf/qtest/qpdf/qpdfjob-ctest.out b/qpdf/qtest/qpdf/qpdfjob-ctest.out index 22d431de..1778e70a 100644 --- a/qpdf/qtest/qpdf/qpdfjob-ctest.out +++ b/qpdf/qtest/qpdf/qpdfjob-ctest.out @@ -6,5 +6,6 @@ json test passed WARNING: xref-with-short-size.pdf (xref stream, offset 16227): Cross-reference stream data has the wrong size; expected = 52; actual = 56 qpdfjob json: operation succeeded with warnings; resulting file may have some problems json warn test passed -qpdfjob json: an output file name is required; use - for standard output +|custom|qpdfjob json|custom|: |custom|an output file name is required; use - for standard output|custom| +|custom|qpdfjob json|custom|: |custom|an output file name is required; use - for standard output|custom| json error test passed |