Skip to content

Instantly share code, notes, and snippets.

@DimaVartanian
Last active June 8, 2019 10:16
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DimaVartanian/a8aa73ba814a61f749c0 to your computer and use it in GitHub Desktop.
Save DimaVartanian/a8aa73ba814a61f749c0 to your computer and use it in GitHub Desktop.
Crashlytics CLS_LOG() in Swift
//
// 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 $
@DimaVartanian
Copy link
Author

Updated the gist using NSURL stuff instead. Thanks for the feedback guys!

@fatihyildizhan
Copy link

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

@elsurudo
Copy link

@fatihyildizhan +1

Also, for Swift 3 prep, __FILE__ -> #file, __FUNCTION__ -> #function, __LINE__ -> #line

@nitrag
Copy link

nitrag commented Jan 17, 2017

Thank you @DimaVartanian

Please update StackOverflow answer:

  • Fix Usage. It's not CLS.log()
  • Add missing _ before format: 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
}

@zhenja
Copy link

zhenja commented Oct 28, 2018

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