~damien/jedit-lsp

64441f4cfef6a61728a2b533b89dd5818247472e — Damien Radtke a month ago 487a01e
Limit the number of logs that can be written
2 files changed, 63 insertions(+), 29 deletions(-)

M src/main/java/lsp/LanguageServerPlugin.java
M src/main/java/lsp/Log.java
M src/main/java/lsp/LanguageServerPlugin.java => src/main/java/lsp/LanguageServerPlugin.java +4 -11
@@ 2,14 2,11 @@ package lsp;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.Popup;


@@ 66,17 63,13 @@ public class LanguageServerPlugin extends EBPlugin {

		loadRegisteredServers();

		for (View view : jEdit.getViews()) {
			for (Buffer buffer : view.getBuffers()) {
				Server server = this.registeredServers.get(buffer.getMode());
				if (server != null) {
					server.startIfNeeded(buffer, false);
				}
		for (Buffer buffer : jEdit.getBuffers()) {
			Server server = this.registeredServers.get(buffer.getMode());
			if (server != null) {
				  server.startIfNeeded(buffer, false);
			}
		}

		// XXX: This doesn't seem to be working as part of plugin startup.
		// Maybe it needs to wait for a particular event?
		addCommandPath();
	}


M src/main/java/lsp/Log.java => src/main/java/lsp/Log.java +59 -18
@@ 1,46 1,87 @@
package lsp;

import java.util.concurrent.atomic.AtomicInteger;

// TODO: keep track of the size of activity.log, and stop adding messages
// when it gets too big.

public class Log {
	private static AtomicInteger logCount = new AtomicInteger();
	private static int maxLogCount = 10000;

	private static boolean canLog() {
		int newLogCount = logCount.incrementAndGet();
		if (newLogCount >= maxLogCount) {
			if (newLogCount == maxLogCount) {
				org.gjt.sp.util.Log.log(
						org.gjt.sp.util.Log.WARNING,
						Log.class,
						"Maximum LSP log count of " + maxLogCount
								+ "has been reached. Disabling further logging to preserve space.");
			}
			return false;
		} else {
			return true;
		}
	}

	public static void debug(Object source, Object message) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.DEBUG, source, message);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.DEBUG, source, message);
		}
	}
	

	public static void debug(Object source, Object message, Throwable exception) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.DEBUG, source, message, exception);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.DEBUG, source, message, exception);
		}
	}
	

	public static void error(Object source, Object message) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.ERROR, source, message);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.ERROR, source, message);
		}
	}
	

	public static void error(Object source, Object message, Throwable exception) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.ERROR, source, message, exception);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.ERROR, source, message, exception);
		}
	}
	

	public static void message(Object source, Object message) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.MESSAGE, source, message);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.MESSAGE, source, message);
		}
	}
	

	public static void message(Object source, Object message, Throwable exception) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.MESSAGE, source, message, exception);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.MESSAGE, source, message, exception);
		}
	}
	

	public static void notice(Object source, Object message) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message);
		}
	}
	

	public static void notice(Object source, Object message, Throwable exception) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message, exception);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message, exception);
		}
	}

	public static void warning(Object source, Object message) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.WARNING, source, message);
		}
	}
	

	public static void warning(Object source, Object message, Throwable exception) {
		org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.NOTICE, source, message, exception);
		if (canLog()) {
			org.gjt.sp.util.Log.log(org.gjt.sp.util.Log.WARNING, source, message, exception);
		}
	}
}