Created
April 16, 2015 07:56
-
-
Save DaveWoodCom/71fbb90ca58fd058bf97 to your computer and use it in GitHub Desktop.
An XCGLogDestination that adds the logs to a UITextView
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
extension XCGLogger { | |
public class XCGTextViewLogDestination: XCGLogDestinationProtocol, DebugPrintable { | |
public var owner: XCGLogger | |
public var identifier: String | |
public var outputLogLevel: XCGLogger.LogLevel = .Debug | |
public var showThreadName: Bool = false | |
public var showFileName: Bool = true | |
public var showLineNumber: Bool = true | |
public var showLogLevel: Bool = true | |
public var textView: UITextView | |
public init(textView: UITextView, owner: XCGLogger, identifier: String = "") { | |
self.textView = textView | |
self.owner = owner | |
self.identifier = identifier | |
} | |
public func processLogDetails(logDetails: XCGLogDetails) { | |
var extendedDetails: String = "" | |
if showThreadName { | |
extendedDetails += "[" + (NSThread.isMainThread() ? "main" : (NSThread.currentThread().name != "" ? NSThread.currentThread().name : String(format:"%p", NSThread.currentThread()))) + "] " | |
} | |
if showLogLevel { | |
extendedDetails += "[" + logDetails.logLevel.description() + "] " | |
} | |
if showFileName { | |
extendedDetails += "[" + logDetails.fileName.lastPathComponent + (showLineNumber ? ":" + String(logDetails.lineNumber) : "") + "] " | |
} | |
else if showLineNumber { | |
extendedDetails += "[" + String(logDetails.lineNumber) + "] " | |
} | |
var formattedDate: String = logDetails.date.description | |
if let dateFormatter = owner.dateFormatter { | |
formattedDate = dateFormatter.stringFromDate(logDetails.date) | |
} | |
var fullLogMessage: String = "\(formattedDate) \(extendedDetails)\(logDetails.functionName): \(logDetails.logMessage)\n" | |
textView.text = textView.text + fullLogMessage | |
} | |
public func processInternalLogDetails(logDetails: XCGLogDetails) { | |
var extendedDetails: String = "" | |
if showLogLevel { | |
extendedDetails += "[" + logDetails.logLevel.description() + "] " | |
} | |
var formattedDate: String = logDetails.date.description | |
if let dateFormatter = owner.dateFormatter { | |
formattedDate = dateFormatter.stringFromDate(logDetails.date) | |
} | |
var fullLogMessage: String = "\(formattedDate) \(extendedDetails): \(logDetails.logMessage)\n" | |
textView.text = textView.text + fullLogMessage | |
} | |
// MARK: - Misc methods | |
public func isEnabledForLogLevel (logLevel: XCGLogger.LogLevel) -> Bool { | |
return logLevel >= self.outputLogLevel | |
} | |
// MARK: - DebugPrintable | |
public var debugDescription: String { | |
get { | |
return "XCGTextViewLogDestination: \(identifier) - LogLevel: \(outputLogLevel.description()) showThreadName: \(showThreadName) showLogLevel: \(showLogLevel) showFileName: \(showFileName) showLineNumber: \(showLineNumber)" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And to add it:
log.add(destination: XCGLogger.XCGTextViewLogDestination(textView: textView, owner: log, identifier: "com.cerebralgardens.xcglogger.logdestination.textview"))