Last active
June 8, 2019 10:16
-
-
Save DimaVartanian/a8aa73ba814a61f749c0 to your computer and use it in GitHub Desktop.
Crashlytics CLS_LOG() in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 $ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @DimaVartanian
Please update StackOverflow answer:
_
beforeformat: String