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; IChunkWaiter? waiter;
lock (_syncRoot) { lock (_syncRoot) {
if (!_waits.TryGetValue(msg.AnswerId, out waiter)) { if (!_waits.TryGetValue(msg.AnswerId, out waiter)) {
Program.LogWarning("Socket", $"Invalid wait id '{msg.AnswerId}'!"); Program.LogWarningAsync("Socket", $"Invalid wait id '{msg.AnswerId}'!");
return; return;
} }
} }
@ -62,12 +62,12 @@ public class ConnectedComputer {
lock (_syncRoot) { lock (_syncRoot) {
if (_chunks is null) _chunks = new string[totalChunks]; if (_chunks is null) _chunks = new string[totalChunks];
else if (_chunks.Length != 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; return;
} }
ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed
if (chunk is not null) { 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; return;
} }
chunk = value; chunk = value;
@ -86,7 +86,7 @@ public class ConnectedComputer {
var id = _rnd.Next(); var id = _rnd.Next();
if (!_waits.ContainsKey(id)) if (!_waits.ContainsKey(id))
return 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() { private async Task<bool> HasValidChannels() {
if (await GetValidChannels(_whitelistedChannels).ToArrayAsync() is not { Length: > 0 } channels) { 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; return false;
} }
_channels = channels; _channels = channels;
@ -100,16 +100,16 @@ public class Program : IDisposable {
foreach (var channelId in ids) { foreach (var channelId in ids) {
var channel = await _client.GetChannelAsync(channelId); var channel = await _client.GetChannelAsync(channelId);
if (channel is not ITextChannel textChannel) { if (channel is not ITextChannel textChannel) {
if (channel is null) await LogWarning(BotSource, $"Channel with id [{channelId}] does not exist!"); if (channel is null) await LogWarningAsync(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}]!"); else await LogWarningAsync(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!");
continue; continue;
} }
if (textChannel.Guild is RestGuild guild) { if (textChannel.Guild is RestGuild guild) {
await guild.UpdateAsync(); 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 { } 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; yield return textChannel;
} }
@ -124,9 +124,9 @@ public class Program : IDisposable {
string role => throw new ArgumentException($"Invalid role '{role}'!") string role => throw new ArgumentException($"Invalid role '{role}'!")
}; };
AddComputerSocket(socket, pc); 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) { } 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) { private async Task SocketClosed(IWebSocketConnection socket) {
RemoveComputerSocket(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) 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) { private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) {
if (arg is not SocketUserMessage message) return; if (arg is not SocketUserMessage message) return;
@ -159,7 +159,7 @@ public class Program : IDisposable {
return; return;
} }
await LogInfo("Discord", $"[{arg.Author.Username}] {arg.Content}"); await LogInfoAsync("Discord", $"[{arg.Author.Username}] {arg.Content}");
// TODO: Relay Message to Chat Receiver // TODO: Relay Message to Chat Receiver
} }
@ -183,16 +183,19 @@ public class Program : IDisposable {
private bool IsChannelWhitelisted(ISocketMessageChannel channel) private bool IsChannelWhitelisted(ISocketMessageChannel channel)
=> _whitelistedChannels.Contains(channel.Id); => _whitelistedChannels.Contains(channel.Id);
public static ConfiguredTaskAwaitable LogInfo(string source, string message) => LogAsync(new(LogSeverity.Info, source, message)).ConfigureAwait(false); public static ConfiguredTaskAwaitable LogInfoAsync(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 LogWarningAsync(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 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) { private static async Task LogAsync(LogMessage msg) {
Log(msg); Log(msg);
await Task.CompletedTask; await Task.CompletedTask;
} }
private static void Log(LogMessage msg) { public static void Log(LogMessage msg) {
lock (LogLock) lock (LogLock)
Console.WriteLine(msg.ToString()); Console.WriteLine(msg.ToString());
} }