Add better logging for file open and write errors

This commit is contained in:
Jonas Kvinge
2021-08-09 23:32:26 +02:00
parent 7d61d8e646
commit f1d3cadb3b
15 changed files with 120 additions and 41 deletions

View File

@@ -708,7 +708,11 @@ void CollectionWatcher::UpdateCueAssociatedSongs(const QString &file,
// Load new CUE songs
QFile cue_file(matching_cue);
if (!cue_file.exists() || !cue_file.open(QIODevice::ReadOnly)) return;
if (!cue_file.exists()) return;
if (!cue_file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Could not open CUE file" << matching_cue << "for reading:" << cue_file.errorString();
return;
}
const SongList songs = cue_parser_->Load(&cue_file, matching_cue, path, false);
cue_file.close();
@@ -783,7 +787,12 @@ SongList CollectionWatcher::ScanNewFile(const QString &file, const QString &path
if (cues_processed->contains(matching_cue)) return songs;
QFile cue_file(matching_cue);
if (!cue_file.exists() || !cue_file.open(QIODevice::ReadOnly)) return songs;
if (!cue_file.exists()) return songs;
if (!cue_file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Could not open CUE file" << matching_cue << "for reading:" << cue_file.errorString();
return songs;
}
// Ignore FILEs pointing to other media files.
// Also, watch out for incorrect media files.

View File

@@ -388,7 +388,7 @@ void Database::ExecSchemaCommandsFromFile(QSqlDatabase &db, const QString &filen
// Open and read the database schema
QFile schema_file(filename);
if (!schema_file.open(QIODevice::ReadOnly)) {
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
qFatal("Couldn't open schema file %s for reading: %s", filename.toUtf8().constData(), schema_file.errorString().toUtf8().constData());
return;
}
ExecSchemaCommands(db, QString::fromUtf8(schema_file.readAll()), schema_version, in_transaction);

View File

@@ -281,7 +281,7 @@ SongLoader::Result SongLoader::LoadLocalAsync(const QString &filename) {
// It's a local file, so check if it looks like a playlist. Read the first few bytes.
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
errors_ << tr("Could not open file %1").arg(filename);
errors_ << tr("Could not open file %1 for reading: %2").arg(filename).arg(file.errorString());
return Error;
}
QByteArray data(file.read(PlaylistParser::kMagicSize));
@@ -312,6 +312,10 @@ SongLoader::Result SongLoader::LoadLocalAsync(const QString &filename) {
}
return Success;
}
else {
errors_ << tr("Could not open CUE file %1 for reading: %2").arg(matching_cue).arg(cue.errorString());
return Error;
}
}
// Assume it's just a normal file
@@ -364,6 +368,9 @@ void SongLoader::LoadPlaylist(ParserBase *parser, const QString &filename) {
songs_ = parser->Load(&file, filename, QFileInfo(filename).path());
file.close();
}
else {
errors_ << tr("Could not open playlist file %1 for reading: %2").arg(filename).arg(file.errorString());
}
}

View File

@@ -58,7 +58,7 @@ void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
// Load the file
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Unable to open" << filename;
qLog(Error) << "Could not open stylesheet file" << filename << "for reading:" << file.errorString();
return;
}
QTextStream stream(&file);

View File

@@ -939,6 +939,9 @@ QByteArray ReadDataFromFile(const QString &filename) {
data = file.readAll();
file.close();
}
else {
qLog(Error) << "Failed to open file" << filename << "for reading:" << file.errorString();
}
return data;
}

View File

@@ -188,6 +188,9 @@ AlbumCoverImageResult AlbumCoverChoiceController::LoadImageFromFile(Song *song)
result.cover_url = QUrl::fromLocalFile(cover_file);
}
}
else {
qLog(Error) << "Failed to open cover file" << cover_file << "for reading:" << file.errorString();
}
return result;
@@ -252,9 +255,14 @@ void AlbumCoverChoiceController::SaveCoverToFileManual(const Song &song, const A
if (result.is_jpeg() && fileinfo.completeSuffix().compare("jpg", Qt::CaseInsensitive) == 0) {
QFile file(save_filename);
if (file.open(QIODevice::WriteOnly)) {
file.write(result.image_data);
if (file.write(result.image_data) <= 0) {
qLog(Error) << "Failed writing cover to file" << save_filename << file.errorString();
}
file.close();
}
else {
qLog(Error) << "Failed to open cover file" << save_filename << "for writing:" << file.errorString();
}
}
else {
result.image.save(save_filename);
@@ -596,9 +604,17 @@ QUrl AlbumCoverChoiceController::SaveCoverToFileAutomatic(const Song::Source sou
QUrl cover_url;
if (result.is_jpeg()) {
if (file.open(QIODevice::WriteOnly)) {
if (file.write(result.image_data) > 0) cover_url = QUrl::fromLocalFile(filepath);
if (file.write(result.image_data) > 0) {
cover_url = QUrl::fromLocalFile(filepath);
}
else {
qLog(Error) << "Failed to write cover to file" << file.fileName() << file.errorString();
}
file.close();
}
else {
qLog(Error) << "Failed to open cover file" << file.fileName() << "for writing:" << file.errorString();
}
}
else {
if (result.image.save(filepath, "JPG")) cover_url = QUrl::fromLocalFile(filepath);

View File

@@ -431,34 +431,47 @@ AlbumCoverLoader::TryLoadResult AlbumCoverLoader::TryLoadImage(Task *task) {
if (cover_url.isLocalFile()) {
QFile file(cover_url.toLocalFile());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
qLog(Error) << "Failed to open cover file" << cover_url << "for reading" << file.errorString();
}
}
else {
qLog(Error) << "Failed to open album cover file" << cover_url;
qLog(Error) << "Cover file" << cover_url << "does not exist";
}
}
else if (cover_url.scheme().isEmpty()) { // Assume a local file with no scheme.
QFile file(cover_url.path());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
if (file.exists()) {
if (file.open(QIODevice::ReadOnly)) {
QByteArray image_data = file.readAll();
file.close();
QImage image;
if (!image_data.isEmpty() && task->options.get_image_ && image.loadFromData(image_data)) {
return TryLoadResult(false, !image.isNull(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
}
}
else {
return TryLoadResult(false, !image_data.isEmpty(), type, AlbumCoverImageResult(cover_url, QString(), image_data, image.isNull() ? task->options.default_output_image_ : image));
qLog(Error) << "Failed to open cover file" << cover_url << "for reading" << file.errorString();
}
}
else {
qLog(Error) << "Cover file" << cover_url << "does not exist";
}
}
else if (network_->supportedSchemes().contains(cover_url.scheme())) { // Remote URL
qLog(Debug) << "Loading remote cover from" << cover_url;
@@ -615,7 +628,13 @@ void AlbumCoverLoader::SaveEmbeddedCover(const qint64 id, const QString &song_fi
QFile file(cover_filename);
if (file.size() >= 209715200 || !file.open(QIODevice::ReadOnly)) { // Max 200 MB.
if (file.size() >= 209715200) { // Max 200 MB.
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Failed to open cover file" << cover_filename << "for reading:" << file.errorString();
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
@@ -656,7 +675,13 @@ void AlbumCoverLoader::SaveEmbeddedCover(const qint64 id, const QList<QUrl> &url
QFile file(cover_filename);
if (file.size() >= 209715200 || !file.open(QIODevice::ReadOnly)) { // Max 200 MB.
if (file.size() >= 209715200) { // Max 200 MB.
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Failed to open cover file" << cover_filename << "for reading:" << file.errorString();
emit SaveEmbeddedCoverAsyncFinished(id, false, false);
return;
}

View File

@@ -86,11 +86,11 @@ void CurrentAlbumCoverLoader::TempAlbumCoverLoaded(const quint64 id, AlbumCoverL
result.temp_cover_url = QUrl::fromLocalFile(temp_cover_->fileName());
}
else {
qLog(Error) << "Unable to save cover image to" << temp_cover_->fileName();
qLog(Error) << "Failed to save cover image to" << temp_cover_->fileName() << temp_cover_->errorString();
}
}
else {
qLog(Error) << "Unable to open" << temp_cover_->fileName();
qLog(Error) << "Failed to open" << temp_cover_->fileName() << temp_cover_->errorString();
}
}

View File

@@ -138,8 +138,9 @@ int DeviceDatabaseBackend::AddDevice(const Device &device) {
// Create the songs tables for the device
QString filename(":/schema/device-schema.sql");
QFile schema_file(filename);
if (!schema_file.open(QIODevice::ReadOnly))
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
if (!schema_file.open(QIODevice::ReadOnly)) {
qFatal("Couldn't open schema file %s: %s", filename.toUtf8().constData(), schema_file.errorString().toUtf8().constData());
}
QString schema = QString::fromUtf8(schema_file.readAll());
schema.replace("%deviceid", QString::number(id));

View File

@@ -210,6 +210,12 @@ bool GPodDevice::CopyToStorage(const CopyJob &job) {
track->has_artwork = 1;
}
}
else {
qLog(Error) << "Failed to save" << cover_file->fileName() << cover_file->errorString();
}
}
else {
qLog(Error) << "Failed to open" << cover_file->fileName() << cover_file->errorString();
}
}
else if (!job.cover_source_.isEmpty()) {

View File

@@ -225,6 +225,9 @@ int main(int argc, char *argv[]) {
if (file.open(QIODevice::WriteOnly)) {
file.close();
}
else {
qLog(Error) << "Could not open settings file" << file.fileName() << "for writing:" << file.errorString();
}
}
// Set -rw-------

View File

@@ -110,11 +110,16 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, QByteArray *data, Moo
for (const QString &possible_mood_file : MoodFilenames(filename)) {
QFile f(possible_mood_file);
if (f.open(QIODevice::ReadOnly)) {
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
*data = f.readAll();
f.close();
return Loaded;
if (f.exists()) {
if (f.open(QIODevice::ReadOnly)) {
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
*data = f.readAll();
f.close();
return Loaded;
}
else {
qLog(Error) << "Failed to load moodbar data from" << possible_mood_file << f.errorString();
}
}
}
@@ -174,8 +179,9 @@ void MoodbarLoader::RequestFinished(MoodbarPipeline *request, const QUrl &url) {
QIODevice *cache_file = cache_->prepare(metadata);
if (cache_file) {
cache_file->write(request->data());
cache_->insert(cache_file);
if (cache_file->write(request->data()) > 0) {
cache_->insert(cache_file);
}
}
// Save the data alongside the original as well if we're configured to.
@@ -184,18 +190,18 @@ void MoodbarLoader::RequestFinished(MoodbarPipeline *request, const QUrl &url) {
const QString mood_filename(mood_filenames[0]);
QFile mood_file(mood_filename);
if (mood_file.open(QIODevice::WriteOnly)) {
mood_file.write(request->data());
if (mood_file.write(request->data()) <= 0) {
qLog(Error) << "Error writing to mood file" << mood_filename << mood_file.errorString();
}
mood_file.close();
#ifdef Q_OS_WIN32
if (!SetFileAttributes(reinterpret_cast<LPCTSTR>(mood_filename.utf16()), FILE_ATTRIBUTE_HIDDEN)) {
qLog(Warning) << "Error setting hidden attribute for file" << mood_filename;
}
#endif
}
else {
qLog(Warning) << "Error opening mood file for writing" << mood_filename;
qLog(Error) << "Error opening mood file" << mood_filename << "for writing:" << mood_file.errorString();
}
}
}

View File

@@ -122,6 +122,9 @@ ContextSettingsPage::ContextSettingsPage(SettingsDialog *dialog, QWidget *parent
ui_->preview_normal->setText(text);
file.close();
}
else {
qLog(Error) << "Could not open" << file.fileName() << "for reading:" << file.errorString();
}
}

View File

@@ -105,7 +105,7 @@ void MoodbarSettingsPage::InitMoodbarPreviews() {
// Read the sample data
QFile file(":/mood/sample.mood");
if (!file.open(QIODevice::ReadOnly)) {
qLog(Warning) << "Unable to open moodbar sample file";
qLog(Warning) << "Failed to open moodbar sample file" << file.fileName() << "for reading:" << file.errorString();
return;
}
QByteArray file_data = file.readAll();

View File

@@ -224,7 +224,7 @@ void TidalStreamURLRequest::StreamURLReceived() {
file.remove();
}
if (!file.open(QIODevice::WriteOnly)) {
Error(QString("Failed to open file %1 for writing.").arg(url.toLocalFile()), json_obj);
Error(QString("Failed to open file %1 for writing: %2.").arg(url.toLocalFile(), file.errorString()), json_obj);
emit StreamURLFinished(id_, original_url_, original_url_, Song::FileType_Stream, -1, -1, -1, errors_.first());
return;
}