Skip to content

Commit

Permalink
Add swiftlint support and fix formatting
Browse files Browse the repository at this point in the history
With SwiftLint added to the dependency list it can now be run using `swift run swiftlint`.
  • Loading branch information
kdubb committed Jul 9, 2019
1 parent c734ba2 commit 294b0ce
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 122 deletions.
12 changes: 12 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--swiftversion 5.1
--indent 2
--elseposition next-line
--patternlet inline
--stripunusedargs closure-only
--wraparguments after-first
--binarygrouping none
--decimalgrouping none
--hexgrouping none
--octalgrouping none
--header "//\n// {file}\n// OSLogTrace\n//\n// Copyright © {created.year} Outfox, inc.\n//\n//\n// Distributed under the MIT License, See LICENSE for details.\n//"
--enable blankLinesBetweenScopes
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2019 Outfox, inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "SwiftFormat",
"repositoryURL": "https://github.com/nicklockwood/SwiftFormat.git",
"state": {
"branch": null,
"revision": "0708f1eefe0a2ce2aa42fc3ff86125cdb751e63c",
"version": "0.40.10"
}
}
]
},
"version": 1
}
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let package = Package(
targets: ["OSLogTrace"]),
],
dependencies: [
.package(url: "https://github.com/nicklockwood/SwiftFormat.git", .upToNextMinor(from: "0.40.10"))
],
targets: [
.target(
Expand Down
61 changes: 25 additions & 36 deletions Sources/Activity.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
//
// File.swift
//
// Activity.swift
// OSLogTrace
//
// Created by Kevin Wooten on 7/8/19.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
import os.activity


/// An OS activity
///
public struct Activity {

/// Unique ID of the activity
public typealias ID = os_activity_id_t

/// Activity with no current traits.
///
/// When used as a parent activity, it is the equivalent of a passing the `detached` flag.
Expand All @@ -32,9 +33,8 @@ public struct Activity {
/// Options to create activity objects
///
public struct Options: OptionSet {

public let rawValue: UInt32

public init(rawValue: UInt32) {
self.rawValue = rawValue
}
Expand All @@ -47,10 +47,9 @@ public struct Activity {
/// level activity. This allows users to see what activity triggered work without actually relating the activities.
public static let detached = Options(rawValue: OS_ACTIVITY_FLAG_DETACHED.rawValue)

/// Will only create a new activity if none present. If an activity ID is already present, a new object will be
/// returned with the same activity ID underneath.
/// Will only create a new activity if none present. If an activity ID is already present, a new object will be
/// returned with the same activity ID underneath.
public static let ifNonePresent = Options(rawValue: OS_ACTIVITY_FLAG_IF_NONE_PRESENT.rawValue)

}

/// Returns the ID of the this activity.
Expand Down Expand Up @@ -79,8 +78,8 @@ public struct Activity {
public init(_ description: StaticString, parent: Activity = .current, options: Options = [], dso: UnsafeRawPointer? = #dsohandle) {
guard let dso = dso.map({ UnsafeMutableRawPointer(mutating: $0) }) else { fatalError("No DSO handle") }
impl = description.withUTF8Buffer { ptr in
return ptr.withMemoryRebound(to: Int8.self) { cptr in
return _os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
ptr.withMemoryRebound(to: Int8.self) { cptr in
_os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
}
}
}
Expand All @@ -96,8 +95,8 @@ public struct Activity {
public init(_ description: StaticString, parent: Activity = .current, options: Options = [], dso: UnsafeRawPointer? = #dsohandle, block: @convention(block) () -> Void) {
guard let dso = dso.map({ UnsafeMutableRawPointer(mutating: $0) }) else { fatalError("No DSO handle") }
impl = description.withUTF8Buffer { ptr in
return ptr.withMemoryRebound(to: Int8.self) { cptr in
return _os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
ptr.withMemoryRebound(to: Int8.self) { cptr in
_os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
}
}
run(block: block)
Expand All @@ -114,8 +113,8 @@ public struct Activity {
public init(_ description: StaticString, parent: Activity = .current, options: Options = [], dso: UnsafeRawPointer? = #dsohandle, block: () throws -> Void) rethrows {
guard let dso = dso.map({ UnsafeMutableRawPointer(mutating: $0) }) else { fatalError("No DSO handle") }
impl = description.withUTF8Buffer { ptr in
return ptr.withMemoryRebound(to: Int8.self) { cptr in
return _os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
ptr.withMemoryRebound(to: Int8.self) { cptr in
_os_activity_create(dso, cptr.baseAddress!, parent.impl, os_activity_flag_t(rawValue: options.rawValue))
}
}
try run(block: block)
Expand All @@ -126,7 +125,7 @@ public struct Activity {
public init(_ impl: OS_os_activity) {
self.impl = impl
}

/// Executes a block within the context of the activty.
///
/// - Parameters:
Expand All @@ -135,7 +134,7 @@ public struct Activity {
public func run(block: @convention(block) () -> Void) {
os_activity_apply(impl, block)
}

/// Executes a block within the context of the activty, optionally returning a value
/// or throwing errors.
///
Expand All @@ -145,23 +144,23 @@ public struct Activity {
public func run<R>(block: () throws -> R) rethrows -> R {
var result: Result<R, Error>?

os_activity_apply(impl, {
os_activity_apply(impl) {
do {
result = .success(try block())
}
catch {
result = .failure(error)
}
})
}

switch result! {
case .success(let value): return value
case .failure(let error): try { throw error }()
}

fatalError()
}

/// Manual scope manager that allows leaving a previously entered scope at
/// a specific time.
///
Expand All @@ -170,17 +169,15 @@ public struct Activity {
/// managed scope.
///
public struct Scope {

fileprivate var state = os_activity_scope_state_s()

/// Leaves this scope for the owning activity.
///
public mutating func leave() {
os_activity_scope_leave(&state)
}

}

/// Creates and automatically enters a scope for the this
/// activity.
///
Expand Down Expand Up @@ -228,7 +225,7 @@ public struct Activity {
}
}
}

/// Accesses the "unsafe" interface for activities.
///
/// - Important: The unsafe interface is named as such, and
Expand All @@ -240,21 +237,15 @@ public struct Activity {
/// purposes.
///
public static let unsafe: ActivityUnsafe = _ActivityUnsafe()

}


public protocol ActivityUnsafe {

/// Retrieves the current active ID hierarchy
///
func getActiveIDs(max: Int) -> [Activity.ID]

}


fileprivate struct _ActivityUnsafe : ActivityUnsafe {

private struct _ActivityUnsafe: ActivityUnsafe {
@available(macOS, deprecated: 10.12)
@available(iOS, deprecated: 10)
@available(tvOS, deprecated: 10)
Expand All @@ -265,9 +256,7 @@ fileprivate struct _ActivityUnsafe : ActivityUnsafe {
os_activity_get_active(&ids, &idCount)
return Array(ids.prefix(Int(idCount)))
}

}


private let _none = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bitPattern: -2), "_os_activity_none"), to: OS_os_activity.self)
private let _current = unsafeBitCast(dlsym(UnsafeMutableRawPointer(bitPattern: -2), "_os_activity_current"), to: OS_os_activity.self)
35 changes: 16 additions & 19 deletions Sources/LogMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@
// LogMessage.swift
// OSLogTrace
//
// Created by Kevin Wooten on 6/5/19.
// Copyright © 2019 Outfox, Inc. All rights reserved.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import _SwiftOSOverlayShims
import Foundation
import os
import os.log
import _SwiftOSOverlayShims


public enum LogArgumentType : String {
public enum LogArgumentType: String {
case bytes = "iec-bytes"
case bitrate = "iec-bitrate"
case time = "time_t"
case error = "errno"
case `default` = ""
}

public enum LogArgumentView : String {
public enum LogArgumentView: String {
case `public` = "public"
case `private` = "private"
case `default` = ""
}

public enum LogArgumentRadix : String {
public enum LogArgumentRadix: String {
case decimal = "u"
case octal = "o"
case hex = "x"
}

public struct LogMessage: ExpressibleByStringInterpolation {


public struct StringInterpolation: StringInterpolationProtocol {
var format = ""
var arguments: [CVarArg] = []
Expand All @@ -54,31 +53,31 @@ public struct LogMessage: ExpressibleByStringInterpolation {
arguments.append(String(describing: value))
}

public mutating func appendInterpolation<N>(_ value: N?, view: LogArgumentView = .default) where N : NSObjectProtocol & CVarArg {
public mutating func appendInterpolation<N>(_ value: N?, view: LogArgumentView = .default) where N: NSObjectProtocol & CVarArg {
guard let value = value else { format += "<nil>"; return }
format += "%\(spec(view: view))@"
arguments.append(value)
}

public mutating func appendInterpolation<S>(_ value: S?, view: LogArgumentView = .default) where S : StringProtocol & CVarArg {
public mutating func appendInterpolation<S>(_ value: S?, view: LogArgumentView = .default) where S: StringProtocol & CVarArg {
guard let value = value else { format += "<nil>"; return }
format += "%\(spec(view: view))s"
arguments.append(value)
}

public mutating func appendInterpolation<F>(_ value: F?, view: LogArgumentView = .default) where F : BinaryFloatingPoint & CVarArg {
public mutating func appendInterpolation<F>(_ value: F?, view: LogArgumentView = .default) where F: BinaryFloatingPoint & CVarArg {
guard let value = value else { format += "<nil>"; return }
format += "%\(spec(view: view))\(prefix(value))g"
arguments.append(value)
}

public mutating func appendInterpolation<SI>(_ value: SI?, type: LogArgumentType = .default, view: LogArgumentView = .default) where SI : SignedInteger & CVarArg {
public mutating func appendInterpolation<SI>(_ value: SI?, type: LogArgumentType = .default, view: LogArgumentView = .default) where SI: SignedInteger & CVarArg {
guard let value = value else { format += "<nil>"; return }
format += "%\(spec(view: view, type: type))\(prefix(value))d"
arguments.append(value)
}

public mutating func appendInterpolation<UI>(_ value: UI?, radix: LogArgumentRadix, view: LogArgumentView = .default) where UI : UnsignedInteger & CVarArg {
public mutating func appendInterpolation<UI>(_ value: UI?, radix: LogArgumentRadix, view: LogArgumentView = .default) where UI: UnsignedInteger & CVarArg {
guard let value = value else { format += "<nil>"; return }
format += "%\(spec(view: view))\(prefix(value))\(radix.rawValue)"
arguments.append(value)
Expand All @@ -95,7 +94,6 @@ public struct LogMessage: ExpressibleByStringInterpolation {
format += "%s"
arguments.append(value.uuidString)
}

}

let interpolation: StringInterpolation
Expand All @@ -107,7 +105,7 @@ public struct LogMessage: ExpressibleByStringInterpolation {
}

public init(stringInterpolation: StringInterpolation) {
self.interpolation = stringInterpolation
interpolation = stringInterpolation
}

public func log(type: OSLogType, log: OSLog, prefix: String, dso: UnsafeRawPointer = #dsohandle) {
Expand All @@ -118,7 +116,6 @@ public struct LogMessage: ExpressibleByStringInterpolation {
}
}
}

}

private func spec(view: LogArgumentView = .default, type: LogArgumentType = .default) -> String {
Expand All @@ -133,5 +130,5 @@ private func spec(view: LogArgumentView = .default, type: LogArgumentType = .def
let intPrefixes = ["hh", "h", "l", "ll"]
let floatPrefixes = ["", "", "L"]

func prefix<T : BinaryInteger>(_ value: T) -> String { return intPrefixes[MemoryLayout<T>.size/8] }
func prefix<T : BinaryFloatingPoint>(_ value: T) -> String { return floatPrefixes[Int(ceil(Double(MemoryLayout<T>.size)/32.0))] }
func prefix<T: BinaryInteger>(_ value: T) -> String { return intPrefixes[MemoryLayout<T>.size / 8] }
func prefix<T: BinaryFloatingPoint>(_ value: T) -> String { return floatPrefixes[Int(ceil(Double(MemoryLayout<T>.size) / 32.0))] }
Loading

0 comments on commit 294b0ce

Please sign in to comment.