Skip to content

Instantly share code, notes, and snippets.

@pxpgraphics
Last active December 25, 2015 19:14
Show Gist options
  • Save pxpgraphics/4035e77d373b81ba5035 to your computer and use it in GitHub Desktop.
Save pxpgraphics/4035e77d373b81ba5035 to your computer and use it in GitHub Desktop.
// MARK: PXPAssert
/**
Traditional C-style assert with an optional message.
Use this function for internal sanity checks that are active
during testing but do not impact performance of shipping code.
* In playgrounds and -Onone builds (the default for Xcode's Debug
configuration): if `condition` evaluates to false, stop program
execution in a debuggable state after printing `message`.
* In -O builds (the default for Xcode's Release configuration),
if `condition` evaluates to false, prints `message`.
- parameter file: The name of the file; defaults to the current localized file.
- parameter function: The name of the function; defaults to the function within which the call is made.
- parameter line: The line number; defaults to the line number within the file that the call is made.
*/
@transparent
@inline(__always)
public func PXPAssert(
@autoclosure condition: () -> Bool,
@autoclosure _ message: () -> String = String(),
file: StaticString = __FILE__,
function: StaticString = __FUNCTION__,
line: UInt = __LINE__
) {
func _printStackTrace() {
for symbol in NSThread.callStackSymbols() {
print("\t" + symbol)
}
}
if !condition() {
let localizedFile = NSURL(string: String(file))?.lastPathComponent ?? "<Unknown file>"
print("Assertion failed at \(function) in \(localizedFile):\(line) — \(message())")
#if DEBUG
// Only abort in debug mode:
_printStackTrace()
abort()
#endif
}
}
func testPXPAssert() {
let A = 1
let B = 2
PXPAssert(A == B, "A should equal B!")
}
@pxpgraphics
Copy link
Author

Sample console output:

Assertion failed at testPXPAssert() in AppDelegate.swift:59 — A should equal B!
    0   PXPAssert                           0x0000000102edefcb _TFF8PXPAssert8PXPAssertFTKT_SbKT_SS4fileVSs12StaticString8functionS0_4lineSu_T_L_16_printStackTraceFT_T_ + 91
    1   PXPAssert                           0x0000000102edf88f _TF8PXPAssert12testPXPAssertFT_T_ + 1343
    2   PXPAssert                           0x0000000102edfa95 _TFC8PXPAssert11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 53
    3   PXPAssert                           0x0000000102edfc43 _TToFC8PXPAssert11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqGVSs10DictionaryCSo8NSObjectPSs9AnyObject____Sb + 179
    4   UIKit                               0x00000001038d31f1 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 272
    5   UIKit                               0x00000001038d4397 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3415
    6   UIKit                               0x00000001038dacc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
    7   UIKit                               0x00000001038d7e7b -[UIApplication workspaceDidEndTransaction:] + 188
    8   FrontBoardServices                  0x0000000106c94754 -[FBSSerialQueue _performNext] + 192
    9   FrontBoardServices                  0x0000000106c94ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    10  CoreFoundation                      0x0000000102feba31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    11  CoreFoundation                      0x0000000102fe195c __CFRunLoopDoSources0 + 556
    12  CoreFoundation                      0x0000000102fe0e13 __CFRunLoopRun + 867
    13  CoreFoundation                      0x0000000102fe0828 CFRunLoopRunSpecific + 488
    14  UIKit                               0x00000001038d77cd -[UIApplication _run] + 402
    15  UIKit                               0x00000001038dc610 UIApplicationMain + 171
    16  PXPAssert                           0x0000000102ee00bd main + 109
    17  libdyld.dylib                       0x000000010590892d start + 1
(lldb) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment