diff --git a/Package.swift b/Package.swift index 692780d..a2dae55 100644 --- a/Package.swift +++ b/Package.swift @@ -8,9 +8,13 @@ let package = Package( products: [ .library(name: "HummingbirdMustache", targets: ["HummingbirdMustache"]), ], - dependencies: [], + dependencies: [ + .package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"), + ], targets: [ - .target(name: "HummingbirdMustache", dependencies: []), + .target(name: "HummingbirdMustache", dependencies: [ + .product(name: "Logging", package: "swift-log"), + ]), .testTarget(name: "HummingbirdMustacheTests", dependencies: ["HummingbirdMustache"]), ] ) diff --git a/Sources/HummingbirdMustache/Library+FileSystem.swift b/Sources/HummingbirdMustache/Library+FileSystem.swift index 11fb28f..07472d1 100644 --- a/Sources/HummingbirdMustache/Library+FileSystem.swift +++ b/Sources/HummingbirdMustache/Library+FileSystem.swift @@ -1,8 +1,9 @@ import Foundation +import Logging extension HBMustacheLibrary { /// Load templates from a folder - func loadTemplates(from directory: String, withExtension extension: String = "mustache") { + func loadTemplates(from directory: String, withExtension extension: String = "mustache", logger: Logger?) { var directory = directory if !directory.hasSuffix("/") { directory += "/" @@ -14,8 +15,11 @@ extension HBMustacheLibrary { guard path.hasSuffix(extWithDot) else { continue } guard let data = fs.contents(atPath: directory + path) else { continue} let string = String(decoding: data, as: Unicode.UTF8.self) - guard let template = try? HBMustacheTemplate(string: string) else { continue } - + guard let template = try? HBMustacheTemplate(string: string) else { + logger?.error("Failed to load \(path)") + continue + } + logger?.debug("Loading \(path)") // drop ".mustache" from path to get name let name = String(path.dropLast(extWithDot.count)) register(template, named: name) diff --git a/Sources/HummingbirdMustache/Library.swift b/Sources/HummingbirdMustache/Library.swift index e3ad730..0c48900 100644 --- a/Sources/HummingbirdMustache/Library.swift +++ b/Sources/HummingbirdMustache/Library.swift @@ -1,3 +1,5 @@ +import Logging + /// Class holding a collection of mustache templates. /// /// Each template can reference the others via a partial using the name the template is registered under @@ -16,9 +18,9 @@ public class HBMustacheLibrary { /// the folder is recursive and templates in subfolders will be registered with the name `subfolder/template`. /// - Parameter directory: Directory to look for mustache templates /// - Parameter extension: Extension of files to look for - public init(directory: String, withExtension extension: String = "mustache") { + public init(directory: String, withExtension extension: String = "mustache", logger: Logger? = nil) { self.templates = [:] - self.loadTemplates(from: directory) + self.loadTemplates(from: directory, withExtension: `extension`, logger: logger) } /// Register template under name