- fix crash when GPU module is enabled (#74)

This commit is contained in:
Serhiy Mytrovtsiy
2020-08-26 12:45:15 +02:00
parent 1d98b8cb8e
commit ab1f7ff86e
4 changed files with 21 additions and 7 deletions

View File

@@ -35,7 +35,7 @@ public struct GPUs: value_t {
public var widget_value: Double {
get {
return list[0].utilization
return list.isEmpty ? 0 : list[0].utilization
}
}
}
@@ -87,7 +87,7 @@ public class GPU: Module {
}
private func infoCallback(_ value: GPUs?) {
guard value != nil else {
guard value != nil && !value!.list.isEmpty else {
return
}

View File

@@ -39,8 +39,8 @@ internal class InfoReader: Reader<GPUs> {
return false
}
let pciMatch = "0x" + Data([deviceID[1], deviceID[0], vendorID[1], vendorID[0]]).map { String(format: "%02hhX", $0) }.joined()
let accMatch = accelerator["IOPCIMatch"] as? String ?? accelerator["IOPCIPrimaryMatch"] as? String ?? ""
let pciMatch = "0x" + Data([deviceID[1], deviceID[0], vendorID[1], vendorID[0]]).map { String(format: "%02hhX", $0) }.joined().lowercased()
let accMatch = (accelerator["IOPCIMatch"] as? String ?? accelerator["IOPCIPrimaryMatch"] as? String ?? "").lowercased()
return accMatch.range(of: pciMatch) != nil
}) else { return }
@@ -53,10 +53,10 @@ internal class InfoReader: Reader<GPUs> {
return
}
guard let model = matchedGPU.object(forKey: "model") as? Data else {
guard let model = matchedGPU.object(forKey: "model") as? Data, var modelName = String(data: model, encoding: .ascii) else {
return
}
let modelName = String(data: model, encoding: .ascii)!.replacingOccurrences(of: "\0", with: "")
modelName = modelName.replacingOccurrences(of: "\0", with: "")
guard let IOClass = accelerator.object(forKey: "IOClass") as? String else {
return

View File

@@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>39</string>
<string>44</string>
<key>Description</key>
<string>Simple macOS system monitor in your menu bar</string>
<key>LSApplicationCategoryType</key>

View File

@@ -938,3 +938,17 @@ public class ColorView: NSView {
}
}
}
public struct Log: TextOutputStream {
public func write(_ string: String) {
let fm = FileManager.default
let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
if let handle = try? FileHandle(forWritingTo: log) {
handle.seekToEndOfFile()
handle.write(string.data(using: .utf8)!)
handle.closeFile()
} else {
try? string.data(using: .utf8)?.write(to: log)
}
}
}