From e0720eaa78a56dafe5f5e572387b3a0cc8c88ec9 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 18 Jun 2022 10:33:27 -0400 Subject: Use the default logger for other writes to stdout/stderr When there is no context for writing output or error messages, use the default logger. --- libqpdf/QPDFArgParser.cc | 3 +- libqpdf/QPDFJob_argv.cc | 73 +++++++++++++++++++-------------------------- libqpdf/QPDFJob_config.cc | 8 +++-- libqpdf/QPDFObjectHandle.cc | 24 +++++++-------- libqpdf/qpdf-c.cc | 9 ++++-- libqpdf/qpdfjob-c.cc | 7 +++-- 6 files changed, 59 insertions(+), 65 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFArgParser.cc b/libqpdf/QPDFArgParser.cc index 63197684..c58edec7 100644 --- a/libqpdf/QPDFArgParser.cc +++ b/libqpdf/QPDFArgParser.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -235,7 +236,7 @@ QPDFArgParser::argCompletionZsh() void QPDFArgParser::argHelp(std::string const& p) { - std::cout << getHelp(p); + QPDFLogger::defaultLogger()->info(getHelp(p)); exit(0); } diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc index a6fb5df7..aa4755e8 100644 --- a/libqpdf/QPDFJob_argv.cc +++ b/libqpdf/QPDFJob_argv.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -104,10 +105,10 @@ void ArgParser::argVersion() { auto whoami = this->ap.getProgname(); - std::cout << whoami << " version " << QPDF::QPDFVersion() << std::endl - << "Run " << whoami - << " --copyright to see copyright and license information." - << std::endl; + *QPDFLogger::defaultLogger()->getInfo() + << whoami << " version " << QPDF::QPDFVersion() << "\n" + << "Run " << whoami + << " --copyright to see copyright and license information.\n"; } void @@ -117,48 +118,35 @@ ArgParser::argCopyright() // Make sure the output looks right on an 80-column display. // 1 2 3 4 5 6 7 8 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 - std::cout + *QPDFLogger::defaultLogger()->getInfo() << this->ap.getProgname() - << " version " << QPDF::QPDFVersion() << std::endl - << std::endl - << "Copyright (c) 2005-2022 Jay Berkenbilt" - << std::endl - << "QPDF is licensed under the Apache License, Version 2.0 (the \"License\");" - << std::endl - << "you may not use this file except in compliance with the License." - << std::endl - << "You may obtain a copy of the License at" - << std::endl - << std::endl - << " http://www.apache.org/licenses/LICENSE-2.0" - << std::endl - << std::endl - << "Unless required by applicable law or agreed to in writing, software" - << std::endl - << "distributed under the License is distributed on an \"AS IS\" BASIS," - << std::endl - << "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." - << std::endl - << "See the License for the specific language governing permissions and" - << std::endl - << "limitations under the License." - << std::endl - << std::endl - << "Versions of qpdf prior to version 7 were released under the terms" - << std::endl - << "of version 2.0 of the Artistic License. At your option, you may" - << std::endl - << "continue to consider qpdf to be licensed under those terms. Please" - << std::endl - << "see the manual for additional information." - << std::endl; + << " version " << QPDF::QPDFVersion() << "\n" + << "\n" + << "Copyright (c) 2005-2022 Jay Berkenbilt\n" + << "QPDF is licensed under the Apache License, Version 2.0 (the \"License\");\n" + << "you may not use this file except in compliance with the License.\n" + << "You may obtain a copy of the License at\n" + << "\n" + << " http://www.apache.org/licenses/LICENSE-2.0\n" + << "\n" + << "Unless required by applicable law or agreed to in writing, software\n" + << "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + << "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + << "See the License for the specific language governing permissions and\n" + << "limitations under the License.\n" + << "\n" + << "Versions of qpdf prior to version 7 were released under the terms\n" + << "of version 2.0 of the Artistic License. At your option, you may\n" + << "continue to consider qpdf to be licensed under those terms. Please\n" + << "see the manual for additional information.\n"; // clang-format on } void ArgParser::argJsonHelp() { - std::cout << QPDFJob::json_out_schema_v1() << std::endl; + *QPDFLogger::defaultLogger()->getInfo() + << QPDFJob::json_out_schema_v1() << "\n"; } void @@ -166,10 +154,10 @@ ArgParser::argShowCrypto() { auto crypto = QPDFCryptoProvider::getRegisteredImpls(); std::string default_crypto = QPDFCryptoProvider::getDefaultProvider(); - std::cout << default_crypto << std::endl; + *QPDFLogger::defaultLogger()->getInfo() << default_crypto << "\n"; for (auto const& iter: crypto) { if (iter != default_crypto) { - std::cout << iter << std::endl; + *QPDFLogger::defaultLogger()->getInfo() << iter << "\n"; } } } @@ -407,7 +395,8 @@ ArgParser::argEndCopyAttachment() void ArgParser::argJobJsonHelp() { - std::cout << QPDFJob::job_json_schema_v1() << std::endl; + *QPDFLogger::defaultLogger()->getInfo() + << QPDFJob::job_json_schema_v1() << "\n"; } void diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index 5a343f79..b9b53ebd 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -1,5 +1,6 @@ #include +#include #include #include @@ -648,9 +649,10 @@ QPDFJob::Config::passwordFile(std::string const& parameter) o.m->password = QUtil::make_shared_cstr(lines.front()); if (lines.size() > 1) { - std::cerr << this->o.m->message_prefix - << ": WARNING: all but the first line of" - << " the password file are ignored" << std::endl; + *QPDFLogger::defaultLogger()->getError() + << this->o.m->message_prefix + << ": WARNING: all but the first line of" + << " the password file are ignored\n"; } } return this; diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 56a545d6..1578f38f 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -578,13 +579,12 @@ QPDFObjectHandle::getIntValueAsInt() if (v < INT_MIN) { QTC::TC("qpdf", "QPDFObjectHandle int returning INT_MIN"); warnIfPossible( - "requested value of integer is too small; returning INT_MIN", - false); + "requested value of integer is too small; returning INT_MIN"); result = INT_MIN; } else if (v > INT_MAX) { QTC::TC("qpdf", "QPDFObjectHandle int returning INT_MAX"); warnIfPossible( - "requested value of integer is too big; returning INT_MAX", false); + "requested value of integer is too big; returning INT_MAX"); result = INT_MAX; } else { result = static_cast(v); @@ -610,7 +610,7 @@ QPDFObjectHandle::getUIntValue() if (v < 0) { QTC::TC("qpdf", "QPDFObjectHandle uint returning 0"); warnIfPossible( - "unsigned value request for negative number; returning 0", false); + "unsigned value request for negative number; returning 0"); } else { result = static_cast(v); } @@ -635,15 +635,12 @@ QPDFObjectHandle::getUIntValueAsUInt() if (v < 0) { QTC::TC("qpdf", "QPDFObjectHandle uint uint returning 0"); warnIfPossible( - "unsigned integer value request for negative number; returning 0", - false); + "unsigned integer value request for negative number; returning 0"); result = 0; } else if (v > UINT_MAX) { QTC::TC("qpdf", "QPDFObjectHandle uint returning UINT_MAX"); - warnIfPossible( - "requested value of unsigned integer is too big;" - " returning UINT_MAX", - false); + warnIfPossible("requested value of unsigned integer is too big;" + " returning UINT_MAX"); result = UINT_MAX; } else { result = static_cast(v); @@ -3000,16 +2997,15 @@ QPDFObjectHandle::typeWarning( } void -QPDFObjectHandle::warnIfPossible( - std::string const& warning, bool throw_if_no_description) +QPDFObjectHandle::warnIfPossible(std::string const& warning) { QPDF* context = 0; std::string description; dereference(); if (this->obj->getDescription(context, description)) { warn(context, QPDFExc(qpdf_e_damaged_pdf, "", description, 0, warning)); - } else if (throw_if_no_description) { - throw std::runtime_error(warning); + } else { + *QPDFLogger::defaultLogger()->getError() << warning << "\n"; } } diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index 8a940e3c..468811b3 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -185,8 +186,9 @@ qpdf_cleanup(qpdf_data* qpdf) qpdf_oh_release_all(*qpdf); if ((*qpdf)->error.get()) { QTC::TC("qpdf", "qpdf-c cleanup warned about unhandled error"); - std::cerr << "WARNING: application did not handle error: " - << (*qpdf)->error->what() << std::endl; + *QPDFLogger::defaultLogger()->getWarn() + << "WARNING: application did not handle error: " + << (*qpdf)->error->what() << "\n"; } delete *qpdf; *qpdf = 0; @@ -898,7 +900,8 @@ trap_oh_errors( " to ERROR HANDLING in qpdf-c.h")); qpdf->oh_error_occurred = true; } - std::cerr << qpdf->error->what() << std::endl; + *QPDFLogger::defaultLogger()->getError() + << qpdf->error->what() << "\n"; } return fallback(); } diff --git a/libqpdf/qpdfjob-c.cc b/libqpdf/qpdfjob-c.cc index 0e8ded02..4e6666be 100644 --- a/libqpdf/qpdfjob-c.cc +++ b/libqpdf/qpdfjob-c.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -19,7 +20,8 @@ qpdfjob_run_from_argv(char const* const argv[]) j.initializeFromArgv(argv); j.run(); } catch (std::exception& e) { - std::cerr << whoami << ": " << e.what() << std::endl; + *QPDFLogger::defaultLogger()->getError() + << whoami << ": " << e.what() << "\n"; return QPDFJob::EXIT_ERROR; } return j.getExitCode(); @@ -48,7 +50,8 @@ qpdfjob_run_from_json(char const* json) j.initializeFromJson(json); j.run(); } catch (std::exception& e) { - std::cerr << "qpdfjob json: " << e.what() << std::endl; + *QPDFLogger::defaultLogger()->getError() + << "qpdfjob json: " << e.what() << "\n"; return QPDFJob::EXIT_ERROR; } return j.getExitCode(); -- cgit v1.2.3-54-g00ecf