Skip to content

Instantly share code, notes, and snippets.

@dimroc
Last active July 28, 2017 10:46
Show Gist options
  • Save dimroc/2aef1b6b1e391f0085d2 to your computer and use it in GitHub Desktop.
Save dimroc/2aef1b6b1e391f0085d2 to your computer and use it in GitHub Desktop.
Wrapper around the Objective C DDLog found in CocoaLumberjack 2.x
import Foundation
public let defaultDebugLevel = DDLogLevel.Debug
public let defaultTag = NSString(format: "ManhattanForum")
class DDLogHelper {
class func SwiftLogMacro(async: Bool, level: DDLogLevel, flag flg: DDLogFlag, context: Int = 0, file: String = __FILE__, function: String = __FUNCTION__, line: UWord = __LINE__, tag: AnyObject? = defaultTag, format: String, args: CVaListPointer) {
let string = NSString(format: format, arguments: args) as String
SwiftLogMacro(async, level: level, flag: flg, context: context, file: file, function: function, line: line, tag: tag, string: string)
}
class func SwiftLogMacro(isAsynchronous: Bool, level: DDLogLevel, flag: DDLogFlag, context: Int = 0, file: String = __FILE__, function: String = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = defaultTag, string: String) {
// Tell the DDLogMessage constructor to copy the C strings that get passed to it.
// Had to hardcode context to 0.
let logMessage = DDLogMessage(message: string, level: level, flag: flag, context: context, file: file, function: function, line: line, tag: tag, options: DDLogMessageOptions.CopyFunction | DDLogMessageOptions.CopyFile, timestamp: NSDate())
DDLog.log(isAsynchronous, message: logMessage)
}
class func debug(logText: String, level: DDLogLevel = defaultDebugLevel, file: String = __FILE__, function: String = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = true, args: CVarArgType...) {
SwiftLogMacro(async, level: level, flag: DDLogFlag.Debug, file: file, function: function, line: line, format: logText, args: getVaList(args))
}
}
@dimroc
Copy link
Author

dimroc commented Jan 8, 2015

Usage:

        DDLogHelper.debug("Launching Papertrail logging for ManhattanForum")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment