Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift debugPrint vs print

Tags:

swift

Is there a way to have the app crash and burn at runtime on print()? Override print() in Swift runtime with an implementation that'd preconditionFailure("STOP using print()").

Basically it's a part of the team Pavlov's dog training in progress: I want people to use debugPrint rather than print to pollute console in debug builds only.

UPD20180525: matt is right: print output does not go to a live console of a real device, it somehow only ends up on lldb console.

NSLog output thought DOES go to the device's console so what needs to be killed at runtime or compile time for non debug builds is NSLog

This is what was actually needed:

#if DEBUG
#else
public func NSLog(_ format: String, _ args: CVarArg...)
{

}
#endif

(cause there is no real need to get rid of print which is harmless in release builds)

like image 342
Anton Tropashko Avatar asked Feb 25 '26 10:02

Anton Tropashko


1 Answers

While I'm not a fan of this, please do not make this a crashing operation. Break the build, not the runtime.

@available(*, unavailable, message: "Our team has agreed not to use print.")
func print(_ items: Any..., separator: String = "", terminator: String = "\n") {}

This will turn references to print into a compile-time error:

error: 'print(_:separator:terminator:)' is unavailable: Our team has agreed not to use print.
like image 99
Rob Napier Avatar answered Feb 28 '26 03:02

Rob Napier