From b4b2219aedc56e3e6958d18a522f697a92799829 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 1 Aug 2023 20:23:48 +1000 Subject: [PATCH] Log file path in libpng errors & warnings properly --- source/base/StarPackedAssetSource.cpp | 11 ++++++--- source/core/StarImage.cpp | 34 +++++++++++---------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/source/base/StarPackedAssetSource.cpp b/source/base/StarPackedAssetSource.cpp index b335c2f..59e64cc 100644 --- a/source/base/StarPackedAssetSource.cpp +++ b/source/base/StarPackedAssetSource.cpp @@ -95,8 +95,8 @@ StringList PackedAssetSource::assetPaths() const { IODevicePtr PackedAssetSource::open(String const& path) { struct AssetReader : public IODevice { - AssetReader(FilePtr file, StreamOffset offset, StreamOffset size) - : file(file), fileOffset(offset), assetSize(size), assetPos(0) { + AssetReader(FilePtr file, String path, StreamOffset offset, StreamOffset size) + : file(file), path(path), fileOffset(offset), assetSize(size), assetPos(0) { setMode(IOMode::Read); } @@ -119,6 +119,10 @@ IODevicePtr PackedAssetSource::open(String const& path) { return assetPos; } + String deviceName() const override { + return strf("{}:{}", file->deviceName(), path); + } + bool atEnd() override { return assetPos >= assetSize; } @@ -133,6 +137,7 @@ IODevicePtr PackedAssetSource::open(String const& path) { } FilePtr file; + String path; StreamOffset fileOffset; StreamOffset assetSize; StreamOffset assetPos; @@ -142,7 +147,7 @@ IODevicePtr PackedAssetSource::open(String const& path) { if (!p) throw AssetSourceException::format("Requested file '{}' does not exist in the packed assets file", path); - return make_shared(m_packedFile, p->first, p->second); + return make_shared(m_packedFile, path, p->first, p->second); } ByteArray PackedAssetSource::read(String const& path) { diff --git a/source/core/StarImage.cpp b/source/core/StarImage.cpp index 1494503..33c37a4 100644 --- a/source/core/StarImage.cpp +++ b/source/core/StarImage.cpp @@ -5,16 +5,19 @@ namespace Star { +void logPngError(png_structp png_ptr, png_const_charp c) { + Logger::debug("PNG error in file: '{}', {}", ((IODevice*)png_get_error_ptr(png_ptr))->deviceName(), c); +}; + +void logPngWarning(png_structp png_ptr, png_const_charp c) { + Logger::debug("PNG warning in file: '{}', {}", ((IODevice*)png_get_error_ptr(png_ptr))->deviceName(), c); +}; + +void readPngData(png_structp pngPtr, png_bytep data, png_size_t length) { + ((IODevice*)png_get_io_ptr(pngPtr))->readFull((char*)data, length); +}; + Image Image::readPng(IODevicePtr device) { - auto logPngError = [](png_structp png_ptr, png_const_charp c) { - Logger::debug("PNG error in file: '{}', {}", (char*)png_get_error_ptr(png_ptr), c); - }; - - auto readPngData = [](png_structp pngPtr, png_bytep data, png_size_t length) { - IODevice* device = (IODevice*)png_get_io_ptr(pngPtr); - device->readFull((char*)data, length); - }; - png_byte header[8]; device->readFull((char*)header, sizeof(header)); @@ -26,7 +29,7 @@ Image Image::readPng(IODevicePtr device) { throw ImageException("Internal libPNG error"); // Use custom warning function to suppress cerr warnings - png_set_error_fn(png_ptr, (png_voidp)device->deviceName().utf8Ptr(), logPngError, logPngError); + png_set_error_fn(png_ptr, (png_voidp)device.get(), logPngError, logPngWarning); png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { @@ -111,15 +114,6 @@ Image Image::readPng(IODevicePtr device) { } tuple Image::readPngMetadata(IODevicePtr device) { - auto logPngError = [](png_structp png_ptr, png_const_charp c) { - Logger::debug("PNG error in file: '{}', {}", (char*)png_get_error_ptr(png_ptr), c); - }; - - auto readPngData = [](png_structp pngPtr, png_bytep data, png_size_t length) { - IODevice* device = (IODevice*)png_get_io_ptr(pngPtr); - device->readFull((char*)data, length); - }; - png_byte header[8]; device->readFull((char*)header, sizeof(header)); @@ -131,7 +125,7 @@ tuple Image::readPngMetadata(IODevicePtr device) { throw ImageException("Internal libPNG error"); // Use custom warning function to suppress cerr warnings - png_set_error_fn(png_ptr, (png_voidp)device->deviceName().utf8Ptr(), logPngError, logPngError); + png_set_error_fn(png_ptr, (png_voidp)device.get(), logPngError, logPngWarning); png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) {