Skip to content

Instantly share code, notes, and snippets.

@vtardia
Last active October 3, 2018 08:42
Show Gist options
  • Save vtardia/3f7d17efd7b258e82b62 to your computer and use it in GitHub Desktop.
Save vtardia/3f7d17efd7b258e82b62 to your computer and use it in GitHub Desktop.
Swift LogUtils - allows to redirect log entries to /Library/Logs/<AppName>.log
//
// AppDelegate.swift
// SwiftApp
//
// Created by Vito Tardia on 02/04/15.
// Copyright (c) 2014 Vito Tardia. All rights reserved.
//
import Cocoa
import Foundation
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var appInfo: Dictionary<NSObject,AnyObject>
var appName: String!
override init() {
// Init local parameters
self.appInfo = CFBundleGetInfoDictionary(CFBundleGetMainBundle()) as Dictionary
self.appName = appInfo["CFBundleName"] as! String
// Init parent
super.init()
// Other init below...
SetCustomLogFilename(self.appName)
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
InfoLog(String(format:"Application %@ starting", self.appName))
DebugLog(String(format:"A debug message: %@", "BBB"))
var test = TestObj()
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
InfoLog(String(format:"Application %@ will terminate", self.appName))
}
}
//
// LogUtils.swift
// SwiftApp
//
// Created by Vito Tardia on 02/04/15.
// Copyright (c) 2014 Vito Tardia. All rights reserved.
//
import Foundation
var logFileHandle: NSFileHandle?
var original_stderr: Int32?;
#if DEBUG
func DebugLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Debug>: " + message + " [" + file + ":%i]", line) }()
}
func InfoLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Info>: " + message + " [" + file + ":%i]", line) }()
}
func WarningLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Warning>: " + message + " [" + file + ":%i]", line) }()
}
func ErrorLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Error>: " + message + " [" + file + ":%i]", line) }()
}
#else
func DebugLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
}
func InfoLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Info>: " + message) }()
}
func WarningLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Warning>: " + message) }()
}
func ErrorLog(message: String, file: String = __FILE__, line: Int = __LINE__) {
return { NSLog("<Error>: " + message) }()
}
#endif
// Redirect log to /Library/Logs/<AppName>.log
func SetCustomLogFilename(name: String) {
// Search log directory path
if let logDirectory: NSURL = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.LibraryDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true, error: nil)?.URLByAppendingPathComponent("Logs/") {
// Calculate full log file path
if let logFilePath = logDirectory.URLByAppendingPathComponent(String(format:"%@.log", name)) as NSURL! {
// Save STDERR
var stderr = NSFileHandle.fileHandleWithStandardError()
original_stderr = dup(stderr.fileDescriptor)
// Create an empty log file at path, NSFileHandle doesn't do it!
if !NSFileManager.defaultManager().isWritableFileAtPath(logFilePath.path!) {
"".writeToFile(logFilePath.path!, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
}
if let logFileHandle = NSFileHandle(forWritingAtPath: logFilePath.path!) {
// (Try to) Redirect STDERR to log file
var err:Int32? = dup2(logFileHandle.fileDescriptor, stderr.fileDescriptor)
// Something went wrong
if (err == -1) {
ErrorLog(String(format:"Could not redirect stderr, error %d", errno))
}
}
}
}
}
//
// TestObj.swift
// SwiftApp
//
// Created by Vito Tardia on 02/04/15.
// Copyright (c) 2014 Vito Tardia. All rights reserved.
//
import Foundation
class TestObj {
var name: String
init() {
self.name = "Foo"
InfoLog(String(format:"Loading: %@", self.name))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment