Improve file logging
This commit is contained in:
parent
80d3c08a93
commit
04272c620e
@ -175,11 +175,11 @@ final class FileSystem {
|
|||||||
|
|
||||||
private func loadImage(atPath path: String) -> (image: NSImage, changed: Bool)? {
|
private func loadImage(atPath path: String) -> (image: NSImage, changed: Bool)? {
|
||||||
guard let (data, changed) = getData(atPath: path) else {
|
guard let (data, changed) = getData(atPath: path) else {
|
||||||
print("Failed to load image data \(path)")
|
log.add(error: "Failed to load file", source: path)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
guard let image = NSImage(data: data) else {
|
guard let image = NSImage(data: data) else {
|
||||||
print("Failed to read image \(path)")
|
log.add(error: "Failed to read image", source: path)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return (image, changed)
|
return (image, changed)
|
||||||
@ -193,35 +193,34 @@ final class FileSystem {
|
|||||||
|
|
||||||
let standardSize = NSSize(width: CGFloat(width), height: height ?? CGFloat(width) / 16 * 9)
|
let standardSize = NSSize(width: CGFloat(width), height: height ?? CGFloat(width) / 16 * 9)
|
||||||
guard sourceUrl.exists else {
|
guard sourceUrl.exists else {
|
||||||
log.add(error: "Missing image \(source) with size (\(width),\(desiredHeight ?? -1)",
|
log.add(error: "Missing file with size (\(width),\(desiredHeight ?? -1))",
|
||||||
source: "Image Processor")
|
source: source)
|
||||||
return standardSize
|
return standardSize
|
||||||
}
|
}
|
||||||
guard let imageSize = loadImage(atPath: image.source)?.image.size else {
|
guard let imageSize = loadImage(atPath: image.source)?.image.size else {
|
||||||
log.add(error: "Unreadable image \(source) with size (\(width),\(desiredHeight ?? -1)",
|
log.add(error: "Unreadable image with size (\(width),\(desiredHeight ?? -1))",
|
||||||
source: "Image Processor")
|
source: source)
|
||||||
return standardSize
|
return standardSize
|
||||||
}
|
}
|
||||||
let scaledSize = imageSize.scaledDown(to: CGFloat(width))
|
let scaledSize = imageSize.scaledDown(to: CGFloat(width))
|
||||||
|
|
||||||
guard let existing = imageTasks[destination] else {
|
guard let existing = imageTasks[destination] else {
|
||||||
//print("Image(\(image.width),\(image.desiredHeight ?? -1)) requested for \(destination)")
|
|
||||||
imageTasks[destination] = image
|
imageTasks[destination] = image
|
||||||
return scaledSize
|
return scaledSize
|
||||||
}
|
}
|
||||||
guard existing.source == source else {
|
guard existing.source == source else {
|
||||||
log.add(error: "Multiple sources (\(existing.source),\(source)) for image \(destination)",
|
log.add(error: "Multiple sources (\(existing.source),\(source))",
|
||||||
source: "Image Processor")
|
source: destination)
|
||||||
return scaledSize
|
return scaledSize
|
||||||
}
|
}
|
||||||
guard existing.hasSimilarRatio(as: image) else {
|
guard existing.hasSimilarRatio(as: image) else {
|
||||||
log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!)) for image \(destination)",
|
log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!))",
|
||||||
source: "Image Processor")
|
source: destination)
|
||||||
return scaledSize
|
return scaledSize
|
||||||
}
|
}
|
||||||
if image.width > existing.width {
|
if image.width > existing.width {
|
||||||
log.add(info: "Increasing size of image \(destination) from \(existing.width) to \(width)",
|
log.add(info: "Increasing size from \(existing.width) to \(width)",
|
||||||
source: "Image Processor")
|
source: destination)
|
||||||
imageTasks[destination] = image
|
imageTasks[destination] = image
|
||||||
}
|
}
|
||||||
return scaledSize
|
return scaledSize
|
||||||
@ -236,7 +235,7 @@ final class FileSystem {
|
|||||||
|
|
||||||
private func createImageIfNeeded(_ image: ImageOutput, for destination: String) {
|
private func createImageIfNeeded(_ image: ImageOutput, for destination: String) {
|
||||||
guard let (sourceImageData, sourceImageChanged) = getData(atPath: image.source) else {
|
guard let (sourceImageData, sourceImageChanged) = getData(atPath: image.source) else {
|
||||||
log.add(error: "Failed to open image \(image.source)", source: "Image Processor")
|
log.add(error: "Failed to open file", source: image.source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,18 +249,18 @@ final class FileSystem {
|
|||||||
// Ensure that image file is supported
|
// Ensure that image file is supported
|
||||||
let ext = destinationUrl.pathExtension.lowercased()
|
let ext = destinationUrl.pathExtension.lowercased()
|
||||||
guard MediaType.isProcessableImage(ext) else {
|
guard MediaType.isProcessableImage(ext) else {
|
||||||
log.add(info: "Copying image \(image.source)", source: "Image Processor")
|
log.add(info: "Copying image", source: image.source)
|
||||||
do {
|
do {
|
||||||
let sourceUrl = input.appendingPathComponent(image.source)
|
let sourceUrl = input.appendingPathComponent(image.source)
|
||||||
try destinationUrl.ensureParentFolderExistence()
|
try destinationUrl.ensureParentFolderExistence()
|
||||||
try sourceUrl.copy(to: destinationUrl)
|
try sourceUrl.copy(to: destinationUrl)
|
||||||
} catch {
|
} catch {
|
||||||
log.add(error: "Failed to copy image \(image.source) to \(destination)", source: "Image Processor")
|
log.add(error: "Failed to copy image", source: destination)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard let sourceImage = NSImage(data: sourceImageData) else {
|
guard let sourceImage = NSImage(data: sourceImageData) else {
|
||||||
print("Failed to read image \(image.source)")
|
log.add(error: "Failed to read file", source: image.source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,36 +272,36 @@ final class FileSystem {
|
|||||||
let scaledSize = scaledImage.size
|
let scaledSize = scaledImage.size
|
||||||
|
|
||||||
if abs(scaledImage.size.width - desiredWidth) > 2 {
|
if abs(scaledImage.size.width - desiredWidth) > 2 {
|
||||||
print("[WARN] Image \(destination) scaled incorrectly (wanted width \(desiredWidth), is \(scaledSize.width))")
|
log.add(warning: "Desired width \(desiredWidth), is \(scaledSize.width))", source: destination)
|
||||||
}
|
}
|
||||||
if abs(destinationSize.height - scaledImage.size.height) > 2 {
|
if abs(destinationSize.height - scaledImage.size.height) > 2 {
|
||||||
print("[WARN] Image \(destination) scaled incorrectly (wanted height \(destinationSize.height), is \(scaledSize.height))")
|
log.add(warning: "Desired height \(destinationSize.height), is \(scaledSize.height))", source: destination)
|
||||||
}
|
}
|
||||||
if let desiredHeight = desiredHeight {
|
if let desiredHeight = desiredHeight {
|
||||||
let desiredRatio = desiredHeight / desiredWidth
|
let desiredRatio = desiredHeight / desiredWidth
|
||||||
let adjustedDesiredHeight = scaledSize.width * desiredRatio
|
let adjustedDesiredHeight = scaledSize.width * desiredRatio
|
||||||
if abs(adjustedDesiredHeight - scaledSize.height) > 5 {
|
if abs(adjustedDesiredHeight - scaledSize.height) > 5 {
|
||||||
log.add(warning: "Image \(image.source): Desired height \(adjustedDesiredHeight) (actually \(desiredHeight)), got \(scaledSize.height) after reduction", source: "Image Processor")
|
log.add(warning: "Desired height \(desiredHeight), got \(scaledSize.height)", source: destination)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if scaledSize.width > desiredWidth {
|
if scaledSize.width > desiredWidth {
|
||||||
print("[WARN] Image \(image.source) is too large (expected width \(desiredWidth), got \(scaledSize.width)")
|
log.add(warning:" Desired width \(desiredWidth), got \(scaledSize.width)", source: destination)
|
||||||
}
|
}
|
||||||
|
|
||||||
let destinationExtension = destinationUrl.pathExtension.lowercased()
|
let destinationExtension = destinationUrl.pathExtension.lowercased()
|
||||||
guard let type = MediaType.supportedImage(destinationExtension) else {
|
guard let type = MediaType.supportedImage(destinationExtension) else {
|
||||||
log.add(error: "No image type for \(destination) with extension \(destinationExtension)",
|
log.add(error: "No image type for extension \(destinationExtension)",
|
||||||
source: "Image Processor")
|
source: destination)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard let tiff = scaledImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) else {
|
guard let tiff = scaledImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) else {
|
||||||
log.add(error: "Failed to get data for image \(image.source)", source: "Image Processor")
|
log.add(error: "Failed to get data", source: image.source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let data = tiffData.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else {
|
guard let data = tiffData.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else {
|
||||||
log.add(error: "Failed to get data for image \(image.source)", source: "Image Processor")
|
log.add(error: "Failed to get data", source: image.source)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
|
Loading…
Reference in New Issue
Block a user