@@ -4,7 +4,7 @@ import SwiftRex
44
55extension Middleware where StateType: Equatable {
66 public func logger(
7- actionTransform: LoggerMiddleware < Self > . ActionTransform = . default,
7+ actionTransform: LoggerMiddleware < Self > . ActionTransform = . default( ) ,
88 actionPrinter: LoggerMiddleware < Self > . ActionLogger = . osLog,
99 stateDiffTransform: LoggerMiddleware < Self > . StateDiffTransform = . diff( ) ,
1010 stateDiffPrinter: LoggerMiddleware < Self > . StateLogger = . osLog,
@@ -75,7 +75,7 @@ public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType
7575
7676extension LoggerMiddleware {
7777 public static func `default`(
78- actionTransform: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . ActionTransform = . default,
78+ actionTransform: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . ActionTransform = . default( ) ,
7979 actionPrinter: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . ActionLogger = . osLog,
8080 stateDiffTransform: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . StateDiffTransform = . diff( ) ,
8181 stateDiffPrinter: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . StateLogger = . osLog,
@@ -98,9 +98,9 @@ extension LoggerMiddleware {
9898 public enum StateLogger {
9999 case osLog
100100 case file( URL )
101- case custom( ( String ? ) -> Void )
101+ case custom( ( String ) -> Void )
102102
103- func log( state: String ? ) {
103+ func log( state: String ) {
104104 switch self {
105105 case . osLog: LoggerMiddleware . osLog ( state: state)
106106 case let . file( url) : LoggerMiddleware . fileLog ( state: state, to: url)
@@ -109,20 +109,12 @@ extension LoggerMiddleware {
109109 }
110110 }
111111
112- private static func osLog( state: String ? ) {
113- if let possibleStateChanges = state {
114- os_log ( . debug, log: . default, " %{PUBLIC}@ " , possibleStateChanges)
115- } else {
116- os_log ( . debug, log: . default, " %{PUBLIC}@ " , " 🏛 No state mutation " )
117- }
112+ private static func osLog( state: String ) {
113+ os_log ( . debug, log: . default, " %{PUBLIC}@ " , state)
118114 }
119115
120- private static func fileLog( state: String ? , to fileURL: URL ) {
121- if let possibleStateChanges = state {
122- try ? possibleStateChanges. write ( toFile: fileURL. absoluteString, atomically: false , encoding: . utf8)
123- } else {
124- try ? " 🏛 No state mutation " . write ( toFile: fileURL. absoluteString, atomically: false , encoding: . utf8)
125- }
116+ private static func fileLog( state: String , to fileURL: URL ) {
117+ try ? state. write ( toFile: fileURL. absoluteString, atomically: false , encoding: . utf8)
126118 }
127119}
128120
@@ -131,14 +123,15 @@ extension LoggerMiddleware {
131123 public enum StateDiffTransform {
132124 case diff( linesOfContext: Int = 2 , prefixLines: String = " 🏛 " )
133125 case newStateOnly
134- case custom( ( StateType ? , StateType ) -> String ? )
126+ case custom( ( StateType ? , StateType ) -> String )
135127
136- func transform( oldState: StateType ? , newState: StateType ) -> String ? {
128+ func transform( oldState: StateType ? , newState: StateType ) -> String {
137129 switch self {
138130 case let . diff( linesOfContext, prefixLines) :
139131 let stateBefore = dumpToString ( oldState)
140132 let stateAfter = dumpToString ( newState)
141133 return Difference . diff ( old: stateBefore, new: stateAfter, linesOfContext: linesOfContext, prefixLines: prefixLines)
134+ ?? " \( prefixLines) No state mutation "
142135 case . newStateOnly:
143136 return dumpToString ( newState)
144137 case let . custom( closure) :
@@ -177,15 +170,18 @@ extension LoggerMiddleware {
177170// MARK: Action Transform
178171extension LoggerMiddleware {
179172 public enum ActionTransform {
180- case `default`
173+ case `default`( actionPrefix : String = " \n 🕹 " , sourcePrefix : String = " \n 🎪 " )
181174 case actionNameOnly
182175 case custom( ( InputActionType , ActionSource ) -> String )
183176
184177 func transform( action: InputActionType , source: ActionSource ) -> String {
185178 switch self {
186- case . default: return " \n 🕹 \( action) \n 🎪 \( source. file. split ( separator: " / " ) . last ?? " " ) : \( source. line) \( source. function) "
187- case . actionNameOnly: return " \( action) "
188- case let . custom( closure) : return closure ( action, source)
179+ case let . default( actionPrefix, sourcePrefix) :
180+ return " \( actionPrefix) \( action) \( sourcePrefix) \( source. file. split ( separator: " / " ) . last ?? " " ) : \( source. line) \( source. function) "
181+ case . actionNameOnly:
182+ return " \( action) "
183+ case let . custom( closure) :
184+ return closure ( action, source)
189185 }
190186 }
191187 }
0 commit comments