Skip to content

Instantly share code, notes, and snippets.

@DaveWoodCom
Created April 16, 2015 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveWoodCom/71fbb90ca58fd058bf97 to your computer and use it in GitHub Desktop.
Save DaveWoodCom/71fbb90ca58fd058bf97 to your computer and use it in GitHub Desktop.
An XCGLogDestination that adds the logs to a UITextView
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)"
}
}
}
}
@horseshoe7
Copy link

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