Changeset 96d31995 for launchers


Ignore:
Timestamp:
Oct 11, 2018 5:01:21 PM (2 years ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
e3ab9f8e
Parents:
3988a86
Message:

OSX Launcher: cleanup and restructuring the main swift file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • launchers/macosx/I2PLauncher/SwiftMainDelegate.swift

    r3988a86 r96d31995  
    1010import Cocoa
    1111
     12class Logger {
     13  static func MLog<T>(level:Int32, _ object: T?,file:String = #file, function:String = #function, line:Int = #line) {
     14    SBridge.logProxy(level, formattedMsg: "\(makeTag(function: function, file: file, line: line)) : \(object)")
     15  }
     16 
     17  private static func makeTag(function: String, file: String, line: Int) -> String{
     18    let url = NSURL(fileURLWithPath: file)
     19    let className:String! = url.lastPathComponent == nil ? file: url.lastPathComponent!
     20    return "\(className) \(function)[\(line)]"
     21  }
     22 
     23}
     24
     25
    1226@objc class SwiftMainDelegate : NSObject {
    1327 
    1428  let statusBarController = StatusBarController()
    1529  let sharedRouterMgmr = RouterManager.shared()
    16   static let javaDetector = DetectJava()
    1730 
    1831  override init() {
    1932    super.init()
    20     if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
    21     SwiftMainDelegate.javaDetector.findIt()
    22       if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
    23         print("Could not find java....")
     33    if (!DetectJava.shared().isJavaFound()) {
     34    DetectJava.shared().findIt()
     35      if (!DetectJava.shared().isJavaFound()) {
     36        Logger.MLog(level:3, "Could not find java....")
    2437        terminate("No java..")
    2538      }
    2639    }
    27     let javaBinPath = SwiftMainDelegate.javaDetector.javaHome
    28     RouterProcessStatus.knownJavaBinPath = javaBinPath
    29     print("Found java home = ", javaBinPath)
     40    let javaBinPath = DetectJava.shared().javaBinary
     41    Logger.MLog(level:1, "".appendingFormat("Found java home = %@", javaBinPath!))
    3042   
    3143    let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
     
    3345      RouterProcessStatus.isRouterRunning = true
    3446      RouterProcessStatus.isRouterChildProcess = false
    35       print("I2P Router seems to be running")
     47      Logger.MLog(level:2, "I2P Router seems to be running")
    3648    } else {
    3749      RouterProcessStatus.isRouterRunning = false
    38       print("I2P Router seems to NOT be running")
     50      Logger.MLog(level:2, "I2P Router seems to NOT be running")
    3951    }
    4052  } // End of init()
     
    4355    var i2pPath = NSHomeDirectory()
    4456    i2pPath += "/Library/I2P"
    45     var jExecPath:String = RouterProcessStatus.knownJavaBinPath!
    46    
    47     // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
    48     // If JRE is detected, binary follows - if it's JDK, home follows.
    49     if (jExecPath.hasSuffix("Home")) {
    50       jExecPath += "/jre/bin/java"
    51     }
     57    let jExecPath:String = "/usr/libexec/java_home -v 1.7+ --exec java "
    5258   
    5359    let jarPath = i2pPath + "/lib/i2p.jar"
    5460   
    55     let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
     61    let subCmd = jExecPath + "-cp " + jarPath + " net.i2p.CoreVersion"
    5662   
    5763    let cmdArgs:[String] = ["-c", subCmd]
     
    6268      if (results.status == 0) {
    6369        let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
    64         NSLog("I2P version detected: %@",i2pVersion ?? "Unknown")
     70        Logger.MLog(level: 1, "".appendingFormat("I2P version detected: %@",i2pVersion ?? "Unknown"))
    6571        RouterProcessStatus.routerVersion = i2pVersion
    6672        RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
    67         RouterManager.shared().eventManager.trigger(eventName: "router_can_start", information: i2pVersion)
    6873      } else {
    69         NSLog("Non zero exit code from subprocess while trying to detect version number!")
     74        Logger.MLog(level: 2, "Non zero exit code from subprocess while trying to detect version number!")
    7075        for line in results.errorsLines {
    71           NSLog(line)
     76          Logger.MLog(level: 2, line)
    7277        }
    7378      }
    7479    } else {
    75       print("Warning: Version Detection did NOT captured output")
     80      Logger.MLog(level: 1, "Warning: Version Detection did NOT captured output")
    7681    }
    7782  }
    7883 
    7984  @objc func applicationDidFinishLaunching() {
    80     var i2pPath = NSHomeDirectory()
    81     i2pPath += "/Library/I2P"
    82    
    8385  }
    8486 
     
    9799  @objc func applicationWillTerminate() {
    98100    // Shutdown stuff
     101    let userPreferences = UserDefaults.standard
     102    if (!userPreferences.bool(forKey: "letRouterLiveEvenLauncherDied")) {
     103      RouterManager.shared().routerRunner.TeardownLaunchd()
     104      sleep(2)
     105      let status: AgentStatus? = RouterRunner.launchAgent?.status()
     106      if status != .unloaded {
     107        Logger.MLog(level:2, "Router service not yet stopped")
     108        RouterManager.shared().routerRunner.TeardownLaunchd()
     109        sleep(5)
     110      }
     111    }
    99112  }
    100113 
    101114  @objc func terminate(_ why: Any?) {
    102     print("Stopping cause of ", why!)
     115    Logger.MLog(level:2, "".appendingFormat("Stopping cause of ", why! as! CVarArg))
    103116  }
    104117}
Note: See TracChangeset for help on using the changeset viewer.