Compare commits

...

2 Commits

Author SHA1 Message Date
Leif f2c1a68072 Update logging and printing 2022-10-15 14:32:20 -06:00
Leif e97ac7cc5e Add SwiftUILogger 2022-10-15 14:17:14 -06:00
2 changed files with 62 additions and 5 deletions

View File

@ -8,7 +8,8 @@ let package = Package(
platforms: [
.iOS(.v14),
.macOS(.v11),
.watchOS(.v7)
.watchOS(.v7),
.tvOS(.v14)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
@ -19,14 +20,18 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(
.package(
url: "https://github.com/0xOpenBytes/c",
from: "1.1.1"
),
.package(
.package(
url: "https://github.com/0xLeif/SwiftUILogger",
from: "0.1.0"
),
.package(
url: "https://github.com/0xLeif/swift-custom-dump",
from: "0.4.1"
)
)
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@ -35,6 +40,7 @@ let package = Package(
name: "CacheStore",
dependencies: [
"c",
"SwiftUILogger",
.product(name: "CustomDump", package: "swift-custom-dump")
]
),

View File

@ -1,6 +1,28 @@
import Combine
import CustomDump
import SwiftUI
import SwiftUILogger
private enum Logger {
static let app = SwiftUILogger(name: "App")
}
public extension SwiftUILogger {
static var app: SwiftUILogger { Logger.app }
}
class StoreLogger: SwiftUILogger {
override func log(
level: SwiftUILogger.Level,
message: String,
error: Error? = nil,
_ file: StaticString = #fileID,
_ line: Int = #line
) {
SwiftUILogger.app.log(level: level, message: message, error: error, file, line)
super.log(level: level, message: message, error: error, file, line)
}
}
// MARK: -
@ -10,6 +32,7 @@ open class Store<Key: Hashable, Action, Dependency>: ObservableObject, ActionHan
private var isDebugging: Bool
private var cacheStoreObserver: AnyCancellable?
private var effects: [AnyHashable: Task<(), Never>]
private lazy var logger: StoreLogger = StoreLogger(name: Unmanaged.passUnretained(self).toOpaque().debugDescription)
var cacheStore: CacheStore<Key>
var actionHandler: StoreActionHandler<Key, Action, Dependency>
@ -276,6 +299,7 @@ extension Store {
func send(_ action: Action) -> ActionEffect<Action>? {
if isDebugging {
logger.info(message: "New Action: \(customDump(action)) \(debugIdentifier)")
print("[\(formattedDate)] 🟡 New Action: \(customDump(action)) \(debugIdentifier)")
}
@ -297,17 +321,27 @@ extension Store {
}
if isDebugging {
logger.success(
message: "Handled Action: \(customDump(action)) \(debugIdentifier)"
)
print(
"""
[\(formattedDate)] 📣 Handled Action: \(customDump(action)) \(debugIdentifier)
--------------- State Output ------------
"""
)
if cacheStore.isCacheEqual(to: cacheStoreCopy) {
logger.log(
level: .info,
message: "No State Change"
)
print("\t🙅 No State Change")
} else {
if let diff = diff(cacheStore.cache, cacheStoreCopy.cache) {
logger.warning(
message: "State Changed"
)
logger.info(message: diff)
print(
"""
\t State Changed
@ -315,6 +349,20 @@ extension Store {
"""
)
} else {
logger.warning(
message: "State Changed"
)
logger.info(
message: """
--- Was ---
\(debuggingStateDelta(forUpdatedStore: cacheStore))
-----------
***********
--- Now ---
\(debuggingStateDelta(forUpdatedStore: cacheStoreCopy))
-----------
"""
)
print(
"""
\t State Changed
@ -330,6 +378,9 @@ extension Store {
}
}
logger.success(
message: "End Action: \(customDump(action)) \(debugIdentifier)"
)
print(
"""
--------------- State End ---------------