Added the ability to terminate log searches.
This commit is contained in:
@@ -7,7 +7,8 @@ import java.io.FileReader;
|
|||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.ArrayList;
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.regex.PatternSyntaxException;
|
import java.util.regex.PatternSyntaxException;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
@@ -22,11 +23,12 @@ public class LogHandler extends Thread
|
|||||||
{
|
{
|
||||||
private CommandSender sender;
|
private CommandSender sender;
|
||||||
private String regex, fileName;
|
private String regex, fileName;
|
||||||
private static ArrayList<CommandSender> stillSearching = new ArrayList<>();
|
private static Map<String, LogHandler> activeSearches = new TreeMap<>();
|
||||||
public int totalFiles = 0;
|
public int totalFiles = 0;
|
||||||
public int filesSearched = 0;
|
public int filesSearched = 0;
|
||||||
public int totalLines = 0;
|
public int totalLines = 0;
|
||||||
public int currentLine = 0;
|
public int currentLine = 0;
|
||||||
|
private boolean isCanceled = false;
|
||||||
|
|
||||||
protected LogHandler(CommandSender sender, String regex, String fileName)
|
protected LogHandler(CommandSender sender, String regex, String fileName)
|
||||||
{
|
{
|
||||||
@@ -37,15 +39,24 @@ public class LogHandler extends Thread
|
|||||||
|
|
||||||
public void doSearch()
|
public void doSearch()
|
||||||
{
|
{
|
||||||
if (stillSearching.contains(sender))
|
String id = Utils.getID(sender);
|
||||||
|
if (activeSearches.containsKey(id))
|
||||||
{
|
{
|
||||||
Logs.logger.message(sender, true, "§4 DO NOT EVER TRY TO QUERY TWO SEARCHES AT ONCE. Go die...!");
|
Logs.logger.message(sender, true, "§4 DO NOT EVER TRY TO QUERY TWO SEARCHES AT ONCE. Go die...!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stillSearching.add(sender);
|
activeSearches.put(Utils.getID(sender), this);
|
||||||
this.start();
|
this.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void cancel(CommandSender sender) {
|
||||||
|
LogHandler handler = activeSearches.remove(Utils.getID(sender));
|
||||||
|
if (handler == null)
|
||||||
|
Logs.logger.message(sender, true, "You aren't running a search.");
|
||||||
|
else
|
||||||
|
handler.isCanceled = true;
|
||||||
|
}
|
||||||
|
|
||||||
/** Searches the logs for a certain regex and forwards any matches to the sender.
|
/** Searches the logs for a certain regex and forwards any matches to the sender.
|
||||||
*
|
*
|
||||||
* @param sender the issuer of the search
|
* @param sender the issuer of the search
|
||||||
@@ -55,6 +66,7 @@ public class LogHandler extends Thread
|
|||||||
{
|
{
|
||||||
long starttime = System.currentTimeMillis();
|
long starttime = System.currentTimeMillis();
|
||||||
int matches = 0;
|
int matches = 0;
|
||||||
|
String id = Utils.getID(sender);
|
||||||
Logs.logger.message(sender, "Starting log search for &e" + regex + "&7 in &e" + fileName
|
Logs.logger.message(sender, "Starting log search for &e" + regex + "&7 in &e" + fileName
|
||||||
+ " &7now.");
|
+ " &7now.");
|
||||||
Logs.logger.message(sender, "&cDon't run another query until this one is done!");
|
Logs.logger.message(sender, "&cDon't run another query until this one is done!");
|
||||||
@@ -74,7 +86,7 @@ public class LogHandler extends Thread
|
|||||||
{
|
{
|
||||||
Logs.logger.message(sender, true, "An error occured trying to compile the filename pattern!");
|
Logs.logger.message(sender, true, "An error occured trying to compile the filename pattern!");
|
||||||
Logs.logger.message(sender, true, "&2Reason: &7" + e.getDescription());
|
Logs.logger.message(sender, true, "&2Reason: &7" + e.getDescription());
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +102,7 @@ public class LogHandler extends Thread
|
|||||||
if (totalFiles == 0)
|
if (totalFiles == 0)
|
||||||
{
|
{
|
||||||
Logs.logger.message(sender, true, "No files found!");
|
Logs.logger.message(sender, true, "No files found!");
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -106,7 +118,7 @@ public class LogHandler extends Thread
|
|||||||
{
|
{
|
||||||
Logs.logger.message(sender, true, "An error occured trying to compile the search pattern!");
|
Logs.logger.message(sender, true, "An error occured trying to compile the search pattern!");
|
||||||
Logs.logger.message(sender, true, "&2Reason: " + e.getDescription());
|
Logs.logger.message(sender, true, "&2Reason: " + e.getDescription());
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (File file : files)
|
for (File file : files)
|
||||||
@@ -118,12 +130,16 @@ public class LogHandler extends Thread
|
|||||||
new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
|
new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
|
||||||
matches += searchStream(inputReader, searchPattern, sender, file.getName());
|
matches += searchStream(inputReader, searchPattern, sender, file.getName());
|
||||||
inputReader.close();
|
inputReader.close();
|
||||||
|
if (isCanceled)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BufferedReader inputReader = new BufferedReader(new FileReader(file));
|
BufferedReader inputReader = new BufferedReader(new FileReader(file));
|
||||||
matches += searchStream(inputReader, searchPattern, sender, file.getName());
|
matches += searchStream(inputReader, searchPattern, sender, file.getName());
|
||||||
inputReader.close();
|
inputReader.close();
|
||||||
|
if (isCanceled)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
filesSearched++;
|
filesSearched++;
|
||||||
if (progress)
|
if (progress)
|
||||||
@@ -137,14 +153,15 @@ public class LogHandler extends Thread
|
|||||||
{
|
{
|
||||||
Logs.logger.message(sender, true,
|
Logs.logger.message(sender, true,
|
||||||
"An unexpected error occured, please check your search parameters and try again!");
|
"An unexpected error occured, please check your search parameters and try again!");
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(id);
|
||||||
|
|
||||||
if ((boolean) DataManager.getOrDefault(Utils.getID(sender), "Logs", "summary", true))
|
if ((boolean) DataManager.getOrDefault(Utils.getID(sender), "Logs", "summary", true))
|
||||||
{
|
{
|
||||||
String[] message = new String[2];
|
String[] message = new String[2];
|
||||||
message[0] = "§aYour search completed after " + (System.currentTimeMillis() - starttime) + "ms!";
|
message[0] = (isCanceled? "§aYou search was §cterminated§a after " : "§aYour search completed after ") + (System.currentTimeMillis() - starttime) + "ms!";
|
||||||
message[1] = "§7In total: §e" + filesSearched + "§7 File(s) and §e" + totalLines
|
message[1] = "§7In total: §e" + filesSearched + "§7 File(s) and §e" + totalLines
|
||||||
+ "§7 Line(s) were searched, §a" + matches + "§7 Match(es) were found!";
|
+ "§7 Line(s) were searched, §a" + matches + "§7 Match(es) were found!";
|
||||||
Logs.logger.message(sender, message);
|
Logs.logger.message(sender, message);
|
||||||
@@ -174,13 +191,15 @@ public class LogHandler extends Thread
|
|||||||
currentLine = 0;
|
currentLine = 0;
|
||||||
while ((line = inputReader.readLine()) != null)
|
while ((line = inputReader.readLine()) != null)
|
||||||
{
|
{
|
||||||
|
if (isCanceled)
|
||||||
|
break;
|
||||||
totalLines++;
|
totalLines++;
|
||||||
currentLine++;
|
currentLine++;
|
||||||
if (searchPattern.matcher(line).matches())
|
if (searchPattern.matcher(line).matches())
|
||||||
{
|
{
|
||||||
if (((p != null) && (!p.isOnline())))
|
if (((p != null) && (!p.isOnline())))
|
||||||
{
|
{
|
||||||
stillSearching.remove(sender);
|
activeSearches.remove(Utils.getID(sender));
|
||||||
throw new IOException("The player has left during the search. Aborting now.");
|
throw new IOException("The player has left during the search. Aborting now.");
|
||||||
}
|
}
|
||||||
LogEntry entry = new LogEntry(filename, line, currentLine, totalLines);
|
LogEntry entry = new LogEntry(filename, line, currentLine, totalLines);
|
||||||
|
|||||||
@@ -1,34 +1,34 @@
|
|||||||
command log {
|
command log {
|
||||||
perm utils.logs;
|
perm utils.logs;
|
||||||
alias logs;
|
alias logs;
|
||||||
|
type player;
|
||||||
|
|
||||||
search [string:file(s)] [string:search...] {
|
search [string:file(s)] [string:search...] {
|
||||||
run search_logs file(s) search;
|
run search_logs file(s) search;
|
||||||
help Performs the specified search operation on the logs. Wildcards are supported in filenames. Search string is a regex.;
|
help Performs the specified search operation on the logs. Wildcards are supported in filenames. Search string is a regex.;
|
||||||
type player;
|
}
|
||||||
|
terminate {
|
||||||
|
run terminate_search;
|
||||||
|
help Stops the search.;
|
||||||
}
|
}
|
||||||
format {
|
format {
|
||||||
run show_format;
|
run show_format;
|
||||||
help Displays your current log output format with an example result.;
|
help Displays your current log output format with an example result.;
|
||||||
type player;
|
|
||||||
}
|
}
|
||||||
format_help {
|
format_help {
|
||||||
run show_format_help;
|
run show_format_help;
|
||||||
help Displays all available placeholders for the formatting;
|
help Displays all available placeholders for the formatting;
|
||||||
type player;
|
|
||||||
}
|
}
|
||||||
option_help {
|
option_help {
|
||||||
run show_option_help;
|
run show_option_help;
|
||||||
help Displays all available options.;
|
help Displays all available options.;
|
||||||
type player;
|
|
||||||
}
|
}
|
||||||
set format [string:format] {
|
set format [string:format] {
|
||||||
run set_format format;
|
run set_format format;
|
||||||
help Sets a new log output format;
|
help Sets a new log output format;
|
||||||
type player;
|
|
||||||
}
|
}
|
||||||
set [string:option] [boolean:state] {
|
set [string:option] [boolean:state] {
|
||||||
run set_option option state;
|
run set_option option state;
|
||||||
help Allows you to enable or disable various features such as sumamries, live progress updates, etc...;
|
help Allows you to enable or disable various features such as sumamries, live progress updates, etc...;
|
||||||
type player;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,13 @@ public class Logs implements Module
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Command(hook = "terminate_search")
|
||||||
|
public boolean terminate_search(CommandSender sender)
|
||||||
|
{
|
||||||
|
LogHandler.cancel(sender);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// FORMATTING
|
// FORMATTING
|
||||||
@Command(hook = "show_format")
|
@Command(hook = "show_format")
|
||||||
public boolean show_format(CommandSender sender)
|
public boolean show_format(CommandSender sender)
|
||||||
|
|||||||
Reference in New Issue
Block a user