Skip to content

Commit

Permalink
Fix LogAction, let it extend a more generic one.
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Nov 24, 2015
1 parent 1c608ec commit 98ae3fb
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package fr.neatmonster.nocheatplus.actions.types;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import fr.neatmonster.nocheatplus.NCPAPIProvider;
import fr.neatmonster.nocheatplus.actions.Action;
import fr.neatmonster.nocheatplus.actions.ActionList;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile;
import fr.neatmonster.nocheatplus.config.ConfigFileWithActions;
import fr.neatmonster.nocheatplus.config.ConfigManager;
import fr.neatmonster.nocheatplus.logging.LogManager;
import fr.neatmonster.nocheatplus.logging.StreamID;
import fr.neatmonster.nocheatplus.utilities.ColorUtil;

/**
* Generic log action, capable of logging to any stream of LogManager with any
* log level. Custom actions might need an altered action factory or override
* toString, due to action prefixes/suffixes.
*
* @author asofold
*
*/
public class GenericLogAction extends ActionWithParameters<ViolationData, ActionList> {

public static class GenericLogActionConfig {
/**
* Config path to check for optimized actions or during runtime. Set to
* null, to always log
*/
public final String configPathActive;
public final boolean chatColor;
public final StreamID streamID;
public final Level level;
/**
* Suffix for log actions, such as i for ingame, f for file and c for
* console. Band-aid for writing log actions back to config.
*/
public final String actionConfigSuffix;

public GenericLogActionConfig(String configPathActive, StreamID streamID, boolean chatColor, Level level, String actionSuffix) {
this.configPathActive = configPathActive;
this.streamID = streamID;
this.chatColor = chatColor;
this.level = level;
this.actionConfigSuffix = actionSuffix;
}
}

private final GenericLogActionConfig[] configs;

/** Set if to check the configuration flag on execute being called. */
private final boolean checkActive;
/** Set if any config demands replacing color. */
private final boolean replaceColor;
/** Set if any config demands stripping color. */
private final boolean stripColor;

public GenericLogAction(final String name, final int delay, final int repeat, final String message,
final boolean checkActive, final GenericLogActionConfig... configs) {
super(name, delay, repeat, message);
final List<GenericLogActionConfig> temp = new ArrayList<GenericLogAction.GenericLogActionConfig>(configs.length);
boolean replaceColor = false;
boolean stripColor = false;
boolean checkActiveUseful = false;
for (int i = 0; i < configs.length; i++) {
GenericLogActionConfig config = configs[i];
if (config == null) {
continue;
}
temp.add(config);
if (config.chatColor) {
replaceColor = true;
} else {
stripColor = true;
}
if (config.configPathActive != null) {
checkActiveUseful = true;
}
}
this.configs = temp.toArray(new GenericLogActionConfig[temp.size()]);
this.checkActive = checkActive ? checkActiveUseful : false;
this.replaceColor = replaceColor;
this.stripColor = stripColor;
}

@Override
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE) || configs.length == 0) {
return null;
}
final List<GenericLogActionConfig> temp = new ArrayList<GenericLogAction.GenericLogActionConfig>(configs.length);
for (int i = 0; i < configs.length; i ++) {
final GenericLogActionConfig logConfig = configs[i];
if (checkActive && logConfig.configPathActive != null && !config.getBoolean(logConfig.configPathActive)) {
continue;
}
temp.add(logConfig);
}
if (temp.isEmpty()) {
return null;
}
final GenericLogActionConfig[] logConfigs = temp.toArray(new GenericLogActionConfig[temp.size()]);
return new GenericLogAction(name, delay, repeat, message, false, logConfigs);
}

@Override
public boolean execute(final ViolationData violationData) {
// TODO: Consider permission caching or removing the feature? [Besides, check earlier?]
if (violationData.player.hasPermission(violationData.getPermissionSilent())) {
return false;
}
final LogManager logManager = NCPAPIProvider.getNoCheatPlusAPI().getLogManager();
final String message = super.getMessage(violationData);
final String messageNoColor = stripColor ? ColorUtil.removeColors(message) : null;
final String messageWithColor = replaceColor ? ColorUtil.replaceColors(message) : null;
final ConfigFile configFile = checkActive ? ConfigManager.getConfigFile() : null;
for (int i = 0; i < configs.length; i ++) {
final GenericLogActionConfig config = configs[i];
if (checkActive && config.configPathActive != null && !configFile.getBoolean(config.configPathActive)) {
continue;
}
logManager.log(config.streamID, config.level, config.chatColor ? messageWithColor : messageNoColor);
}
return false;
}

/**
* Create the string that's used to define the action in the configuration file.
*
* @return the string
*/
@Override
public String toString() {
final StringBuilder builder = new StringBuilder(32 + 2 * configs.length);
builder.append("log:" + name + ":" + delay + ":" + repeat + ":");
for (int i = 0; i < configs.length; i ++) {
builder.append(configs[i].actionConfigSuffix);
}
return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package fr.neatmonster.nocheatplus.actions.types;

import fr.neatmonster.nocheatplus.NCPAPIProvider;
import fr.neatmonster.nocheatplus.actions.Action;
import fr.neatmonster.nocheatplus.actions.ActionList;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import java.util.logging.Level;

import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFileWithActions;
import fr.neatmonster.nocheatplus.logging.LogManager;
import fr.neatmonster.nocheatplus.logging.Streams;
import fr.neatmonster.nocheatplus.utilities.ColorUtil;

/**
* Print a log message to various locations.
* Default log action for standard targets.
*/
public class LogAction extends ActionWithParameters<ViolationData, ActionList> {

// Some flags to decide where the log message should show up, based on the configuration file.
/** Log to chat? */
public final boolean toChat;
public class LogAction extends GenericLogAction {

/** Log to console? */
public final boolean toConsole;

/** Log to file? */
public final boolean toFile;
protected static final GenericLogActionConfig configIngame = new GenericLogActionConfig(
ConfPaths.LOGGING_BACKEND_INGAMECHAT_ACTIVE, Streams.NOTIFY_INGAME, true, Level.INFO, "i");
protected static final GenericLogActionConfig configConsole = new GenericLogActionConfig(
ConfPaths.LOGGING_BACKEND_CONSOLE_ACTIVE, Streams.SERVER_LOGGER, false, Level.INFO, "c");
protected static final GenericLogActionConfig configFile = new GenericLogActionConfig(
ConfPaths.LOGGING_BACKEND_FILE_ACTIVE, Streams.DEFAULT_FILE, false, Level.INFO, "f");

/**
* Instantiates a new log action.
* Instantiates a new log action (not optimized).
*
* @param name
* the name
Expand All @@ -45,55 +37,7 @@ public class LogAction extends ActionWithParameters<ViolationData, ActionList> {
*/
public LogAction(final String name, final int delay, final int repeat, final boolean toChat,
final boolean toConsole, final boolean toFile, final String message) {
super(name, delay, repeat, message);
// Might switch to only store the prefixes (null = deactivated).
this.toChat = toChat;
this.toConsole = toConsole;
this.toFile = toFile;
}

@Override
public Action<ViolationData, ActionList> getOptimizedCopy(final ConfigFileWithActions<ViolationData, ActionList> config, final Integer threshold) {
if (!config.getBoolean(ConfPaths.LOGGING_ACTIVE)) {
return null;
}
return this;
}

/*
* (non-Javadoc)
*
* @see
* fr.neatmonster.nocheatplus.actions.Action#execute(fr.neatmonster.nocheatplus
* .checks.ViolationData)
*/
@Override
public boolean execute(final ViolationData violationData) {
if (!violationData.player.hasPermission(violationData.getPermissionSilent())) {
final String message = super.getMessage(violationData);
final LogManager logManager = NCPAPIProvider.getNoCheatPlusAPI().getLogManager();
if (toChat) {
logManager.info(Streams.NOTIFY_INGAME, ColorUtil.replaceColors(message));
}
if (toConsole) {
logManager.info(Streams.SERVER_LOGGER, ColorUtil.removeColors(message));
}
if (toFile) {
logManager.info(Streams.DEFAULT_FILE, ColorUtil.removeColors(message));
}
}
return false;
}

/**
* Create the string that's used to define the action in the logfile.
*
* @return the string
*/
@Override
public String toString() {
return "log:" + name + ":" + delay + ":" + repeat + ":" + (toConsole ? "c" : "") + (toChat ? "i" : "")
+ (toFile ? "f" : "");
super(name, delay, repeat, message, true, toChat ? configIngame : null, toConsole ? configConsole : null, toFile ? configFile : null);
}

}

0 comments on commit 98ae3fb

Please sign in to comment.