Skip to content

Instantly share code, notes, and snippets.

@zhenja
Last active October 28, 2018 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenja/63ede509692bc64dcb84f38aac349053 to your computer and use it in GitHub Desktop.
Save zhenja/63ede509692bc64dcb84f38aac349053 to your computer and use it in GitHub Desktop.
Crashlytics CLS_LOG() in Swift
//
// Created by Dima Vartanian on 10/29/15.
// https://gist.github.com/DimaVartanian/a8aa73ba814a61f749c0
// Updated by me
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"])
///
/// - Parameters:
/// - format: String which should be printed to the console. Default: Empty string
/// - args: Arguments as array used in format parameter. Default: Empty array
/// - file: Path of the called file. Default: #file
/// - function: Function name. Default: #function
/// - line: Line number. Default: #line
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment