add logging

This commit is contained in:
2026-01-20 02:58:07 +01:00
parent 2cda6c8608
commit e52a5ff08d
2 changed files with 30 additions and 13 deletions

View File

@@ -0,0 +1,8 @@
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object Logger {
fun log(message: Any) {
println("[${LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-DD-HH:mm:ss"))}]: $message")
}
}

View File

@@ -63,11 +63,11 @@ val bullshit = listOf<String>(
"You should talk about this to a medical professional, but for now here is what I think: ",
"You kick Sc0tt? You kick his body like the Football? Jail for User! Jail for User for one thousand years! ",
"You're on the right track! ",
"You've almost got it! ",
"That is almost correct! ",
"Here are my 2 cents on this topic: ",
"I will piss on the moon. ",
"Here is what I think: ",
"You've almost got it!",
"That is almost correct!",
"Here are my 2 cents on this topic:",
"I will piss on the moon.",
"Here is what I think:",
)
object ConversationHandler {
@@ -165,7 +165,7 @@ class Message(val user: Boolean, messagetext: String)
val text: String
init {
text = if (!user) bullshit[Random.nextInt(0..bullshit.size)] + messagetext
text = if (!user) bullshit[Random.nextInt(0..bullshit.size)].trim(' ') + ' ' + messagetext
else messagetext
}
@@ -181,7 +181,7 @@ class Message(val user: Boolean, messagetext: String)
}
fun main(args: Array<String>) {
println("HTTP kiszolgáló a(z) 127.0.0.1:$httpPort címen")
Logger.log("HTTP server started on 127.0.0.1:$httpPort")
runEmbeddedServer()
}
@@ -207,13 +207,16 @@ fun runEmbeddedServer()
route("/admin")
{
get("/api/all_messages") {
Logger.log("Admin login")
call.respondText(ConversationHandler.listAllConversations())
}
get("/api/all_messages/{id}") {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also{ call.respondText(it?.sendAll(true) ?: "") }
}.also{
call.respondText(it?.sendAll(true) ?: "")
}
}
put("/api/write/{id}")
@@ -225,15 +228,18 @@ fun runEmbeddedServer()
call.respond(HttpStatusCode.OK)
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
} catch (e: Exception) {
Logger.log("Error: ${e.message}")
call.respond(HttpStatusCode.NotFound)
}
}
}
}
staticFiles("/admin", File(resourcesPath+"/admin.html"))
staticFiles("/resources", File(resourcesPath))
staticFiles("/", File(resourcesPath+"/index.html"))
staticFiles("/admin", File(resourcesPath+"/admin.html"))
staticFiles("/about", File(resourcesPath+"/about.html"))
put("/api/write/{id}")
@@ -245,7 +251,10 @@ fun runEmbeddedServer()
call.respond(HttpStatusCode.OK)
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
} catch (e: Exception) {
call.respond(HttpStatusCode.NotFound)
Logger.log("Error: ${e.message}")
}
}
@@ -257,7 +266,7 @@ fun runEmbeddedServer()
get("/api/init") {
ConversationHandler.newConv()
println(ConversationHandler.conversations.last().ID)
Logger.log("Conversation ${ConversationHandler.conversations.last().ID} initialized")
call.respondText(ConversationHandler.conversations.last().ID.toString())
}
@@ -265,7 +274,7 @@ fun runEmbeddedServer()
if (ConversationHandler.conversations.filter { it.ID == call.parameters["id"]?.toInt()}.size != 1)
{
ConversationHandler.newConv(call.parameters["id"]?.toInt() ?: -1)
println(ConversationHandler.conversations.last().ID)
Logger.log("Conversation #${ConversationHandler.conversations.last().ID} reinitialised")
}
call.respond(HttpStatusCode.OK)