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 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)" | |
} | |
} | |
} | |
} |
This helped me out, thanks. FWIW, tried running this on iOS 9 beta 2 device, and had fatal crash at line 46:
function signature specialization <Arg[0] = Exploded> of ext.iATN.XCGLogger.XCGLogger.XCGTextViewLogDestination.processLogDetails (ext.iATN.XCGLogger.XCGLogger.XCGTextViewLogDestination)(XCGLogger.XCGLogDetails) -> ()
I've updated this for the latest version (4.0.0) in Swift
extension XCGLogger {
public class XCGTextViewLogDestination: DestinationProtocol {
public var owner: XCGLogger?
public var identifier: String
public var outputLevel: XCGLogger.Level = .debug
public var showThreadName: Bool = false
public var showFileName: Bool = true
public var showLineNumber: Bool = true
public var showLogLevel: Bool = true
public var haveLoggedAppDetails: Bool = false
public var formatters: [LogFormatterProtocol]? = []
public var filters: [FilterProtocol]? = []
public var textView: UITextView
public init(textView: UITextView, owner: XCGLogger, identifier: String = "") {
self.textView = textView
self.owner = owner
self.identifier = identifier
}
public func process(logDetails: LogDetails) {
var extendedDetails: String = ""
if showThreadName {
extendedDetails += "[" + (Thread.isMainThread ? "main" : (Thread.current.name! != "" ? Thread.current.name! : String(format:"%p", Thread.current))) + "] "
}
if showLogLevel {
extendedDetails += "[" + logDetails.level.description + "] "
}
if showFileName {
let filename = NSURL(fileURLWithPath: logDetails.fileName).lastPathComponent!
extendedDetails += "[" + filename + (showLineNumber ? ":" + String(logDetails.lineNumber) : "") + "] "
}
else if showLineNumber {
extendedDetails += "[" + String(logDetails.lineNumber) + "] "
}
var formattedDate: String = logDetails.date.description
if let dateFormatter = owner!.dateFormatter {
formattedDate = dateFormatter.string(from: logDetails.date)
}
let fullLogMessage: String = "\(formattedDate) \(extendedDetails)\(logDetails.functionName): \(logDetails.message)\n"
textView.text = textView.text + fullLogMessage
}
public func processInternal(logDetails: LogDetails) {
var extendedDetails: String = ""
if showLogLevel {
extendedDetails += "[" + logDetails.level.description + "] "
}
var formattedDate: String = logDetails.date.description
if let dateFormatter = owner!.dateFormatter {
formattedDate = dateFormatter.string(from: logDetails.date)
}
let fullLogMessage: String = "\(formattedDate) \(extendedDetails): \(logDetails.message)\n"
textView.text = textView.text + fullLogMessage
}
// MARK: - Misc methods
public func isEnabledFor(level: XCGLogger.Level) -> Bool {
return level >= self.outputLevel
}
// MARK: - DebugPrintable
public var debugDescription: String {
get {
return "XCGTextViewLogDestination: \(identifier) - LogLevel: \(outputLevel.description) showThreadName: \(showThreadName) showLogLevel: \(showLogLevel) showFileName: \(showFileName) showLineNumber: \(showLineNumber)"
}
}
}
}
And to add it:
log.add(destination: XCGLogger.XCGTextViewLogDestination(textView: textView, owner: log, identifier: "com.cerebralgardens.xcglogger.logdestination.textview"))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used your example code in my OSX project. I just replaced UITextView with NSTextView, and it worked for me :) Now my code both logs to my app window and file. However, I was just a bit suspicious with line:
textView.text = textView.text + fullLogMessage
So I extended NSTextView with an appendText method:
textView.appendText(fullLogMessage)
The code for appendText extension is:
extension NSTextView {
func appendText(line: String) {
}