Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Logging refactor #339

Merged
merged 6 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions windows/QMK Toolbox/HidConsole/HidConsoleDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HidLibrary;
using System.Linq;
using System.Text;

namespace QMK_Toolbox.HidConsole
{
Expand Down Expand Up @@ -43,19 +44,33 @@ public override string ToString()
return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})";
}

private string currentLine = "";

private void HidDeviceReportEvent(HidReport report)
{
if (HidDevice.IsConnected)
{
var outputString = string.Empty;
for (var i = 0; i < report.Data.Length; i++)
// Check if we have a completed line queued
int lineEnd = currentLine.IndexOf('\n');
if (lineEnd == -1)
{
// Partial line or nothing - append incoming report to current line
string reportString = Encoding.UTF8.GetString(report.Data).Trim('\0');
currentLine += reportString;
}

// Check again for a completed line
lineEnd = currentLine.IndexOf('\n');
while (lineEnd >= 0)
{
outputString += (char)report.Data[i];
if (i % 16 != 15 || i >= report.Data.Length) continue;
// Fire delegate with completed lines until we have none left
string completedLine = currentLine.Substring(0, lineEnd);
currentLine = currentLine.Substring(lineEnd + 1);
lineEnd = currentLine.IndexOf('\n');
consoleReportReceived?.Invoke(this, completedLine);
}
consoleReportReceived?.Invoke(this, outputString);
outputString = string.Empty;

// Reregister this callback
HidDevice.ReadReport(HidDeviceReportEvent);
}
}
Expand Down
109 changes: 109 additions & 0 deletions windows/QMK Toolbox/LogTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace QMK_Toolbox
{
class LogTextBox : RichTextBox
{
public void LogBootloader(string message)
{
Log(message, MessageType.Bootloader);
}

public void LogCommand(string message)
{
Log(message, MessageType.Command);
}

public void LogCommandError(string message)
{
Log(message, MessageType.CommandError);
}

public void LogCommandOutput(string message)
{
Log(message, MessageType.CommandOutput);
}

public void LogError(string message)
{
Log(message, MessageType.Error);
}

public void LogHid(string message)
{
Log(message, MessageType.Hid);
}

public void LogHidOutput(string message)
{
Log(message, MessageType.HidOutput);
}

public void LogInfo(string message)
{
Log(message, MessageType.Info);
}

public void LogUsb(string message)
{
Log(message, MessageType.Usb);
}

public void Log(string message, MessageType type)
{
if (message.Length > 1 && message.Last() == '\n')
{
message = message.Remove(message.Length - 1);
}
string[] lines = message.Split('\n');

foreach (string line in lines)
{
switch (type)
{
case MessageType.Bootloader:
AppendText($"{line}\n", Color.Yellow);
break;
case MessageType.Command:
AppendText($"> {line}\n", Color.White);
break;
case MessageType.CommandError:
AppendText("> ", Color.LightCoral);
AppendText($"{line}\n", Color.Silver);
break;
case MessageType.CommandOutput:
AppendText("> ", Color.White);
AppendText($"{line}\n", Color.Silver);
break;
case MessageType.Error:
AppendText($"{line}\n", Color.LightCoral);
break;
case MessageType.Hid:
AppendText($"{line}\n", Color.SkyBlue);
break;
case MessageType.HidOutput:
AppendText("> ", Color.SkyBlue);
AppendText($"{line}\n", Color.Azure);
break;
case MessageType.Info:
AppendText("* ", Color.White);
AppendText($"{line}\n", Color.Silver);
break;
case MessageType.Usb:
AppendText($"{line}\n", Color.White);
break;
}
}
}

public void AppendText(string text, Color color)
{
SelectionStart = TextLength;
SelectionLength = text.Length;
SelectionColor = color;
SelectedText = text;
}
}
}
4 changes: 2 additions & 2 deletions windows/QMK Toolbox/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading