Skip to content

Commit

Permalink
Remove swift-log dependency
Browse files Browse the repository at this point in the history
swift-log is only used in the top-level executable targets and it means
we don't need logging abstraction layer for now. Reducing the number of
dependencies makes it easier to integrate in apple/swift build system.
  • Loading branch information
kateinoigakukun committed Nov 10, 2023
1 parent 941f447 commit 4b3d129
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@
"version" : "508.0.1"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.2"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-system", .upToNextMinor(from: "1.1.1")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-format.git", from: "508.0.1"),
Expand All @@ -39,7 +38,6 @@ let package = Package(
"WasmKit",
"WASI",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
.product(name: "SystemPackage", package: "swift-system"),
]
),
Expand All @@ -64,7 +62,6 @@ let package = Package(
dependencies: [
"WasmKit",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Logging", package: "swift-log"),
.product(name: "SystemPackage", package: "swift-system"),
]
),
Expand Down
25 changes: 11 additions & 14 deletions Sources/CLI/Run/Run.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import ArgumentParser
import Foundation
import Logging
import SystemPackage
import WASI
import WasmKit

private let logger = Logger(label: "com.WasmKit.CLI")

struct Run: ParsableCommand {
@Flag
var verbose = false
Expand Down Expand Up @@ -43,20 +40,14 @@ struct Run: ParsableCommand {
var arguments: [String] = []

func run() throws {
LoggingSystem.bootstrap {
var handler = StreamLogHandler.standardOutput(label: $0)
handler.logLevel = verbose ? .info : .warning
return handler
}

logger.info("Started parsing module")
log("Started parsing module", verbose: true)

let module: Module
if verbose {
let (parsedModule, parseTime) = try measure {
try parseWasm(filePath: FilePath(path))
}
logger.info("Finished parsing module: \(parseTime)")
log("Finished parsing module: \(parseTime)", verbose: true)
module = parsedModule
} else {
module = try parseWasm(filePath: FilePath(path))
Expand All @@ -77,7 +68,7 @@ struct Run: ParsableCommand {

let (_, invokeTime) = try measure(execution: invoke)

logger.info("Finished invoking function \"\(path)\": \(invokeTime)")
log("Finished invoking function \"\(path)\": \(invokeTime)", verbose: true)
}

func deriveInterceptor() throws -> (interceptor: GuestTimeProfiler, finalize: () -> Void)? {
Expand Down Expand Up @@ -135,14 +126,14 @@ struct Run: ParsableCommand {
parameters.append(parameter)
}
guard let functionName else {
logger.error("No function specified to run in a given module.")
log("Error: No function specified to run in a given module.")
return nil
}

let runtime = Runtime(interceptor: interceptor)
let moduleInstance = try runtime.instantiate(module: module)
return {
logger.info("Started invoking function \"\(functionName)\" with parameters: \(parameters)")
log("Started invoking function \"\(functionName)\" with parameters: \(parameters)", verbose: true)
let results = try runtime.invoke(moduleInstance, function: functionName, with: parameters)
print(results.description)
}
Expand All @@ -161,4 +152,10 @@ struct Run: ParsableCommand {
let formattedTime = numberFormatter.string(from: nanoseconds)! + " ns"
return (result, formattedTime)
}

@Sendable func log(_ message: String, verbose: Bool = false) {
if !verbose || self.verbose {
fputs(message + "\n", stderr)
}
}
}
22 changes: 10 additions & 12 deletions Sources/Spectest/Spectest.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ArgumentParser
import Foundation
import Logging
import SystemPackage
import WasmKit

Expand All @@ -22,20 +21,19 @@ struct Spectest: AsyncParsableCommand {
var parallel: Bool = true

func run() async throws {
LoggingSystem.bootstrap {
var handler = StreamLogHandler.standardOutput(label: $0)
handler.logLevel = verbose ? .info : .warning
return handler
let printVerbose = self.verbose
@Sendable func log(_ message: String, verbose: Bool = false) {
if !verbose || printVerbose {
fputs(message + "\n", stderr)
}
}

let logger = Logger(label: "com.WasmKit.CLI.Spectest")

let include = self.include.flatMap { $0.split(separator: ",").map(String.init) } ?? []
let exclude = self.exclude.flatMap { $0.split(separator: ",").map(String.init) } ?? []

let testCases: [TestCase]
do {
testCases = try TestCase.load(include: include, exclude: exclude, in: path, logger: logger)
testCases = try TestCase.load(include: include, exclude: exclude, in: path, log: { log($0) })
} catch {
fatalError("failed to load test: \(error)")
}
Expand All @@ -57,13 +55,13 @@ struct Spectest: AsyncParsableCommand {
try testCase.run(spectestModule: hostModule) { testCase, command, result in
switch result {
case let .failed(reason):
logger.warning("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)")
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)")
case let .skipped(reason):
logger.info("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)")
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner) \(reason)", verbose: true)
case .passed:
logger.info("\(testCase.content.sourceFilename):\(command.line): \(result.banner)")
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner)", verbose: true)
default:
logger.warning("\(testCase.content.sourceFilename):\(command.line): \(result.banner)")
log("\(testCase.content.sourceFilename):\(command.line): \(result.banner)")
}
testCaseResults.append(result)
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/Spectest/TestCase.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Logging
import SystemPackage
import WasmKit

Expand Down Expand Up @@ -86,7 +85,7 @@ struct TestCase {
return isDirectory
}

static func load(include: [String], exclude: [String], in path: String, logger: Logger? = nil) throws -> [TestCase] {
static func load(include: [String], exclude: [String], in path: String, log: ((String) -> Void)? = nil) throws -> [TestCase] {
let fileManager = FileManager.default
let filePath = FilePath(path)
let dirPath: String
Expand Down Expand Up @@ -126,7 +125,7 @@ struct TestCase {

var testCases: [TestCase] = []
for filePath in filePaths where try matchesPattern(filePath) {
logger?.info("loading \(filePath)")
log?("loading \(filePath)")
let path = dirPath + "/" + filePath
guard let data = fileManager.contents(atPath: path) else {
assertionFailure("failed to load \(filePath)")
Expand Down

0 comments on commit 4b3d129

Please sign in to comment.