Skip to content

Commit

Permalink
Lag Log feature (disabled) (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hubcapp authored Nov 5, 2023
1 parent 5c573c0 commit 9c53f21
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
36 changes: 36 additions & 0 deletions src/Client/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
Expand All @@ -38,6 +40,8 @@ public class Logger {
private static final String LOG_FILE_PREFIX = "rscplus_";
private static final String LOG_FILE_EXTENSION = ".log";
private static PrintWriter m_logWriter;
private static PrintWriter m_lagWriter;
private static String m_lagWriter_filename;
private static int levelFixedWidth = 0;
private static String m_uncoloredMessage = "";

Expand Down Expand Up @@ -266,4 +270,36 @@ public static void Opcode(int timestamp, String type, int opcode, byte[] data) {
}
}
}

public static void Lag(String eventType, int frame) {
m_lagWriter.write(System.currentTimeMillis() + "," + frame + "," + eventType + "\n");
m_lagWriter.flush();
}

public static void initializeLagLog() {
String lagLogDir = Settings.Dir.LOGS + File.separator + "laglog";
Util.makeDirectory(lagLogDir);
m_lagWriter_filename =
lagLogDir + File.separator + "measured_lag-" + System.currentTimeMillis() + ".log";
try {
m_lagWriter = new PrintWriter(Files.newOutputStream(Paths.get(m_lagWriter_filename)));
} catch (Exception e) {
Error("Could not start logging lag.");
return;
}
Info("Started lag logging @ " + m_lagWriter_filename);
}

public static void finalizeLagLog() {
if (m_lagWriter == null) {
return;
}

try {
m_lagWriter.close();
} catch (Exception e) {
Error("Could not stop logging lag.");
}
Info("Finished lag logging @ " + m_lagWriter_filename);
}
}
4 changes: 4 additions & 0 deletions src/Client/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ public class Settings {
public static boolean LOAD_CHAT_HISTORY_BOOL = false;
public static boolean HIGHLIGHT_ITEMS_MENU_BOOL = false;

public static HashMap<String, Boolean> LOG_LAG = new HashMap<String, Boolean>();

// determines which preset to load, or your custom settings :-)
public static String currentProfile = "custom";

Expand Down Expand Up @@ -1845,6 +1847,8 @@ public static void definePresets(Properties props) {
LAG_INDICATOR.put("all", true);
LAG_INDICATOR.put("custom", getPropBoolean(props, "indicators", LAG_INDICATOR.get("default")));

defineStaticPreset(LOG_LAG, getPropBoolean(props, "log_lag", false));

SHOW_PLAYER_POSITION.put("vanilla", false);
SHOW_PLAYER_POSITION.put("vanilla_resizable", false);
SHOW_PLAYER_POSITION.put("lite", false);
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Speedrun.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public static void saveAndQuitSpeedrun() {

public static int loadSpeedrun() {
File[] fList = new File(Settings.Dir.SPEEDRUN).listFiles();
if (fList.length == 0) return REASON_NO_PREVIOUS_FILES;
if (fList == null || fList.length == 0) return REASON_NO_PREVIOUS_FILES;
Arrays.sort(fList);
File newestData = fList[fList.length - 1];
// This file can be "found" again later because the filename is based on startTimes[0], which
Expand Down
9 changes: 9 additions & 0 deletions src/Game/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,11 @@ public static void allTheWayLoggedIn() {
Settings.save();
}

if (Settings.LOG_LAG.get(Settings.currentProfile)) {
Logger.initializeLagLog();
Logger.Lag("Login", Replay.timestamp);
}

// Get keybind to open the config window so that we can tell the player how to open it
if (Settings.REMIND_HOW_TO_OPEN_SETTINGS.get(Settings.currentProfile)) {
String configWindowShortcut = "";
Expand Down Expand Up @@ -1408,6 +1413,10 @@ public static void disconnect_hook() {
player_id = -1;
knowWhoIAm = false;
Client.tipOfDay = -1;
if (Settings.LOG_LAG.get(Settings.currentProfile)) {
Logger.Lag("Disconnect", Replay.timestamp);
}
Logger.finalizeLagLog();
}

// check if login attempt is not a valid login or reconnect, send to disconnect hook
Expand Down
53 changes: 34 additions & 19 deletions src/Game/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public class Renderer {
private static int bankResetTimer = 0;
private static boolean show_bank_last = false;

private static boolean laggedLastFrame = false;

public static int getFogColor(int attenuation, int val) {
int clearColor = getClearColor();

Expand Down Expand Up @@ -1456,26 +1458,39 @@ public static void present(Image image) {
if (Replay.isPlaying && Replay.fpsPlayMultiplier > 1.0)
threshold = 35 * 3; // this is to prevent blinking during fastforward

if (Settings.LAG_INDICATOR.get(Settings.currentProfile)
&& Replay.getServerLag() >= threshold) {
x = width - 80;
y = height - 80;
setAlpha(g2, alpha_time);
g2.drawImage(Launcher.icon_warn.getImage(), x, y, 32, 32, null);
x += 16;
y += 38;
drawShadowText(g2, "Server Lag", x, y, color_fatigue, true);
y += 12;
int lag = (Replay.getServerLag() - 31) * Replay.getFrameTimeSlice();
drawShadowText(
g2,
new DecimalFormat("0.0").format((float) lag / 1000.0f) + "s",
x,
y,
color_low,
true);
setAlpha(g2, 1.0f);
if (Replay.getServerLag() >= threshold) {
if (Settings.LOG_LAG.get(Settings.currentProfile)) {
if (!laggedLastFrame) {
Logger.Lag("LagStart", Replay.timestamp);
laggedLastFrame = true;
}
}
if (Settings.LAG_INDICATOR.get(Settings.currentProfile)) {
x = width - 80;
y = height - 80;
setAlpha(g2, alpha_time);
g2.drawImage(Launcher.icon_warn.getImage(), x, y, 32, 32, null);
x += 16;
y += 38;
drawShadowText(g2, "Server Lag", x, y, color_fatigue, true);
y += 12;
int lag = (Replay.getServerLag() - 31) * Replay.getFrameTimeSlice();
drawShadowText(
g2,
new DecimalFormat("0.0").format((float) lag / 1000.0f) + "s",
x,
y,
color_low,
true);
setAlpha(g2, 1.0f);
}
} else {
if (laggedLastFrame && Settings.LOG_LAG.get(Settings.currentProfile)) {
Logger.Lag("LagStop", Replay.timestamp);
laggedLastFrame = false;
}
}

if (!(Replay.isPlaying && !Settings.TRIGGER_ALERTS_REPLAY.get(Settings.currentProfile))) {
g2.setFont(font_big);
if (Settings.FATIGUE_ALERT.get(Settings.currentProfile)
Expand Down

0 comments on commit 9c53f21

Please sign in to comment.