2022-10-02 18:31:08 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2022-10-05 15:02:30 +08:00
|
|
|
|
using static MinecraftClient.Settings;
|
2021-03-07 14:23:26 +08:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Logger
|
|
|
|
|
|
{
|
|
|
|
|
|
public class FilteredLogger : LoggerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
protected enum FilterChannel { Debug, Chat }
|
|
|
|
|
|
|
|
|
|
|
|
protected bool ShouldDisplay(FilterChannel channel, string msg)
|
|
|
|
|
|
{
|
2022-10-06 18:12:32 +08:00
|
|
|
|
if (Config.Logging.FilterMode == LoggingConfigHealper.LoggingConfig.FilterModeEnum.disable)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Regex? regexToUse = null;
|
2021-03-07 14:23:26 +08:00
|
|
|
|
// Convert to bool for XOR later. Whitelist = 0, Blacklist = 1
|
|
|
|
|
|
switch (channel)
|
|
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
case FilterChannel.Chat:
|
|
|
|
|
|
string chat = Config.Logging.ChatFilterRegex;
|
|
|
|
|
|
if (string.IsNullOrEmpty(chat))
|
|
|
|
|
|
regexToUse = null;
|
|
|
|
|
|
else
|
|
|
|
|
|
regexToUse = new(chat);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case FilterChannel.Debug:
|
|
|
|
|
|
string debug = Config.Logging.DebugFilterRegex;
|
|
|
|
|
|
if (string.IsNullOrEmpty(debug))
|
|
|
|
|
|
regexToUse = null;
|
|
|
|
|
|
else
|
|
|
|
|
|
regexToUse = new(debug);
|
|
|
|
|
|
break;
|
2021-03-07 14:23:26 +08:00
|
|
|
|
}
|
refactor: convert == null / != null to is null / is not null in 31 files
Replace old-style null comparisons with modern C# pattern matching
syntax across Commands, Protocol, Mapping, ChatBots, Physics,
Inventory, Logger, CommandHandler, Scripting, Crypto, and other modules.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 00:42:59 +00:00
|
|
|
|
if (regexToUse is not null)
|
2021-03-07 14:23:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
// IsMatch and white/blacklist result can be represented using XOR
|
|
|
|
|
|
// e.g. matched(true) ^ blacklist(true) => shouldn't log(false)
|
2022-10-06 18:12:32 +08:00
|
|
|
|
if (Config.Logging.FilterMode == LoggingConfigHealper.LoggingConfig.FilterModeEnum.blacklist)
|
|
|
|
|
|
return !regexToUse.IsMatch(msg);
|
|
|
|
|
|
else if (Config.Logging.FilterMode == LoggingConfigHealper.LoggingConfig.FilterModeEnum.whitelist)
|
|
|
|
|
|
return regexToUse.IsMatch(msg);
|
|
|
|
|
|
else
|
|
|
|
|
|
return true;
|
2021-03-07 14:23:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
else return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Debug(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (DebugEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ShouldDisplay(FilterChannel.Debug, msg))
|
|
|
|
|
|
{
|
|
|
|
|
|
Log("§8[DEBUG] " + msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
// Don't write debug lines here as it could cause a stack overflow
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 17:24:25 +02:00
|
|
|
|
public override void PacketDebug(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Settings.Config.Logging.PacketDebugMessages)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ShouldDisplay(FilterChannel.Debug, msg))
|
|
|
|
|
|
{
|
|
|
|
|
|
Log("§8[DEBUG] " + msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-07 14:23:26 +08:00
|
|
|
|
public override void Info(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (InfoEnabled)
|
|
|
|
|
|
ConsoleIO.WriteLogLine(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Warn(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (WarnEnabled)
|
|
|
|
|
|
Log("§6[WARN] " + msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Error(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ErrorEnabled)
|
|
|
|
|
|
Log("§c[ERROR] " + msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Chat(string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ChatEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ShouldDisplay(FilterChannel.Chat, msg))
|
|
|
|
|
|
{
|
2026-05-04 11:36:01 +00:00
|
|
|
|
ConsoleIO.WriteChatLineIfVisible(msg);
|
2021-03-07 14:23:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
else Debug("[Logger] One Chat message filtered: " + msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|