Skip to content
Permalink
Browse files

Feature/jn/swift 5.0 (#8)

Swift 5.0 support
  • Loading branch information...
jnewc committed Apr 5, 2019
1 parent 2ed4946 commit 6c5296fe52eec0793e052603a8c9165dedb0f19d
@@ -1,12 +1,12 @@
before_install:
- pod repo update > /dev/null
- pod install --repo-update
script:
- xcodebuild -destination 'platform=iOS Simulator,name=iPhone 8' -workspace Cosmic.xcworkspace -scheme Cosmic -enableCodeCoverage YES build test
- xcodebuild -destination 'platform=iOS Simulator,name=iPhone X' -workspace Cosmic.xcworkspace -scheme Cosmic -enableCodeCoverage YES build test
after_script:
- bash <(curl -s https://codecov.io/bash) -J Cosmic


language: objective-c
osx_image: xcode9.3
osx_image: xcode10.2
xcode_workspace: Cosmic.xcworkspace # path to your xcodeproj folder
xcode_scheme: Cosmic

Large diffs are not rendered by default.

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -1,8 +1,22 @@
// swift-tools-version:4.2
import PackageDescription

let package = Package(
name: "Cosmic",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/BlueSocket", majorVersion: 1, minor: 0)
]
.package(url: "https://github.com/IBM-Swift/BlueSocket", from: "1.0.0")
],
targets: [
.target(
name: "Cosmic",
dependencies: [
"Socket"
]
),
.testTarget(
name: "CosmicTests",
dependencies: [ "Cosmic" ]
)
],
swiftLanguageVersions: [.v4_2]
)
@@ -1,15 +1,15 @@
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
platform :ios, '11.0'
use_frameworks!

target 'Cosmic' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for Cosmic
pod 'BlueSocket', "~> 1.0.44"

target 'CosmicTests' do
inherit! :search_paths
#inherit! :search_paths
end

end
@@ -1,16 +1,16 @@
PODS:
- BlueSocket (1.0.44)
- BlueSocket (1.0.45)

DEPENDENCIES:
- BlueSocket (~> 1.0.44)

SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
https://github.com/cocoapods/specs.git:
- BlueSocket

SPEC CHECKSUMS:
BlueSocket: 4812c58c34e6d667bc43736ec9c855cf408b98f0
BlueSocket: b59f969c51b723f1297aceecf12ad5113a6db70d

PODFILE CHECKSUM: 78cbfe148ed6042de5afd0f186d49c4ed9b622b8
PODFILE CHECKSUM: ce8b11e2add1c592d85230d8b7529654f72c375f

COCOAPODS: 1.5.0
COCOAPODS: 1.6.1
@@ -20,7 +20,7 @@ public protocol LogReporter {

public extension LogReporter {

public func createLogger<T: Logger>(with type: T.Type) -> T {
func createLogger<T: Logger>(with type: T.Type) -> T {

if !LoggerCache.keys.contains(className) {
LoggerCache[className] = T.init()
@@ -30,7 +30,7 @@ public extension LogReporter {

}

public var logger: DefaultLoggerType {
var logger: DefaultLoggerType {
return self.createLogger(with: DefaultLoggerType.self)
}

@@ -89,7 +89,7 @@ public extension Logger {
/// domain-specific and diagnostic information.
///
/// - Parameter messages: The messages to log
public func debug(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
func debug(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
guard enabled(.debug) else { return }
let metadata = LogMetadata(file: file, line: line, function: function)
log(format(message, metadata), logLevel: .debug, metadata: metadata)
@@ -106,7 +106,7 @@ public extension Logger {
/// Info logs should describe all high-level interactions.
///
/// - Parameter messages: The info messages to log
public func info(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
func info(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
guard enabled(.info) else { return }
let metadata = LogMetadata(file: file, line: line, function: function)
log(format(message, metadata), logLevel: .info, metadata: metadata)
@@ -120,7 +120,7 @@ public extension Logger {
/// occur such as the absence of configuration or validation errors
///
/// - Parameter messages: The warning messages to log
public func warn(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
func warn(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
guard enabled(.warn) else { return }
let metadata = LogMetadata(file: file, line: line, function: function)
log(format(message, metadata), logLevel: .warn, metadata: metadata)
@@ -138,7 +138,7 @@ public extension Logger {
/// has occurred (for example, by reaching illegal if/else branches)
///
/// - Parameter messages: The error messages to log
public func error(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
func error(_ message: String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) {
guard enabled(.error) else { return }
let metadata = LogMetadata(file: file, line: line, function: function)
log(format(message, metadata), logLevel: .error, metadata: metadata)
@@ -15,55 +15,59 @@ typealias JSONFormatterDictionary = [String: Any]
typealias JSONFormatterConverterCompletion = (String) -> JSONFormatterDictionary

protocol JSONFormatterConverter {

/// <#Description#>
var converter: JSONFormatterConverterCompletion { get }

func toJSON(message: String, metadata: LogMetadata) -> String?

}


/// Converts a string-based textual message into string-based JSON
class JSONFormatter: LogFormatter, JSONFormatterConverter {

let converter: JSONFormatterConverterCompletion

var options: JSONSerialization.WritingOptions = []


var options: JSONSerialization.WritingOptions = {
if #available(macOS 10.13, *) {
return [ .sortedKeys ]
}
return []
}()
init(converter: @escaping JSONFormatterConverterCompletion) {
self.converter = converter
}

func toJSON(message: String, metadata: LogMetadata) -> String? {
let dict = self.converter(message)
return JSONSerialization.string(withJSONObject: dict, options: options)
}

func format(message: String, metadata: LogMetadata) -> String {
guard let string = toJSON(message: message, metadata: metadata) else { return message }
return string
}


}

// MARK: NewLineBatchFormatter
public class JSONBatchFormatter: BatchFormatter {

private let jsonFormatter: JSONFormatter

init(converter: @escaping JSONFormatterConverterCompletion) {
jsonFormatter = JSONFormatter(converter: converter)
}

public func format(message: String, metadata: LogMetadata) -> String {
return jsonFormatter.format(message: message, metadata: metadata)
}

public func format(batch: [(String, LogMetadata)]) -> String {
return batch.map({ format(message: $0.0, metadata: $0.1) }).joined(separator: "\n")
}

}
@@ -48,7 +48,7 @@ class FormatterTests: XCTestCase {

let formattedMessage = formatter.format(message: "Testing", metadata: LogMetadata())

XCTAssertMatches(input: formattedMessage, pattern: "<\\d{2}>\\d \(dateTimeExpr) Cosmic Unknown - - - Testing")
XCTAssertMatches(input: formattedMessage, pattern: "<\\d{2}>\\d \(dateTimeExpr) Cosmic com.apple.dt.xctest.tool - - - Testing")
}

func testJSONFormatter() {
@@ -0,0 +1,18 @@
//
// Extensions.swift
// Cosmic
//
// Created by Jack Newcombe on 05/02/2019.
// Copyright © 2019 Jack Newcombe. All rights reserved.
//
import XCTest
import Foundation

extension XCTestCase {

var isRunningInXcode: Bool {
return ProcessInfo.processInfo.environment.keys.contains("SIMULATOR_UDID")
}

}
@@ -47,7 +47,7 @@ class LogMetadataTests: XCTestCase {
XCTAssertEqual(entry.message, "A log")

XCTAssertEqual(metadata.file.description.components(separatedBy: "/").last, "LogMetadataTests.swift")
XCTAssertEqual(metadata.function.description, "createLog")
XCTAssertEqual(metadata.function.description, "createLog(_:)")
XCTAssertEqual(metadata.line, 2)
}

@@ -10,58 +10,59 @@ import XCTest
@testable import Cosmic

class LogReporterTests: XCTestCase, DefaultLogReporter {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testDefaultLogger() {

let loggerWithClass = self.createLogger(with: PrintLogger.self)

let a = String(describing: type(of: self.logger))
let b = String(describing: type(of: loggerWithClass))

XCTAssertEqual(a, b)

}

func testLoggerClassName() {
let name = self.className

XCTAssertEqual(name, "LogReporterTests")
}

func testLoggerInSubclass() {
let object = B()

let loggerA = object.getLoggerA()
let loggerB = object.getLoggerB()

XCTAssert(loggerA === loggerB)
}
}


class A: LogReporter {

typealias DefaultLoggerType = PrintLogger

func getLoggerA() -> Logger {
return logger
}

}

class B: A {

func getLoggerB() -> Logger {
return logger
}

}
@@ -67,7 +67,7 @@ class PrintLoggerTests: XCTestCase {

logs.forEach {
printLogger.debug($0)
XCTAssertEqual(stream.array[logs.index(of: $0)!], $0)
XCTAssertEqual(stream.array[logs.firstIndex(of: $0)!], $0)
}

}
@@ -78,7 +78,7 @@ class PrintLoggerTests: XCTestCase {

logs.forEach {
printLogger.info($0)
XCTAssertEqual(stream.array[logs.index(of: $0)!], $0)
XCTAssertEqual(stream.array[logs.firstIndex(of: $0)!], $0)
}

}
@@ -89,7 +89,7 @@ class PrintLoggerTests: XCTestCase {

logs.forEach {
printLogger.warn($0)
XCTAssertEqual(stream.array[logs.index(of: $0)!], $0)
XCTAssertEqual(stream.array[logs.firstIndex(of: $0)!], $0)
}

}
@@ -100,7 +100,7 @@ class PrintLoggerTests: XCTestCase {

logs.forEach {
printLogger.error($0)
XCTAssertEqual(stream.array[logs.index(of: $0)!], $0)
XCTAssertEqual(stream.array[logs.firstIndex(of: $0)!], $0)
}

}
@@ -113,7 +113,7 @@ class PrintLoggerTests: XCTestCase {

logs.forEach {
printLogger.info($0)
XCTAssertEqual(stream.array[logs.index(of: $0)!], $0)
XCTAssertEqual(stream.array[logs.firstIndex(of: $0)!], $0)
}
}

0 comments on commit 6c5296f

Please sign in to comment.
You can’t perform that action at this time.