Skip to content

Commit

Permalink
fix crash issue, log crash stack and update lomod
Browse files Browse the repository at this point in the history
  • Loading branch information
fuji246 committed Dec 21, 2022
1 parent 2f18641 commit 2d9d2ea
Show file tree
Hide file tree
Showing 238 changed files with 18,686 additions and 912 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<key>LomoAgent.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>0</integer>
</dict>
<key>LomoAgentLauncher.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>7</integer>
<integer>8</integer>
</dict>
</dict>
</dict>
Expand Down
63 changes: 62 additions & 1 deletion LomoAgent/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Cocoa
import CocoaLumberjack
import CrashReporter

let launcherAppId = "lomoware.lomorage.LomoAgentLauncher"

Expand Down Expand Up @@ -74,6 +75,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {

private var fileLogger: DDFileLogger!

internal var isDebuggerAttached: Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout<kinfo_proc>.stride
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}

func setFileLoggerLevel(_ loglevel: DDLogLevel) {
DDLogInfo("set log level to: \(loglevel.rawValue)")
DDLog.remove(fileLogger)
Expand Down Expand Up @@ -105,6 +115,38 @@ class AppDelegate: NSObject, NSApplicationDelegate {
DDLogInfo("logging to \(String(describing: fileLogger.logFileManager.logsDirectory))")
}

private func loadCrashLog() {
// Try loading the crash report.
let config = PLCrashReporterConfig(signalHandlerType: .mach, symbolicationStrategy: .all)
guard let crashReporter = PLCrashReporter(configuration: config) else {
DDLogWarn("Could not create an instance of PLCrashReporter")
return
}

if crashReporter.hasPendingCrashReport() {
do {
let data = try crashReporter.loadPendingCrashReportDataAndReturnError()

// Retrieving crash reporter data.
let report = try PLCrashReport(data: data)

// We could send the report from here, but we'll just print out some debugging info instead.
if let text = PLCrashReportTextFormatter.stringValue(for: report, with: PLCrashReportTextFormatiOS) {
DDLogError("=============>CrashReport")
DDLogError(text)
DDLogError("CrashReport<=============")
} else {
DDLogWarn("CrashReporter: can't convert report to text")
}
} catch let error {
DDLogWarn("CrashReporter failed to load and parse with error: \(error)")
}
}

// Purge the report.
crashReporter.purgePendingCrashReport()
}

func applicationWillFinishLaunching(_ aNotification: Notification) {
let version = Bundle.main.infoDictionary?["CFBundleVersion"] as! String
let arguments = CommandLine.arguments
Expand All @@ -118,11 +160,30 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

// Uncomment and implement isDebuggerAttached to safely run this code with a debugger.
// See: https://github.com/microsoft/plcrashreporter/blob/2dd862ce049e6f43feb355308dfc710f3af54c4d/Source/Crash%20Demo/main.m#L96
if !isDebuggerAttached {
// It is strongly recommended that local symbolication only be enabled for non-release builds.
// Use [] for release versions.
let config = PLCrashReporterConfig(signalHandlerType: .mach, symbolicationStrategy: .all)
guard let crashReporter = PLCrashReporter(configuration: config) else {
print("Could not create an instance of PLCrashReporter")
return
}

// Enable the Crash Reporter.
do {
try crashReporter.enableAndReturnError()
} catch let error {
print("Warning: Could not enable crash reporter: \(error)")
}
}
if !exit {
setupLogger()
loadCrashLog()
DDLogInfo("LomoAgent version: \(version)")
UserDefaults.standard.set(UUID().uuidString, forKey: PREF_ADMIN_TOKEN)
NotificationCenter.default.post(name: .NotifyStart, object: self)
// NotificationCenter.default.post(name: .NotifyStart, object: self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions LomoAgent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>19739f3</string>
<string>2f18641</string>
<key>CFBundleVersion</key>
<string>2022_11_15.15_20_26.0.19739f3</string>
<string>2022_12_21.11_24_23.0.2f18641</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
6 changes: 0 additions & 6 deletions LomoAgent/PreferencesWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,6 @@ class PreferencesWindow: NSWindowController, NSWindowDelegate {
if let lomodPort = UInt16(self.portTextField.stringValue), !isPortOpen(port: lomodPort) {
self.userTipsLabel.textColor = .red
self.userTipsLabel.stringValue = usedPortLocalized
} else {
self.userTipsLabel.textColor = .red
self.userTipsLabel.stringValue = userTipsReportIssue
if let logDir = getLogDir() {
NSWorkspace.shared.open(URL(fileURLWithPath: logDir))
}
}
}
}
Expand Down
41 changes: 35 additions & 6 deletions LomoAgent/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StatusMenuController: NSObject {

@IBOutlet weak var statusMenu: NSMenu!
var settingsMenuItem: NSMenuItem!
var preferencesWindow: PreferencesWindow!
var preferencesWindow: PreferencesWindow?
var userWindow: UserWindow!
var aboutWindow: AboutWindow!
var lomodTask: Process?
Expand All @@ -24,6 +24,8 @@ class StatusMenuController: NSObject {
let pingReaptingTimer = RepeatingTimer(timeInterval: 5)
static let autoUpdateHour = 4
static let autoUpdateMinute = 0
static let pingTimeoutSec = 180.0
var pingTimeout = Date(timeIntervalSinceNow: pingTimeoutSec)

var lomodService: LomodService!

Expand All @@ -36,7 +38,7 @@ class StatusMenuController: NSObject {
@IBOutlet weak var restartMenuItem: NSMenuItem!

@IBAction func settingsClicked(_ sender: Any) {
preferencesWindow.showWindow(nil)
preferencesWindow?.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)
}

Expand Down Expand Up @@ -170,6 +172,10 @@ class StatusMenuController: NSObject {
scheduleAutoUpdate()

lomodService = getLomodService()

DispatchQueue.global().async {
NotificationCenter.default.post(name: .NotifyStart, object: self)
}
}

func getSecondsOffsetTo(hour: Int, minute: Int) -> Int {
Expand Down Expand Up @@ -217,6 +223,7 @@ class StatusMenuController: NSObject {
lomodService.checkServerStatus { (systemInfo, connectErr) in
if connectErr == nil {
DDLogError("pingLomod succ!")
self.pingTimeout = Date(timeIntervalSinceNow: StatusMenuController.pingTimeoutSec)

DispatchQueue.main.async {
self.lomodService.getUserList()
Expand All @@ -240,7 +247,32 @@ class StatusMenuController: NSObject {
}
} else {
DDLogError("pingLomod error!")
if Date() >= self.pingTimeout && UserDefaults.standard.string(forKey: PREF_HOME_DIR) != nil {
DispatchQueue.main.async {
if let prefWindow = self.preferencesWindow,
prefWindow.userTipsLabel.stringValue != userTipsReportIssue{
prefWindow.showWindow(nil)
prefWindow.userTipsLabel.textColor = .red
prefWindow.userTipsLabel.stringValue = userTipsReportIssue
if let logDir = getLogDir() {
NSWorkspace.shared.open(URL(fileURLWithPath: logDir))
}
NSApp.activate(ignoringOtherApps: true)
}
}
}
}
}
}

fileprivate func showWindowToSetHomeDir() {
DispatchQueue.main.async {
if let prefWindow = self.preferencesWindow {
prefWindow.showWindow(nil)
prefWindow.userTipsLabel.textColor = .red
prefWindow.userTipsLabel.stringValue = userTipsConfigureHomeDirAndWaitStart
}
NSApp.activate(ignoringOtherApps: true)
}
}

Expand Down Expand Up @@ -290,10 +322,7 @@ class StatusMenuController: NSObject {
} else {
DDLogError("Need set home directory first")
lomodTask = nil
preferencesWindow.userTipsLabel.textColor = .red
preferencesWindow.userTipsLabel.stringValue = userTipsConfigureHomeDirAndWaitStart
preferencesWindow.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)
showWindowToSetHomeDir()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target 'LomoAgent' do
pod 'Zip', '~> 1.1'
pod 'CatCrypto', :git => 'https://github.com/ImKcat/CatCrypto.git'
pod 'Commands', '~> 0.6.0'
pod 'PLCrashReporter'
end

target 'LomoAgentLauncher' do
Expand Down
11 changes: 7 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ PODS:
- CocoaLumberjack/Swift (3.6.1):
- CocoaLumberjack/Core
- Commands (0.6.0)
- PLCrashReporter (1.11.0)
- Zip (1.1.0)

DEPENDENCIES:
- CatCrypto (from `https://github.com/ImKcat/CatCrypto.git`)
- CocoaLumberjack/Swift
- Commands (~> 0.6.0)
- PLCrashReporter
- Zip (~> 1.1)

SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
- Zip
trunk:
- CocoaLumberjack
- Commands
- PLCrashReporter
- Zip

EXTERNAL SOURCES:
CatCrypto:
Expand All @@ -32,8 +34,9 @@ SPEC CHECKSUMS:
CatCrypto: a477899b6be4954e75be4897e732da098cc0a5a8
CocoaLumberjack: b17ae15142558d08bbacf69775fa10c4abbebcc9
Commands: d617aca4118b0f289144e9e93fb03847d9c48a61
PLCrashReporter: 7a9dff14a23ba5d2e28c6160f0bb6fada5e71a8d
Zip: 8877eede3dda76bcac281225c20e71c25270774c

PODFILE CHECKSUM: 89d9f9ab252ea8093c87db9b4f45beff668ff12c
PODFILE CHECKSUM: f13744ecfefc51c15acb4c25f596d488a7d733ad

COCOAPODS: 1.10.1
COCOAPODS: 1.11.3
11 changes: 7 additions & 4 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d9d2ea

Please sign in to comment.