Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active November 12, 2017 07:41
Show Gist options
  • Save matsuda/d110122e3e0e3fcaee62 to your computer and use it in GitHub Desktop.
Save matsuda/d110122e3e0e3fcaee62 to your computer and use it in GitHub Desktop.
log functions for application in Swift
import UIKit
#if DEBUG
func exceptionHandler(exception : NSException) {
NSLog("CRASH: %@", exception);
NSLog("Stack Trace: %@", exception.callStackSymbols);
// Internal error reporting
}
#endif
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Build Settings > Swift Compiler - Custom Flags > Other Swift Flags : -DDEBUG
#if DEBUG
NSSetUncaughtExceptionHandler(exceptionHandler)
#if (arch(i386) || arch(x86_64)) && os(iOS)
{
if let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
print("Documents directory : \n\(docDir)\n")
}
}()
#endif
#endif
return true
}
}
//
// Log.swift
// Log
//
// Created by Kosuke Matsuda on 2016/02/13.
// Copyright © 2016年 Kosuke Matsuda. All rights reserved.
//
import Foundation
public func Log(body: Any? = nil, function: String = #function, file: String = #file, line: Int = #line) {
Log(body == nil ? "" : body, function: function, file: file, line: line)
}
public func Log(@autoclosure body: () -> Any, function: String = #function, file: String = #file, line: Int = #line) {
#if DEBUG
print("[\((file as NSString).lastPathComponent):\(line)] <\(function)> \(body())")
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment