Skip to content

Commit 94ca12f

Browse files
committed
[Core] [NativeAPI] Pass Exception to BotLogEvent
1 parent 9a016ea commit 94ca12f

11 files changed

Lines changed: 39 additions & 34 deletions

File tree

Lagrange.Core.NativeAPI/NativeModel/Event/BotLogEventStruct.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static implicit operator BotLogEvent(BotLogEventStruct e)
2929
return new BotLogEvent(
3030
Encoding.UTF8.GetString(e.Tag),
3131
(LogLevel)e.Level,
32-
Encoding.UTF8.GetString(e.Message)
32+
Encoding.UTF8.GetString(e.Message),
33+
null // TODO: Handle exception if needed
3334
);
3435
}
3536
}

Lagrange.Core/BotContext.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,44 @@ internal BotContext(BotConfig config, BotKeystore keystore, BotAppInfo appInfo)
4444

4545
#region Shortcut Methods
4646

47-
public void LogCritical(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
47+
public void LogCritical(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, Exception? exception = null, params object?[] args)
4848
{
49-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Critical, string.Format(text, args)));
49+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Critical, string.Format(text, args), exception));
5050
}
5151

52-
public void LogError(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
52+
public void LogError(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, Exception? exception = null, params object?[] args)
5353
{
5454
if (Config.LogLevel > LogLevel.Error) return;
5555

56-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Error, string.Format(text, args)));
56+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Error, string.Format(text, args), exception));
5757
}
5858

59-
public void LogWarning(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
59+
public void LogWarning(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, Exception? exception = null, params object?[] args)
6060
{
6161
if (Config.LogLevel > LogLevel.Warning) return;
6262

63-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Warning, string.Format(text, args)));
63+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Warning, string.Format(text, args), exception));
6464
}
6565

6666
public void LogInfo(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
6767
{
6868
if (Config.LogLevel > LogLevel.Information) return;
6969

70-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Information, string.Format(text, args)));
70+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Information, string.Format(text, args), null));
7171
}
7272

7373
public void LogDebug(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
7474
{
7575
if (Config.LogLevel > LogLevel.Debug) return;
7676

77-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Debug, string.Format(text, args)));
77+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Debug, string.Format(text, args), null));
7878
}
7979

8080
public void LogTrace(string tag, [StringSyntax(StringSyntaxAttribute.CompositeFormat)] string text, params object?[] args)
8181
{
8282
if (Config.LogLevel > LogLevel.Trace) return;
8383

84-
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Trace, string.Format(text, args)));
84+
EventInvoker.PostEvent(new BotLogEvent(tag, LogLevel.Trace, string.Format(text, args), null));
8585
}
8686

8787
#endregion

Lagrange.Core/Common/IAndroidBotSignProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ internal class DefaultAndroidBotSignProvider(BotContext context) : IAndroidBotSi
187187
}
188188
catch (Exception e)
189189
{
190-
context.LogWarning(Tag, "Failed to get sign: {0}", e.Message);
190+
context.LogWarning(Tag, "Failed to get sign: {0}", e, e.Message);
191191
return null;
192192
}
193193
}
@@ -213,7 +213,7 @@ public async Task<byte[]> GetEnergy(long uin, string data)
213213
}
214214
catch (Exception e)
215215
{
216-
context.LogWarning(Tag, "Failed to get energy: {0}", e.Message);
216+
context.LogWarning(Tag, "Failed to get energy: {0}", e, e.Message);
217217
return [];
218218
}
219219
}
@@ -238,7 +238,7 @@ public async Task<byte[]> GetDebugXwid(long uin, string data)
238238
}
239239
catch (Exception e)
240240
{
241-
context.LogWarning(Tag, "Failed to get debug_xwid: {0}", e.Message);
241+
context.LogWarning(Tag, "Failed to get debug_xwid: {0}", e, e.Message);
242242
return [];
243243
}
244244
}

Lagrange.Core/Events/EventArgs/BotLogEvent.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ public enum LogLevel
1010
Critical = 5
1111
}
1212

13-
public class BotLogEvent(string tag, LogLevel level, string message) : EventBase
13+
public class BotLogEvent(string tag, LogLevel level, string message, Exception? Exception) : EventBase
1414
{
1515
public string Tag { get; } = tag;
1616

1717
public LogLevel Level { get; } = level;
1818

1919
public string Message { get; } = message;
20+
21+
public Exception? Exception { get; } = Exception;
2022

2123
public override string ToEventMessage() => $"[{Tag}] [{Level.ToString().ToUpper()}]: {Message}";
2224
}

Lagrange.Core/Events/EventInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal void PostEvent<T>(T ev) where T : EventBase => Task.Run(async () =>
6464
Console.WriteLine($"Failed to post event: {ev}");
6565
return;
6666
}
67-
PostEvent(new BotLogEvent(Tag, LogLevel.Error, ex.ToString()));
67+
PostEvent(new BotLogEvent(Tag, LogLevel.Error, ex.ToString(), ex));
6868
}
6969
});
7070

Lagrange.Core/Internal/Context/EventContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public async Task HandleServerPacket(SsoPacket packet)
140140
}
141141
catch (ServiceNotFoundException e)
142142
{
143-
_context.LogWarning(Tag, "Service not found for command: {0}", e.Command);
143+
_context.LogWarning(Tag, "Service not found for command: {0}", e, e.Command);
144144
}
145145
}
146146

Lagrange.Core/Internal/Context/HighwayContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void OnDisconnect() { }
7777

7878
public void OnSocketError(Exception e, ReadOnlyMemory<byte> data = default)
7979
{
80-
_context.LogError(Tag, "Highway Socket error: {0}", e.Message);
80+
_context.LogError(Tag, "Highway Socket error: {0}", e, e.Message);
8181
if (e.StackTrace is { } stack) _context.LogDebug(Tag, stack);
8282
}
8383

@@ -182,7 +182,7 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
182182
}
183183
catch (Exception e)
184184
{
185-
_context.LogError(Tag, "Highway HTTP error: {0}", e.Message);
185+
_context.LogError(Tag, "Highway HTTP error: {0}", e, e.Message);
186186
if (e.StackTrace is { } stack) _context.LogDebug(Tag, stack);
187187
}
188188
finally

Lagrange.Core/Internal/Context/SocketContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task<bool> Connect()
5151
bool connected = await _client.Connect(servers[0]);
5252

5353
if (connected) _context.LogInfo(Tag, "Connected to the server {0}", servers[0]);
54-
else _context.LogError(Tag, "Failed to connect to the server {0}", servers[0]);
54+
else _context.LogError(Tag, "Failed to connect to the server {0}", null,servers[0]);
5555

5656
return connected;
5757
}

Lagrange.Core/Internal/Logic/WtExchangeLogic.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public async ValueTask Incoming(ProtocolEvent e)
5858
{
5959
case KickEvent kick:
6060
{
61-
_context.LogError(Tag, "Kicked by server: {0} | {1}", kick.TipsTitle, kick.TipsInfo);
61+
_context.LogError(Tag, "Kicked by server: {0} | {1}", null, kick.TipsTitle, kick.TipsInfo);
6262
_context.EventInvoker.PostEvent(new BotOfflineEvent(BotOfflineEvent.Reasons.Kicked, (kick.TipsTitle, kick.TipsInfo)));
6363
_context.IsOnline = false;
6464
_timers[SsoHeartBeatTag].Change(Timeout.Infinite, Timeout.Infinite);
@@ -109,7 +109,7 @@ public async Task<bool> Logout()
109109
}
110110
else
111111
{
112-
_context.LogError(Tag, "Logout failed, directly offline {0}", result.Message);
112+
_context.LogError(Tag, "Logout failed, directly offline {0}", null, result.Message);
113113
}
114114
}
115115

@@ -121,7 +121,7 @@ private async Task<bool> ManualLogin(long uin, string? password)
121121
{
122122
if (string.IsNullOrEmpty(password) && _context.Config.Protocol.IsAndroid())
123123
{
124-
_context.LogError(Tag, "Android Platform can not use QRLogin, Please fill in password");
124+
_context.LogCritical(Tag, "Android Platform can not use QRLogin, Please fill in password");
125125
return false;
126126
}
127127

@@ -150,7 +150,7 @@ private async Task<bool> ManualLogin(long uin, string? password)
150150
if (await _transEmpSource.Task) return await Online();
151151
break;
152152
default:
153-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.State, result.Tips);
153+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.State, result.Tips);
154154
_context.EventInvoker.PostEvent(new BotLoginEvent((int)result.State, result.Tips));
155155
break;
156156
}
@@ -259,7 +259,7 @@ private async Task<bool> ManualLogin(long uin, string? password)
259259
}
260260
else
261261
{
262-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.RetCode, result.Error);
262+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.RetCode, result.Error);
263263
_context.EventInvoker.PostEvent(new BotLoginEvent(result.RetCode, result.Error));
264264
}
265265
}
@@ -315,7 +315,7 @@ private async Task<bool> ManualLogin(long uin, string? password)
315315
if (await _transEmpSource.Task) return await Online();
316316
break;
317317
default:
318-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.State, result.Tips);
318+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.State, result.Tips);
319319
_context.EventInvoker.PostEvent(new BotLoginEvent((int)result.State, result.Tips));
320320
return false;
321321
}
@@ -344,7 +344,7 @@ public async Task<long> ResolveUinByQid(string qid)
344344
}
345345
else if (result is { Error: { } error })
346346
{
347-
_context.LogError(Tag, "Failed to resolve uin: {0} | {1}", error.Item1, error.Item2);
347+
_context.LogError(Tag, "Failed to resolve uin: {0} | {1}", null, error.Item1, error.Item2);
348348
}
349349

350350
return 0;
@@ -398,7 +398,7 @@ private async Task<bool> Online()
398398
}
399399
catch (LagrangeException e) when (e.InnerException is InvalidOperationException invalid)
400400
{
401-
_context.LogError(Tag, "Failed to send InfoSyncEvent: {0}", invalid.Message);
401+
_context.LogError(Tag, "Failed to send InfoSyncEvent: {0}", null, invalid.Message);
402402
}
403403

404404
return false;
@@ -451,7 +451,7 @@ private void OnQueryState(object? state) => Task.Run(async () =>
451451
}
452452
else
453453
{
454-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.State, result.Tips);
454+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.State, result.Tips);
455455
_transEmpSource.TrySetResult(false);
456456
}
457457

@@ -469,15 +469,15 @@ private void OnQueryState(object? state) => Task.Run(async () =>
469469
}
470470
else
471471
{
472-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.RetCode, result.Error);
472+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.RetCode, result.Error);
473473
_transEmpSource.TrySetResult(false);
474474
}
475475

476476
_context.EventInvoker.PostEvent(new BotLoginEvent(result.RetCode, result.Error));
477477
}
478478
break;
479479
case { State: TransEmp12EventResp.TransEmpState.Canceled or TransEmp12EventResp.TransEmpState.Invalid or TransEmp12EventResp.TransEmpState.CodeExpired }:
480-
_context.LogCritical(Tag, "QR Code State: {0}", transEmp12.State);
480+
_context.LogCritical(Tag, "QR Code State: {0}", null, transEmp12.State);
481481

482482
_transEmpSource.TrySetResult(false);
483483
_timers[QueryStateTag].Change(Timeout.Infinite, Timeout.Infinite);
@@ -521,7 +521,7 @@ private void OnNewDevice(object? state) => Task.Run(async () =>
521521
}
522522
else
523523
{
524-
_context.LogError(Tag, "Login failed: {0} | Message: {1}", result.State, result.Tips);
524+
_context.LogError(Tag, "Login failed: {0} | Message: {1}", null, result.State, result.Tips);
525525
_transEmpSource.TrySetResult(false);
526526
}
527527
}

Lagrange.Core/Internal/Services/OidbService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override async ValueTask<TEventResp> Parse(ReadOnlyMemory<byte> input,
3030
var oidb = ProtoHelper.Deserialize<Oidb>(input.Span);
3131
if (oidb.Result != 0)
3232
{
33-
context.LogWarning(Tag, "Error: {0}, Message: {1}", oidb.Result, oidb.Message);
33+
context.LogWarning(Tag, "Error: {0}, Message: {1}", null, oidb.Result, oidb.Message);
3434
throw new OperationException((int)oidb.Result, oidb.Message);
3535
}
3636

0 commit comments

Comments
 (0)