Skip to content

Commit d6a93ca

Browse files
committed
chore: Add logging to all JSON schema/parsing errors and command messages
We don't receive command messages at high frequency, and schema/parsing errors should be extremely visible. Add better logging for both of these.
1 parent d088136 commit d6a93ca

9 files changed

Lines changed: 22 additions & 12 deletions

File tree

Buttplug.Core/ButtplugJsonMessageParser.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,44 @@ public ButtplugMessage[] Deserialize(string aJsonMsg)
7777
_bpLogger.Trace($"Got JSON Message: {aJsonMsg}");
7878

7979
var res = new List<ButtplugMessage>();
80-
JArray a;
80+
JArray msgArray;
8181
try
8282
{
83-
a = JArray.Parse(aJsonMsg);
83+
msgArray = JArray.Parse(aJsonMsg);
8484
}
8585
catch (JsonReaderException e)
8686
{
87-
_bpLogger.Debug($"Not valid JSON: {aJsonMsg}");
88-
_bpLogger.Debug(e.Message);
89-
res.Add(new Error("Not valid JSON", ErrorClass.ERROR_MSG, ButtplugConsts.SystemMsgId));
87+
res.Add(_bpLogger.LogErrorMsg(ButtplugConsts.SystemMsgId, ErrorClass.ERROR_MSG, "Not valid JSON"));
9088
return res.ToArray();
9189
}
9290

93-
var errors = _schema.Validate(a);
91+
var errors = _schema.Validate(msgArray);
9492
if (errors.Any())
9593
{
96-
res.Add(new Error("Message does not conform to schema: " + string.Join(", ", errors.Select(aErr => aErr.ToString()).ToArray()), ErrorClass.ERROR_MSG, ButtplugConsts.SystemMsgId));
94+
res.Add(_bpLogger.LogErrorMsg(ButtplugConsts.SystemMsgId, ErrorClass.ERROR_MSG, "Message does not conform to schema: " + string.Join(", ", errors.Select(aErr => aErr.ToString()).ToArray())));
9795
return res.ToArray();
9896
}
9997

100-
if (!a.Any())
98+
if (!msgArray.Any())
10199
{
102-
res.Add(new Error("No messages in array", ErrorClass.ERROR_MSG, ButtplugConsts.SystemMsgId));
100+
res.Add(_bpLogger.LogErrorMsg(ButtplugConsts.SystemMsgId, ErrorClass.ERROR_MSG, "No messages in array"));
103101
return res.ToArray();
104102
}
105103

106104
// JSON input is an array of messages.
107105
// We currently only handle the first one.
108-
foreach (var o in a.Children<JObject>())
106+
foreach (var o in msgArray.Children<JObject>())
109107
{
110108
if (!o.Properties().Any())
111109
{
112-
res.Add(new Error("No message name available", ErrorClass.ERROR_MSG, ButtplugConsts.SystemMsgId));
110+
res.Add(_bpLogger.LogErrorMsg(ButtplugConsts.SystemMsgId, ErrorClass.ERROR_MSG, "No message name available"));
113111
continue;
114112
}
115113

116114
var msgName = o.Properties().First().Name;
117115
if (!_messageTypes.Keys.Any() || !_messageTypes.Keys.Contains(msgName))
118116
{
119-
res.Add(new Error($"{msgName} is not a valid message class", ErrorClass.ERROR_MSG, ButtplugConsts.SystemMsgId));
117+
res.Add(_bpLogger.LogErrorMsg(ButtplugConsts.SystemMsgId, ErrorClass.ERROR_MSG, $"{msgName} is not a valid message class"));
120118
continue;
121119
}
122120

Buttplug.Server.Managers.XInputGamepadManager/XInputGamepadDevice.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public XInputGamepadDevice(IButtplugLogManager aLogManager, Controller aDevice)
2020

2121
private Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
2222
{
23+
BpLogger.Debug("Stopping Device " + Name);
2324
return HandleSingleMotorVibrateCmd(new SingleMotorVibrateCmd(aMsg.DeviceIndex, 0, aMsg.Id));
2425
}
2526

Buttplug.Server/Bluetooth/Devices/FleshlightLaunch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
7070
// actually stop movement. It just makes it move really slow.
7171
// However, since each move it makes is finite (unlike setting vibration on some devices),
7272
// so we can assume it will be a short move, similar to what we do for the Kiiroo toys.
73+
BpLogger.Debug("Stopping Device " + Name);
7374
return Task.FromResult<ButtplugMessage>(new Ok(aMsg.Id));
7475
}
7576

Buttplug.Server/Bluetooth/Devices/Kiiroo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private Task<ButtplugMessage> HandleStopDeviceCmd([NotNull] ButtplugDeviceMessag
5252
// Right now, this is a nop. The Onyx doesn't have any sort of permanent movement state,
5353
// and its longest movement is like 150ms or so. The Pearl is supposed to vibrate but I've
5454
// never gotten that to work. So for now, we just return ok.
55+
BpLogger.Debug("Stopping Device " + Name);
5556
return Task.FromResult<ButtplugMessage>(new Ok(aMsg.Id));
5657
}
5758

Buttplug.Server/Bluetooth/Devices/Lovense.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public Lovense(IButtplugLogManager aLogManager,
141141

142142
private async Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
143143
{
144+
BpLogger.Debug("Stopping Device " + Name);
144145
return await HandleSingleMotorVibrateCmd(new SingleMotorVibrateCmd(aMsg.DeviceIndex, 0, aMsg.Id));
145146
}
146147

Buttplug.Server/Bluetooth/Devices/Vibratissimo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public Vibratissimo(IButtplugLogManager aLogManager,
5454

5555
private async Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
5656
{
57+
BpLogger.Debug("Stopping Device " + Name);
5758
return await HandleSingleMotorVibrateCmd(new SingleMotorVibrateCmd(aMsg.DeviceIndex, 0, aMsg.Id));
5859
}
5960

Buttplug.Server/Bluetooth/Devices/VorzeA10Cyclone.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public VorzeA10Cyclone(IButtplugLogManager aLogManager,
4444

4545
private async Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aMsg)
4646
{
47+
BpLogger.Debug("Stopping Device " + Name);
4748
return await HandleVorzeA10CycloneCmd(new VorzeA10CycloneCmd(aMsg.DeviceIndex, 0, false, aMsg.Id));
4849
}
4950

Buttplug.Server/ButtplugServer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public async Task<ButtplugMessage> SendMessage([NotNull] ButtplugMessage aMsg)
146146
switch (aMsg)
147147
{
148148
case RequestLog m:
149+
_bpLogger.Debug("Got RequestLog Message");
149150
_bpLogManager.Level = m.LogLevel;
150151
return new Ok(id);
151152

@@ -159,6 +160,7 @@ public async Task<ButtplugMessage> SendMessage([NotNull] ButtplugMessage aMsg)
159160
return new Ok(id);
160161

161162
case RequestServerInfo rsi:
163+
_bpLogger.Debug("Got RequestServerInfo Message");
162164
_receivedRequestServerInfo = true;
163165
_pingTimer?.Start();
164166
ClientConnected?.Invoke(this, new MessageReceivedEventArgs(rsi));

Buttplug.Server/DeviceManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ public async Task<ButtplugMessage> SendMessage(ButtplugMessage aMsg)
126126
switch (aMsg)
127127
{
128128
case StartScanning _:
129+
_bpLogger.Debug("Got StartScanning Message");
129130
StartScanning();
130131
return new Ok(id);
131132

132133
case StopScanning _:
134+
_bpLogger.Debug("Got StopScanning Message");
133135
StopScanning();
134136
return new Ok(id);
135137

136138
case StopAllDevices _:
139+
_bpLogger.Debug("Got StopAllDevices Message");
137140
var isOk = true;
138141
var errorMsg = string.Empty;
139142
foreach (var d in _devices.ToList())
@@ -155,6 +158,7 @@ public async Task<ButtplugMessage> SendMessage(ButtplugMessage aMsg)
155158

156159
return new Error(errorMsg, Error.ErrorClass.ERROR_DEVICE, aMsg.Id);
157160
case RequestDeviceList _:
161+
_bpLogger.Debug("Got RequestDeviceList Message");
158162
var msgDevices = _devices
159163
.Select(aDevice => new DeviceMessageInfo(aDevice.Key, aDevice.Value.Name,
160164
GetAllowedMessageTypesAsStrings(aDevice.Value).ToArray())).ToList();

0 commit comments

Comments
 (0)