Convert fprintf(stderr, ...); exit(EXIT_FAILURE); to qFatal(...)
The serialization conversion example used raw C's way to abort on error; change to using Qt's way of doing the same. Likewise, convert the various other uses of fprintf(stderr, ...) to qWarning() and of printf(...) to qInfo(). Pick-to: 6.6 6.5 Task-number: QTBUG-111228 Change-Id: Ia8821d3c20f58f71c106028ec422ad473c11e164 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
parent
b80be2a684
commit
4e20b852bb
@ -10,6 +10,7 @@
|
||||
#include <QCborStreamWriter>
|
||||
#include <QCborValue>
|
||||
#include <QDataStream>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QFloat16>
|
||||
#include <QMetaType>
|
||||
@ -179,9 +180,8 @@ void CborDiagnosticDumper::saveFile(QIODevice *f, const QVariant &contents,
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unknown CBOR diagnostic option '%s'. Available options are:\n%s",
|
||||
qPrintable(s), diagnosticHelp);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown CBOR diagnostic option '%s'. Available options are:\n%s",
|
||||
qPrintable(s), diagnosticHelp);
|
||||
}
|
||||
|
||||
QTextStream out(f);
|
||||
@ -240,13 +240,12 @@ QVariant CborConverter::loadFile(QIODevice *f, const Converter *&outputConverter
|
||||
QCborValue contents = QCborValue::fromCbor(reader);
|
||||
qint64 offset = reader.currentOffset();
|
||||
if (reader.lastError()) {
|
||||
fprintf(stderr, "Error loading CBOR contents (byte %lld): %s\n", offset,
|
||||
qPrintable(reader.lastError().toString()));
|
||||
fprintf(stderr, " bytes: %s\n",
|
||||
(ptr ? mapped.mid(offset, 9) : f->read(9)).toHex(' ').constData());
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal().nospace()
|
||||
<< "Error loading CBOR contents (byte " << offset
|
||||
<< "): " << reader.lastError().toString()
|
||||
<< "\n bytes: " << (ptr ? mapped.mid(offset, 9) : f->read(9));
|
||||
} else if (offset < mapped.size() || (!ptr && f->bytesAvailable())) {
|
||||
fprintf(stderr, "Warning: bytes remaining at the end of the CBOR stream\n");
|
||||
qWarning("Warning: bytes remaining at the end of the CBOR stream");
|
||||
}
|
||||
|
||||
if (outputConverter == nullptr)
|
||||
@ -316,9 +315,8 @@ void CborConverter::saveFile(QIODevice *f, const QVariant &contents, const QStri
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unknown CBOR format option '%s'. Valid options are:\n%s",
|
||||
qPrintable(s), cborOptionHelp);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown CBOR format option '%s'. Valid options are:\n%s",
|
||||
qPrintable(s), cborOptionHelp);
|
||||
}
|
||||
//! [4]
|
||||
QCborValue v =
|
||||
|
@ -80,10 +80,8 @@ QVariant DataStreamConverter::loadFile(QIODevice *f, const Converter *&outputCon
|
||||
outputConverter = &debugTextDumper;
|
||||
|
||||
char c;
|
||||
if (f->read(sizeof(signature) - 1) != signature || !f->getChar(&c) || (c != 'l' && c != 'B')) {
|
||||
fprintf(stderr, "Could not load QDataStream file: invalid signature.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (f->read(sizeof(signature) - 1) != signature || !f->getChar(&c) || (c != 'l' && c != 'B'))
|
||||
qFatal("Could not load QDataStream file: invalid signature.");
|
||||
|
||||
QDataStream ds(f);
|
||||
ds.setByteOrder(c == 'l' ? QDataStream::LittleEndian : QDataStream::BigEndian);
|
||||
@ -125,15 +123,13 @@ void DataStreamConverter::saveFile(QIODevice *f, const QVariant &contents,
|
||||
continue;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Invalid version number '%s': must be a number from 1 to %d.\n",
|
||||
qPrintable(pair.last()), QDataStream::Qt_DefaultCompiledVersion);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Invalid version number '%s': must be a number from 1 to %d.",
|
||||
qPrintable(pair.last()), QDataStream::Qt_DefaultCompiledVersion);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unknown QDataStream formatting option '%s'. Available options are:\n%s",
|
||||
qFatal("Unknown QDataStream formatting option '%s'. Available options are:\n%s",
|
||||
qPrintable(option), dataStreamOptionHelp);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char c = order == QDataStream::LittleEndian ? 'l' : 'B';
|
||||
|
@ -18,10 +18,8 @@ static const char jsonOptionHelp[] = "compact=no|yes Use compact JS
|
||||
static QJsonDocument convertFromVariant(const QVariant &v)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromVariant(v);
|
||||
if (!doc.isObject() && !doc.isArray()) {
|
||||
fprintf(stderr, "Could not convert contents to JSON.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!doc.isObject() && !doc.isArray())
|
||||
qFatal("Could not convert contents to JSON.");
|
||||
return doc;
|
||||
}
|
||||
|
||||
@ -75,9 +73,8 @@ QVariant JsonConverter::loadFile(QIODevice *f, const Converter *&outputConverter
|
||||
if (doc.isNull())
|
||||
doc = QJsonDocument::fromJson(f->readAll(), &error);
|
||||
if (error.error) {
|
||||
fprintf(stderr, "Could not parse JSON content: offset %d: %s",
|
||||
error.offset, qPrintable(error.errorString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Could not parse JSON content: offset %d: %s",
|
||||
error.offset, qPrintable(error.errorString()));
|
||||
}
|
||||
if (outputConverter == null)
|
||||
return QVariant();
|
||||
@ -94,9 +91,8 @@ void JsonConverter::saveFile(QIODevice *f, const QVariant &contents,
|
||||
} else if (s == "compact=yes"_L1) {
|
||||
format = QJsonDocument::Compact;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown option '%s' to JSON output. Valid options are:\n%s",
|
||||
qPrintable(s), jsonOptionHelp);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown option '%s' to JSON output. Valid options are:\n%s",
|
||||
qPrintable(s), jsonOptionHelp);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,8 +43,8 @@ static const Converter *prepareConverter(QString format, Converter::Direction di
|
||||
stream->open(mode);
|
||||
|
||||
if (!stream->isOpen()) {
|
||||
fprintf(stderr, "Could not open \"%s\" for %s: %s\n",
|
||||
qPrintable(stream->fileName()), dirn, qPrintable(stream->errorString()));
|
||||
qFatal("Could not open \"%s\" for %s: %s",
|
||||
qPrintable(stream->fileName()), dirn, qPrintable(stream->errorString()));
|
||||
} else if (format == "auto"_L1) {
|
||||
for (const Converter *conv : std::as_const(*availableConverters)) {
|
||||
if (conv->directions().testFlag(direction) && conv->probeFile(stream))
|
||||
@ -54,21 +54,20 @@ static const Converter *prepareConverter(QString format, Converter::Direction di
|
||||
return nullptr;
|
||||
|
||||
// Input format, however, we must know before we can call that:
|
||||
fprintf(stderr, "Could not determine input format. Specify it with the -I option.\n");
|
||||
qFatal("Could not determine input format. Specify it with the -I option.");
|
||||
} else {
|
||||
for (const Converter *conv : std::as_const(*availableConverters)) {
|
||||
if (conv->name() == format) {
|
||||
if (!conv->directions().testFlag(direction)) {
|
||||
fprintf(stderr, "File format \"%s\" cannot be used for %s\n",
|
||||
qPrintable(format), dirn);
|
||||
qWarning("File format \"%s\" cannot be used for %s",
|
||||
qPrintable(format), dirn);
|
||||
continue; // on the off chance there's another with the same name
|
||||
}
|
||||
return conv;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "Unknown %s file format \"%s\"\n", dirn, qPrintable(format));
|
||||
qFatal("Unknown %s file format \"%s\"", dirn, qPrintable(format));
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
Q_UNREACHABLE_RETURN(nullptr);
|
||||
}
|
||||
|
||||
@ -135,17 +134,16 @@ int main(int argc, char *argv[])
|
||||
if (conv->name() == format) {
|
||||
const char *help = conv->optionsHelp();
|
||||
if (help) {
|
||||
printf("The following options are available for format '%s':\n\n%s",
|
||||
qPrintable(format), help);
|
||||
qInfo("The following options are available for format '%s':\n\n%s",
|
||||
qPrintable(format), help);
|
||||
} else {
|
||||
printf("Format '%s' supports no options.\n", qPrintable(format));
|
||||
qInfo("Format '%s' supports no options.", qPrintable(format));
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Unknown file format '%s'\n", qPrintable(format));
|
||||
return EXIT_FAILURE;
|
||||
qFatal("Unknown file format '%s'", qPrintable(format));
|
||||
}
|
||||
|
||||
QStringList files = parser.positionalArguments();
|
||||
|
@ -46,9 +46,8 @@ void NullConverter::saveFile(QIODevice *f, const QVariant &contents,
|
||||
const QStringList &options) const
|
||||
{
|
||||
if (!options.isEmpty()) {
|
||||
fprintf(stderr, "Unknown option '%s' to null output. This format has no options.\n",
|
||||
qPrintable(options.first()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown option '%s' to null output. This format has no options.",
|
||||
qPrintable(options.first()));
|
||||
}
|
||||
|
||||
Q_UNUSED(f);
|
||||
|
@ -98,9 +98,8 @@ void TextConverter::saveFile(QIODevice *f, const QVariant &contents,
|
||||
const QStringList &options) const
|
||||
{
|
||||
if (!options.isEmpty()) {
|
||||
fprintf(stderr, "Unknown option '%s' to text output. This format has no options.\n",
|
||||
qPrintable(options.first()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown option '%s' to text output. This format has no options.",
|
||||
qPrintable(options.first()));
|
||||
}
|
||||
|
||||
QTextStream out(f);
|
||||
|
@ -49,9 +49,8 @@ static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options option
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML %s '%s'.", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
}
|
||||
|
||||
xml.readNext();
|
||||
@ -91,9 +90,8 @@ static VariantOrderedMap::value_type mapEntryFromXml(QXmlStreamReader &xml,
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML %s '%s'.", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
}
|
||||
|
||||
return { key, value };
|
||||
@ -135,9 +133,8 @@ static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML %s '%s'.", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(xml.name().toString()));
|
||||
}
|
||||
|
||||
xml.readNext();
|
||||
@ -154,9 +151,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
if (name == "map"_L1)
|
||||
return mapFromXml(xml, options);
|
||||
if (name != "value"_L1) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML key '%s'.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML key '%s'.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
|
||||
}
|
||||
|
||||
QXmlStreamAttributes attrs = xml.attributes();
|
||||
@ -169,9 +165,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
if (xml.isCDATA() || xml.isCharacters() || xml.isEndElement())
|
||||
break;
|
||||
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(name.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML %s '%s'.", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(name.toString()));
|
||||
}
|
||||
|
||||
QStringView text = xml.text();
|
||||
@ -191,9 +186,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
// let's see floating point
|
||||
double d = text.toDouble(&ok);
|
||||
if (!ok) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: could not interpret '%s' as a number.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML: could not interpret '%s' as a number.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
|
||||
}
|
||||
result = d;
|
||||
}
|
||||
@ -207,9 +201,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
} else if (encoding.isEmpty() || encoding == "base64"_L1) {
|
||||
result = QByteArray::fromBase64(data);
|
||||
} else {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
|
||||
}
|
||||
} else if (type == "string"_L1) {
|
||||
result = text.toString();
|
||||
@ -228,9 +221,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
} else if (c == '0') {
|
||||
++n;
|
||||
} else if (!c.isSpace()) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: invalid bit string '%s'.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML: invalid bit string '%s'.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(text.toString()));
|
||||
}
|
||||
}
|
||||
ba.resize(n);
|
||||
@ -248,16 +240,14 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
else
|
||||
id = QMetaType::fromName(type.toLatin1()).id();
|
||||
if (id == QMetaType::UnknownType) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: unknown type '%s'.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML: unknown type '%s'.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
|
||||
}
|
||||
|
||||
result = text.toString();
|
||||
if (!result.convert(QMetaType(id))) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML: could not parse content as type '%s'.\n",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML: could not parse content as type '%s'.",
|
||||
xml.lineNumber(), xml.columnNumber(), qPrintable(type.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,9 +256,8 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
||||
} while (xml.isComment() || xml.isWhitespace());
|
||||
|
||||
if (!xml.isEndElement()) {
|
||||
fprintf(stderr, "%lld:%lld: Invalid XML %s '%s'.\n", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(name.toString()));
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("%lld:%lld: Invalid XML %s '%s'.", xml.lineNumber(), xml.columnNumber(),
|
||||
qPrintable(xml.tokenString()), qPrintable(name.toString()));
|
||||
}
|
||||
|
||||
xml.readNext();
|
||||
@ -388,8 +377,7 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
|
||||
xml.writeAttribute(typeString, QString::fromLatin1(typeName));
|
||||
xml.writeCharacters(copy.toString());
|
||||
} else {
|
||||
fprintf(stderr, "XML: don't know how to serialize type '%s'.\n", typeName);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("XML: don't know how to serialize type '%s'.", typeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -435,10 +423,8 @@ QVariant XmlConverter::loadFile(QIODevice *f, const Converter *&outputConverter)
|
||||
QXmlStreamReader xml(f);
|
||||
xml.readNextStartElement();
|
||||
QVariant v = variantFromXml(xml, outputConverter->outputOptions());
|
||||
if (xml.hasError()) {
|
||||
fprintf(stderr, "XML error: %s", qPrintable(xml.errorString()));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (xml.hasError())
|
||||
qFatal("XML error: %s", qPrintable(xml.errorString()));
|
||||
|
||||
return v;
|
||||
}
|
||||
@ -453,9 +439,8 @@ void XmlConverter::saveFile(QIODevice *f, const QVariant &contents,
|
||||
} else if (s == "compact=yes"_L1) {
|
||||
compact = true;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown option '%s' to XML output. Valid options are:\n%s",
|
||||
qPrintable(s), xmlOptionHelp);
|
||||
exit(EXIT_FAILURE);
|
||||
qFatal("Unknown option '%s' to XML output. Valid options are:\n%s",
|
||||
qPrintable(s), xmlOptionHelp);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user