Skip to content

Commit

Permalink
Merge pull request #4063 from kwvanderlinde/experiment/reduce-connect…
Browse files Browse the repository at this point in the history
…ion-logging

Clean up logging
  • Loading branch information
cwisniew authored May 16, 2023
2 parents d93687b + 539c6a7 commit 3d4a679
Show file tree
Hide file tree
Showing 34 changed files with 415 additions and 271 deletions.
30 changes: 2 additions & 28 deletions src/main/java/net/rptools/lib/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -211,7 +210,7 @@ public static String getContentType(InputStream in) {
String type = "";
try {
type = URLConnection.guessContentTypeFromStream(in);
if (log.isDebugEnabled()) log.debug("result from guessContentTypeFromStream() is " + type);
log.debug("result from guessContentTypeFromStream() is {}", type);
} catch (IOException e) {
}
return type;
Expand All @@ -227,8 +226,7 @@ public static String getContentType(InputStream in) {
public static String getContentType(URL url) {
String type = "";
type = URLConnection.guessContentTypeFromName(url.getPath());
if (log.isDebugEnabled())
log.debug("result from guessContentTypeFromName(" + url.getPath() + ") is " + type);
log.debug("result from guessContentTypeFromName({}) is {}", url.getPath(), type);
return type;
}

Expand Down Expand Up @@ -260,30 +258,6 @@ public static BufferedReader getFileAsReader(File file) throws IOException {
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
}

/**
* Given a URL this method determines the content type of the URL (if possible) and then returns a
* Reader with the appropriate character encoding.
*
* @param url the source of the data stream
* @return String representing the data
* @throws IOException in case of an I/O error
*/
public static Reader getURLAsReader(URL url) throws IOException {
InputStreamReader isr = null;
URLConnection conn = null;
// We're assuming character here, but it could be bytes. Perhaps we should
// check the MIME type returned by the network server?
conn = url.openConnection();
if (log.isDebugEnabled()) {
String type = URLConnection.guessContentTypeFromName(url.getPath());
log.debug("result from guessContentTypeFromName(" + url.getPath() + ") is " + type);
type = getContentType(conn.getInputStream());
// Now make a guess and change 'encoding' to match the content type...
}
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
return isr;
}

public static InputStream getFileAsInputStream(File file) throws IOException {
return getURLAsInputStream(file.toURI().toURL());
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/net/rptools/lib/io/PackedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import net.rptools.lib.CodeTimer;
import net.rptools.lib.FileUtil;
import net.rptools.lib.ModelVersionManager;
import net.rptools.maptool.client.AppState;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.model.Asset;
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.GUID;
Expand Down Expand Up @@ -290,7 +292,7 @@ public void save() throws IOException {
return;
}
saveTimer = new CodeTimer("PackedFile.save");
saveTimer.setEnabled(log.isDebugEnabled());
saveTimer.setEnabled(AppState.isCollectProfilingData());

// Create the new file
File newFile = new File(tmpDir, new GUID() + ".pak");
Expand Down Expand Up @@ -402,8 +404,9 @@ public void save() throws IOException {
IOUtils.closeQuietly(zout);
saveTimer.stop("cleanup");

if (log.isDebugEnabled()) log.debug(saveTimer);
saveTimer = null;
if (saveTimer.isEnabled()) {
MapTool.getProfilingNoteFrame().addText(saveTimer.toString());
}
}
}

Expand Down Expand Up @@ -729,8 +732,7 @@ public InputStream getFileAsInputStream(String path) throws IOException {
InputStream in = zipFile.getInputStream(entry);
if (in == null) throw new FileNotFoundException(path);
String type = FileUtil.getContentType(in);
if (log.isDebugEnabled() && type != null)
log.debug("FileUtil.getContentType() returned " + type);
log.debug("FileUtil.getContentType() returned {}", type);
return in;
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/rptools/maptool/client/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1231,9 +1231,7 @@ public static void setMruCampaigns(List<File> mruCampaigns) {
path = file.getCanonicalPath();
} catch (IOException e) {
// Probably pretty rare, but we want to know about it
if (log.isInfoEnabled()) {
log.info("unexpected during file.getCanonicalPath()", e); // $NON-NLS-1$
}
log.info("unexpected during file.getCanonicalPath()", e); // $NON-NLS-1$
path = file.getPath();
}
// It's important that '%3A' is done last. Note that the pathSeparator may not be a colon on
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/rptools/maptool/client/AppUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static File getAppHome(String subdir) {
RuntimeException re =
new RuntimeException(
I18N.getText("msg.error.unableToCreateDataDir", path.getAbsolutePath()));
if (log != null && log.isInfoEnabled()) {
if (log != null) {
log.info("msg.error.unableToCreateDataDir", re);
}
throw re;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/rptools/maptool/client/AutoSaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public void start() {
if (autoSaveTimer == null) {
autoSaveTimer = new Timer(1000, (Object) -> execute());
autoSaveTimer.setRepeats(false);
if (log.isDebugEnabled())
log.debug("Logging level of 'DEBUG' sets timeout to seconds"); // $NON-NLS-1$
log.debug("Logging level of 'DEBUG' sets timeout to seconds"); // $NON-NLS-1$
next(true);
}
}
Expand All @@ -66,7 +65,9 @@ private void execute() {
private boolean executeAndContinue() {

int interval =
AppPreferences.getAutoSaveIncrement() * (log.isDebugEnabled() ? 1000 : 60 * 1000);
AppPreferences.getAutoSaveIncrement()
* 1000
* (DeveloperOptions.Toggle.AutoSaveMeasuredInSeconds.isEnabled() ? 1 : 60);

// auto-save is turned off with <= 0
if (interval <= 0) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/net/rptools/maptool/client/ChatAutoSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ private static TimerTask createTimer(final long timeout) {
new TimerTask() {
@Override
public void run() {
if (log.isDebugEnabled())
log.debug("Chat log autosave countdown complete from " + timeout); // $NON-NLS-1$
log.debug("Chat log autosave countdown complete from {}", timeout); // $NON-NLS-1$
if (chatlog == null) {
String filename = AppPreferences.getChatFilenameFormat();
// FJE Ugly kludge to replace older default entry with newer default
Expand All @@ -63,8 +62,7 @@ public void run() {
}
File chatFile =
new File(AppUtil.getAppHome("autosave").toString(), chatlog); // $NON-NLS-1$
if (log.isInfoEnabled())
log.info("Saving log to '" + chatFile + "'"); // $NON-NLS-1$ //$NON-NLS-2$
log.info("Saving log to '{}'", chatFile); // $NON-NLS-1$ //$NON-NLS-2$

CommandPanel chat = MapTool.getFrame().getCommandPanel();
String old = MapTool.getFrame().getStatusMessage();
Expand All @@ -75,7 +73,7 @@ public void run() {
try (FileWriter writer = new FileWriter(chatFile)) {
writer.write(chat.getMessageHistory());
}
if (log.isInfoEnabled()) log.info("Log saved"); // $NON-NLS-1$
log.info("Log saved"); // $NON-NLS-1$
} catch (IOException e) {
// If this happens should we track it and turn off the autosave? Perhaps
// after a certain number of consecutive failures? Or maybe just lengthen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void handleMessage(String id, byte[] message) {
try {
var msg = Message.parseFrom(message);
var msgType = msg.getMessageTypeCase();
log.info(id + " got: " + msgType);
log.debug(id + " got: " + msgType);

switch (msgType) {
case ADD_TOPOLOGY_MSG -> handle(msg.getAddTopologyMsg());
Expand Down Expand Up @@ -163,7 +163,7 @@ public void handleMessage(String id, byte[] message) {
case UPDATE_PLAYER_STATUS_MSG -> handle(msg.getUpdatePlayerStatusMsg());
default -> log.warn(msgType + "not handled.");
}
log.info(id + " handled: " + msgType);
log.debug(id + " handled: " + msgType);
} catch (Exception e) {
log.error(e);
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ private void handle(UpdatePlayerStatusMsg updatePlayerStatusMsg) {
.orElse(null);

if (player == null) {
log.info("UpdatePlayerStatusMsg failed. No player with name: '" + playerName + "'");
log.warn("UpdatePlayerStatusMsg failed. No player with name: '" + playerName + "'");
return;
}

Expand Down
79 changes: 79 additions & 0 deletions src/main/java/net/rptools/maptool/client/DeveloperOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client;

import java.util.Arrays;
import java.util.List;
import java.util.prefs.Preferences;
import net.rptools.maptool.language.I18N;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class DeveloperOptions {
private static final Logger log = LogManager.getLogger(DeveloperOptions.class);
private static final Preferences prefs =
Preferences.userRoot().node(AppConstants.APP_NAME + "/prefs");

public enum Toggle {
/**
* When enabled, make auto-save 60x more frequent by interpreting the user-provided value as
* seconds instead of minutes.
*/
AutoSaveMeasuredInSeconds("autoSaveMeasuredInSeconds"),

/** When enabled, draw boundaries around each partition. */
ShowPartitionDrawableBoundaries("showPartitionDrawableBoundaries"),

/**
* When enabled, shows F, G, H scores for each cell encountered during pathfinding, as well as
* blocked moved.
*/
ShowAiDebugging("showAiDebugging"),

/** When enabled, recalculates the grid shape each time it is needed. */
IgnoreGridShapeCache("ignoreGridShapeCache"),
;

private final String key;

Toggle(String key) {
this.key = key;
}

public String getKey() {
return key;
}

public boolean isEnabled() {
return prefs.getBoolean(key, true);
}

public void setEnabled(boolean enabled) {
prefs.putBoolean(key, enabled);
}

public String getLabel() {
return I18N.getText(String.format("Preferences.developer.%s.label", key));
}

public String getTooltip() {
return I18N.getText(String.format("Preferences.developer.%s.tooltip", key));
}
}

public static List<Toggle> getEnabledOptions() {
return Arrays.stream(Toggle.values()).filter(Toggle::isEnabled).toList();
}
}
23 changes: 23 additions & 0 deletions src/main/java/net/rptools/maptool/client/MapTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,29 @@ private static void postInitialize() {
.getCurrentZoneRenderer()
.getZone()
.setTopologyTypes(AppPreferences.getTopologyTypes());

final var enabledDeveloperOptions = DeveloperOptions.getEnabledOptions();
if (!enabledDeveloperOptions.isEmpty()) {
final var message = new StringBuilder();
message
.append("<p>")
.append(I18N.getText("Preferences.developer.info.developerOptionsInUse"))
.append("</p><ul>");
for (final var option : enabledDeveloperOptions) {
message.append("<li>").append(option.getLabel()).append("</li>");
}
message
.append("</ul><p>")
.append(
I18N.getText(
"Preferences.developer.info.developerOptionsInUsePost",
I18N.getText("menu.edit"),
I18N.getText("action.preferences"),
I18N.getText("Preferences.tab.developer")))
.append("</p>");

showWarning(message.toString());
}
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/net/rptools/maptool/client/MapToolConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package net.rptools.maptool.client;

import static net.rptools.maptool.server.proto.Message.MessageTypeCase.HEARTBEAT_MSG;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import net.rptools.clientserver.ConnectionFactory;
Expand Down Expand Up @@ -102,13 +100,7 @@ public void close() throws IOException {
}

public void sendMessage(Message msg) {
var msgType = msg.getMessageTypeCase();
var logText = player.getName() + " sent " + msg.getMessageTypeCase();
if (msgType == HEARTBEAT_MSG) {
log.debug(logText);
} else {
log.info(logText);
}
log.debug(player.getName() + " sent " + msg.getMessageTypeCase());
connection.sendMessage(msg.toByteArray());
}
}
Loading

0 comments on commit 3d4a679

Please sign in to comment.