123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.debug.commands;
-
- import com.dmdirc.addons.debug.Debug;
- import com.dmdirc.addons.debug.DebugCommand;
- import com.dmdirc.commandparser.CommandArguments;
- import com.dmdirc.commandparser.commands.IntelligentCommand;
- import com.dmdirc.commandparser.commands.context.CommandContext;
- import com.dmdirc.interfaces.WindowModel;
- import com.dmdirc.ui.input.AdditionalTabTargets;
-
- import javax.annotation.Nonnull;
- import javax.inject.Inject;
- import javax.inject.Provider;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.slf4j.Marker;
-
- import static com.dmdirc.util.LogUtils.APP_ERROR;
- import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
- import static com.dmdirc.util.LogUtils.FATAL_USER_ERROR;
- import static com.dmdirc.util.LogUtils.USER_ERROR;
-
- /**
- * Creates DMDirc errors with the specified parameters.
- */
- public class FakeError extends DebugCommand implements IntelligentCommand {
-
- private static final Logger LOG = LoggerFactory.getLogger(FakeError.class);
-
- @Inject
- public FakeError(final Provider<Debug> commandProvider) {
- super(commandProvider);
- }
-
- @Override
- public String getName() {
- return "error";
- }
-
- @Override
- public String getUsage() {
- return "<user|app> [<low|medium|high|fatal|unknown>] - Creates an error"
- + " with the specified parameters, defaults to high priority.";
- }
-
- @Override
- public void execute(@Nonnull final WindowModel origin,
- final CommandArguments args, final CommandContext context) {
- if ((args.getArguments().length == 1
- || args.getArguments().length == 2)
- && "user".equals(args.getArguments()[0])) {
- raiseError(getLevel(args.getArguments()), false);
- } else if ((args.getArguments().length == 1
- || args.getArguments().length == 2)
- && "app".equals(args.getArguments()[0])) {
- raiseError(getLevel(args.getArguments()), true);
- } else {
- showUsage(origin, args.isSilent(), getName(), getUsage());
- }
- }
-
- private void raiseError(final String level, final boolean appError) {
- final Marker marker = appError ? APP_ERROR : USER_ERROR;
- switch (level.toUpperCase()) {
- case "FATAL":
- LOG.error(appError ? FATAL_APP_ERROR : FATAL_USER_ERROR, "Debug error message",
- new IllegalArgumentException());
- break;
- case "HIGH":
- LOG.error(marker, "Debug error message", new IllegalArgumentException());
- break;
- case "MEDIUM":
- LOG.warn(marker, "Debug error message", new IllegalArgumentException());
- break;
- case "INFO":
- LOG.info(marker, "Debug error message", new IllegalArgumentException());
- break;
- default:
- LOG.info(marker, "Debug error message", new IllegalArgumentException());
- }
- }
-
- /**
- * Returns the error level specified by the provided arguments.
- *
- * @param args command arguments
- *
- * @return Error level
- */
- private String getLevel(final String... args) {
- if (args.length >= 2) {
- return args[1].toUpperCase();
- } else {
- return "HIGH";
- }
- }
-
- @Override
- public AdditionalTabTargets getSuggestions(final int arg,
- final IntelligentCommandContext context) {
- final AdditionalTabTargets res = new AdditionalTabTargets();
- res.excludeAll();
-
- if (arg == 1) {
- res.add("user");
- res.add("app");
- } else if (arg == 2) {
- res.add("low");
- res.add("medium");
- res.add("high");
- res.add("fatal");
- }
-
- return res;
- }
-
- }
|