aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-06-18 15:40:41 +0200
committerJay Berkenbilt <ejb@ql.org>2022-06-18 15:54:40 +0200
commit83be2191b4f3eb8906160d61f61cae48532ee651 (patch)
treeedc4298b13dba0bdd501954489549725218c2373 /libqpdf
parent641e92c6a7662a01f488947c3791f3b77e85517f (diff)
downloadqpdf-83be2191b4f3eb8906160d61f61cae48532ee651.tar.zst
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.
Diffstat (limited to 'libqpdf')
-rw-r--r--libqpdf/QPDFJob.cc48
-rw-r--r--libqpdf/QPDFLogger.cc5
2 files changed, 35 insertions, 18 deletions
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<QPDFWriter::ProgressReporter>(
// 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 <qpdf/Pl_Discard.hh>
#include <qpdf/Pl_OStream.hh>
+#include <qpdf/QUtil.hh>
#include <iostream>
#include <stdexcept>
@@ -182,6 +183,9 @@ QPDFLogger::setError(std::shared_ptr<Pipeline> p)
void
QPDFLogger::setSave(std::shared_ptr<Pipeline> p)
{
+ if (this->m->p_save == p) {
+ return;
+ }
if (p == this->m->p_stdout) {
auto pt = dynamic_cast<Pl_Track*>(p.get());
if (pt->getUsed()) {
@@ -192,6 +196,7 @@ QPDFLogger::setSave(std::shared_ptr<Pipeline> 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;
}