Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarsz authored Aug 27, 2022
2 parents 794d25d + eb19ea9 commit 8c381b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.scarsz</groupId>
<artifactId>jdaappender</artifactId>
<version>1.0.2</version>
<version>1.0.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
84 changes: 50 additions & 34 deletions src/main/java/me/scarsz/jdaappender/ChannelLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Getter;
import lombok.SneakyThrows;
import lombok.Synchronized;
import me.scarsz.jdaappender.adapter.JavaLoggingAdapter;
import me.scarsz.jdaappender.adapter.SystemLoggingAdapter;
import net.dv8tion.jda.api.JDA;
Expand Down Expand Up @@ -119,37 +120,42 @@ public void flush() {
TextChannel loggingChannel = channelSupplier.get();
if (loggingChannel != null && loggingChannel.getJDA().getStatus() == JDA.Status.CONNECTED) {
LogItem logItem;
while ((logItem = messageQueue.poll()) != null) {
if (logItem.getMessage() == null && logItem.getThrowable() == null) {
// Nothing to log, likely due to being cleared during formatting
continue;
synchronized (stack) {
while ((logItem = messageQueue.poll()) != null) {
if (logItem.getMessage() == null && logItem.getThrowable() == null) {
// Nothing to log, likely due to being cleared during formatting
continue;
}

if (logItem.getFormattedLength(config) > LogItem.CLIPPING_MAX_LENGTH) {
throw new IllegalStateException("Log item longer than Discord's max content length: " + logItem);
}

if (!canFit(logItem)) {
if (stack.size() == 0) throw new IllegalStateException("Can't fit LogItem into empty stack: " + logItem);
dumpStack();
}

stack.add(logItem);
dirtyBit.set(true);
}

if (logItem.getFormattedLength(config) > LogItem.CLIPPING_MAX_LENGTH) {
throw new IllegalStateException("Log item longer than Discord's max content length: " + logItem);
if (dirtyBit.get() && stack.size() > 0) {
currentMessage = updateMessage();
dirtyBit.set(false);
}

if (!canFit(logItem)) {
if (stack.size() == 0) throw new IllegalStateException("Can't fit LogItem into empty stack: " + logItem);
dumpStack();
}

stack.add(logItem);
dirtyBit.set(true);
}

if (dirtyBit.get() && stack.size() > 0) {
currentMessage = updateMessage();
dirtyBit.set(false);
}
}
}

/**
* Push the current LogItem stack to Discord, then dump the stack, starting a new message.
*/
@Synchronized("stack")
public void dumpStack() {
if (stack.size() > 0) updateMessage();
try {
if (stack.size() > 0) updateMessage();
} catch (IllegalStateException ignored) {}
stack.clear();
currentMessage = null;
}
Expand All @@ -159,6 +165,7 @@ public void dumpStack() {
* @param logItem the log item to check for fitment of
* @return true if the log item will fit, false if it won't and a new stack + message will be started to accommodate
*/
@Synchronized("stack")
public boolean canFit(LogItem logItem) {
int lengthSum = 0;
for (LogItem item : stack) {
Expand Down Expand Up @@ -187,23 +194,31 @@ public boolean canFit(LogItem logItem) {
return lengthSum + logItem.format(config).length() + 5 <= Message.MAX_CONTENT_LENGTH;
}

private Message updateMessage() {
if (stack.size() == 0) throw new IllegalStateException("No messages on stack");
private Message updateMessage() throws IllegalStateException {
TextChannel channel;
StringJoiner joiner;

StringJoiner joiner = new StringJoiner("\n");
for (LogItem item : stack) {
boolean willSplit = config.isSplitCodeBlockForLinks() && item.getMessage() != null && URL_PATTERN.matcher(item.getMessage()).find();
synchronized (stack) {
if (stack.size() == 0) throw new IllegalStateException("No messages on stack");

String formatted = item.format(config);
channel = channelSupplier.get();
if (channel == null) throw new IllegalStateException("Channel unavailable");

if (!willSplit && config.isColored()) {
formatted = item.getLevel().getLevelSymbol() + " " + formatted;
}
joiner = new StringJoiner("\n");
for (LogItem item : stack) {
boolean willSplit = config.isSplitCodeBlockForLinks() && item.getMessage() != null && URL_PATTERN.matcher(item.getMessage()).find();

if (willSplit) {
joiner.add("```\n" + formatted + "\n```" + (config.isColored() ? "diff" : ""));
} else {
joiner.add(formatted);
String formatted = item.format(config);

if (!willSplit && config.isColored()) {
formatted = item.getLevel().getLevelSymbol() + " " + formatted;
}

if (willSplit) {
joiner.add("```\n" + formatted + "\n```" + (config.isColored() ? "diff" : ""));
} else {
joiner.add(formatted);
}
}
}

Expand Down Expand Up @@ -231,7 +246,7 @@ private Message updateMessage() {
}
}

return channelSupplier.get().sendMessage(full).complete();
return channel.sendMessage(full).complete();
}

/**
Expand All @@ -242,6 +257,7 @@ private Message updateMessage() {
*/
public void recreateChannel(@Nullable String reason) {
TextChannel channel = channelSupplier.get();
if (channel == null) throw new IllegalStateException("Channel unavailable");
channel.createCopy()
.setPosition(channel.getPositionRaw())
.flatMap(textChannel -> {
Expand Down

0 comments on commit 8c381b8

Please sign in to comment.