Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
added tic and toc global aliases and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Apr 4, 2016
1 parent 86275fd commit 3ef17f4
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 21 deletions.
16 changes: 12 additions & 4 deletions Evergreen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
87515FCB1CB2ADD500E0120D /* StenographyTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87515FCA1CB2ADD500E0120D /* StenographyTestCase.swift */; };
87515FCD1CB2AEDB00E0120D /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87515FCC1CB2AEDB00E0120D /* LoggingTests.swift */; };
87C376071C23830000053D87 /* Evergreen.h in Headers */ = {isa = PBXBuildFile; fileRef = 87C376031C23830000053D87 /* Evergreen.h */; settings = {ATTRIBUTES = (Public, ); }; };
87C376081C23830000053D87 /* Formatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C376041C23830000053D87 /* Formatter.swift */; };
87C376091C23830000053D87 /* Handler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C376051C23830000053D87 /* Handler.swift */; };
87C3760A1C23830000053D87 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C376061C23830000053D87 /* Logger.swift */; };
87C3760F1C23835100053D87 /* EvergreenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C3760B1C23833700053D87 /* EvergreenTests.swift */; };
87C3760F1C23835100053D87 /* LoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87C3760B1C23833700053D87 /* LoggerTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -25,14 +27,16 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
87515FCA1CB2ADD500E0120D /* StenographyTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StenographyTestCase.swift; path = Tests/StenographyTestCase.swift; sourceTree = SOURCE_ROOT; };
87515FCC1CB2AEDB00E0120D /* LoggingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LoggingTests.swift; path = Tests/LoggingTests.swift; sourceTree = SOURCE_ROOT; };
87539B1F19DB23F00084658D /* Evergreen.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Evergreen.framework; sourceTree = BUILT_PRODUCTS_DIR; };
87539B2A19DB23F00084658D /* EvergreenTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EvergreenTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
87C376011C2382F400053D87 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Sources/Info.plist; sourceTree = SOURCE_ROOT; };
87C376031C23830000053D87 /* Evergreen.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Evergreen.h; path = Sources/Evergreen.h; sourceTree = SOURCE_ROOT; };
87C376041C23830000053D87 /* Formatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Formatter.swift; path = Sources/Formatter.swift; sourceTree = SOURCE_ROOT; };
87C376051C23830000053D87 /* Handler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Handler.swift; path = Sources/Handler.swift; sourceTree = SOURCE_ROOT; };
87C376061C23830000053D87 /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Logger.swift; path = Sources/Logger.swift; sourceTree = SOURCE_ROOT; };
87C3760B1C23833700053D87 /* EvergreenTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EvergreenTests.swift; path = Tests/EvergreenTests.swift; sourceTree = SOURCE_ROOT; };
87C3760B1C23833700053D87 /* LoggerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LoggerTests.swift; path = Tests/LoggerTests.swift; sourceTree = SOURCE_ROOT; };
87C3760C1C23833700053D87 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = Tests/Info.plist; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -96,7 +100,9 @@
87539B2B19DB23F00084658D /* EvergreenTests */ = {
isa = PBXGroup;
children = (
87C3760B1C23833700053D87 /* EvergreenTests.swift */,
87515FCA1CB2ADD500E0120D /* StenographyTestCase.swift */,
87C3760B1C23833700053D87 /* LoggerTests.swift */,
87515FCC1CB2AEDB00E0120D /* LoggingTests.swift */,
87539B2C19DB23F00084658D /* Supporting Files */,
);
name = EvergreenTests;
Expand Down Expand Up @@ -230,7 +236,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
87C3760F1C23835100053D87 /* EvergreenTests.swift in Sources */,
87515FCB1CB2ADD500E0120D /* StenographyTestCase.swift in Sources */,
87C3760F1C23835100053D87 /* LoggerTests.swift in Sources */,
87515FCD1CB2AEDB00E0120D /* LoggingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
99 changes: 99 additions & 0 deletions Evergreen.xcodeproj/xcshareddata/xcschemes/Evergreen.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87539B1E19DB23F00084658D"
BuildableName = "Evergreen.framework"
BlueprintName = "Evergreen"
ReferencedContainer = "container:Evergreen.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87539B2919DB23F00084658D"
BuildableName = "EvergreenTests.xctest"
BlueprintName = "EvergreenTests"
ReferencedContainer = "container:Evergreen.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87539B1E19DB23F00084658D"
BuildableName = "Evergreen.framework"
BlueprintName = "Evergreen"
ReferencedContainer = "container:Evergreen.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87539B1E19DB23F00084658D"
BuildableName = "Evergreen.framework"
BlueprintName = "Evergreen"
ReferencedContainer = "container:Evergreen.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "87539B1E19DB23F00084658D"
BuildableName = "Evergreen.framework"
BlueprintName = "Evergreen"
ReferencedContainer = "container:Evergreen.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
16 changes: 10 additions & 6 deletions Evergreen.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 30 additions & 7 deletions Sources/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,60 @@ public func getLoggerForFile(file: String = #file) -> Logger {
return Logger.loggerForFile(file)
}

/// Logs the event using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func log<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, forLevel logLevel: LogLevel? = nil, function: String = #function, file: String = #file, line: Int = #line)
{
Logger.loggerForFile(file).log(message, error: error, forLevel: logLevel, function: function, file: file, line: line)
}

/// Logs the event with the Verbose log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Verbose log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func verbose<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Verbose, function: function, file: file, line: line)
}
/// Logs the event with the Debug log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Debug log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func debug<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Debug, function: function, file: file, line: line)
}
/// Logs the event with the Info log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Info log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func info<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Info, function: function, file: file, line: line)
}
/// Logs the event with the Warning log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Warning log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func warning<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Warning, function: function, file: file, line: line)
}
/// Logs the event with the Error log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Error log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func error<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Error, function: function, file: file, line: line)
}
/// Logs the event with the Critical log level using a logger that is appropriate for the caller. See `Logger.log:forLevel:` for further documentation.
/// Logs the event with the Critical log level using a logger that is appropriate for the caller.
/// - seealso: `Logger.log(_:, forLevel:)`
public func critical<M>(@autoclosure(escaping) message: () -> M, error: ErrorType? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Evergreen.log(message, error: error, forLevel: .Critical, function: function, file: file, line: line)
}


/// Alias for `Logger.tic` for a logger that is appropriate for the caller.
/// - seealso: `Logger.tic`
public func tic<M>(@autoclosure(escaping) andLog message: () -> M, error: ErrorType? = nil, forLevel logLevel: LogLevel? = nil, timerKey: String? = nil, function: String = #function, file: String = #file, line: Int = #line)
{
Logger.loggerForFile(file).tic(andLog: message, error: error, forLevel: logLevel, timerKey: timerKey, function: function, file: file, line: line)
}

/// Alias for `Logger.toc` for a logger that is appropriate for the caller.
/// - seealso: `Logger.toc`
public func toc<M>(@autoclosure(escaping) andLog message: () -> M, error: ErrorType? = nil, forLevel logLevel: LogLevel? = nil, timerKey: String? = nil, function: String = #function, file: String = #file, line: Int = #line)
{
Logger.loggerForFile(file).toc(andLog: message, error: error, forLevel: logLevel, timerKey: timerKey, function: function, file: file, line: line)
}


/// Reads the logging configuration from environment variables. Every environment variable with prefix 'Evergreen' is evaluated as a logger key path and assigned a log level corresponding to its value. Values should match the log level descriptions, e.g. 'Debug'. Valid environment variable declarations would be e.g. 'Evergreen = Debug' or 'Evergreen.MyLogger = Verbose'.
public func configureFromEnvironment()
{
Expand Down
7 changes: 3 additions & 4 deletions Tests/EvergreenTests.swift → Tests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
//

import XCTest
@testable import Evergreen

@testable
import Evergreen

class EvergreenTests: XCTestCase {
class LoggerTests: StenographyTestCase {

func testDefaultLoggerIdentity() {
let defaultLogger = Evergreen.defaultLogger
Expand Down Expand Up @@ -59,7 +58,7 @@ class EvergreenTests: XCTestCase {
let logLevelFromUppercaseDescription = LogLevel(description: logLevel.description.uppercaseString)
XCTAssert(logLevelFromUppercaseDescription != nil, "Can't initialize log level from description \(logLevel.description.uppercaseString).")
XCTAssert(logLevelFromUppercaseDescription == logLevel, "Log level \(logLevelFromUppercaseDescription!) initialized from description \(logLevel.description) does not match \(logLevel).")
i++
i += 1
}
XCTAssert(logLevels.sort(<) == logLevels, "Log levels initialized by sequencial raw values are not ordered by comparison operator.")
}
Expand Down
63 changes: 63 additions & 0 deletions Tests/LoggingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// LoggingTests.swift
// Evergreen
//
// Created by Nils Fischer on 04.04.16.
// Copyright © 2016 viWiD Webdesign & iOS Development. All rights reserved.
//

import XCTest
@testable import Evergreen


class LoggingTests: StenographyTestCase {


func testLogLevels() {
var expectedRecordCount = 0
let message = "message"
log(message)
expectedRecordCount = records.count // there may be application info logged at the beginning
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
XCTAssert(records[expectedRecordCount - 1].description.containsString(message))
// undefined
log(message)
expectedRecordCount += 1
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
Evergreen.logLevel = .Debug
log(message, forLevel: .Verbose)
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
log(message, forLevel: .Debug)
expectedRecordCount += 1
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
log(message, forLevel: .Info)
expectedRecordCount += 1
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
}

func testErrorLogging() {
enum TestError: ErrorType, CustomStringConvertible, CustomDebugStringConvertible {
case Failure
var debugDescription: String { return "custom_debug_description" }
var description: String { return "custom_description"}
}
let error = TestError.Failure
Evergreen.log("Something failed", error: error)
XCTAssert(records.count == 1)
XCTAssert(records[0].description.containsString("custom_debug_description"), "Logged error does not contain the error's debug description, which should be prioritized over its description.")
}

func testTimeLogging() {
let message = "message"
tic(andLog: message)
var expectedRecordCount = records.count // there may be application info logged at the beginning
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
XCTAssert(records[expectedRecordCount - 1].description.containsString(message))
toc(andLog: message)
expectedRecordCount += 1
XCTAssertEqual(records.count, expectedRecordCount, "The expected record \(expectedRecordCount) count does not match the number of logged records \(records.count): \(records)")
XCTAssert(records[expectedRecordCount - 1].description.containsString(message))
XCTAssert(records[expectedRecordCount - 1].description.containsString("TIME"))
}

}
27 changes: 27 additions & 0 deletions Tests/StenographyTestCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// StenographyHandlerTestCase.swift
// Evergreen
//
// Created by Nils Fischer on 04.04.16.
// Copyright © 2016 viWiD Webdesign & iOS Development. All rights reserved.
//

import Foundation
import XCTest
import Evergreen


class StenographyTestCase: XCTestCase {

var stenographyHandler: StenographyHandler!
var records: [Record] {
return stenographyHandler.records
}

override func setUp() {
super.setUp()
stenographyHandler = StenographyHandler()
Evergreen.defaultLogger.handlers = [ stenographyHandler ]
}

}

0 comments on commit 3ef17f4

Please sign in to comment.