Skip to content

Commit

Permalink
Merge pull request #153 from square/sebastianv1/cleansec-logger
Browse files Browse the repository at this point in the history
Replace print with os_log.
  • Loading branch information
sebastianv1 committed Jun 26, 2020
2 parents 60c5206 + 3633470 commit b76d204
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
5 changes: 3 additions & 2 deletions cleansec/CleansecFramework/Cleansec.swift
Expand Up @@ -8,6 +8,7 @@

import Foundation
import SwiftAstParser
import os.log

/**
Primary public inteface for pipeline steps.
Expand Down Expand Up @@ -40,14 +41,14 @@ public struct Cleansec {
do {
try process.run()
} catch {
print("Plugin process failed: \(error)")
os_log("Plugin process failed: %@", type: .debug, String(describing: error))
return nil
}
let data = output.fileHandleForReading.readDataToEndOfFile()
do {
return try JSONDecoder().decode(ModuleRepresentation.self, from: data)
} catch {
print("Failed to parse plugin output to `ModuleRepresentation`. Make sure you using JSONDecoder over a `ModuleRepresentation` instance.")
os_log("Failed to parse plugin output to `ModuleRepresentation`. Make sure you using JSONDecoder over a `ModuleRepresentation` instance.", type: .info)
return nil
}
}
Expand Down
5 changes: 3 additions & 2 deletions cleansec/CleansecFramework/Linking/Linker.swift
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import os.log

/**
The Linker is repsonsible for linking `ReferenceProvider` instances to their respective `DanglingProvider`.
Expand All @@ -28,7 +29,7 @@ public struct Linker {
let danglingProviders = components.flatMap { $0.danglingProviders } + modules.flatMap { $0.danglingProviders }
let danglingProvidersByReference = danglingProviders.reduce(into: [String:DanglingProvider]()) { (dict, provider) in
if let _ = dict[provider.reference] {
print("Warning: Duplicate dangling provider reference: \(provider.reference)")
os_log("Warning: Duplicate dangling provider reference: %@", type: .debug, provider.reference)
}
dict[provider.reference] = provider
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public struct Linker {
private static func link(referenceProviders: [ReferenceProvider], danglingProvidersByReference: [String:DanglingProvider]) -> [StandardProvider] {
return referenceProviders.compactMap { referenceProvider -> StandardProvider? in
guard let linked = danglingProvidersByReference[referenceProvider.reference] else {
print("Warning: Failed to find pointee for reference provider: \(referenceProvider)")
os_log("Warning: Failed to find pointee for reference provider: %@", type: .debug, referenceProvider.type)
return nil
}
return StandardProvider(
Expand Down
2 changes: 1 addition & 1 deletion cleansec/CleansecFramework/Resolver/ResolutionError.swift
Expand Up @@ -27,7 +27,7 @@ extension ResolutionError: CustomStringConvertible {
var errorDescription = errorPrefix(debug: parent?.debugData)
errorDescription += "Missing Provider: '\(provider)'\n"
if let p = parent {
errorDescription += "Depended upon by: '\(p.type)'\n'\(p.type)' --> '\(provider)'\n"
errorDescription += "Depended upon by: '\(p.type)'"
}

return errorDescription
Expand Down
11 changes: 6 additions & 5 deletions cleansec/CleansecFramework/Visitors/BindingsVisitor.swift
Expand Up @@ -8,6 +8,7 @@

import Foundation
import SwiftAstParser
import os.log

struct BindingsResult {
let standardProviders: [StandardProvider]
Expand Down Expand Up @@ -39,7 +40,7 @@ struct BindingsVisitor: SyntaxVisitor {
var bindingVisitor = ProviderVisitor(type: type)
bindingVisitor.walk(node)
guard let providerResult = bindingVisitor.finalize() else {
print("Found binding expression, but failed to create any semblance of a provider. \(node.raw)")
os_log("Found binding expression, but failed to create any semblance of a provider. %@", type: .debug, node.raw)
return
}

Expand All @@ -50,7 +51,7 @@ struct BindingsVisitor: SyntaxVisitor {
var danglingVisitor = DanglingProviderVisitor(type: type)
danglingVisitor.walk(node)
guard let foundReference = danglingVisitor.finalize() else {
print("Unknown dangling reference provider type \(node.raw)")
os_log("Unknown dangling reference provider type %@", type: .debug, node.raw)
return
}
danglingProviderBuilder = danglingProviderBuilder.setReference(foundReference)
Expand All @@ -60,7 +61,7 @@ struct BindingsVisitor: SyntaxVisitor {
referenceVisitor.walkChildren(node)
switch referenceVisitor.finalize() {
case .unknown:
print("Failed to parse reference node: \(node)")
os_log("Failed to parse reference node: %@", type: .debug, node.raw)
return
case .dependencies(let dependencies):
referenceProviderBuilder = referenceProviderBuilder.setDependencies(dependencies: dependencies)
Expand All @@ -82,13 +83,13 @@ struct BindingsVisitor: SyntaxVisitor {
if let moduleName = node.raw.firstCapture(#"substitution\sM\s->\s(.*)\)\)]"#) {
includedModules.append(moduleName)
} else {
print("Found included module, but could not parse its name. \(node)")
os_log("Found included module, but could not parse its name. %@", type: .debug, node.raw)
}
} else if node.raw.contains(BindingAPI.installComponent.rawValue) {
if let subcomponentName = node.raw.firstCapture(#"substitution\sC\s->\s(.*)\)\)]"#) {
installedSubcomponents.append(subcomponentName)
} else {
print("Found installed subcomponent, but could not parse its name.")
os_log("Found installed subcomponent, but could not parse its name. %@", type: .debug, node.raw)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions cleansec/CleansecFramework/Visitors/FileVisitor.swift
Expand Up @@ -8,6 +8,7 @@

import Foundation
import SwiftAstParser
import os.log

/**
Primary parsing object that will walk a source file node and extract any cleanse objects.
Expand Down Expand Up @@ -49,7 +50,7 @@ struct FileVisitor: SyntaxVisitor {
break
case .module:
guard let moduleName = inheritableNode.raw.firstCapture(#"interface type='(.*)\.Type'"#) else {
print("Found module. Unable to parse interface type: \(inheritableNode.raw)")
os_log("Found module. Unable to parse interface type: %@", type: .debug, inheritableNode.raw)
return
}
var bindingsVisitor = BindingsVisitor()
Expand All @@ -59,7 +60,7 @@ struct FileVisitor: SyntaxVisitor {
)
case .component:
guard let componentName = inheritableNode.raw.firstCapture(#"interface type='(.*)\.Type'"#) else {
print("Found component. Unable to parse interface type: \(inheritableNode.raw)")
os_log("Found component. Unable to parse interface type: %@", type: .debug, inheritableNode.raw)
return
}
let isRoot = inheritableNode.inherits?.matches("^(Cleanse.)?RootComponent") ?? false
Expand All @@ -69,7 +70,7 @@ struct FileVisitor: SyntaxVisitor {
componentRootVisitor.walk(inheritableNode)
let (seed, rootProvider) = componentRootVisitor.finalize()
guard let root = rootProvider else {
print("Unable to discern root provider for component \(componentName). Not creating component for: \(inheritableNode.raw)")
os_log("Unable to discern root provider for component %@. Not creating component for: %@", type: .debug, componentName, inheritableNode.raw)
return
}
switch root {
Expand Down
Expand Up @@ -8,6 +8,7 @@

import Foundation
import SwiftAstParser
import os.log

public enum ProviderResult {
case provider(StandardProvider)
Expand Down Expand Up @@ -80,13 +81,13 @@ public struct ProviderVisitor: SyntaxVisitor {
if let tag = node.type.allCaptures(#"(\w+(?:\.\w+)*)(?=>)"#).last {
bindingTypeBuilder = bindingTypeBuilder.setTaggedBinding(tag: tag)
} else {
print("Found tagged provider, but failed to parse Tag")
os_log("Found tagged provider, but failed to parse Tag", type: .debug)
}
case .scopedProvider:
if let scope = node.type.allCaptures(#"(\w+)(?=>)"#).last {
bindingTypeBuilder = bindingTypeBuilder.setScopedBinding(scope: scope)
} else {
print("Found scoped provider, but failed to parse scope")
os_log("Found scoped provider, but failed to parse scope", type: .debug)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions cleansec/SwiftAstParser/Regex.swift
@@ -1,4 +1,5 @@
import Foundation
import os.log

public struct RegexCapture {
public let groups: [String]
Expand All @@ -14,7 +15,7 @@ public extension String {
}
return true
} catch {
print("Failed to formulate regex: \(error)")
os_log("Failed to formulate regex: %@", type: .debug, String(describing: error))
return false
}
}
Expand All @@ -35,7 +36,7 @@ public extension String {
return RegexCapture(groups: captures)
}
} catch {
print("Failed to formulate regex: \(error)")
os_log("Failed to formulate regex: %@", type: .debug, String(describing: error))
return []
}
}
Expand Down

0 comments on commit b76d204

Please sign in to comment.