summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/modules/logs/Logs.java
blob: 054befddbaa1e348ca7fcf574d00714bbc05244d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.redstoner.modules.logs;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.command.CommandSender;

import com.nemez.cmdmgr.Command;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.modules.Module;
import com.redstoner.modules.ModuleLogger;
import com.redstoner.modules.datamanager.DataManager;

@Commands(CommandHolderType.File)
@Version(major = 5, minor = 0, revision = 0, compatible = 4)
public class Logs implements Module
{
	public static final String defaultFormat = "§7 > %f: %r";
	private final LogEntry example_1 = new LogEntry("1970-01-01-2.log.gz",
			"[01:23:45] [Async Chat Thread - #1337/INFO]:  §aFooBar §7→ §4THIS SERVER SUCKS", 14, 73);
	private final LogEntry example_2 = new LogEntry("1970-01-01-2.log.gz",
			"[01:23:45] [Server thread/INFO]: admin issued server command: /ban FooBar Ab00se", 15, 74);
	protected static ModuleLogger logger;
	
	@Override
	public boolean onEnable()
	{
		Module.super.onEnable();
		logger = getLogger();
		return true;
	}
	
	public static File getLogsDir()
	{
		return new File((String) DataManager.getConfigOrDefault("logs.root", "logs"));
	}
	
	@Command(hook = "search_logs")
	public boolean search_logs(CommandSender sender, String files, String search)
	{
		LogHandler handler = new LogHandler(sender, search, files);
		handler.doSearch();
		return true;
	}
	
	@Command(hook = "terminate_search")
	public boolean terminate_search(CommandSender sender)
	{
		LogHandler.cancel(sender);
		return true;
	}
	
	// FORMATTING
	@Command(hook = "show_format")
	public boolean show_format(CommandSender sender)
	{
		showExample(sender);
		return true;
	}
	
	@Command(hook = "set_format")
	public boolean set_format(CommandSender sender, String format)
	{
		if (format.equals("--reset"))
			format = defaultFormat;
		format = format.replace("&", "§").replace("$$", "&");
		DataManager.setData(sender, "format", format);
		showExample(sender, format);
		return true;
	}
	
	private void showExample(CommandSender sender)
	{
		showExample(sender, (String) DataManager.getOrDefault(sender, "format", defaultFormat));
	}
	
	private void showExample(CommandSender sender, String format)
	{
		sender.sendMessage(getLogger().getHeader());
		sender.sendMessage("Your format is: " + format);
		sender.sendMessage("Here's an example of what it would look like in an actual log search:");
		boolean colors = (boolean) DataManager.getOrDefault(sender, "colors", true);
		if ((boolean) DataManager.getOrDefault(sender, "progress", true))
		{
			sender.sendMessage("§7So far, §e1§7/§e2§7 File(s) and §e68§7 Line(s) were searched.");
		}
		sender.sendMessage(example_1.applyFormat(format, colors));
		sender.sendMessage(example_2.applyFormat(format, colors));
		if ((boolean) DataManager.getOrDefault(sender, "summary", true))
		{
			sender.sendMessage("§aSearch completed after 39ms!");
			sender.sendMessage(
					"§7In total: §e2§7 File(s) and §e105§7 Line(s) were searched, §a2§7 Match(es) were found!");
		}
	}
	
	@Command(hook = "show_format_help")
	public boolean format_help(CommandSender sender)
	{
		//@noformat
		String[] format_help = new String[] {
				" &e%l&cine&7 -> Linenumber in the current file", 
				" &e%L&cine&7 -> Global linenumber (sum of all previous files + current line)", 
				" &e%f&cile&7 -> Complete filename", 
				" &e%r&caw&7 -> The raw line containing the text as it appears in the logs", 
				"",
				" &7Use %% to gain a literal %."};
		//@format
		getLogger().message(sender, format_help);
		return true;
	}
	
	// SEARCH OPTIONS
	@Command(hook = "show_option_help")
	public boolean show_options(CommandSender sender)
	{
		List<String> options = new ArrayList<>(Option.values().length + 1);
		options.add("Available options are:");
		for (Option o : Option.values())
			options.add(" - " + o.toString());
		getLogger().message(sender, options.toArray(new String[] {}));
		return true;
	}
	
	@Command(hook = "set_option")
	public boolean set_option(CommandSender sender, String option, boolean state)
	{
		option = option.toLowerCase();
		Option o = null;
		try
		{
			o = Option.valueOf(option);
		}
		catch (IllegalArgumentException e)
		{}
		if (o == null)
		{
			getLogger().message(sender, true,
					"Invalid option! To get a list of all available options, run &e/logs option_help");
			return true;
		}
		DataManager.setData(sender, option, state);
		getLogger().message(sender,
				"Successfully turned displaying of &e" + option + (state ? " &aon&7!" : " &coff&7!"));
		return true;
	}
}

enum Option
{
	summary,
	progress,
	colors
}