Skip to content

Commit

Permalink
Support Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
youming-lin committed Aug 31, 2017
1 parent b80f590 commit 2afeb4c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Packages
build
HeliumLogger.xcodeproj
*.DS_Store
Package.resolved
9 changes: 0 additions & 9 deletions .swift-sample

This file was deleted.

44 changes: 44 additions & 0 deletions Package@swift-4.0.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

/**
* Copyright IBM Corporation 2016, 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import PackageDescription

let package = Package(
name: "HeliumLogger",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "HeliumLogger",
targets: ["HeliumLogger"]
)
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMinor(from: "1.7.0"))
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "HeliumLogger",
dependencies: ["LoggerAPI"]),
.testTarget(
name: "HeliumLoggerTests",
dependencies: ["HeliumLogger"])
]
)
23 changes: 11 additions & 12 deletions Sources/HeliumLogger/HeliumLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,13 @@ public class HeliumLogger {
return formatter
}

#if os(Linux)
#if swift(>=3.1)
typealias RegularExpressionType = NSRegularExpression
#else
typealias RegularExpressionType = RegularExpression
#endif
#else
typealias RegularExpressionType = NSRegularExpression
#if os(Linux) && !swift(>=3.1)
typealias NSRegularExpression = RegularExpression
#endif

private static var tokenRegex: RegularExpressionType? = {
private static var tokenRegex: NSRegularExpression? = {
do {
return try RegularExpressionType(pattern: "\\(%\\w+\\)", options: [])
return try NSRegularExpression(pattern: "\\(%\\w+\\)", options: [])
} catch {
print("Error creating HeliumLogger tokenRegex: \(error)")
return nil
Expand Down Expand Up @@ -230,7 +224,7 @@ extension HeliumLogger : Logger {
/// - Parameter fileName: The file of the source code of the function invoking the
/// logger API.
public func log(_ type: LoggerMessageType, msg: String,
functionName: String, lineNum: Int, fileName: String ) {
functionName: String, lineNum: Int, fileName: String ) {

guard isLogging(type) else {
return
Expand Down Expand Up @@ -306,7 +300,12 @@ extension HeliumLogger : Logger {
guard let range = path.range(of: "/", options: .backwards) else {
return path
}
return path.substring(from: range.upperBound)

#if swift(>=3.2)
return String(path[range.upperBound...])
#else
return path.substring(from: range.upperBound)
#endif
}

/// A function that will indicate if a message with a specified type (`LoggerMessageType`)
Expand Down
42 changes: 22 additions & 20 deletions Tests/HeliumLoggerTests/TestStreamLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class TestStreamLogger : XCTestCase {

static var allTests : [(String, (TestStreamLogger) -> () throws -> Void)] {
return [
("testInfo", testInfo),
("testWarning", testWarning),
("testError", testError),
("testLevel", testLevel),
("testEntry", testEntry),
("testExit", testExit),
("testIsLogging", testIsLogging),
("testUse", testUse)
("testInfo", testInfo),
("testWarning", testWarning),
("testError", testError),
("testLevel", testLevel),
("testEntry", testEntry),
("testExit", testExit),
("testIsLogging", testIsLogging),
("testUse", testUse)
]
}

Expand All @@ -48,14 +48,8 @@ class TestStreamLogger : XCTestCase {
}
}

#if os(Linux)
#if swift(>=3.1)
typealias RegularExpressionType = NSRegularExpression
#else
typealias RegularExpressionType = RegularExpression
#endif
#else
typealias RegularExpressionType = NSRegularExpression
#if os(Linux) && !swift(>=3.1)
typealias NSRegularExpression = RegularExpression
#endif

struct LogMessage {
Expand All @@ -70,9 +64,9 @@ class TestStreamLogger : XCTestCase {

private func getLogMessage(_ logString: String) -> LogMessage {
do {
let regularExpression = try RegularExpressionType(pattern: "\\[.*\\]\\s\\[(.*)\\]\\s\\[.*\\]\\s(.*)", options: [])
let regularExpression = try NSRegularExpression(pattern: "\\[.*\\]\\s\\[(.*)\\]\\s\\[.*\\]\\s(.*)", options: [])
let matches = regularExpression.matches(in: logString, options: [],
range: NSRange(location:0, length: logString.characters.count))
range: NSRange(location:0, length: logString.characters.count))

guard let messageMatch = matches.first else {
return LogMessage()
Expand All @@ -81,15 +75,23 @@ class TestStreamLogger : XCTestCase {
let typeStringRange = messageMatch.range(at: 1)
let typeString = NSString(string: logString).substring(with: typeStringRange)
#else
let typeStringRange = messageMatch.rangeAt(1)
#if swift(>=3.2)
let typeStringRange = messageMatch.range(at: 1)
#else
let typeStringRange = messageMatch.rangeAt(1)
#endif
let typeString = (logString as NSString).substring(with: typeStringRange)
#endif

#if os(Linux)
let messageStringRange = messageMatch.range(at: 2)
let messageString = NSString(string: logString).substring(with: messageStringRange)
#else
let messageStringRange = messageMatch.rangeAt(2)
#if swift(>=3.2)
let messageStringRange = messageMatch.range(at: 2)
#else
let messageStringRange = messageMatch.rangeAt(2)
#endif
let messageString = (logString as NSString).substring(with: messageStringRange)
#endif

Expand Down
5 changes: 4 additions & 1 deletion Tests/HeliumLoggerTests/VerifyLinuxTestCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
**/

#if os(OSX)
// Test disabled on Swift 4 for now due to
// https://bugs.swift.org/browse/SR-5684

#if os(OSX) && !swift(>=3.2)
import XCTest

class VerifyLinuxTestCount: XCTestCase {
Expand Down
36 changes: 26 additions & 10 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,35 @@ import Glibc
srand(UInt32(time(nil)))

// http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift
extension MutableCollection where Indices.Iterator.Element == Index {
mutating func shuffle() {
let c = count
guard c > 1 else { return }
#if swift(>=3.2)
extension MutableCollection {
mutating func shuffle() {
let c = count
guard c > 1 else { return }

for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swap(&self[firstUnshuffled], &self[i])
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swapAt(firstUnshuffled, i)
}
}
}
}
#else
extension MutableCollection where Indices.Iterator.Element == Index {
mutating func shuffle() {
let c = count
guard c > 1 else { return }

for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
guard d != 0 else { continue }
let i = index(firstUnshuffled, offsetBy: d)
swap(&self[firstUnshuffled], &self[i])
}
}
}
#endif

extension Sequence {
func shuffled() -> [Iterator.Element] {
Expand Down

0 comments on commit 2afeb4c

Please sign in to comment.