Files
SwiftLint/Source/SwiftLintFramework/Extensions/QueuedPrint.swift
T
Norio Nomura 94fabdd11d Add fflush(stdout) before fputs(…, stderr)
`Swift.print()` uses buffered stdout by calling `putchar(_:)`.
This avoids mixing strings from stdout and stderr.

fix #432
2016-02-08 10:26:49 +09:00

44 lines
940 B
Swift

//
// QueuedPrint.swift
// SwiftLint
//
// Created by JP Simard on 2015-11-17.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
private let outputQueue: dispatch_queue_t = {
let queue = dispatch_queue_create("io.realm.swiftlint.outputQueue", DISPATCH_QUEUE_SERIAL)
dispatch_set_target_queue(queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))
atexit_b {
dispatch_barrier_sync(queue) {}
}
return queue
}()
/**
A thread-safe version of Swift's standard print().
- parameter object: Object to print.
*/
public func queuedPrint<T>(object: T) {
dispatch_async(outputQueue) {
print(object)
}
}
/**
A thread-safe, newline-terminated version of fputs(..., stderr).
- parameter string: String to print.
*/
public func queuedPrintError(string: String) {
dispatch_async(outputQueue) {
fflush(stdout)
fputs(string + "\n", stderr)
}
}