-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
189 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
Sources/AppcuesKit/Presentation/Debugger/DebugLogger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// DebugLogger.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2023-10-25. | ||
// Copyright © 2023 Appcues. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 13.0, *) | ||
internal class DebugLogger: ObservableObject, Logging { | ||
let previousLogger: Logging? | ||
|
||
@Published var log: [Log] = [] | ||
|
||
init(previousLogger: Logging?) { | ||
self.previousLogger = previousLogger | ||
} | ||
|
||
func debug(_ message: StaticString, _ args: CVarArg...) { | ||
previousLogger?.debug(message, args) | ||
log(message, type: .debug, args) | ||
} | ||
|
||
func info(_ message: StaticString, _ args: CVarArg...) { | ||
previousLogger?.info(message, args) | ||
log(message, type: .info, args) | ||
} | ||
|
||
func log(_ message: StaticString, _ args: CVarArg...) { | ||
previousLogger?.log(message, args) | ||
log(message, type: .log, args) | ||
} | ||
|
||
func error(_ message: StaticString, _ args: CVarArg...) { | ||
previousLogger?.error(message, args) | ||
log(message, type: .error, args) | ||
} | ||
|
||
func fault(_ message: StaticString, _ args: CVarArg...) { | ||
previousLogger?.fault(message, args) | ||
log(message, type: .fault, args) | ||
} | ||
|
||
private func log(_ message: StaticString, type: Level, _ args: [CVarArg]) { | ||
guard Thread.isMainThread else { | ||
DispatchQueue.main.async { self.log(message, type: type, args) } | ||
return | ||
} | ||
|
||
// Convert the os_log StaticString to a normal format String | ||
let normalizedMessage = message.description | ||
.replacingOccurrences(of: "{public}", with: "") | ||
.replacingOccurrences(of: "{private}", with: "") | ||
let item = Log( | ||
level: type, | ||
message: String(format: normalizedMessage, args) | ||
) | ||
|
||
log.append(item) | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
extension DebugLogger { | ||
struct Log: Identifiable { | ||
let id = UUID() | ||
let timestamp = Date() | ||
let level: Level | ||
let message: String | ||
} | ||
|
||
enum Level: CaseIterable { | ||
case debug, info, log, error, fault | ||
|
||
var description: String { | ||
switch self { | ||
case .debug: return "Debug" | ||
case .info: return "Info" | ||
case .log: return "Log" | ||
case .error: return "Error" | ||
case .fault: return "Fault" | ||
} | ||
} | ||
|
||
var color: Color { | ||
switch self { | ||
case .debug, .info: return .secondary | ||
case .log: return .primary | ||
case .error, .fault: return .red | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Sources/AppcuesKit/Presentation/Debugger/Panel/DebugLogUI.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// DebugLogUI.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2023-10-25. | ||
// Copyright © 2023 Appcues. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 13.0, *) | ||
internal enum DebugLogUI { | ||
struct LoggerView: View { | ||
@EnvironmentObject var logger: DebugLogger | ||
|
||
var body: some View { | ||
List { | ||
ForEach(logger.log.suffix(20).reversed()) { log in | ||
NavigationLink(destination: DetailView(log: log)) { | ||
VStack(alignment: .leading) { | ||
Text("\(log.level.description): \(log.timestamp.description)") | ||
.fontWeight(.bold) | ||
Text(log.message) | ||
.lineLimit(10) | ||
} | ||
.font(.system(size: 14, design: .monospaced)) | ||
.foregroundColor(log.level.color) | ||
} | ||
} | ||
} | ||
.navigationBarTitle("", displayMode: .inline) | ||
} | ||
} | ||
|
||
private struct DetailView: View { | ||
let log: DebugLogger.Log | ||
|
||
var body: some View { | ||
ScrollView { | ||
VStack(alignment: .leading) { | ||
Text("Level: \(log.level.description)") | ||
.fontWeight(.bold) | ||
Text("Timestamp: \(log.timestamp.description)") | ||
.fontWeight(.bold) | ||
Divider() | ||
Text(log.message) | ||
} | ||
.font(.system(size: 14, design: .monospaced)) | ||
.foregroundColor(log.level.color) | ||
.padding() | ||
} | ||
.navigationBarTitle("", displayMode: .inline) | ||
.navigationBarItems(trailing: Button("Copy Log") { | ||
UIPasteboard.general.string = log.message | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters