0001 // 0002 // FileDestination.swift 0003 // SwiftyBeaver 0004 // 0005 // Created by Sebastian Kreutzberger on 05.12.15. 0006 // Copyright © 2015 Sebastian Kreutzberger 0007 // Some rights reserved: http://opensource.org/licenses/MIT 0008 // 0009 0010 import Foundation 0011 0012 public class FileDestination: BaseDestination { 0013 0014 public var logFileURL: NSURL 0015 0016 override public var defaultHashValue: Int {return 2} 0017 let fileManager
FileDestination.swift:22 logFileURL = url.URLByAppendingPathComponent("swiftybeaver.log", isDirectory: false)FileDestination.swift:24 logFileURL = NSURL()FileDestination.swift:45 saveToFile(str, url: logFileURL)= NSFileManager.defaultManager() 0018 var fileHandle
FileDestination.swift:21 if let url = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first {FileDestination.swift:61 if fileManager.fileExistsAtPath(url.path!) == false {: NSFileHandle? = nil 0019 0020 public override init() { 0021 if let url = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first { 0022 logFileURL = url.URLByAppendingPathComponent("swiftybeaver.log", isDirectory: false) 0023 } else { 0024 logFileURL = NSURL() 0025 } 0026 super.init() 0027 0028 // bash font color, first value is intensity, second is color 0029 // see http://bit.ly/1Otu3Zr to learn more 0030 blue = "0;34m" // replace first 0 with 1 to make it bold 0031 green = "0;32m" 0032 yellow = "0;33m" 0033 red = "0;31m" 0034 magenta = "0;35m" 0035 cyan = "0;36m" 0036 silver = "0;37m" 0037 reset = "\u{001b}[0m" 0038 } 0039 0040 // append to file. uses full base class functionality 0041 override public func send(level: SwiftyBeaver.Level, msg: String, thread: String, path: String, function: String, line: Int) -> String? { 0042 let formattedString = super.send(level, msg: msg, thread: thread, path: path, function: function, line: line) 0043 0044 if let str = formattedString { 0045 saveToFile(str, url: logFileURL) 0046 } 0047 return formattedString 0048 } 0049 0050 deinit { 0051 // close file handle if set 0052 if let fileHandle = fileHandle { 0053 fileHandle.closeFile() 0054 } 0055 } 0056 0057 /// appends a string as line to a file. 0058 /// returns boolean about success 0059 func saveToFile
FileDestination.swift:52 if let fileHandle = fileHandle {FileDestination.swift:67 if fileHandle == nil {FileDestination.swift:69 fileHandle = try NSFileHandle(forWritingToURL: url)FileDestination.swift:71 if let fileHandle = fileHandle {(str: String, url: NSURL) -> Bool { 0060 do { 0061 if fileManager.fileExistsAtPath(url.path!) == false { 0062 // create file if not existing 0063 let line = str + "\n" 0064 try line.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding) 0065 } else { 0066 // append to end of file 0067 if fileHandle == nil { 0068 // initial setting of file handle 0069 fileHandle = try NSFileHandle(forWritingToURL: url) 0070 } 0071 if let fileHandle = fileHandle { 0072 fileHandle.seekToEndOfFile() 0073 let line = str + "\n" 0074 let data = line.dataUsingEncoding(NSUTF8StringEncoding)! 0075 fileHandle.writeData(data) 0076 } 0077 } 0078 return true 0079 } catch let error { 0080 print("SwiftyBeaver could not write to file \(url). \(error)") 0081 return false 0082 } 0083 } 0084 } 0085 0086
FileDestination.swift:45 saveToFile(str, url: logFileURL)