Added support for varying log levels configurable in the server config (see log_level in default.conf)

Error logs now properly print to stderr when possible
Colour-coded logs
Addressed a scenario in which outgoing packet writes could get hung up
Fixed interface incorrect definition message on server startup
This commit is contained in:
Ceikry
2023-03-01 08:01:14 +00:00
committed by Ryan
parent e1faa64af7
commit 88a2f354b4
171 changed files with 723 additions and 475 deletions
@@ -269,7 +269,7 @@ public class RandomFunction {
rand -= item.weight;
}
//We should get here if and only if the weighted chance table is empty.
//System.out.println("ERROR ROLLING WEIGHTED CHANCE: WEIGHT SUM AND INDIVIDUAL WEIGHTS DO NOT MATCH!!");
return null;
}
+47 -76
View File
@@ -2,11 +2,11 @@ package core.tools
import com.github.ajalt.mordant.rendering.TextColors
import com.github.ajalt.mordant.terminal.*
import com.google.protobuf.ByteString.Output
import core.ServerConstants
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.Writer
import core.api.log
import java.io.OutputStream
import java.io.PrintStream
import java.text.SimpleDateFormat
import java.util.*
@@ -17,105 +17,76 @@ import java.util.*
*/
object SystemLogger {
val t = Terminal()
val errT = t.forStdErr()
val formatter = SimpleDateFormat("HH:mm:ss")
var tradeLog: Writer? = null
var tradeLogWriter: BufferedWriter? = null
@JvmStatic
fun initTradeLogger(){
if(!File(ServerConstants.LOGS_PATH + "trade" + File.separator + "test").exists()){
File(ServerConstants.LOGS_PATH + "trade" + File.separator + "test").mkdirs()
}
tradeLog = FileWriter(ServerConstants.LOGS_PATH + "trade" + File.separator + SimpleDateFormat("dd-MM-yyyy").format(System.currentTimeMillis()) + ".log")
tradeLogWriter = BufferedWriter(tradeLog!!)
}
@JvmStatic()
fun flushLogs() {
try {
tradeLogWriter?.flush()
tradeLogWriter?.close()
} catch(ignored: Exception) {}
}
fun getTime(): String{
private fun getTime(): String{
return "[" + formatter.format(Date(System.currentTimeMillis())) +"]"
}
@JvmStatic
fun logInfo(clazz: Class<*>, vararg messages: String){
for(m in messages){
val msg = "${getTime()}: [${clazz.simpleName}] $m"
if(m.isNotBlank()) {
fun processLogEntry(clazz: Class<*>, log: Log, message: String) {
when (log) {
Log.FINE -> {
if (ServerConstants.LOG_LEVEL < LogLevel.VERBOSE)
return
val msg = TextColors.gray("${getTime()}: [${clazz.simpleName}] $message")
t.println(msg)
}
}
}
@JvmStatic
fun logErr(clazz: Class<*>, message: String){
val msg = "${getTime()}: [${clazz.simpleName}] $message"
if(message.isNotBlank()) {
t.println(msg)
}
}
Log.INFO -> {
if (ServerConstants.LOG_LEVEL < LogLevel.DETAILED)
return
@JvmStatic
fun logWarn(clazz: Class<*>, message: String){
val msg = "${getTime()}: [${clazz.simpleName}] $message"
if(message.isNotBlank()) {
t.println(msg)
}
}
val msg = "${getTime()}: [${clazz.simpleName}] $message"
t.println(msg)
}
@JvmStatic
fun logAlert(clazz: Class<*>, message: String){
val msg = "${getTime()}: [${clazz.simpleName}] $message"
if(message.isNotBlank()) {
t.println(msg)
}
}
Log.WARN -> {
if (ServerConstants.LOG_LEVEL < LogLevel.CAUTIOUS)
return
@JvmStatic
fun logAI(clazz: Class<*>, message: String){
val msg = "${getTime()}: [${clazz.simpleName}] $message"
if(message.isNotBlank()) {
t.println(msg)
}
}
val msg = TextColors.yellow("${getTime()}: [${clazz.simpleName}] $message")
t.println(msg)
}
@JvmStatic
fun logRE(message: String){
if(message.isNotBlank()) t.println("${getTime()}: ${TextColors.gray("[RAND] $message")}")
Log.ERR -> {
val msg = "${getTime()}: [${clazz.simpleName}] $message"
errT.println(msg)
}
}
}
@JvmStatic
fun logGE(message: String){
if(message.isNotBlank()) t.println("${getTime()}: ${TextColors.gray("[ GE] $message")}")
}
@JvmStatic
fun logTrade(message: String){
try {
if (message.isNotBlank()) {
if (tradeLogWriter == null) logWarn(this::class.java, "Trade Logger is null!")
tradeLogWriter?.write("${getTime()}: $message")
tradeLogWriter?.newLine()
}
} catch(ignored: Exception){}
log(this::class.java, Log.FINE, "[ GE] $message")
}
@JvmStatic fun logStartup(message: String)
{
logInfo(this::class.java, "[STARTUP] $message")
log(this::class.java, Log.FINE, "[STARTUP] $message")
}
@JvmStatic fun logShutdown(message: String)
{
logInfo(this::class.java,"[SHUTDOWN] $message")
log(this::class.java, Log.FINE, "[SHUTDOWN] $message")
}
fun logMS(s: String) {
if(s.isNotBlank()) t.println("${getTime()}: ${TextColors.gray("[ MS] $s")}")
log(this::class.java, Log.FINE, "[ MS] $s")
}
}
enum class LogLevel {
SILENT,
CAUTIOUS,
DETAILED,
VERBOSE
}
enum class Log {
FINE,
INFO,
WARN,
ERR
}
+2 -2
View File
@@ -51,7 +51,7 @@ public final class TimeStamp {
long current = System.currentTimeMillis();
long difference = current - interval;
if (debug || difference > 100) {
System.out.println("Interval " + info + " - time elapsed=" + difference + "ms.");
}
interval = current;
return difference;
@@ -65,7 +65,7 @@ public final class TimeStamp {
long current = System.currentTimeMillis();
long difference = current - start;
if (debug) {
System.out.println("Interval " + info + " - time elapsed=" + difference + "ms.");
}
return difference;
}
@@ -22,7 +22,7 @@ public class Results {
return result;
} catch (Exception e) {
System.out.println("Column '" + column + "' not found.");
}
return null;
@@ -38,7 +38,7 @@ public class Results {
return result;
} catch (Exception e) {
System.out.println("Column '" + column + "' not found.");
}
return -1;
@@ -60,7 +60,7 @@ public class Results {
return columns;
} catch (Exception e) {
System.out.println("Unable to gather columns.");
}
return null;
@@ -72,7 +72,7 @@ public class Results {
try {
return !set().next();
} catch (Exception e) {
System.out.println("Error occurred, while checking for results.");
}
return true;