From 04272c620e737b2b514a683b05563f645b924bed Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 26 Aug 2022 22:28:34 +0200 Subject: [PATCH] Improve file logging --- WebsiteGenerator/Files/FileSystem.swift | 49 ++++++++++++------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/WebsiteGenerator/Files/FileSystem.swift b/WebsiteGenerator/Files/FileSystem.swift index 7dd47b0..74b1005 100644 --- a/WebsiteGenerator/Files/FileSystem.swift +++ b/WebsiteGenerator/Files/FileSystem.swift @@ -175,11 +175,11 @@ final class FileSystem { private func loadImage(atPath path: String) -> (image: NSImage, changed: Bool)? { 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 } 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 (image, changed) @@ -193,35 +193,34 @@ final class FileSystem { let standardSize = NSSize(width: CGFloat(width), height: height ?? CGFloat(width) / 16 * 9) guard sourceUrl.exists else { - log.add(error: "Missing image \(source) with size (\(width),\(desiredHeight ?? -1)", - source: "Image Processor") + log.add(error: "Missing file with size (\(width),\(desiredHeight ?? -1))", + source: source) return standardSize } guard let imageSize = loadImage(atPath: image.source)?.image.size else { - log.add(error: "Unreadable image \(source) with size (\(width),\(desiredHeight ?? -1)", - source: "Image Processor") + log.add(error: "Unreadable image with size (\(width),\(desiredHeight ?? -1))", + source: source) return standardSize } let scaledSize = imageSize.scaledDown(to: CGFloat(width)) guard let existing = imageTasks[destination] else { - //print("Image(\(image.width),\(image.desiredHeight ?? -1)) requested for \(destination)") imageTasks[destination] = image return scaledSize } guard existing.source == source else { - log.add(error: "Multiple sources (\(existing.source),\(source)) for image \(destination)", - source: "Image Processor") + log.add(error: "Multiple sources (\(existing.source),\(source))", + source: destination) return scaledSize } guard existing.hasSimilarRatio(as: image) else { - log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!)) for image \(destination)", - source: "Image Processor") + log.add(error: "Multiple ratios (\(existing.ratio!),\(image.ratio!))", + source: destination) return scaledSize } if image.width > existing.width { - log.add(info: "Increasing size of image \(destination) from \(existing.width) to \(width)", - source: "Image Processor") + log.add(info: "Increasing size from \(existing.width) to \(width)", + source: destination) imageTasks[destination] = image } return scaledSize @@ -236,7 +235,7 @@ final class FileSystem { private func createImageIfNeeded(_ image: ImageOutput, for destination: String) { 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 } @@ -250,18 +249,18 @@ final class FileSystem { // Ensure that image file is supported let ext = destinationUrl.pathExtension.lowercased() guard MediaType.isProcessableImage(ext) else { - log.add(info: "Copying image \(image.source)", source: "Image Processor") + log.add(info: "Copying image", source: image.source) do { let sourceUrl = input.appendingPathComponent(image.source) try destinationUrl.ensureParentFolderExistence() try sourceUrl.copy(to: destinationUrl) } 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 } 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 } @@ -273,36 +272,36 @@ final class FileSystem { let scaledSize = scaledImage.size 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 { - 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 { let desiredRatio = desiredHeight / desiredWidth let adjustedDesiredHeight = scaledSize.width * desiredRatio 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 } } 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() guard let type = MediaType.supportedImage(destinationExtension) else { - log.add(error: "No image type for \(destination) with extension \(destinationExtension)", - source: "Image Processor") + log.add(error: "No image type for extension \(destinationExtension)", + source: destination) return } 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 } 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 } do {