-
-
Save DimaVartanian/a8aa73ba814a61f749c0 to your computer and use it in GitHub Desktop.
// | |
// Created by Dima Vartanian on 10/29/15. | |
// | |
import Foundation | |
import Crashlytics | |
// this method gives us pretty much the same functionality as the CLS_LOG macro, but written as a Swift function, the only differences are that we have to use array syntax for the argument list and that we don't get see if the method being called is a class method or an instance method. We also have to define the DEBUG compiler flag with -D DEBUG. | |
/// Usage: | |
/// | |
/// CLS.log("message!") | |
/// CLS.log("message with parameter 1: %@ and 2: %@", ["First", "Second"]) | |
/// | |
func CLS_LOG_SWIFT(format: String = "", _ args: [CVarArg] = [], file: String = #file, function: String = #function, line: Int = #line) | |
{ | |
let filename = URL(string: file)?.lastPathComponent.components(separatedBy: ".").first | |
#if DEBUG | |
CLSNSLogv("\(filename).\(function) line \(line) $ \(format)", getVaList(args)) | |
#else | |
CLSLogv("\(filename).\(function) line \(line) $ \(format)", getVaList(args)) | |
#endif | |
} | |
// CLS_LOG() output: -[ClassName methodName:] line 10 $ | |
// CLS_LOG_SWIFT() output: ClassName.methodName line 10 $ |
In case anyone finds this moving to Swift 2, you need to cast to NSString
twice to get this to work.
((file as NSString).lastPathComponent as NSString)
As of my knowledge, Swift 2.0 casting String
to NSString
is discouraged.
Quote:
... you should almost never need to use the NSString class directly in your own code.
Apple also wants us to move away from filepath operations via strings and enforces to use URLs for that.
let filename = NSURL(string:file)?.lastPathComponent?.componentsSeparatedByString(".").first
Updated the gist using NSURL
stuff instead. Thanks for the feedback guys!
Not a big deal but if you add '!' after filename you can avoid from Optional("") on Dashboard.
CLSNSLogv("(filename!).(function) line (line) $ (format)", getVaList(args))
CLSLogv("(filename!).(function) line (line) $ (format)", getVaList(args))
without ! -> Optional("ClassName").methodName
with ! -> ClassName.methodName
Also, for Swift 3 prep, __FILE__
-> #file
, __FUNCTION__
-> #function
, __LINE__
-> #line
Thank you @DimaVartanian
Please update StackOverflow answer:
- Fix Usage. It's not CLS.log()
- Add missing
_
beforeformat: String
func CLS_LOG_SWIFT(_ format: String = "", _ args: [CVarArg] = [], file: String = #file, function: String = #function, line: Int = #line) {
let filename = URL(string: file)?.lastPathComponent.components(separatedBy: ".").first
#if DEBUG
CLSNSLogv("\(filename).\(function) line \(line) $ \(format)", getVaList(args))
#else
CLSLogv("\(filename).\(function) line \(line) $ \(format)", getVaList(args))
#endif
}
Updated with path encoding:
func CLS_LOG_SWIFT(format: String = "", _ args: [CVarArg] = [], file: String = #file, function: String = #function, line: Int = #line)
{
guard let path = file.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
return
}
guard let filename = URL(string: path)?.lastPathComponent.components(separatedBy: ".").first else {
return
}
#if DEBUG
CLSNSLogv("\(String(describing: filename)).\(function) line \(line) $ \(format)", getVaList(args))
#else
CLSLogv("\(filename).\(function) line \(line) $ \(format)", getVaList(args))
#endif
}
https://gist.github.com/zhenja/63ede509692bc64dcb84f38aac349053
+1 Awesome