From 83be2191b4f3eb8906160d61f61cae48532ee651 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 18 Jun 2022 09:40:41 -0400 Subject: Use "save" logger when saving data to standard output This includes the output PDF, streams from --show-object and attachments from --save-attachment. This also enables --verbose and --progress to work with saving to stdout. --- libqpdf/QPDFJob.cc | 48 ++++++++++++++++++++++++++++++------------------ libqpdf/QPDFLogger.cc | 5 +++++ 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 4ea03edb..9f76bdf4 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -691,22 +691,21 @@ QPDFJob::checkConfiguration() " before the -- that follows --encrypt."); } + bool save_to_stdout = false; if (m->require_outfile && m->outfilename && (strcmp(m->outfilename.get(), "-") == 0)) { if (m->split_pages) { usage("--split-pages may not be used when" " writing to standard output"); } - if (this->m->verbose) { - usage("--verbose may not be used when" - " writing to standard output"); - } - if (m->progress) { - usage("--progress may not be used when" - " writing to standard output"); - } + save_to_stdout = true; + } + if (!m->attachment_to_show.empty()) { + save_to_stdout = true; + } + if (save_to_stdout) { + this->m->log->saveToStandardOutput(); } - if ((!m->split_pages) && QUtil::same_file(m->infilename.get(), m->outfilename.get())) { QTC::TC("qpdf", "QPDFJob same file error"); @@ -918,10 +917,11 @@ QPDFJob::doShowObj(QPDF& pdf) obj.warnIfPossible("unable to filter stream data"); error = true; } else { - QUtil::binary_stdout(); - Pl_StdioFile out("stdout", stdout); + // If anything has been written to standard output, + // this will fail. + this->m->log->saveToStandardOutput(); obj.pipeStreamData( - &out, + this->m->log->getSave().get(), (filter && m->normalize) ? qpdf_ef_normalize : 0, filter ? qpdf_dl_all : qpdf_dl_none); } @@ -1023,9 +1023,10 @@ QPDFJob::doShowAttachment(QPDF& pdf) "attachment " + m->attachment_to_show + " not found"); } auto efs = fs->getEmbeddedFileStream(); - QUtil::binary_stdout(); - Pl_StdioFile out("stdout", stdout); - efs.pipeStreamData(&out, 0, qpdf_dl_all); + // saveToStandardOutput has already been called, but it's harmless + // to call it again, so do as defensive coding. + this->m->log->saveToStandardOutput(); + efs.pipeStreamData(this->m->log->getSave().get(), 0, qpdf_dl_all); } void @@ -3138,14 +3139,17 @@ QPDFJob::setWriterOptions(QPDF& pdf, QPDFWriter& w) parse_version(m->force_version, version, extension_level); w.forcePDFVersion(version, extension_level); } - if (m->progress && m->outfilename) { + if (m->progress) { + char const* outfilename = this->m->outfilename + ? this->m->outfilename.get() + : "standard output"; w.registerProgressReporter( std::shared_ptr( // line-break new ProgressReporter( *this->m->log->getInfo(), this->m->message_prefix, - m->outfilename.get()))); + outfilename))); } } @@ -3273,7 +3277,15 @@ QPDFJob::writeOutfile(QPDF& pdf) } else { // QPDFWriter must have block scope so the output file will be // closed after write() finishes. - QPDFWriter w(pdf, m->outfilename.get()); + QPDFWriter w(pdf); + if (this->m->outfilename) { + w.setOutputFilename(m->outfilename.get()); + } else { + // saveToStandardOutput has already been called, but + // calling it again is defensive and harmless. + this->m->log->saveToStandardOutput(); + w.setOutputPipeline(this->m->log->getSave().get()); + } setWriterOptions(pdf, w); w.write(); } diff --git a/libqpdf/QPDFLogger.cc b/libqpdf/QPDFLogger.cc index 859d6dcd..3b25c050 100644 --- a/libqpdf/QPDFLogger.cc +++ b/libqpdf/QPDFLogger.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -182,6 +183,9 @@ QPDFLogger::setError(std::shared_ptr p) void QPDFLogger::setSave(std::shared_ptr p) { + if (this->m->p_save == p) { + return; + } if (p == this->m->p_stdout) { auto pt = dynamic_cast(p.get()); if (pt->getUsed()) { @@ -192,6 +196,7 @@ QPDFLogger::setSave(std::shared_ptr p) if (this->m->p_info == this->m->p_stdout) { this->m->p_info = this->m->p_stderr; } + QUtil::binary_stdout(); } this->m->p_save = p; } -- cgit v1.2.3-54-g00ecf