Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. //
  2. // LoggingPrint.swift
  3. //
  4.  
  5. import Foundation
  6.  
  7. /**
  8. Prints the filename, function name, line number and textual representation of `object` and a newline character into
  9. the standard output if the build setting for "Active Complilation Conditions" (SWIFT_ACTIVE_COMPILATION_CONDITIONS) defines `DEBUG`.
  10.  
  11. The current thread is a prefix on the output. <UI> for the main thread, <BG> for anything else.
  12.  
  13. Only the first parameter needs to be passed to this funtion.
  14.  
  15. The textual representation is obtained from the `object` using `String(reflecting:)` which works for _any_ type.
  16. To provide a custom format for the output make your object conform to `CustomDebugStringConvertible` and provide your format in
  17. the `debugDescription` parameter.
  18.  
  19. :param: object The object whose textual representation will be printed. If this is an expression, it is lazily evaluated.
  20. :param: file The name of the file, defaults to the current file without the ".swift" extension.
  21. :param: function The name of the function, defaults to the function within which the call is made.
  22. :param: line The line number, defaults to the line number within the file that the call is made.
  23. */
  24. func loggingPrint<T>(_ object: @autoclosure () -> T, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
  25. #if DEBUG
  26. let value = object()
  27. let fileURL = NSURL(string: file)?.lastPathComponent ?? "Unknown file"
  28. let queue = Thread.isMainThread ? "UI" : "BG"
  29.  
  30. print("<\(queue)> \(fileURL) \(function)[\(line)]: " + String(reflecting: value))
  31. #endif
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement