This article describes how to use Crashlytics logging in objective-c. However, after going throught the installation steps for propertly referencing Crashlytics and Fabric into my project, I don't seem to have access to that method.
Looking at the Crashlytics.h file, I can see it defined using compiler flags:
#ifdef DEBUG
#define CLS_LOG(__FORMAT__, ...) CLSNSLog((@"%s line %d $ " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define CLS_LOG(__FORMAT__, ...) CLSLog((@"%s line %d $ " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif
This block just appears to wrap the CLSNLog
and the CLSLog
functions depending on the compiler flag.
So, thinking I'd just go straight to the source, I tried to reference CLSLog directly from a swift file. Still no luck:
My-Bridging-Header.h:
#import <Crashlytics/Crashlytics.h>
Log.swift:
import Foundation
import Fabric
import Crashlytics
func Log(message: String) {
NSLog("%@", message)
CLS_LOG("%@", message)
CLSLog("%@", message)
}
The last two lines in the Log function throw the error, Use of unresolved identifier
. Crashlytics crash reporting works just fine, except for the logging feature. According to this article, logging support for Swift has been implemented.
As far as versions go, I'm running the latest version of Fabric/Crashlytics (December release, at the time of this post).
(Interesting note, I can see/use CLSLogv()
...)
Does anyone know the correct way to incorporate CLS_LOG
for use in a Swift project?
Here is my version adapted from Dima's answer. I have no need of the arguments, since you can do all the formatting within the Swift string that you pass.
And you would use it like this:
EDIT: Updated to Swift 3.1
Mike from Crashlytics here.
To use custom logging in Swift, just use CLSLogv or CLSNSLogv. You need to make an array and then call getVaList function on that array.
Here's a snippet:
For CLSNSLogv:
Any one who want to log error usin Crashlytics can use the below code and its working fine for me :)
error is NSERROR object that holds the error that produced during some action
How about like this?
No need for the array then, just list the variadic parameters
You have to create an intermediary bridge like this:
CrashlyticsBridge.h:
CrashlyticsBridge.m
My-Bridging-Header.h:
Then, you can simply add that to your
Log
function:This will give you the Crashlytics logging and NSLogging while you are debugging.
I needed something similar to
CLS_LOG()
in Swift that printed out contextual information about the location of the call. Normally this would not be possible without preprocessor directives but I found out how to replicate this behavior pretty closely in Swift here: https://developer.apple.com/swift/blog/?id=15The identifiers we need (
#file, #function, #line
) show information about the caller if you set them as default values in an argument list.Note: If you are logging errors that may have
%
symbols in them, such as network query strings, this may crash. You'll need to join the string first (e.g.let string = "\(filename).\(function) line \(line) $ \(message)"
)Swift 3 version (note: this is a global function, so it should be placed outside of any struct or class definition):
Swift 2 version:
And here is a gist with some more information and the actual file I put this code in: https://gist.github.com/DimaVartanian/a8aa73ba814a61f749c0
As you can see it is pretty close to the original macro and only differs in that you can't see if you are calling a class method or an instance method, and that you need to include your format argument list enclosed in an array. Both are limitations I believe there is no way around right now but pretty minor. You also need to make sure DEBUG is defined in your Swift compiler flags. It does not carry over from your regular flags automatically.