Skip to content

Commit

Permalink
plug logging console back in
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 29, 2024
1 parent c8e7285 commit ea7fea6
Show file tree
Hide file tree
Showing 72 changed files with 240 additions and 230 deletions.
4 changes: 2 additions & 2 deletions client/src/main/java/EntropyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import screen.ScreenCache;
import util.*;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class EntropyMain implements Registry
{
Expand All @@ -23,7 +23,7 @@ public static void main(String[] args)
//Initialise interfaces etc
Debug.initialise(new DebugOutputSystemOut());
Thread.setDefaultUncaughtExceptionHandler(new LoggerUncaughtExceptionHandler());
MainUtilKt.setLoggingContextFields();
MainUtilKt.configureLogging();
AbstractClient.setInstance(new DesktopEntropyClient());

//Dev mode
Expand Down
5 changes: 1 addition & 4 deletions client/src/main/java/bean/AbstractDevScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.KeyStroke;

import util.Debug;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public abstract class AbstractDevScreen extends FocusableWindow
{
Expand Down
3 changes: 1 addition & 2 deletions client/src/main/java/bean/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;

import util.Debug;
import util.DialogUtil;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class FileUploader extends JPanel
implements ActionListener
Expand Down
6 changes: 1 addition & 5 deletions client/src/main/java/object/ReplayTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -31,7 +28,6 @@
import screen.ReplayDialog;
import screen.ReplayFilterPanel;
import screen.ScreenCache;
import util.Debug;
import util.DialogUtil;
import util.EntropyColour;
import util.FileUtil;
Expand All @@ -41,7 +37,7 @@
import util.ReplayRowWrapper;
import util.TableUtil;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class ReplayTable extends JTable
implements MouseListener,
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/object/RoomTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import util.MessageUtil;
import util.TableUtil;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public final class RoomTable extends JTable
implements MouseListener
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/online/screen/GameRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.Preferences;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

/**
* This is an actual room as seen by the player
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/online/util/DesktopEntropyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public String sendSyncOnDevice(MessageSender runnable)
@Override
public void checkForUpdates()
{
Globals.INSTANCE.getUpdateManager().checkForUpdates(OnlineConstants.ENTROPY_VERSION_NUMBER);
ClientGlobals.INSTANCE.getUpdateManager().checkForUpdates(OnlineConstants.ENTROPY_VERSION_NUMBER);
}
}
2 changes: 1 addition & 1 deletion client/src/main/java/online/util/HeartbeatRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import util.AbstractClient;
import util.MessageUtil;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class HeartbeatRunnable implements Runnable
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/online/util/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.HashMap;
import java.util.List;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class ResponseHandler implements XmlConstants
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Timer;
import java.util.*;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public abstract class GameScreen extends TransparentPanel
implements BidListener,
Expand Down
13 changes: 6 additions & 7 deletions client/src/main/java/screen/MainScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import online.screen.TestHarness;
import online.util.XmlBuilderDesktop;
import org.w3c.dom.Document;
import screen.online.PlayOnlineDialog;
import util.*;
import utils.InjectedThings;
import utils.CoreGlobals;

import javax.swing.*;
import javax.swing.border.BevelBorder;
Expand All @@ -28,7 +27,7 @@
import java.util.prefs.Preferences;

import static screen.online.PlayOnlineDialogKt.showPlayOnlineDialog;
import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;
import static utils.ThreadUtilKt.dumpThreadStacks;

public final class MainScreen extends AbstractDevScreen
Expand Down Expand Up @@ -678,10 +677,10 @@ public String processCommand(String command)
}

if (command.equals("health")) {
Globals.INSTANCE.getHealthCheckApi().doHealthCheck();
ClientGlobals.INSTANCE.getHealthCheckApi().doHealthCheck();
} else if (command.startsWith("server ")) {
var serverCommand = command.replace("server ", "");
Globals.INSTANCE.getDevApi().doServerCommand(serverCommand);
ClientGlobals.INSTANCE.getDevApi().doServerCommand(serverCommand);
} else if (command.equals("keygen")) {
var key = KeyGeneratorUtil.generateSymmetricKey();
textToShow = EncryptionUtil.convertSecretKeyToString(key);
Expand Down Expand Up @@ -922,8 +921,8 @@ else if (source == mntmReportBug)
}
else if (source == mntmViewLogs)
{
InjectedThings.loggingConsole.setVisible(true);
InjectedThings.loggingConsole.toFront();
ClientGlobals.loggingConsole.setVisible(true);
ClientGlobals.loggingConsole.toFront();
}
else if (source == mntmPreferences)
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/screen/ReplayInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.awt.event.WindowListener;
import java.io.File;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class ReplayInterface extends JFrame
implements ListSelectionListener,
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/screen/SimulationDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.HashMap;
import java.util.Vector;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class SimulationDialog extends JDialog
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/util/CpuStrategies.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Random;
import java.util.Vector;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class CpuStrategies
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/util/DialogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

@Deprecated()
public class DialogUtil
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/util/ReplayConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class ReplayConverter implements Registry
{
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/util/ReplayConverterVersion0.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import static utils.InjectedThings.logger;
import static utils.CoreGlobals.logger;

public class ReplayConverterVersion0
{
Expand Down
12 changes: 9 additions & 3 deletions client/src/main/kotlin/MainUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import logging.KEY_DEV_MODE
import logging.KEY_OPERATING_SYSTEM
import logging.KEY_USERNAME
import util.AbstractClient
import util.ClientGlobals
import util.CoreRegistry.INSTANCE_STRING_DEVICE_ID
import util.CoreRegistry.instance
import util.OnlineConstants
import utils.InjectedThings.logger
import utils.CoreGlobals
import utils.CoreGlobals.logger
import utils.getUsername

fun setLoggingContextFields() {
fun configureLogging() {
CoreGlobals.slf4jLogger.addAppender(ClientGlobals.consoleAppender)
ClientGlobals.consoleAppender.start()
logger.addContextListener(ClientGlobals.loggingConsole)

logger.addToContext(KEY_USERNAME, getUsername())
logger.addToContext(KEY_APP_VERSION, OnlineConstants.ENTROPY_VERSION_NUMBER)
logger.addToContext(KEY_OPERATING_SYSTEM, AbstractClient.operatingSystem)
logger.addToContext(KEY_DEVICE_ID, getDeviceId())
logger.addToContext(KEY_DEV_MODE, AbstractClient.devMode)
logger.addToContext(KEY_DEV_MODE, AbstractClient.devMode.toString())
}

fun getDeviceId() = instance.get(INSTANCE_STRING_DEVICE_ID, null) ?: setDeviceId()
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/kotlin/help/HelpPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter
import javax.swing.text.Highlighter
import screen.ScreenCache
import util.Registry
import utils.InjectedThings.logger
import utils.CoreGlobals.logger
import utils.getAllChildComponentsForType

/** Object representing a 'page' of the help dialog. */
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/kotlin/http/HttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kong.unirest.JsonObjectMapper
import kong.unirest.Unirest
import kong.unirest.UnirestException
import org.apache.http.HttpHeaders
import utils.InjectedThings.logger
import utils.CoreGlobals.logger

class HttpClient(private val baseUrl: String) {
private val jsonObjectMapper = JsonObjectMapper()
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/kotlin/http/SessionApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import javax.swing.JOptionPane
import javax.swing.SwingUtilities
import kong.unirest.HttpMethod
import screen.ScreenCache
import util.ClientGlobals
import util.DialogUtilNew
import util.Globals
import util.OnlineConstants

class SessionApi(private val httpClient: HttpClient) {
Expand Down Expand Up @@ -47,7 +47,7 @@ class SessionApi(private val httpClient: HttpClient) {
)

if (ans == JOptionPane.YES_OPTION) {
Globals.updateManager.checkForUpdates(
ClientGlobals.updateManager.checkForUpdates(
OnlineConstants.ENTROPY_VERSION_NUMBER
)
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/kotlin/screen/HelpDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import javax.swing.tree.TreeSelectionModel
import kotlin.math.max
import util.AchievementsUtil.UnlockAchievementTask
import util.Registry
import utils.InjectedThings.logger
import utils.CoreGlobals.logger

class HelpDialog : JFrame(), TreeSelectionListener, WindowListener, Registry {
private val fundamentalsTheDeck = FundamentalsTheDeck()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package logging
package screen

import bean.FocusableWindow
import bean.WrapLayout
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.AppenderBase
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
Expand All @@ -18,9 +23,22 @@ import javax.swing.text.BadLocationException
import javax.swing.text.DefaultStyledDocument
import javax.swing.text.StyleConstants
import javax.swing.text.StyleContext
import logging.ILogContextListener
import logging.KEY_STACK
import logging.errorObject
import logging.extractStackTrace
import logging.findLogField
import logging.loggingCode
import utils.CoreGlobals
import utils.runOnEventThread

class LoggingConsole : FocusableWindow(), ILogDestination {
class LoggingConsoleAppender(private val console: LoggingConsole) : AppenderBase<ILoggingEvent>() {
override fun append(p0: ILoggingEvent) {
console.log(p0)
}
}

class LoggingConsole : FocusableWindow(), ILogContextListener {
override val windowName = "Console"

val doc = DefaultStyledDocument()
Expand All @@ -45,20 +63,20 @@ class LoggingConsole : FocusableWindow(), ILogDestination {
contextPanel.layout = WrapLayout()
}

override fun log(record: LogRecord) {
fun log(record: ILoggingEvent) {
val cx = StyleContext()
val text = record.toString()
val text = record.toConsoleString()
val style = cx.addStyle(text, null)

if (record.severity == Level.ERROR) {
if (record.level == Level.ERROR) {
StyleConstants.setForeground(style, Color.RED)
}

try {
doc.insertString(doc.length, "\n$text", style)
record.getThrowableStr()?.let { doc.insertString(doc.length, "\n$it", style) }
record.errorObject()?.let { doc.insertString(doc.length, "\n$it", style) }

val threadStack = record.keyValuePairs[KEY_STACK]
val threadStack = record.findLogField(KEY_STACK)
threadStack?.let { doc.insertString(doc.length, "\n$it", style) }

textArea.select(doc.length, doc.length)
Expand All @@ -80,6 +98,16 @@ class LoggingConsole : FocusableWindow(), ILogDestination {
}
}

private fun ILoggingEvent.toConsoleString(): String {
val dateStr =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withLocale(Locale.UK)
.withZone(ZoneId.systemDefault())
.format(CoreGlobals.clock.instant())

return "$dateStr [$loggingCode] $message"
}

private fun factoryLabelForContext(field: Map.Entry<String, Any?>): Component {
val label = JLabel("${field.key}: ${field.value}")
label.foreground = Color.GREEN
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/kotlin/screen/SimpleDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JPanel
import utils.InjectedThings.logger
import utils.CoreGlobals.logger

abstract class SimpleDialog : JDialog(), ActionListener {
protected val panelOkCancel = JPanel()
Expand Down
Loading

0 comments on commit ea7fea6

Please sign in to comment.