Skip to content

Commit

Permalink
Limit event log item count
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Apr 2, 2024
1 parent c553b5f commit 851f131
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions app/src/main/java/net/pipe01/pinepartner/scripting/Runner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.mozilla.javascript.NativeConsole
import org.mozilla.javascript.ScriptableObject
import java.time.LocalDateTime
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger

data class ScriptDependencies(
val db: AppDatabase,
Expand All @@ -61,7 +60,7 @@ class Runner(val plugin: Plugin, deps: ScriptDependencies) {

private val _hasStarted = AtomicBoolean(false)

private var eventCounter = AtomicInteger()
private var eventCounter = 0
private val _events = mutableListOf<LogEvent>()
val events get() = _events.toList()

Expand Down Expand Up @@ -112,17 +111,21 @@ class Runner(val plugin: Plugin, deps: ScriptDependencies) {
}

private fun addEvent(severity: EventSeverity, message: String, stackTrace: List<StackTraceEntry>?) {
//TODO: Limit number of events stored
//TODO: Maybe lock this whole thing to ensure the _events list is ordered correctly
_events.add(
LogEvent(
index = eventCounter.getAndIncrement(),
severity = severity,
message = message,
stackTrace = stackTrace,
time = LocalDateTime.now(),
synchronized(_events) {
while (_events.size > 100) {
_events.removeFirst()
}

_events.add(
LogEvent(
index = eventCounter++,
severity = severity,
message = message,
stackTrace = stackTrace,
time = LocalDateTime.now(),
)
)
)
}
}

fun start() {
Expand Down

0 comments on commit 851f131

Please sign in to comment.