Skip to content

Commit

Permalink
Added convenient methods to log debug and info
Browse files Browse the repository at this point in the history
  • Loading branch information
AllDmeat committed Dec 19, 2024
1 parent 1743fcc commit 3d66e06
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
136 changes: 136 additions & 0 deletions Sources/BlackBox/BlackBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,142 @@ extension BlackBox {
) {
BlackBox.instance.logEnd(event)
}

// MARK: - Level in function name

/// Logs debug message
/// - Parameters:
/// - message: Message to log
/// - userInfo: Additional info you'd like to see alongside log
/// - serviceInfo: to be deleted
/// - category: Category of log. E.g. View Lifecycle.
/// - parentEvent: Parent log of current log. May be useful for traces.
/// - fileID: The fileID where the logs occurs. Containts module name and filename. The default is the fileID of the function where you call log.
/// - function: The function where the logs occurs. The default is the function name from where you call log.
/// - line: The line where the logs occurs. The default is the line in function from where you call log.
public static func debug(
_ message: StaticString,
userInfo: BBUserInfo? = nil,
serviceInfo: BBServiceInfo? = nil,
category: String? = nil,
parentEvent: GenericEvent? = nil,
fileID: StaticString = #fileID,
function: StaticString = #function,
line: UInt = #line
) {
BlackBox.instance.log(
message,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: .debug,
category: category,
parentEvent: parentEvent,
fileID: fileID,
function: function,
line: line
)
}

/// Logs info message
/// - Parameters:
/// - message: Message to log
/// - userInfo: Additional info you'd like to see alongside log
/// - serviceInfo: to be deleted
/// - category: Category of log. E.g. View Lifecycle.
/// - parentEvent: Parent log of current log. May be useful for traces.
/// - fileID: The fileID where the logs occurs. Containts module name and filename. The default is the fileID of the function where you call log.
/// - function: The function where the logs occurs. The default is the function name from where you call log.
/// - line: The line where the logs occurs. The default is the line in function from where you call log.
public static func info(
_ message: StaticString,
userInfo: BBUserInfo? = nil,
serviceInfo: BBServiceInfo? = nil,
category: String? = nil,
parentEvent: GenericEvent? = nil,
fileID: StaticString = #fileID,
function: StaticString = #function,
line: UInt = #line
) {
BlackBox.instance.log(
message,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: .info,
category: category,
parentEvent: parentEvent,
fileID: fileID,
function: function,
line: line
)
}

/// Logs measurement start with debug level
/// - Parameters:
/// - message: Measurement name
/// - userInfo: Additional info you'd like to see alongside log
/// - serviceInfo: to be deleted
/// - category: Category of log. E.g. View Lifecycle.
/// - parentEvent: Parent log of current log. May be useful for traces.
/// - fileID: The fileID where the logs occurs. Containts module name and filename. The default is the fileID of the function where you call log.
/// - function: The function where the logs occurs. The default is the function name from where you call log.
/// - line: The line where the logs occurs. The default is the line in function from where you call log.
/// - Returns: Started measurement
public static func debugStart(
_ message: StaticString,
userInfo: BBUserInfo? = nil,
serviceInfo: BBServiceInfo? = nil,
category: String? = nil,
parentEvent: GenericEvent? = nil,
fileID: StaticString = #fileID,
function: StaticString = #function,
line: UInt = #line
) -> StartEvent {
BlackBox.instance.logStart(
message,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: .debug,
category: category,
parentEvent: parentEvent,
fileID: fileID,
function: function,
line: line
)
}

/// Logs measurement start with info level
/// - Parameters:
/// - message: Measurement name
/// - userInfo: Additional info you'd like to see alongside log
/// - serviceInfo: to be deleted
/// - category: Category of log. E.g. View Lifecycle.
/// - parentEvent: Parent log of current log. May be useful for traces.
/// - fileID: The fileID where the logs occurs. Containts module name and filename. The default is the fileID of the function where you call log.
/// - function: The function where the logs occurs. The default is the function name from where you call log.
/// - line: The line where the logs occurs. The default is the line in function from where you call log.
/// - Returns: Started measurement
public static func infoStart(
_ message: StaticString,
userInfo: BBUserInfo? = nil,
serviceInfo: BBServiceInfo? = nil,
category: String? = nil,
parentEvent: GenericEvent? = nil,
fileID: StaticString = #fileID,
function: StaticString = #function,
line: UInt = #line
) -> StartEvent {
BlackBox.instance.logStart(
message,
userInfo: userInfo,
serviceInfo: serviceInfo,
level: .info,
category: category,
parentEvent: parentEvent,
fileID: fileID,
function: function,
line: line
)
}
}

// MARK: - Instance
Expand Down
9 changes: 9 additions & 0 deletions Tests/BlackBoxTests/BackwardsCompatibilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ class PublicApiTests: XCTestCase {
function: #function,
line: #line
)

BlackBox.debug("Message")
BlackBox.info("Message")

let startEventDebug = BlackBox.debugStart("Message")
BlackBox.logEnd(startEventDebug)

let startEventInfo = BlackBox.infoStart("Message")
BlackBox.logEnd(startEventInfo)
}

func test_events() {
Expand Down
12 changes: 11 additions & 1 deletion Tests/BlackBoxTests/BlackBoxGenericEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class BlackBoxGenericEventTests: BlackBoxTestCase {
XCTAssertEqual(testableLogger.genericEvent?.level, .warning)
}

func test_levelInFuncNameDebug() {
BlackBox.debug("Test")
XCTAssertEqual(testableLogger.genericEvent?.level, .debug)
}

func test_levelInFuncNameInfo() {
BlackBox.info("Test")
XCTAssertEqual(testableLogger.genericEvent?.level, .info)
}

func test_defaultLevel() {
BlackBox.log("Test")
XCTAssertEqual(testableLogger.genericEvent?.level, .debug)
Expand Down Expand Up @@ -73,7 +83,7 @@ class BlackBoxGenericEventTests: BlackBoxTestCase {

func test_line() {
BlackBox.log("Test")
XCTAssertEqual(testableLogger.genericEvent?.source.line, 75)
XCTAssertEqual(testableLogger.genericEvent?.source.line, 85)
}

func test_durationFormattedIsNil() {
Expand Down
12 changes: 11 additions & 1 deletion Tests/BlackBoxTests/BlackBoxStartEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class BlackBoxStartEventTests: BlackBoxTestCase {
XCTAssertEqual(testableLogger.startEvent?.level, .warning)
}

func test_levelInFuncNameDebug() {
let _ = BlackBox.debugStart("Test")
XCTAssertEqual(testableLogger.startEvent?.level, .debug)
}

func test_levelInFuncNameInfo() {
let _ = BlackBox.infoStart("Test")
XCTAssertEqual(testableLogger.startEvent?.level, .info)
}

func test_defaultLevel() {
let _ = BlackBox.logStart("Test")
XCTAssertEqual(testableLogger.startEvent?.level, .debug)
Expand Down Expand Up @@ -77,7 +87,7 @@ class BlackBoxStartEventTests: BlackBoxTestCase {

func test_line() {
let _ = BlackBox.logStart("Test")
XCTAssertEqual(testableLogger.startEvent?.source.line, 79)
XCTAssertEqual(testableLogger.startEvent?.source.line, 89)
}

func test_durationFormattedIsNil() {
Expand Down

0 comments on commit 3d66e06

Please sign in to comment.