Skip to content

Commit 3f320a3

Browse files
committed
new better logs version
1 parent 2ef1015 commit 3f320a3

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

Diff for: commandbasedarchitecture_coroutine/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ group = "io.scal"
2525
final def siteUrl = 'https://github.com/scalio/command-based-architecture'
2626
final def gitUrl = "https://github.com/scalio/command-based-architecture.git"
2727
// This is the library version used when deploying the artifact
28-
version = "0.5.0"
28+
version = "0.5.1"
2929

3030
final Properties properties = new Properties()
3131
properties.load(project.rootProject.file('local.properties').newDataInputStream())

Diff for: commandbasedarchitecture_coroutine/src/main/java/io/scal/commandbasedarchitecture/CommandManager.kt

+22-17
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ interface CommandManager<State> {
4545
class CommandManagerImpl<State>(
4646
private val dataState: MutableLiveData<State>,
4747
private val coroutineScope: CoroutineScope,
48-
private val loggerCallback: ((message: String) -> Unit)? = null
48+
private val infoLoggerCallback: ((message: String) -> Unit)? = null,
49+
private val errorLoggerCallback: ((message: String, error: Throwable) -> Unit)? = null
4950
) : CommandManager<State> {
5051

5152
private val activated = AtomicBoolean(true)
@@ -62,19 +63,19 @@ class CommandManagerImpl<State>(
6263
override fun clearPendingCommands(clearRule: (ActionCommand<*, State>) -> Boolean) {
6364
val wasCommands = pendingActionCommands.size
6465
pendingActionCommands.removeAll(clearRule)
65-
logMessage("Clear: was - $wasCommands, now - ${pendingActionCommands.size}")
66+
logInfoMessage("Clear: was - $wasCommands, now - ${pendingActionCommands.size}")
6667
}
6768

6869
@MainThread
6970
override fun blockExecutions() {
7071
activated.set(false)
71-
logMessage("Execution: BLOCKED")
72+
logInfoMessage("Execution: BLOCKED")
7273
}
7374

7475
@MainThread
7576
override fun allowExecutions() {
7677
activated.set(true)
77-
logMessage("Execution: ALLOWED")
78+
logInfoMessage("Execution: ALLOWED")
7879

7980
runPendingActions()
8081
}
@@ -87,21 +88,21 @@ class CommandManagerImpl<State>(
8788
runningActionCommands.toList()
8889
)
8990
) {
90-
logMessage("Adding: ADDED to the queue - $actionCommand")
91+
logInfoMessage("Adding: ADDED to the queue - $actionCommand")
9192

9293
pendingActionCommands.add(actionCommand)
9394

9495
runPendingActions()
9596
} else {
96-
logMessage("Adding: SKIPPED from the queue - $actionCommand")
97+
logInfoMessage("Adding: SKIPPED from the queue - $actionCommand")
9798
}
9899
}
99100

100101
private fun runPendingActions() {
101102
if (!activated.get()) return
102103

103104
pendingActionCommands.forEach {
104-
logMessage("Run: onCommandWasAdded for $it")
105+
logInfoMessage("Run: onCommandWasAdded for $it")
105106

106107
dataState.setValueIfNotTheSame(
107108
it.onCommandWasAdded(getCurrentDataState())
@@ -110,7 +111,7 @@ class CommandManagerImpl<State>(
110111

111112
val firstCommand = pendingActionCommands.firstOrNull() ?: return
112113
if (runningActionCommands.any { it.shouldBlockOtherTask(firstCommand) }) {
113-
logMessage("Run: BLOCKED for $firstCommand")
114+
logInfoMessage("Run: BLOCKED for $firstCommand")
114115
return
115116
}
116117
if (firstCommand.shouldExecuteAction(
@@ -119,13 +120,13 @@ class CommandManagerImpl<State>(
119120
runningActionCommands.toList()
120121
)
121122
) {
122-
logMessage("Run: ALLOWED for: $firstCommand")
123+
logInfoMessage("Run: ALLOWED for: $firstCommand")
123124

124125
pendingActionCommands.remove(firstCommand)
125126
runningActionCommands.add(firstCommand)
126127
executeCommand(firstCommand)
127128
} else {
128-
logMessage("Run: POSTPONED for: $firstCommand")
129+
logInfoMessage("Run: POSTPONED for: $firstCommand")
129130
}
130131

131132
val newFirstCommand = pendingActionCommands.firstOrNull() ?: return
@@ -137,7 +138,7 @@ class CommandManagerImpl<State>(
137138
private fun <Result> executeCommand(actionCommand: ActionCommand<Result, State>) {
138139
coroutineScope
139140
.launch(Dispatchers.Main) {
140-
logMessage("Execute: STARTING - $actionCommand")
141+
logInfoMessage("Execute: STARTING - $actionCommand")
141142
try {
142143
dataState.setValueIfNotTheSame(
143144
actionCommand.onExecuteStarting(getCurrentDataState())
@@ -148,13 +149,13 @@ class CommandManagerImpl<State>(
148149
actionCommand.onExecuteSuccess(getCurrentDataState(), result)
149150
)
150151

151-
logMessage("Execute: EXECUTED - $actionCommand")
152+
logInfoMessage("Execute: EXECUTED - $actionCommand")
152153
} catch (e: Throwable) {
153154
dataState.setValueIfNotTheSame(
154155
actionCommand.onExecuteFail(getCurrentDataState(), e)
155156
)
156157

157-
logMessage("Execute: FAILED - $actionCommand, $e")
158+
logErrorMessage("Execute: FAILED - $actionCommand, $e", e)
158159
}
159160
dataState.setValueIfNotTheSame(
160161
actionCommand.onExecuteFinished(getCurrentDataState())
@@ -164,10 +165,10 @@ class CommandManagerImpl<State>(
164165
runningActionCommands.remove(actionCommand)
165166

166167
if (null == it) {
167-
logMessage("Execute: FINISHED - $actionCommand")
168+
logInfoMessage("Execute: FINISHED - $actionCommand")
168169
runPendingActions()
169170
} else {
170-
logMessage("Execute: CANCELLED - $actionCommand")
171+
logInfoMessage("Execute: CANCELLED - $actionCommand")
171172
}
172173
}
173174
}
@@ -176,8 +177,12 @@ class CommandManagerImpl<State>(
176177
private fun getCurrentDataState(): State =
177178
dataState.value as State
178179

179-
private fun logMessage(message: String) {
180-
loggerCallback?.invoke(message)
180+
private fun logInfoMessage(message: String) {
181+
infoLoggerCallback?.invoke(message)
182+
}
183+
184+
private fun logErrorMessage(message: String, e: Throwable) {
185+
errorLoggerCallback?.invoke(message, e)
181186
}
182187
}
183188

Diff for: commandbasedarchitecture_rxjava/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ group = "io.scal"
2525
final def siteUrl = 'https://github.com/scalio/command-based-architecture'
2626
final def gitUrl = "https://github.com/scalio/command-based-architecture.git"
2727
// This is the library version used when deploying the artifact
28-
version = "0.5.0"
28+
version = "0.5.1"
2929

3030
final Properties properties = new Properties()
3131
properties.load(project.rootProject.file('local.properties').newDataInputStream())

Diff for: commandbasedarchitecture_rxjava/src/main/java/io/scal/commandbasedarchitecture/CommandManager.kt

+22-17
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class CommandManagerImpl<State>(
4747
private val dataState: MutableLiveData<State>,
4848
private val mainThreadScheduler: Scheduler,
4949
private val compositeDisposable: CompositeDisposable,
50-
private val loggerCallback: ((message: String) -> Unit)? = null
50+
private val infoLoggerCallback: ((message: String) -> Unit)? = null,
51+
private val errorLoggerCallback: ((message: String, error: Throwable) -> Unit)? = null
5152
) : CommandManager<State> {
5253

5354
private val activated = AtomicBoolean(true)
@@ -64,19 +65,19 @@ class CommandManagerImpl<State>(
6465
override fun clearPendingCommands(clearRule: (ActionCommand<*, State>) -> Boolean) {
6566
val wasCommands = pendingActionCommands.size
6667
pendingActionCommands.removeAll(clearRule)
67-
logMessage("Clear: was - $wasCommands, now - ${pendingActionCommands.size}")
68+
logInfoMessage("Clear: was - $wasCommands, now - ${pendingActionCommands.size}")
6869
}
6970

7071
@MainThread
7172
override fun blockExecutions() {
7273
activated.set(false)
73-
logMessage("Execution: BLOCKED")
74+
logInfoMessage("Execution: BLOCKED")
7475
}
7576

7677
@MainThread
7778
override fun allowExecutions() {
7879
activated.set(true)
79-
logMessage("Execution: ALLOWED")
80+
logInfoMessage("Execution: ALLOWED")
8081

8182
runPendingActions()
8283
}
@@ -89,21 +90,21 @@ class CommandManagerImpl<State>(
8990
runningActionCommands.toList()
9091
)
9192
) {
92-
logMessage("Adding: ADDED to the queue - $actionCommand")
93+
logInfoMessage("Adding: ADDED to the queue - $actionCommand")
9394

9495
pendingActionCommands.add(actionCommand)
9596

9697
runPendingActions()
9798
} else {
98-
logMessage("Adding: SKIPPED from the queue - $actionCommand")
99+
logInfoMessage("Adding: SKIPPED from the queue - $actionCommand")
99100
}
100101
}
101102

102103
private fun runPendingActions() {
103104
if (!activated.get()) return
104105

105106
pendingActionCommands.forEach {
106-
logMessage("Run: onCommandWasAdded for $it")
107+
logInfoMessage("Run: onCommandWasAdded for $it")
107108

108109
dataState.setValueIfNotTheSame(
109110
it.onCommandWasAdded(getCurrentDataState())
@@ -112,7 +113,7 @@ class CommandManagerImpl<State>(
112113

113114
val firstCommand = pendingActionCommands.firstOrNull() ?: return
114115
if (runningActionCommands.any { it.shouldBlockOtherTask(firstCommand) }) {
115-
logMessage("Run: BLOCKED for $firstCommand")
116+
logInfoMessage("Run: BLOCKED for $firstCommand")
116117
return
117118
}
118119
if (firstCommand.shouldExecuteAction(
@@ -121,13 +122,13 @@ class CommandManagerImpl<State>(
121122
runningActionCommands.toList()
122123
)
123124
) {
124-
logMessage("Run: ALLOWED for: $firstCommand")
125+
logInfoMessage("Run: ALLOWED for: $firstCommand")
125126

126127
pendingActionCommands.remove(firstCommand)
127128
runningActionCommands.add(firstCommand)
128129
executeCommand(firstCommand)
129130
} else {
130-
logMessage("Run: POSTPONED for: $firstCommand")
131+
logInfoMessage("Run: POSTPONED for: $firstCommand")
131132
}
132133

133134
val newFirstCommand = pendingActionCommands.firstOrNull() ?: return
@@ -139,7 +140,7 @@ class CommandManagerImpl<State>(
139140
private fun <Result> executeCommand(actionCommand: ActionCommand<Result, State>) {
140141
val disposable = Completable
141142
.fromAction {
142-
logMessage("Execute: STARTING - $actionCommand")
143+
logInfoMessage("Execute: STARTING - $actionCommand")
143144

144145
dataState.setValueIfNotTheSame(
145146
actionCommand.onExecuteStarting(getCurrentDataState())
@@ -154,13 +155,13 @@ class CommandManagerImpl<State>(
154155
)
155156
.observeOn(mainThreadScheduler)
156157
.doOnDispose {
157-
logMessage("Execute: CANCELLED - $actionCommand")
158+
logInfoMessage("Execute: CANCELLED - $actionCommand")
158159

159160
runningActionCommands.remove(actionCommand)
160161
}
161162
.subscribe(
162163
{
163-
logMessage("Execute: EXECUTED - $actionCommand")
164+
logInfoMessage("Execute: EXECUTED - $actionCommand")
164165

165166
dataState.setValueIfNotTheSame(
166167
actionCommand.onExecuteSuccess(getCurrentDataState(), it)
@@ -173,7 +174,7 @@ class CommandManagerImpl<State>(
173174
actionCommand.onExecuteFail(getCurrentDataState(), it)
174175
)
175176

176-
logMessage("Execute: FAILED - $actionCommand, $it")
177+
logErrorMessage("Execute: FAILED - $actionCommand, $it", it)
177178

178179
onRunningTaskFinished(actionCommand)
179180
}
@@ -187,7 +188,7 @@ class CommandManagerImpl<State>(
187188
)
188189
runningActionCommands.remove(actionCommand)
189190

190-
logMessage("Execute: FINISHED - $actionCommand")
191+
logInfoMessage("Execute: FINISHED - $actionCommand")
191192

192193
runPendingActions()
193194
}
@@ -196,8 +197,12 @@ class CommandManagerImpl<State>(
196197
private fun getCurrentDataState(): State =
197198
dataState.value as State
198199

199-
private fun logMessage(message: String) {
200-
loggerCallback?.invoke(message)
200+
private fun logInfoMessage(message: String) {
201+
infoLoggerCallback?.invoke(message)
202+
}
203+
204+
private fun logErrorMessage(message: String, e: Throwable) {
205+
errorLoggerCallback?.invoke(message, e)
201206
}
202207
}
203208

Diff for: sample_coroutine/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ dependencies {
3838
implementation fileTree(dir: 'libs', include: ['*.jar'])
3939

4040
// implementation project(path: ':commandbasedarchitecture_coroutine')
41-
implementation 'io.scal:commandbasedarchitecture_coroutine:0.5.0'
41+
implementation 'io.scal:commandbasedarchitecture_coroutine:0.5.1'
4242

4343
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4444

4545
implementation 'androidx.appcompat:appcompat:1.1.0'
4646
implementation 'com.google.android.material:material:1.1.0'
47-
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta4"
47+
implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta6"
4848
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
4949

5050
implementation "androidx.core:core-ktx:1.2.0"

0 commit comments

Comments
 (0)