further refine splitting of logs

This commit is contained in:
Florian Stinglmayr 2024-09-18 21:10:41 +02:00
parent 8eaf94f634
commit 2bf8d9018d
2 changed files with 36 additions and 16 deletions

View File

@ -228,7 +228,7 @@ public class DiscordLogGenerator {
int curchunk = 0; int curchunk = 0;
string botheader = BotHeader().Trim() + "\n"; string botheader = BotHeader().Trim() + "\n";
// Leave room for botheder and some leeway // Leave room for botheader and some leeway
int maxlength = (2000 - botheader.Length - 10); int maxlength = (2000 - botheader.Length - 10);
// Then try to collate chunks // Then try to collate chunks
@ -249,6 +249,14 @@ public class DiscordLogGenerator {
chunks[i] = chunks[i].Insert(0, botheader); chunks[i] = chunks[i].Insert(0, botheader);
} }
// Now finally check if any one chunk is bigger than max length
for (int i = 0; i < chunks.Count; i++) {
if (chunks[i].Length > maxcount) {
throw new ApplicationException(
string.Format("a chunk turned out bigger than {0}", maxcount));
}
}
return chunks.ToArray(); return chunks.ToArray();
} }
} }

View File

@ -653,22 +653,36 @@ public partial class MainWindow : MetroWindow {
} }
} }
private void PostToDiscordWebhook(DiscordWebhook hook) { private void PostToDiscordWebhook(IEnumerable<DiscordWebhook> hooks) {
if (string.IsNullOrEmpty(DiscordLog.Text)) { if (string.IsNullOrEmpty(DiscordLog.Text)) {
return; return;
} }
try {
DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator;
var chunks = discord.SplitLog(DiscordLog.Text);
foreach (var chunk in chunks) { DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator;
DiscordPoster.PostToDiscord(hook, chunk); string[] chunks;
try {
chunks = discord.SplitLog(DiscordLog.Text);
} catch (Exception) {
MessageBox.Show(
"The log could not be split into discord appropriate length.\n" +
"This happens with the bigger logs formats if you do lots of things in one system.\n" +
"Try posting the log in the OneLine format.",
"Sorry!", MessageBoxButton.OK, MessageBoxImage.Error
);
return;
}
foreach (var hook in hooks) {
try {
foreach (var chunk in chunks) {
DiscordPoster.PostToDiscord(hook, chunk);
}
Log(string.Format("successfully posted to discord webhook `{0}`", hook.Name));
} catch (Exception ex) {
Log(string.Format("failed to post to discord webhook `{0}`: {1}",
hook.Name, ex.Message));
} }
Log(string.Format("successfully posted to discord webhook {0}",
hook.Name));
} catch (Exception ex) {
Log(string.Format("failed to post to discord webhook {0}: {1}",
hook.Name, ex.Message));
} }
} }
@ -683,12 +697,10 @@ public partial class MainWindow : MetroWindow {
if (hook == null) { if (hook == null) {
return; return;
} }
PostToDiscordWebhook(hook); PostToDiscordWebhook(new DiscordWebhook[] { hook });
} }
private void PostToAll_Click(object sender, RoutedEventArgs e) { private void PostToAll_Click(object sender, RoutedEventArgs e) {
foreach (DiscordWebhook hook in Config.Global.Webhooks) { PostToDiscordWebhook(Config.Global.Webhooks);
PostToDiscordWebhook(hook);
}
} }
} }