working on it ...
Explore Public Snippets
Found 3 snippets matching: swlog
public by marksimon232 283249 0 7 1
Swift 2: Custom logger that mimics NSLog
Using this initializer, you can easily build a custom logger that mimics NSLog:
public func SWLog(format: String, _ args: CVarArgType...) { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate( "mm:ss:SSS", options: 0, locale: NSLocale.currentLocale()) let timeString = dateFormatter.stringFromDate(NSDate()) print("\(timeString): " + String(format: format, arguments: args)) }
internal func BuildSimpleTimeFormatter() -> NSDateFormatter { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("mm:ss:SSS", options: 0, locale: NSLocale.currentLocale()) return dateFormatter } internal let dateFormatter = BuildSimpleTimeFormatter() public func SWLog(format: String, _ args: CVarArgType...) { #if DEBUG let timeString = dateFormatter.stringFromDate(NSDate()) print("\(timeString): " + String(format: format, arguments: args), &errStream) #endif }
public struct StderrOutputStream: OutputStreamType { // Support for file redirection back to console private static var fpos : fpos_t = 0 private static var fd = dup(fileno(stderr)) private static var isRedirected_ = false public static var isRedirected : Bool {return isRedirected_} // When false, the output file is rewritten and not appended private static var appendToFile = true // When true, output is echoed to stdout public static var echo = false // When non-nil, output is redirected to the specified file path public static func redirectOutputToPath(path: String?) { if let path = path { if !isRedirected { // Set up if this is new redirection fflush(stderr) var pos = UnsafeMutablePointer<fpos_t>.alloc(1) fgetpos(stderr, pos) fpos = pos.memory free(pos) fd = dup(fileno(stderr)) } if appendToFile { freopen(path, "a", stderr) } else { freopen(path, "w", stderr) } isRedirected_ = true } else { if !isRedirected { // do not respond if not redirected return } fflush(stderr) dup2(fd, fileno(stderr)) close(fd) clearerr(stderr) fsetpos(stderr, &fpos) isRedirected_ = false } } public static let stream = StderrOutputStream() public func write(string: String) { fputs(string, stderr) if StderrOutputStream.echo && StderrOutputStream.isRedirected {fputs(string, stdout)} } } public var errStream = StderrOutputStream.stream func swlog(format : String, args : CVarArgType...) { println(String(format: format, arguments: args), &errStream) }