Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleanup log output #345

Merged
merged 18 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions FreeAPS/Sources/APS/OpenAPS/JavaScriptWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@ import JavaScriptCore

private let contextLock = NSRecursiveLock()

extension String {
var lowercasingFirst: String { prefix(1).lowercased() + dropFirst() }
var uppercasingFirst: String { prefix(1).uppercased() + dropFirst() }
var camelCased: String {
guard !isEmpty else { return "" }
let parts = components(separatedBy: .alphanumerics.inverted)
let first = parts.first!.lowercasingFirst
let rest = parts.dropFirst().map(\.uppercasingFirst)
return ([first] + rest).joined()
}

var pascalCased: String {
guard !isEmpty else { return "" }
let parts = components(separatedBy: .alphanumerics.inverted)
let first = parts.first!.uppercasingFirst
let rest = parts.dropFirst().map(\.uppercasingFirst)
return ([first] + rest).joined()
}
}

final class JavaScriptWorker {
private let processQueue = DispatchQueue(label: "DispatchQueue.JavaScriptWorker")
private let virtualMachine: JSVirtualMachine
@SyncAccess(lock: contextLock) private var commonContext: JSContext? = nil
private var consoleLogs: [String] = []
private var logContext: String = ""

init() {
virtualMachine = processQueue.sync { JSVirtualMachine()! }
Expand All @@ -20,18 +42,50 @@ final class JavaScriptWorker {
}
}
let consoleLog: @convention(block) (String) -> Void = { message in
debug(.openAPS, "JavaScript log: \(message)")
let trimmedMessage = message.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmedMessage.isEmpty {
// debug(.openAPS, "JavaScript log: \(trimmedMessage)")
self.consoleLogs.append("\(trimmedMessage)")
}
}

context.setObject(
consoleLog,
forKeyedSubscript: "_consoleLog" as NSString
)
return context
}

// New method to flush aggregated logs
private func outputLogs() {
var outputLogs = consoleLogs.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
consoleLogs.removeAll()

if outputLogs.isEmpty { return }

if logContext == "prepare/autosens.js" {
outputLogs = outputLogs.split(separator: "\n").map { logLine in
logLine.replacingOccurrences(
of: "^[-+=x!]|u\\(|\\)|\\d{1,2}h$",
with: "",
options: .regularExpression
)
}.joined(separator: "\n")
}

if !outputLogs.isEmpty {
outputLogs.split(separator: "\n").forEach { logLine in
if !"\(logLine)".trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
debug(.openAPS, "\(logContext): \(logLine)")
}
}
}
}

@discardableResult func evaluate(script: Script) -> JSValue! {
evaluate(string: script.body)
logContext = script.name
let result = evaluate(string: script.body)
outputLogs()
return result
}

private func evaluate(string: String) -> JSValue! {
Expand All @@ -52,6 +106,7 @@ final class JavaScriptWorker {
commonContext = createContext()
defer {
commonContext = nil
outputLogs()
}
return execute(self)
}
Expand Down
4 changes: 2 additions & 2 deletions FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ final class OpenAPS {

private func middlewareScript(name: String) -> Script? {
if let body = storage.retrieveRaw(name) {
return Script(name: "Middleware", body: body)
return Script(name: name, body: body)
}

if let url = Foundation.Bundle.main.url(forResource: "javascript/\(name)", withExtension: "") {
return Script(name: "Middleware", body: try! String(contentsOf: url))
return Script(name: name, body: try! String(contentsOf: url))
}

return nil
Expand Down