Search in sources :

Example 1 with ConsoleMessage

use of github.scarsz.discordsrv.objects.ConsoleMessage in project DiscordSRV by Scarsz.

the class ConsoleAppender method append.

@Override
public void append(LogEvent event) {
    final String eventLevel = event.getLevel().name().toUpperCase();
    String line = event.getMessage().getFormattedMessage();
    final DiscordSRV plugin = DiscordSRV.getPlugin();
    if (plugin.getConsoleChannel() == null)
        return;
    plugin.getConsoleMessageQueue().add(new ConsoleMessage(eventLevel, line));
}
Also used : DiscordSRV(github.scarsz.discordsrv.DiscordSRV) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage)

Example 2 with ConsoleMessage

use of github.scarsz.discordsrv.objects.ConsoleMessage in project DiscordSRV by Scarsz.

the class ConsoleMessageQueueWorker method chopHead.

/**
 * Chops down the head {@link ConsoleMessage} in the queue to parts that don't exceed the {@link Message#MAX_CONTENT_LENGTH} after formatting.
 *
 * @param wrapperLength The length of the message wrapper (prefix + suffix)
 */
private void chopHead(int wrapperLength) {
    ConsoleMessage consoleMessage;
    String formattedMessage;
    while ((consoleMessage = queue.poll()) != null) {
        formattedMessage = consoleMessage.toString();
        if (formattedMessage != null) {
            break;
        }
    }
    if (consoleMessage != null) {
        // length added to the message by the formatting
        int formattingDelta = consoleMessage.toString().length() - consoleMessage.getLine().length();
        // maximum line length, accounting for formatting, prefix/suffix, line break, and LINE_WRAP_INDENT
        int maxLineLength = Message.MAX_CONTENT_LENGTH - wrapperLength - formattingDelta - 2;
        String[] lines = WordUtils.wrap(consoleMessage.getLine(), maxLineLength, "\n", true).split("\n");
        // traverse each line in reverse order, to ensure they can be correctly added back to the head of the queue
        for (int i = lines.length - 1; i >= 1; i--) {
            String line = lines[i].trim();
            if (!line.isEmpty()) {
                queue.addFirst(new ConsoleMessage(consoleMessage.getEventLevel(), LINE_WRAP_INDENT + line));
            }
        }
        // omit indent on the first message
        queue.addFirst(new ConsoleMessage(consoleMessage.getEventLevel(), lines[0]));
    }
}
Also used : ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage)

Example 3 with ConsoleMessage

use of github.scarsz.discordsrv.objects.ConsoleMessage in project DiscordSRV by Scarsz.

the class ConsoleMessageQueueWorker method run.

@Override
public void run() {
    while (true) {
        try {
            // don't process, if we get disconnected, another measure to prevent UnknownHostException spam
            if (DiscordUtil.getJda().getStatus() != JDA.Status.CONNECTED) {
                Thread.sleep(3000);
                continue;
            }
            final String prefix = LangUtil.Message.CONSOLE_CHANNEL_MESSAGE_PREFIX.toString();
            final String suffix = LangUtil.Message.CONSOLE_CHANNEL_MESSAGE_SUFFIX.toString();
            final int wrapperLength = prefix.length() + suffix.length();
            // reuse message builder to avoid garbage - guaranteed to never grow beyond Message.MAX_CONTENT_LENGTH
            message.setLength(0);
            ConsoleMessage consoleMessage;
            // peek to avoid polling a message that we can't process from the queue
            while ((consoleMessage = queue.peek()) != null) {
                String formattedMessage = consoleMessage.toString();
                if (formattedMessage == null) {
                    queue.poll();
                    continue;
                }
                final int checkLength = formattedMessage.length() + wrapperLength + 1;
                if (message.length() + checkLength > Message.MAX_CONTENT_LENGTH) {
                    // if the line itself would be too long anyway, chop it down and put parts back in queue
                    if (checkLength > Message.MAX_CONTENT_LENGTH) {
                        chopHead(wrapperLength);
                    }
                    break;
                }
                message.append(formattedMessage).append('\n');
                // finally poll to actually remove the appended message
                queue.poll();
            }
            final String m = message.toString();
            if (StringUtils.isNotBlank(m)) {
                TextChannel textChannel = DiscordSRV.getPlugin().getConsoleChannel();
                if (textChannel != null && textChannel.getGuild().getSelfMember().hasPermission(textChannel, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE)) {
                    textChannel.sendMessage(prefix + m + suffix).queue();
                }
            }
            // make sure rate isn't less than every MIN_SLEEP_TIME_MILLIS because of rate limitations
            long sleepTimeMS = TimeUnit.SECONDS.toMillis(DiscordSRV.config().getIntElse(SLEEP_TIME_SECONDS_KEY, 0));
            if (sleepTimeMS < MIN_SLEEP_TIME_MILLIS) {
                sleepTimeMS = MIN_SLEEP_TIME_MILLIS;
            }
            Thread.sleep(sleepTimeMS);
        } catch (InterruptedException e) {
            DiscordSRV.debug("Broke from Console Message Queue Worker thread: sleep interrupted");
            return;
        } catch (Throwable e) {
            DiscordSRV.error("Error in Console Message Queue worker: " + e.getMessage());
            DiscordSRV.debug(e);
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) ConsoleMessage(github.scarsz.discordsrv.objects.ConsoleMessage)

Aggregations

ConsoleMessage (github.scarsz.discordsrv.objects.ConsoleMessage)3 DiscordSRV (github.scarsz.discordsrv.DiscordSRV)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1