Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate DispatchQueue and add an argument #9

Merged
merged 3 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
make-tatget: [xcode-build, carthage-build-workaround, pod-lib-lint]
xcode: [12.2]
xcode: [12.3]
fail-fast: false
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app
Expand All @@ -30,7 +30,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
xcode: [12.2]
xcode: [12.3]
fail-fast: false
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app
Expand All @@ -46,7 +46,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
xcode: [12.2]
xcode: [12.3]
fail-fast: false
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app
Expand All @@ -59,7 +59,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
swift-version: [5.3.1]
swift-version: [5.3.2]
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand All @@ -70,7 +70,7 @@ jobs:
runs-on: macos-latest
strategy:
matrix:
xcode: [12.2]
xcode: [12.3]
fail-fast: false
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [x.y.z](https://github.com/sushichop/Puppy/releases/tag/x.y.z) (yyyy-mm-dd)

- Instantiate `DispatchQueue` and add an argument named of `asynchronous`. [#9](https://github.com/sushichop/Puppy/pull/9)

## [0.1.2](https://github.com/sushichop/Puppy/releases/tag/0.1.2) (2020-12-05)

- Support new API about FileHandle. [#7](https://github.com/sushichop/Puppy/pull/7)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "danger", "~> 8.1"
gem "danger", "~> 8.2"
gem "danger-swiftlint"
22 changes: 12 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ GEM
colored2 (3.1.2)
cork (0.3.0)
colored2 (~> 3.1)
danger (8.1.0)
danger (8.2.2)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
colored2 (~> 3.1)
Expand All @@ -24,16 +24,18 @@ GEM
no_proxy_fix
octokit (~> 4.7)
terminal-table (~> 1)
danger-swiftlint (0.24.4)
danger-swiftlint (0.24.5)
danger
rake (> 10)
thor (~> 0.19)
faraday (1.1.0)
faraday (1.3.0)
faraday-net_http (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords
faraday-http-cache (2.2.0)
faraday (>= 0.8)
git (1.7.0)
faraday-net_http (1.0.1)
git (1.8.1)
rchardet (~> 1.8)
kramdown (2.3.0)
rexml
Expand All @@ -42,15 +44,15 @@ GEM
multipart-post (2.1.1)
nap (1.1.0)
no_proxy_fix (0.1.2)
octokit (4.18.0)
octokit (4.20.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
open4 (1.3.4)
public_suffix (4.0.6)
rake (13.0.1)
rake (13.0.3)
rchardet (1.8.0)
rexml (3.2.4)
ruby2_keywords (0.0.2)
ruby2_keywords (0.0.4)
sawyer (0.8.2)
addressable (>= 2.3.5)
faraday (> 0.8, < 2.0)
Expand All @@ -60,11 +62,11 @@ GEM
unicode-display_width (1.7.0)

PLATFORMS
ruby
x86_64-darwin-19

DEPENDENCIES
danger (~> 8.1)
danger (~> 8.2)
danger-swiftlint

BUNDLED WITH
2.1.4
2.2.6
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DEFAULT_GOAL := help
HELP_INDENT := 28

SWIFT_VERSION := 5.3.1
SWIFT_VERSION := 5.3.2

.PHONY: help
help:
Expand Down
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"pins": [
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1200;
LastUpgradeCheck = 1220;
LastUpgradeCheck = 1230;
TargetAttributes = {
C78B9219252DCFBD0026B9B1 = {
CreatedOnToolsVersion = 12.0.1;
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/xcshareddata/xcschemes/Puppy.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Puppy.xcodeproj/xcshareddata/xcschemes/SwiftLint.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1230"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Furthermore, it has file log rotation feature and you can also customize the log

### Basic Usage

Logging to mutliple transports(e.g. console and file).
Logging to mutliple transports(e.g. console and file). It is recommended that the first argument of each logger be a unique reverse-order FQDN since it is also used internally for a `DispatchQueue`'s label.

```Swift
import Puppy
Expand Down
12 changes: 6 additions & 6 deletions Sources/Puppy/BaseLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
public protocol Loggerable {

var label: String { get }
var queue: DispatchQueue? { get }

func log(_ level: LogLevel, string: String)
}
Expand All @@ -29,7 +30,7 @@ public class BaseLogger: Loggerable {
formattedMessage = message
}

if let queue = queue, asynchronous {
if let queue = queue {
queue.async {
self.log(level, string: formattedMessage)
}
Expand All @@ -38,13 +39,12 @@ public class BaseLogger: Loggerable {
}
}

public var asynchronous: Bool = true
public let label: String
public let queue: DispatchQueue?

public var label: String
public var queue: DispatchQueue? { return nil }

public init(_ label: String) {
public init(_ label: String, asynchronous: Bool = true) {
self.label = label
self.queue = asynchronous ? DispatchQueue(label: label) : nil
}

public func log(_ level: LogLevel, string: String) {
Expand Down
6 changes: 0 additions & 6 deletions Sources/Puppy/ConsoleLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import Foundation

public class ConsoleLogger: BaseLogger {

public override var queue: DispatchQueue! {
return Self.consoleLoggerQueue
}

private static let consoleLoggerQueue = DispatchQueue(label: "net.sushichop.puppy.consolelogger")

public override func log(_ level: LogLevel, string: String) {
print(string)
}
Expand Down
10 changes: 2 additions & 8 deletions Sources/Puppy/FileLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import Foundation

public class FileLogger: BaseLogger {

public override var queue: DispatchQueue! {
return Self.fileLoggerQueue
}

private static let fileLoggerQueue = DispatchQueue(label: "net.sushichop.puppy.filelogger")

public var flushmode: FlushMode = .always

private var fileHandle: FileHandle!
Expand Down Expand Up @@ -43,7 +37,7 @@ public class FileLogger: BaseLogger {

public func delete(_ url: URL) throws {
do {
try queue.sync {
try queue!.sync {
try FileManager.default.removeItem(at: url)
}
} catch {
Expand All @@ -52,7 +46,7 @@ public class FileLogger: BaseLogger {
}

public func flush() {
queue.sync {
queue!.sync {
fileHandle?.synchronizeFile()
}
}
Expand Down
8 changes: 1 addition & 7 deletions Sources/Puppy/FileRotationLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import Foundation

public class FileRotationLogger: BaseLogger {

public override var queue: DispatchQueue! {
return Self.fileRotationLoggerQueue
}

private static let fileRotationLoggerQueue = DispatchQueue(label: "net.sushichop.puppy.filerotationlogger")

public typealias ByteCount = UInt64
public var maxFileSize: ByteCount = 10 * 1024 * 1024
public var maxArchivedFilesCount: UInt8 = 5
Expand Down Expand Up @@ -48,7 +42,7 @@ public class FileRotationLogger: BaseLogger {

public func delete(_ url: URL) throws {
do {
try queue.sync {
try queue!.sync {
try FileManager.default.removeItem(at: url)
}
} catch {
Expand Down
1 change: 0 additions & 1 deletion Sources/Puppy/LogColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Foundation
/// [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code)
/// [256 COLORS - CHEAT SHEET](https://jonasjacek.github.io/colors/)
public enum LogColor {

case black
case red
case green
Expand Down
10 changes: 2 additions & 8 deletions Sources/Puppy/OSLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ import os.log

public class OSLogger: BaseLogger {

public override var queue: DispatchQueue! {
return Self.osLoggerQueue
}

private static let osLoggerQueue = DispatchQueue(label: "net.sushichop.puppy.oslogger")

private let osLog: OSLog

public init(_ label: String, category: String = "Puppy") {
public init(_ label: String, asynchronous: Bool = true, category: String = "Puppy") {
self.osLog = OSLog(subsystem: label, category: category)
super.init(label)
super.init(label, asynchronous: asynchronous)
}

public override func log(_ level: LogLevel, string: String) {
Expand Down
1 change: 1 addition & 0 deletions Sources/Puppy/Puppy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
typealias Log = Puppy

public class Puppy {

public static var useDebug = false
public static let `default` = Puppy()

Expand Down
1 change: 1 addition & 0 deletions Sources/Puppy/PuppyLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
import Logging

public struct PuppyLogHandler: LogHandler {

public var logLevel: Logger.Level = .info
public var metadata: Logger.Metadata

Expand Down
6 changes: 0 additions & 6 deletions Sources/Puppy/SystemLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import CPuppy

public class SystemLogger: BaseLogger {

public override var queue: DispatchQueue! {
return Self.systemLoggerQueue
}

private static let systemLoggerQueue = DispatchQueue(label: "net.sushichop.puppy.systemlogger")

public override func log(_ level: LogLevel, string: String) {
string.withCString {
let priority = logPriority(level)
Expand Down
15 changes: 9 additions & 6 deletions Tests/PuppyTests/BaseLoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ class BaseLoggerTests: XCTestCase {
log.debug("DEBUG message using BaseLogger")
baseLogger.logLevel = .notice
log.info("🐛 INFO message but THIS MESSAGE MUST NOT DISPLAYED using BaseLogger")
log.notice("NOTICE message using ConsoleLogger")
log.notice("NOTICE message using BaseLogger")
baseLogger.enabled = false
log.notice("🐛 NOTICE message but THIS MESSAGE MUST NOT DISPLAYED using BaseLogger")

log.remove(baseLogger)
}

func testMockLoggerSynchronous() throws {
let mockLogger = MockLogger("com.example.yourapp.mocklogger.sync")
mockLogger.asynchronous = false
let mockLogger = MockLogger("com.example.yourapp.mocklogger.sync", asynchronous: false)
XCTAssertNil(mockLogger.queue)

let log = Puppy()
log.add(mockLogger, withLevel: .debug)

log.trace("TRACE message")
log.verbose("VERBOSE message")
log.debug("DEBUG message")
log.info("INFO message")

mockLogger.enabled = false
log.notice("NOTICE message")
log.warning("WARNING message")
Expand All @@ -55,13 +56,15 @@ class BaseLoggerTests: XCTestCase {

func testMockLoggerAsynchronous() throws {
let mockLogger = MockLogger("com.example.yourapp.mocklogger.async")
XCTAssertNotNil(mockLogger.queue)

let log = Puppy()
log.add(mockLogger, withLevel: .debug)
log.debug("DEBUG message")
log.warning("WARNING message")

let exp = XCTestExpectation(description: "MockLogger Asynchronous")
mockLogger.queue.async {
mockLogger.queue!.async {
XCTAssertEqual(mockLogger.invokedLogCount, 2)
XCTAssertEqual(mockLogger.invokedLogLevels, [.debug, .warning])
XCTAssertEqual(mockLogger.invokedLogStrings, ["DEBUG message", "WARNING message"])
Expand All @@ -84,7 +87,7 @@ class BaseLoggerTests: XCTestCase {
log.error("ERROR message", tag: "error-tag")

let exp = XCTestExpectation(description: "MockLogger LogFormatter")
mockLogger.queue.async {
mockLogger.queue!.async {
XCTAssertEqual(mockLogger.invokedLogCount, 2)
XCTAssertEqual(mockLogger.invokedLogLevels, [.warning, .error])
XCTAssertEqual(mockLogger.invokedLogStrings, [
Expand Down
Loading