Created
June 15, 2015 22:21
-
-
Save leviyehonatan/c4f78889280aa6cb3958 to your computer and use it in GitHub Desktop.
reading and writing to my logfile
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
func existingLog() -> String? { | |
if let logFileHandle = NSFileHandle(forReadingAtPath: logFile) { | |
let data = logFileHandle.readDataToEndOfFile() | |
return NSString(data:data, encoding:NSUTF8StringEncoding) as String? | |
} | |
return nil | |
} | |
func log(log: String) { | |
let logLine = "\(NSDate()) \(log)\n" | |
if let data = logLine.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { | |
if let fileHandle = NSFileHandle(forWritingAtPath: logFile) { | |
fileHandle.seekToEndOfFile() | |
fileHandle.writeData(data) | |
fileHandle.closeFile() | |
} else { | |
NSLog("error getting file handle for log: \(logFile)") | |
} | |
} else { | |
NSLog("error creating data from logline") | |
} | |
NSLog(logLine) | |
if (delegate != nil) { | |
delegate!.log(logLine) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment