Skip to content

Commit f8beaff

Browse files
committed
a start to log contexts
1 parent 1f2a1a4 commit f8beaff

File tree

6 files changed

+134
-31
lines changed

6 files changed

+134
-31
lines changed

src/main/kotlin/com/lambda/interaction/request/DebugLogger.kt

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import com.lambda.Lambda.mc
2121
import com.lambda.gui.LambdaScreen
2222
import com.lambda.gui.dsl.ImGuiBuilder
2323
import com.lambda.module.hud.ManagerDebugLoggers.maxLogEntries
24+
import com.lambda.util.math.a
2425
import imgui.ImGui
26+
import imgui.flag.ImGuiCol
2527
import imgui.flag.ImGuiWindowFlags
2628
import imgui.type.ImBoolean
2729
import imgui.type.ImFloat
30+
import net.minecraft.util.math.BlockPos
2831
import java.awt.Color
2932
import java.util.*
3033

@@ -42,18 +45,18 @@ class DebugLogger(
4245

4346
val logs = LinkedList<LogEntry>()
4447

45-
private fun log(message: String, logColor: LogType) {
46-
logs.add(LogEntry(message, logColor))
47-
if (logs.size > maxLogEntries) {
48+
private fun log(message: String, logColor: LogType, vararg extraContext: String?) {
49+
if (logs.size + 1 > maxLogEntries) {
4850
logs.removeFirst()
4951
}
52+
logs.add(LogEntry(message, logColor, *extraContext))
5053
}
5154

52-
fun debug(message: String) = log(message, LogType.Debug)
53-
fun success(message: String) = log(message, LogType.Success)
54-
fun warning(message: String) = log(message, LogType.Warning)
55-
fun error(message: String) = log(message, LogType.Error)
56-
fun system(message: String) = log(message, LogType.System)
55+
fun debug(message: String, vararg extraContext: String?) = log(message, LogType.Debug, *extraContext)
56+
fun success(message: String, vararg extraContext: String?) = log(message, LogType.Success, *extraContext)
57+
fun warning(message: String, vararg extraContext: String?) = log(message, LogType.Warning, *extraContext)
58+
fun error(message: String, vararg extraContext: String?) = log(message, LogType.Error, *extraContext)
59+
fun system(message: String, vararg extraContext: String?) = log(message, LogType.System, *extraContext)
5760

5861
fun ImGuiBuilder.buildLayout() {
5962
ImGui.setNextWindowSizeConstraints(300f, 400f, windowViewport.workSizeX, windowViewport.workSizeY)
@@ -80,16 +83,30 @@ class DebugLogger(
8083

8184
logs.forEach { logEntry ->
8285
if (shouldDisplay(logEntry)) {
83-
when (val type = logEntry.type) {
84-
LogType.Debug -> textColored("[DEBUG]", type.color)
85-
LogType.Success -> textColored("[SUCCESS]", type.color)
86-
LogType.Warning -> textColored("[WARNING]", type.color)
87-
LogType.Error -> textColored("[ERROR]", type.color)
88-
LogType.System -> textColored("[SYSTEM]", type.color)
86+
val type = logEntry.type
87+
val (logTypeStr, color) = when (type) {
88+
LogType.Debug -> Pair("[DEBUG]", type.color)
89+
LogType.Success -> Pair("[SUCCESS]", type.color)
90+
LogType.Warning -> Pair("[WARNING]", type.color)
91+
LogType.Error -> Pair("[ERROR]", type.color)
92+
LogType.System -> Pair("[SYSTEM]", type.color)
8993
}
9094

91-
sameLine()
92-
textColored(logEntry.message, logEntry.type.color)
95+
val floats = floatArrayOf(0f, 0f, 0f)
96+
val (r, g, b) = color.getColorComponents(floats)
97+
ImGui.pushStyleColor(ImGuiCol.Text, r, g, b, color.a.toFloat())
98+
if (logEntry.type == LogType.System) {
99+
text("$logTypeStr ${logEntry.message}")
100+
} else {
101+
treeNode("$logTypeStr ${logEntry.message}", logEntry.uuid) {
102+
logEntry.extraContext
103+
.filterNotNull()
104+
.forEach {
105+
text(it)
106+
}
107+
}
108+
}
109+
ImGui.popStyleColor()
93110
}
94111
}
95112

@@ -113,16 +130,25 @@ class DebugLogger(
113130

114131
fun clear() = logs.clear()
115132

116-
data class LogEntry(
133+
class LogEntry(
117134
val message: String,
118-
val type: LogType
119-
)
135+
val type: LogType,
136+
vararg val extraContext: String?
137+
) {
138+
val uuid = UUID.randomUUID().toString()
139+
companion object {
140+
fun BlockPos.toLogContext(): String {
141+
val pos = if (this is BlockPos.Mutable) toImmutable() else this
142+
return "Pos: ${pos.toShortString()}"
143+
}
144+
}
145+
}
120146

121147
enum class LogType(val color: Color) {
122-
Debug(Color(255, 255, 255)),
123-
Success(Color(70, 255, 70)),
124-
Warning(Color(255, 255, 70)),
125-
Error(Color(255, 70, 70)),
126-
System(Color(70, 70, 255))
148+
Debug(Color(1.0f, 1.0f, 1.0f, 1f)),
149+
Success(Color(0.28f, 1.0f, 0.28f, 1f)),
150+
Warning(Color(1.0f, 1.0f, 0.28f, 1f)),
151+
Error(Color(1.0f, 0.28f, 0.28f, 1f)),
152+
System(Color(0.28f, 0.28f, 1.0f, 1f))
127153
}
128154
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.interaction.request
19+
20+
interface LogContext {
21+
fun toLogContext(): String
22+
23+
companion object {
24+
@DslMarker
25+
private annotation class LogContextDsl
26+
27+
@LogContextDsl
28+
fun buildLogContext(builder: LogContextBuilder.() -> Unit): String =
29+
LogContextBuilder().apply(builder).build()
30+
31+
private fun LogContextBuilder.build() = logContext
32+
33+
class LogContextBuilder {
34+
var logContext = ""
35+
36+
private var tabs = 0
37+
38+
@LogContextDsl
39+
fun sameLine() =
40+
logContext.replace("\n", " ")
41+
42+
@LogContextDsl
43+
fun text(text: String) {
44+
repeat(tabs) {
45+
logContext += "\t"
46+
}
47+
logContext += "$text\n"
48+
}
49+
50+
@LogContextDsl
51+
fun pushTab() {
52+
tabs++
53+
}
54+
55+
@LogContextDsl
56+
fun popTab() {
57+
tabs--
58+
}
59+
}
60+
}
61+
}

src/main/kotlin/com/lambda/interaction/request/ManagerUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ object ManagerUtils {
3030
system("------------- New Tick -------------")
3131

3232
fun DebugLogger.newStage(tickStage: Event?) =
33-
system("Tick stage ${tickStage?.run { this::class.qualifiedName }}")
33+
system("Tick stage ${tickStage?.run { this.toLogContext() }}")
34+
35+
fun Event.toLogContext() = this::class.qualifiedName?.substringAfter("com.lambda.event.events.")
3436

3537
fun isPosBlocked(pos: BlockPos) =
3638
positionBlockingManagers.any { manager -> manager.blockedPositions.any { blocked -> blocked == pos } }

src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ object BreakManager : RequestHandler<BreakRequest>(
465465
}
466466
logger.debug("Submitting request for hotbar index ${info.context.hotbarIndex} with min swap ticks $minSwapTicks (${hotbarRequest.requestID})")
467467
if (!hotbarRequest.done) {
468-
logger.warning("hotbar request failed (${hotbarRequest.requestID})")
468+
logger.warning("Hotbar request failed (${hotbarRequest.requestID})")
469469
return false
470470
}
471471
if (minSwapTicks > 0) {

src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(
7474
}
7575

7676
override fun SafeContext.handleRequest(request: HotbarRequest) {
77-
logger.debug("Handling request ${request.requestID}")
77+
logger.debug("Handling request:", request.toLogContext())
7878

7979
maxSwapsThisTick = request.swapsPerTick
8080
swapDelay = swapDelay.coerceAtMost(request.swapDelay)
@@ -86,8 +86,8 @@ object HotbarManager : RequestHandler<HotbarRequest>(
8686
} == true
8787

8888
if (sameButLonger) activeRequest?.let { current ->
89-
logger.debug("Request is the same as current, but longer or the same keep time")
9089
request.swapPauseAge = current.swapPauseAge
90+
logger.debug("Request is the same as current, but longer or the same keep time", request.toLogContext())
9191
} else run swap@{
9292
if (request.slot != activeRequest?.slot) {
9393
if (swapsThisTick + 1 > maxSwapsThisTick || swapDelay > 0) return
@@ -108,7 +108,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(
108108
}
109109

110110
activeRequest = request
111-
logger.success("Set active request to ${request.requestID}")
111+
logger.success("Set active request", request.toLogContext())
112112
interaction.syncSelectedSlot()
113113
return
114114
}
@@ -117,7 +117,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(
117117
activeRequest?.let { active ->
118118
val canStopSwap = swapsThisTick < maxSwapsThisTick
119119
if (active.keepTicks <= 0 && tickStage in active.sequenceStageMask && canStopSwap) {
120-
logger.debug("Clearing request and syncing slot")
120+
logger.debug("Clearing request and syncing slot", activeRequest?.toLogContext())
121121
activeRequest = null
122122
interaction.syncSelectedSlot()
123123
}

src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarRequest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
package com.lambda.interaction.request.hotbar
1919

20+
import com.lambda.interaction.request.LogContext
21+
import com.lambda.interaction.request.LogContext.Companion.buildLogContext
2022
import com.lambda.interaction.request.Request
2123

2224
class HotbarRequest(
2325
val slot: Int,
2426
override val config: HotbarConfig,
2527
override var keepTicks: Int = config.keepTicks,
2628
override var swapPause: Int = config.swapPause
27-
) : Request(), HotbarConfig by config {
29+
) : Request(), HotbarConfig by config, LogContext {
2830
override val requestID = ++requestCount
2931

3032
var activeRequestAge = 0
@@ -40,6 +42,18 @@ class HotbarRequest(
4042
override fun submit(queueIfClosed: Boolean) =
4143
HotbarManager.request(this, queueIfClosed)
4244

45+
override fun toLogContext() =
46+
buildLogContext {
47+
text("Hotbar Request")
48+
pushTab()
49+
text("Request ID: $requestID")
50+
text("Slot: $slot")
51+
text("Keep Ticks: $keepTicks")
52+
text("Swap Pause: $swapPause")
53+
text("Swap Pause Age: $swapPauseAge")
54+
text("Active Request Age: $activeRequestAge")
55+
}
56+
4357
companion object {
4458
var requestCount = 0
4559
}

0 commit comments

Comments
 (0)