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

OutputStream Changes #78

Merged
merged 4 commits into from
Jan 26, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ import Foundation
///
internal protocol OutputStream {

/// The current write position or number of bytes
/// written to the stream.
///
var position: UInt64 { get }

/// Write the byte block to the output.
///
/// - Parameter bytes: An array of UInt8 byes to output to the stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ internal enum Standard {

/// Standard output (stdout)
///
static var out: FileOutputStream { // FIXME: change to OutputStream
static var out: OutputStream {
return FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false)
}

/// Standard error (stderr)
///
static var error: FileOutputStream { // FIXME: change to OutputStream
static var error: OutputStream {
return FileOutputStream(fileDescriptor: STDERR_FILENO, closeFd: false)
}
}
2 changes: 1 addition & 1 deletion Sources/TraceLog/Writers & Formatters/FileWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public class FileWriter: OutputStreamWriter {

/// Does the file need to be rotated?
if self.file.stream.position + UInt64(bytes.count) >= file.config.maxSize {
self.file = rotate(file: self.file, fallbackStream: Standard.out, dateFormatter: self.fileNameDateFormatter)
self.file = rotate(file: self.file, fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false), dateFormatter: self.fileNameDateFormatter)
}

/// Write message to log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,4 @@ class StandardStreamTests: XCTestCase {
func testErrorIsOutputStream() {
XCTAssertTrue((Standard.error as Any) is TraceLog.OutputStream)
}

func testOutPosition() {
XCTAssertEqual(Standard.out.position, 0)
}

func testErrorPosition() {
XCTAssertEqual(Standard.error.position, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class FileWriterInternalsTests: XCTestCase {
}

func testOpen() {
let input = open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: Standard.out)
let input = open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))

XCTAssertNotEqual(input, Standard.out)
XCTAssertNotEqual(input, FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))

close(fileStream: input)
}
Expand All @@ -64,28 +64,28 @@ class FileWriterInternalsTests: XCTestCase {
}

/// It should not fallback to the fallback stream:
XCTAssertNotEqual(open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: Standard.out), Standard.out)
XCTAssertNotEqual(open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false)), FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))
XCTAssertTrue(fileManager.fileExists(atPath: "\(testDirectory)/test.log"))
}

func testOpenFailure() throws {

/// It should be the fallback file stream:
XCTAssertEqual(open(fileURL: URL(fileURLWithPath: "file://dev/null"), fallbackStream: Standard.out), Standard.out)
XCTAssertEqual(open(fileURL: URL(fileURLWithPath: "file://dev/null"), fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false)), FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))
}

///
/// Test rotation of a file.
///
func testRotate() throws {
let stream = open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: Standard.out)
let stream = open(fileURL: URL(fileURLWithPath: "\(testDirectory)/test.log"), fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))

let logFile: FileWriter.LogFile = (stream: stream, config: FileWriter.FileConfiguration(name: "test.log", directory: testDirectory))

///
/// Note: This tests ensures it does not fallback to the fallback stream:r.
///
XCTAssertNotEqual(rotate(file: logFile, fallbackStream: Standard.out, dateFormatter: FileWriter.Default.fileNameDateFormatter).stream, Standard.out)
XCTAssertNotEqual(rotate(file: logFile, fallbackStream: FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false), dateFormatter: FileWriter.Default.fileNameDateFormatter).stream, FileOutputStream(fileDescriptor: STDOUT_FILENO, closeFd: false))

/// And of course we make sure the file was created.
XCTAssertTrue(try archiveExists(fileName: "test", fileExt: "log", directory: testDirectory))
Expand Down
20 changes: 18 additions & 2 deletions Tests/TraceLogTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ extension BlockTimerTests {
]
}

extension ConcurrencyModeTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ConcurrencyModeTests = [
("testAsyncOptionEquals", testAsyncOptionEquals),
("testWriterConcurrencyModeProxyAsync", testWriterConcurrencyModeProxyAsync),
("testWriterConcurrencyModeProxyDirect", testWriterConcurrencyModeProxyDirect),
("testWriterConcurrencyModeProxySync", testWriterConcurrencyModeProxySync),
("testWriterModeAsync", testWriterModeAsync),
("testWriterModeDirect", testWriterModeDirect),
("testWriterModeSync", testWriterModeSync),
]
}

extension ConfigurationTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -58,6 +73,8 @@ extension ConsoleWriterTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ConsoleWriterTests = [
("testFailedFormat", testFailedFormat),
("testFailedWrite", testFailedWrite),
("testLogError", testLogError),
("testLogInfo", testLogInfo),
("testLogTrace1", testLogTrace1),
Expand Down Expand Up @@ -265,9 +282,7 @@ extension StandardStreamTests {
// to regenerate.
static let __allTests__StandardStreamTests = [
("testErrorIsOutputStream", testErrorIsOutputStream),
("testErrorPosition", testErrorPosition),
("testOutIsOutputStream", testOutIsOutputStream),
("testOutPosition", testOutPosition),
]
}

Expand Down Expand Up @@ -467,6 +482,7 @@ public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(AsyncWriterProxyTests.__allTests__AsyncWriterProxyTests),
testCase(BlockTimerTests.__allTests__BlockTimerTests),
testCase(ConcurrencyModeTests.__allTests__ConcurrencyModeTests),
testCase(ConfigurationTests.__allTests__ConfigurationTests),
testCase(ConsoleWriterTests.__allTests__ConsoleWriterTests),
testCase(EnvironmentTests.__allTests__EnvironmentTests),
Expand Down