-
Notifications
You must be signed in to change notification settings - Fork 0
/
biensert.swift
61 lines (57 loc) · 1.89 KB
/
biensert.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// bienAssert.swift
// dbien
// 02DEC2024
// Custom assertions that can be enabled in both debug and release builds.
// Provides bienAssert() which can be configured to halt or continue on failure. bienAssert() will not execute the associated code if assertions are disabled.
// Also provides bienVerify() which always logs and continues on failure. bienVerify() will always execute the associated code, it will only log if assertions are enabled.
import Foundation
import os.log
enum AssertionControl {
#if DEBUG
static var areAssertionsEnabled = true
static var continueOnFailure = true
#else
static var areAssertionsEnabled = false
static var continueOnFailure = false
#endif
}
func bienAssert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: StaticString = #file, line: UInt = #line
) {
// We don't execute the code if assertions are disabled.
if AssertionControl.areAssertionsEnabled && !condition() {
let strFile: String = file.description
let fileName = (strFile as NSString).lastPathComponent
os_log(
"[ASSERT] %{public}@:%d %{public}@: %{public}@",
log: OSLog.default,
type: .debug,
fileName,
UInt32(line),
#function,
message())
if !AssertionControl.continueOnFailure {
precondition(false, message(), file: file, line: line)
}
}
}
func bienVerify(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = String(),
file: String = #file, line: UInt = #line
) {
// We always execute a verify statement, we only log if assertions are enabled.
if !condition() && AssertionControl.areAssertionsEnabled {
let fileName = (file as NSString).lastPathComponent
os_log(
"[VERIFY] %{public}@:%d %{public}@: %{public}@",
log: OSLog.default,
type: .debug,
fileName,
UInt32(line),
#function,
message())
}
}