Add the Server Log window

Tools > Server Log (Ctrl+Shift+L), offered with b_virtualserver_log_view,
reads the virtual server's log with logview, a hundred lines at a time,
with Show Older going further back. As in TS3, lines can be narrowed by
level, left out by exclude terms, coloured by keyword, and shown in UTC
or local time; the choices are kept in the settings. The find bar, with
its show-only-matches toggle, takes the place of TS3's separate search
and filter fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-04 13:19:52 +00:00
co-authored by Claude Opus 5.5
parent fb1de8ac5b
commit 8255d986ac
18 changed files with 1190 additions and 6 deletions
@@ -0,0 +1,132 @@
package com.ts3client.config;
import com.ts3client.net.ServerLogEntry.Level;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
/**
* How the Server Log window shows the log: which levels, in which time zone, which lines
* stand out in colour and which are left out — TeamSpeak's ServerLogView settings.
*/
public final class ServerLogSettings {
/**
* Lines containing {@code keyword} (ignoring case) are drawn in these colours.
*
* @param foreground an RGB colour, or {@code null} for the usual text colour
* @param background an RGB colour, or {@code null} for none
*/
public record Highlight(String keyword, Integer foreground, Integer background) {
}
private static final String PREFIX = "serverLog.";
/** The levels the window offers to show; the server's debug levels never reach a virtual server log. */
public static final List<Level> SHOWN_LEVELS = List.of(Level.CRITICAL, Level.ERROR, Level.WARNING, Level.INFO);
public final Set<Level> levels = EnumSet.copyOf(SHOWN_LEVELS);
/** Show times in the local time zone rather than in UTC, as the server writes them. */
public boolean localTime;
/** Checked in order; the first keyword a line contains decides its colours. */
public final List<Highlight> highlights = new ArrayList<>(List.of(
new Highlight("Critical", 0xC00000, null),
new Highlight("Error", 0xE03030, null),
new Highlight("Warning", 0xC07000, null)));
/** Lines containing any of these (ignoring case) are not shown. */
public final List<String> excludes = new ArrayList<>();
/** Whether a line passes the level choice and the exclude filter. */
public boolean shows(Level level, String line) {
if (!levels.contains(level) && SHOWN_LEVELS.contains(level)) return false;
String lower = line.toLowerCase(Locale.ROOT);
for (String term : excludes) {
if (!term.isEmpty() && lower.contains(term.toLowerCase(Locale.ROOT))) return false;
}
return true;
}
/** The first highlight whose keyword {@code line} contains, or {@code null}. */
public Highlight highlightFor(String line) {
String lower = line.toLowerCase(Locale.ROOT);
for (Highlight h : highlights) {
if (!h.keyword().isEmpty() && lower.contains(h.keyword().toLowerCase(Locale.ROOT))) return h;
}
return null;
}
void load(Properties p) {
String lv = p.getProperty(PREFIX + "levels");
if (lv != null) {
levels.clear();
for (String name : lv.split(",")) {
try {
levels.add(Level.valueOf(name.trim()));
} catch (IllegalArgumentException ignored) {
// dropped level name
}
}
}
localTime = Boolean.parseBoolean(p.getProperty(PREFIX + "localTime", Boolean.toString(localTime)));
if (p.getProperty(PREFIX + "highlights") != null) {
highlights.clear();
int n = parseCount(p.getProperty(PREFIX + "highlights"));
for (int i = 0; i < n; i++) {
String keyword = p.getProperty(PREFIX + "highlight." + i + ".keyword");
if (keyword == null) continue;
highlights.add(new Highlight(keyword, parseColor(p.getProperty(PREFIX + "highlight." + i + ".fg")),
parseColor(p.getProperty(PREFIX + "highlight." + i + ".bg"))));
}
}
if (p.getProperty(PREFIX + "excludes") != null) {
excludes.clear();
int n = parseCount(p.getProperty(PREFIX + "excludes"));
for (int i = 0; i < n; i++) {
String term = p.getProperty(PREFIX + "exclude." + i);
if (term != null) excludes.add(term);
}
}
}
void store(Properties p) {
p.stringPropertyNames().stream()
.filter(k -> k.startsWith(PREFIX + "highlight.") || k.startsWith(PREFIX + "exclude."))
.forEach(p::remove);
p.setProperty(PREFIX + "levels", String.join(",", levels.stream().map(Enum::name).toList()));
p.setProperty(PREFIX + "localTime", Boolean.toString(localTime));
p.setProperty(PREFIX + "highlights", Integer.toString(highlights.size()));
for (int i = 0; i < highlights.size(); i++) {
Highlight h = highlights.get(i);
p.setProperty(PREFIX + "highlight." + i + ".keyword", h.keyword());
p.setProperty(PREFIX + "highlight." + i + ".fg", formatColor(h.foreground()));
p.setProperty(PREFIX + "highlight." + i + ".bg", formatColor(h.background()));
}
p.setProperty(PREFIX + "excludes", Integer.toString(excludes.size()));
for (int i = 0; i < excludes.size(); i++) p.setProperty(PREFIX + "exclude." + i, excludes.get(i));
}
private static int parseCount(String v) {
try {
return Math.max(0, Integer.parseInt(v.trim()));
} catch (NumberFormatException e) {
return 0;
}
}
private static Integer parseColor(String v) {
if (v == null || v.isBlank()) return null;
try {
return Integer.parseInt(v.trim().replace("#", ""), 16) & 0xFFFFFF;
} catch (NumberFormatException e) {
return null;
}
}
private static String formatColor(Integer rgb) {
return rgb == null ? "" : String.format("#%06X", rgb);
}
}
@@ -181,6 +181,9 @@ public final class Settings {
/** The ban list columns the user shows, comma-separated column names; empty for the default set. */
public String banListColumns = "";
/** What the Server Log window shows and how. */
public final ServerLogSettings serverLog = new ServerLogSettings();
/** Which actions make a sound, and which ones are important enough to survive muting. */
public final NotificationSettings notifications = new NotificationSettings();
@@ -289,6 +292,7 @@ public final class Settings {
banHighlightOwn = parseB(props.getProperty("banHighlightOwn"), banHighlightOwn);
banListColumns = props.getProperty("banListColumns", banListColumns);
notifications.load(props);
serverLog.load(props);
contactDefaults.load(props);
colorContacts = parseB(props.getProperty("colorContacts"), colorContacts);
descriptionPictures = parseB(props.getProperty("descriptionPictures"), descriptionPictures);
@@ -345,6 +349,7 @@ public final class Settings {
props.setProperty("banHighlightOwn", Boolean.toString(banHighlightOwn));
props.setProperty("banListColumns", banListColumns);
notifications.store(props);
serverLog.store(props);
contactDefaults.store(props);
props.setProperty("colorContacts", Boolean.toString(colorContacts));
props.setProperty("descriptionPictures", Boolean.toString(descriptionPictures));
@@ -671,6 +671,7 @@ final class ConnectionEventHandler implements TS3Listener {
else if ("notifytokenlist".equals(e.getCommand())) conn.privilegeKeys.onListEntry(e);
else if ("notifytokenadd".equals(e.getCommand())) conn.privilegeKeys.onAdded(e);
else if ("notifycomplainlist".equals(e.getCommand())) conn.complaints.onListEntry(e);
else if ("notifyserverlog".equals(e.getCommand())) conn.serverLog.onLine(e);
}
// ---- helpers ----
@@ -0,0 +1,82 @@
package com.ts3client.net;
import com.github.manevolent.ts3j.command.CommandException;
import com.github.manevolent.ts3j.command.SingleCommand;
import com.github.manevolent.ts3j.command.parameter.CommandSingleParameter;
import com.github.manevolent.ts3j.event.UnknownTeamspeakEvent;
import com.github.manevolent.ts3j.protocol.ProtocolRole;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Reads the virtual server's log with {@code logview}, which answers with one
* {@code notifyserverlog} per line, newest first, each also carrying {@code last_pos}: where
* the lines it returned begin in the log file, and so where the older ones end.
*
* <p>Split out of {@link TeamspeakConnection} like {@link BanAdmin}. Every call here blocks
* and must run off the UI thread; the connection's public wrapper does that.
*/
final class ServerLogAdmin {
/** The most lines {@code logview} returns at once. */
static final int PAGE_LINES = 100;
/** What {@code logview} may answer for an empty log. */
private static final int ERROR_DATABASE_EMPTY_RESULT = 0x0501;
private final TeamspeakConnection conn;
/** The {@code logview} reply in progress; one request may be in flight at a time. */
private volatile Reply reply;
private static final class Reply {
final List<ServerLogEntry> entries = Collections.synchronizedList(new ArrayList<>());
volatile long lastPos;
}
ServerLogAdmin(TeamspeakConnection conn) {
this.conn = conn;
}
/**
* Reads the newest lines, or with {@code olderFrom} > 0 those ending there (from an
* earlier {@link ServerLogPage#olderFrom()}).
*/
synchronized ServerLogPage read(long olderFrom) throws Exception {
SingleCommand cmd = new SingleCommand("logview", ProtocolRole.CLIENT);
cmd.add(new CommandSingleParameter("lines", Integer.toString(PAGE_LINES)));
if (olderFrom > 0) cmd.add(new CommandSingleParameter("begin_pos", Long.toString(olderFrom)));
cmd.add(new CommandSingleParameter("reverse", "1"));
cmd.add(new CommandSingleParameter("instance", "0"));
Reply r = new Reply();
reply = r;
try {
conn.socket().executeCommand(cmd).complete();
conn.awaitEventsProcessed();
} catch (CommandException e) {
if (e.getErrorId() != ERROR_DATABASE_EMPTY_RESULT) throw e;
} finally {
reply = null;
}
List<ServerLogEntry> entries = new ArrayList<>(r.entries);
Collections.reverse(entries);
return new ServerLogPage(entries, entries.isEmpty() ? 0 : r.lastPos);
}
void onLine(UnknownTeamspeakEvent e) {
Reply r = reply;
if (r == null) return;
String line = e.get("l");
if (line != null && !line.isEmpty()) r.entries.add(ServerLogEntry.parse(line));
String lastPos = e.get("last_pos");
if (lastPos != null) {
try {
r.lastPos = Long.parseLong(lastPos.trim());
} catch (NumberFormatException ignored) {
// keep what the earlier lines said
}
}
}
}
@@ -0,0 +1,49 @@
package com.ts3client.net;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
/**
* One line of the virtual server's log, as {@code logview} hands it over:
* {@code 2026-10-04 13:11:01.038554|INFO |VirtualServer |1 |message}.
*
* @param timestamp when the server wrote it, in UTC as the server logs; {@code null} if the line had none
* @param channel the server component that wrote it, e.g. {@code VirtualServer} or {@code PktHandler}
*/
public record ServerLogEntry(LocalDateTime timestamp, Level level, String channel, String message) {
/** The server's log levels, most severe first. */
public enum Level {
CRITICAL, ERROR, WARNING, INFO, DEBUG, DEVEL
}
private static final DateTimeFormatter TIMESTAMP = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS]");
/**
* Reads a log line. One whose timestamp is damaged — the server cuts the start off the
* file's very first line — keeps the rest; one that does not follow the format at all
* is kept whole as an info message.
*/
public static ServerLogEntry parse(String line) {
String[] fields = line.split("\\|", 5);
if (fields.length == 5) {
try {
Level level = Level.valueOf(fields[1].trim().toUpperCase(Locale.ROOT));
return new ServerLogEntry(timestamp(fields[0]), level, fields[2].trim(), fields[4]);
} catch (IllegalArgumentException ignored) {
// not a line of ours; fall through
}
}
return new ServerLogEntry(null, Level.INFO, "", line);
}
private static LocalDateTime timestamp(String field) {
try {
return LocalDateTime.parse(field.trim(), TIMESTAMP);
} catch (DateTimeParseException e) {
return null;
}
}
}
@@ -0,0 +1,15 @@
package com.ts3client.net;
import java.util.List;
/**
* A stretch of the server's log, oldest line first.
*
* @param olderFrom where the log's older lines end, to ask for them next; 0 once the start is reached
*/
public record ServerLogPage(List<ServerLogEntry> entries, long olderFrom) {
public boolean hasOlder() {
return olderFrom > 0;
}
}
@@ -97,10 +97,11 @@ public final class TeamspeakConnection implements TS3Listener {
final ConnectionStatsCollector stats = new ConnectionStatsCollector(this);
/** Channel editing and the server's icon store; package-private for {@link ConnectionEventHandler}. */
final ChannelAdmin channels = new ChannelAdmin(this);
/** The server's ban list, privilege keys and complaints; also package-private for {@link ConnectionEventHandler}. */
/** The server's ban list, privilege keys, complaints and log; also package-private for {@link ConnectionEventHandler}. */
final BanAdmin bans = new BanAdmin(this);
final PrivilegeKeyAdmin privilegeKeys = new PrivilegeKeyAdmin(this);
final ComplaintAdmin complaints = new ComplaintAdmin(this);
final ServerLogAdmin serverLog = new ServerLogAdmin(this);
private final AvatarAdmin avatarAdmin = new AvatarAdmin(this);
/*
@@ -1040,6 +1041,16 @@ public final class TeamspeakConnection implements TS3Listener {
});
}
// ---- server log ----
/**
* Reads the server's newest log lines, or with {@code olderFrom} > 0 the ones before an
* earlier page's, delivering them (or a failure message) off the UI thread.
*/
public void requestServerLog(long olderFrom, BiConsumer<ServerLogPage, String> callback) {
run(callback, () -> serverLog.read(olderFrom));
}
// ---- bans ----
/** Reads the server's ban list, delivering it (or a failure message) off the UI thread. */
@@ -437,6 +437,11 @@ public final class ServerSession {
return permits("b_client_complain_list");
}
/** Whether the server grants us {@code b_virtualserver_log_view}; offered while unknown. */
public boolean canViewServerLog() {
return permits("b_virtualserver_log_view");
}
/** Whether the server grants us {@code b_virtualserver_token_list}; offered while unknown. */
public boolean canListPrivilegeKeys() {
return permits("b_virtualserver_token_list");