Skip to content

Commit

Permalink
Autoformat
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKeanice committed Nov 11, 2024
1 parent 9b552d8 commit 682353a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
2 changes: 1 addition & 1 deletion openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
textField.delegate = self
textField.keyboardType = .numbersAndPunctuation
}
//replace expected decimal separator
// replace expected decimal separator
textExtractor = { alert.textFields?[0].text?.replacingOccurrences(of: NSLocale.current.decimalSeparator ?? "", with: ".") }
case .text:
alert.addTextField { textField in
Expand Down
149 changes: 78 additions & 71 deletions openHABWatch/Views/LogsViewer.swift
Original file line number Diff line number Diff line change
@@ -1,107 +1,114 @@
// Copyright (c) 2010-2024 Contributors to the openHAB project
//
// LogView.swift
// openHABWatch
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// Created by Tim Müller-Seydlitz on 31.10.24.
// Copyright © 2024 openHAB e.V. All rights reserved.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0

import Foundation
import OSLog
import SwiftUI

// Thanks to https://useyourloaf.com/blog/fetching-oslog-messages-in-swift/

extension OSLogEntryLog.Level {
fileprivate var description: String {
switch self {
case .undefined: "undefined"
case .debug: "debug"
case .info: "info"
case .notice: "notice"
case .error: "error"
case .fault: "fault"
@unknown default: "default"
private extension OSLogEntryLog.Level {
var description: String {
switch self {
case .undefined: "undefined"
case .debug: "debug"
case .info: "info"
case .notice: "notice"
case .error: "error"
case .fault: "fault"
@unknown default: "default"
}
}
}
}

extension Logger {
static public func fetch(since date: Date,
predicateFormat: String) async throws -> [String] {
let store = try OSLogStore(scope: .currentProcessIdentifier)
let position = store.position(date: date)
let predicate = NSPredicate(format: predicateFormat)
let entries = try store.getEntries(
at: position,
matching: predicate
)

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

var logs: [String] = []
for entry in entries {
try Task.checkCancellation()
if let log = entry as? OSLogEntryLog {
var attributedMessage = AttributedString(dateFormatter.string(from: entry.date))
attributedMessage.font = .headline

logs.append("""
\(dateFormatter.string(from: entry.date)): \
\(log.category):\(log.level.description): \
\(entry.composedMessage)\n
""")
} else {
logs.append("\(entry.date): \(entry.composedMessage)\n")
}
public extension Logger {
static func fetch(since date: Date,
predicateFormat: String) async throws -> [String] {
let store = try OSLogStore(scope: .currentProcessIdentifier)
let position = store.position(date: date)
let predicate = NSPredicate(format: predicateFormat)
let entries = try store.getEntries(
at: position,
matching: predicate
)

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

var logs: [String] = []
for entry in entries {
try Task.checkCancellation()
if let log = entry as? OSLogEntryLog {
var attributedMessage = AttributedString(dateFormatter.string(from: entry.date))
attributedMessage.font = .headline

logs.append("""
\(dateFormatter.string(from: entry.date)): \
\(log.category):\(log.level.description): \
\(entry.composedMessage)\n
""")
} else {
logs.append("\(entry.date): \(entry.composedMessage)\n")
}
}

if logs.isEmpty { logs = ["Nothing found"] }
return logs
}

if logs.isEmpty { logs = ["Nothing found"] }
return logs
}
}

struct LogsViewer: View {
@State private var text = "Loading..."
static private let template = NSPredicate(format:
"(subsystem BEGINSWITH $PREFIX)")

private static let template = NSPredicate(format:
"(subsystem BEGINSWITH $PREFIX)")

let myFont = Font
.system(size: 10)
.monospaced()
.system(size: 10)
.monospaced()

private func fetchLogs() async -> String {
let calendar = Calendar.current
guard let dayAgo = calendar.date(byAdding: .day,
value: -1, to: Date.now) else {
return "Invalid calendar"
guard let dayAgo = calendar.date(
byAdding: .day,
value: -1,
to: Date.now
) else {
return "Invalid calendar"
}

do {
let predicate = Self.template.withSubstitutionVariables(
[
"PREFIX": "org.openhab"
])
[
"PREFIX": "org.openhab"
])

let logs = try await Logger.fetch(since: dayAgo,
predicateFormat: predicate.predicateFormat)
return logs.joined()
let logs = try await Logger.fetch(
since: dayAgo,
predicateFormat: predicate.predicateFormat
)
return logs.joined()
} catch {
return error.localizedDescription
return error.localizedDescription
}
}
}

var body: some View {

ScrollView {
Text(text)
.font(myFont)
.padding()
Text(text)
.font(myFont)
.padding()
}
.task {
text = await fetchLogs()
text = await fetchLogs()
}
}
}
Expand Down

0 comments on commit 682353a

Please sign in to comment.