Log file path in libpng errors & warnings properly

This commit is contained in:
Kae 2023-08-01 20:23:48 +10:00
parent 678a461904
commit b4b2219aed
2 changed files with 22 additions and 23 deletions

View File

@ -95,8 +95,8 @@ StringList PackedAssetSource::assetPaths() const {
IODevicePtr PackedAssetSource::open(String const& path) { IODevicePtr PackedAssetSource::open(String const& path) {
struct AssetReader : public IODevice { struct AssetReader : public IODevice {
AssetReader(FilePtr file, StreamOffset offset, StreamOffset size) AssetReader(FilePtr file, String path, StreamOffset offset, StreamOffset size)
: file(file), fileOffset(offset), assetSize(size), assetPos(0) { : file(file), path(path), fileOffset(offset), assetSize(size), assetPos(0) {
setMode(IOMode::Read); setMode(IOMode::Read);
} }
@ -119,6 +119,10 @@ IODevicePtr PackedAssetSource::open(String const& path) {
return assetPos; return assetPos;
} }
String deviceName() const override {
return strf("{}:{}", file->deviceName(), path);
}
bool atEnd() override { bool atEnd() override {
return assetPos >= assetSize; return assetPos >= assetSize;
} }
@ -133,6 +137,7 @@ IODevicePtr PackedAssetSource::open(String const& path) {
} }
FilePtr file; FilePtr file;
String path;
StreamOffset fileOffset; StreamOffset fileOffset;
StreamOffset assetSize; StreamOffset assetSize;
StreamOffset assetPos; StreamOffset assetPos;
@ -142,7 +147,7 @@ IODevicePtr PackedAssetSource::open(String const& path) {
if (!p) if (!p)
throw AssetSourceException::format("Requested file '{}' does not exist in the packed assets file", path); throw AssetSourceException::format("Requested file '{}' does not exist in the packed assets file", path);
return make_shared<AssetReader>(m_packedFile, p->first, p->second); return make_shared<AssetReader>(m_packedFile, path, p->first, p->second);
} }
ByteArray PackedAssetSource::read(String const& path) { ByteArray PackedAssetSource::read(String const& path) {

View File

@ -5,16 +5,19 @@
namespace Star { 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) { 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]; png_byte header[8];
device->readFull((char*)header, sizeof(header)); device->readFull((char*)header, sizeof(header));
@ -26,7 +29,7 @@ Image Image::readPng(IODevicePtr device) {
throw ImageException("Internal libPNG error"); throw ImageException("Internal libPNG error");
// Use custom warning function to suppress cerr warnings // 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); png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) { if (!info_ptr) {
@ -111,15 +114,6 @@ Image Image::readPng(IODevicePtr device) {
} }
tuple<Vec2U, PixelFormat> Image::readPngMetadata(IODevicePtr device) { tuple<Vec2U, PixelFormat> 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]; png_byte header[8];
device->readFull((char*)header, sizeof(header)); device->readFull((char*)header, sizeof(header));
@ -131,7 +125,7 @@ tuple<Vec2U, PixelFormat> Image::readPngMetadata(IODevicePtr device) {
throw ImageException("Internal libPNG error"); throw ImageException("Internal libPNG error");
// Use custom warning function to suppress cerr warnings // 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); png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) { if (!info_ptr) {