Synchronous logging

Changelog: fixed
This commit is contained in:
Michael Chen 2022-01-12 19:03:51 +01:00
parent 2be3a6e0c7
commit bef9d16888
No known key found for this signature in database
GPG Key ID: 1CBC7AA5671437BB
2 changed files with 21 additions and 18 deletions

View File

@ -19,7 +19,7 @@ public class ConnectedComputer {
IChunkWaiter? waiter;
lock (_syncRoot) {
if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
Program.LogWarning("Socket", $"Invalid wait id '{msg.AnswerId}'!");
Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
return;
}
}
@ -62,12 +62,12 @@ public class ConnectedComputer {
lock (_syncRoot) {
if (_chunks is null) _chunks = new string[totalChunks];
else if (_chunks.Length != totalChunks) {
Program.LogError(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!"));
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!"));
return;
}
ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed
if (chunk is not null) {
Program.LogError(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!"));
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!"));
return;
}
chunk = value;
@ -86,7 +86,7 @@ public class ConnectedComputer {
var id = _rnd.Next();
if (!_waits.ContainsKey(id))
return id;
Program.LogWarning(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
Program.LogWarningAsync(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!");
}
}

View File

@ -83,7 +83,7 @@ public class Program : IDisposable {
private async Task<bool> HasValidChannels() {
if (await GetValidChannels(_whitelistedChannels).ToArrayAsync() is not { Length: > 0 } channels) {
await LogError(BotSource, new InvalidOperationException("No valid textchannel was whitelisted!"));
await LogErrorAsync(BotSource, new InvalidOperationException("No valid textchannel was whitelisted!"));
return false;
}
_channels = channels;
@ -100,16 +100,16 @@ public class Program : IDisposable {
foreach (var channelId in ids) {
var channel = await _client.GetChannelAsync(channelId);
if (channel is not ITextChannel textChannel) {
if (channel is null) await LogWarning(BotSource, $"Channel with id [{channelId}] does not exist!");
else await LogWarning(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
if (channel is null) await LogWarningAsync(BotSource, $"Channel with id [{channelId}] does not exist!");
else await LogWarningAsync(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
continue;
}
if (textChannel.Guild is RestGuild guild) {
await guild.UpdateAsync();
await LogInfo(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {guild.Name} [{guild.Id}]");
await LogInfoAsync(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {guild.Name} [{guild.Id}]");
} else {
await LogWarning(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!");
await LogWarningAsync(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!");
}
yield return textChannel;
}
@ -124,9 +124,9 @@ public class Program : IDisposable {
string role => throw new ArgumentException($"Invalid role '{role}'!")
};
AddComputerSocket(socket, pc);
await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}");
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}");
} catch (ArgumentException e) {
await LogError(WebSocketSource, e);
await LogErrorAsync(WebSocketSource, e);
}
}
@ -140,11 +140,11 @@ public class Program : IDisposable {
private async Task SocketClosed(IWebSocketConnection socket) {
RemoveComputerSocket(socket);
await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!");
await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!");
}
private static async Task SocketOpened(IWebSocketConnection socket)
=> await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!");
=> await LogInfoAsync(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!");
private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) {
if (arg is not SocketUserMessage message) return;
@ -159,7 +159,7 @@ public class Program : IDisposable {
return;
}
await LogInfo("Discord", $"[{arg.Author.Username}] {arg.Content}");
await LogInfoAsync("Discord", $"[{arg.Author.Username}] {arg.Content}");
// TODO: Relay Message to Chat Receiver
}
@ -183,16 +183,19 @@ public class Program : IDisposable {
private bool IsChannelWhitelisted(ISocketMessageChannel channel)
=> _whitelistedChannels.Contains(channel.Id);
public static ConfiguredTaskAwaitable LogInfo(string source, string message) => LogAsync(new(LogSeverity.Info, source, message)).ConfigureAwait(false);
public static ConfiguredTaskAwaitable LogWarning(string source, string message) => LogAsync(new(LogSeverity.Warning, source, message)).ConfigureAwait(false);
public static ConfiguredTaskAwaitable LogError(string source, Exception exception) => LogAsync(new(LogSeverity.Error, source, exception?.Message, exception)).ConfigureAwait(false);
public static ConfiguredTaskAwaitable LogInfoAsync(string source, string message) => LogAsync(new(LogSeverity.Info, source, message)).ConfigureAwait(false);
public static ConfiguredTaskAwaitable LogWarningAsync(string source, string message) => LogAsync(new(LogSeverity.Warning, source, message)).ConfigureAwait(false);
public static ConfiguredTaskAwaitable LogErrorAsync(string source, Exception exception) => LogAsync(new(LogSeverity.Error, source, exception?.Message, exception)).ConfigureAwait(false);
public static void LogInfo(string source, string message) => Log(new(LogSeverity.Info, source, message));
public static void LogWarning(string source, string message) => Log(new(LogSeverity.Warning, source, message));
public static void LogError(string source, Exception exception) => Log(new(LogSeverity.Error, source, exception?.Message, exception));
private static async Task LogAsync(LogMessage msg) {
Log(msg);
await Task.CompletedTask;
}
private static void Log(LogMessage msg) {
public static void Log(LogMessage msg) {
lock (LogLock)
Console.WriteLine(msg.ToString());
}