Replace LogBridge with more generic liblogtiny (ILogTiny), allowing the log context to be passed to libraries without needing them to understand how to windows

This commit is contained in:
Maff 2019-09-30 13:51:15 +01:00
parent 0a2df42a5a
commit 6c9b19ba91
10 changed files with 135 additions and 55 deletions

28
liblogtiny/ILogTiny.cs Normal file
View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace liblogtiny {
public partial class LogTiny {
public enum LogLevel {
Trace,
Debug,
Verbose,
Info,
Warn,
Error,
Critical
};
}
public interface ILogTiny {
void Trace(string msg);
void Debug(string msg);
void Verbose(string msg);
void Info(string msg);
void Warn(string msg);
void Error(string msg);
void Critical(string msg);
void Log(LogTiny.LogLevel level, string msg);
void Raw(string msg);
}
}

View File

@ -0,0 +1,20 @@
using System;
namespace liblogtiny.Interfaces {
public class LConsole : ILogTiny {
public void Trace(string msg) => Log(LogTiny.LogLevel.Trace, msg);
public void Debug(string msg) => Log(LogTiny.LogLevel.Debug, msg);
public void Verbose(string msg) => Log(LogTiny.LogLevel.Verbose, msg);
public void Info(string msg) => Log(LogTiny.LogLevel.Info, msg);
public void Warn(string msg) => Log(LogTiny.LogLevel.Warn, msg);
public void Error(string msg) => Log(LogTiny.LogLevel.Error, msg);
public void Critical(string msg) => Log(LogTiny.LogLevel.Critical, msg);
public void Log(LogTiny.LogLevel level, string msg) {
if(level >= LogTiny.LogLevel.Warn)
Console.Error.WriteLine($"{level}: {msg}");
else
Raw($"{level}: {msg}");
}
public void Raw(string msg) => Console.Out.WriteLine(msg);
}
}

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -1,6 +1,6 @@
using libpaperang; using libpaperang.Helpers;
using libpaperang.Helpers;
using libpaperang.Interfaces; using libpaperang.Interfaces;
using liblogtiny;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
@ -9,6 +9,7 @@ namespace libpaperang.Main {
public IPrinter Printer; public IPrinter Printer;
public Transforms Transform; public Transforms Transform;
public CRC Crc; public CRC Crc;
public ILogTiny logger;
private const uint MagicValue = 0x35769521u; private const uint MagicValue = 0x35769521u;
public Paperang(BaseTypes.Connection connection, BaseTypes.Model model) { public Paperang(BaseTypes.Connection connection, BaseTypes.Model model) {
@ -30,9 +31,12 @@ namespace libpaperang.Main {
throw new PrinterConnectionNotSupportedException(); throw new PrinterConnectionNotSupportedException();
} }
} }
public void SetLogContext(ILogTiny logger) => this.logger = logger;
public void Initialise() { public void Initialise() {
logger?.Trace($"Initialising libpaperang with {Printer.AvailablePrinters.Count} device(s) connected");
Printer.OpenPrinter(Printer.AvailablePrinters.First()); Printer.OpenPrinter(Printer.AvailablePrinters.First());
Printer.Initialise(); Printer.Initialise();
logger?.Trace("Initialised, sending handshake");
Handshake(); Handshake();
} }
public void Handshake() { public void Handshake() {
@ -45,19 +49,29 @@ namespace libpaperang.Main {
Feed(0); Feed(0);
NoOp(); NoOp();
} }
public void Feed(uint ms) => Printer.WriteBytes( public void WriteBytes(byte[] packet) {
logger?.Trace($"Writing packet with length {packet.Length} to printer");
_ = Printer.WriteBytes(packet);
}
public void WriteBytes(byte[] packet, int ms) {
logger?.Trace($"Writing packet with length {packet.Length} to printer with delay of {ms}ms");
_ = Printer.WriteBytes(packet, ms);
}
public void Feed(uint ms) => WriteBytes(
Transform.Packet(BaseTypes.Operations.LineFeed, Transform.Packet(BaseTypes.Operations.LineFeed,
Transform.Arg(BaseTypes.Operations.LineFeed, ms), Transform.Arg(BaseTypes.Operations.LineFeed, ms),
Crc)); Crc));
public void NoOp() => Printer.WriteBytes( public void NoOp() => WriteBytes(
Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc)); Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc));
public void PrintBytes(byte[] data, bool autofeed = true) { public void PrintBytes(byte[] data, bool autofeed = true) {
logger?.Trace($"PrintBytes() invoked with data length of {data.Length}");
List<byte[]> segments = data List<byte[]> segments = data
.Select((b,i) => new {Index=i,Value=b }) .Select((b,i) => new {Index=i,Value=b })
.GroupBy(b=>b.Index/1008) .GroupBy(b=>b.Index/1008)
.Select(b=>b.Select(bb=>bb.Value).ToArray()) .Select(b=>b.Select(bb=>bb.Value).ToArray())
.ToList(); .ToList();
segments.ForEach(b => Printer.WriteBytes( logger?.Trace($"data split into {segments.Count} segment(s)");
segments.ForEach(b => WriteBytes(
Transform.Packet( Transform.Packet(
Transform.Arg(BaseTypes.Operations.Print, (uint)b.Length), Transform.Arg(BaseTypes.Operations.Print, (uint)b.Length),
b, b,

View File

@ -9,4 +9,8 @@
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\liblogtiny\liblogtiny.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "libsharperang", "libsharper
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "libpaperang", "libpaperang\libpaperang.csproj", "{A429CCEB-9331-4CD9-B3C0-A8F736732DEA}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "libpaperang", "libpaperang\libpaperang.csproj", "{A429CCEB-9331-4CD9-B3C0-A8F736732DEA}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "liblogtiny", "liblogtiny\liblogtiny.csproj", "{B7D242A8-F9DE-450B-9C87-5519CA782193}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -30,6 +32,10 @@ Global
{A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Debug|Any CPU.Build.0 = Debug|Any CPU {A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Release|Any CPU.ActiveCfg = Release|Any CPU {A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Release|Any CPU.Build.0 = Release|Any CPU {A429CCEB-9331-4CD9-B3C0-A8F736732DEA}.Release|Any CPU.Build.0 = Release|Any CPU
{B7D242A8-F9DE-450B-9C87-5519CA782193}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7D242A8-F9DE-450B-9C87-5519CA782193}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7D242A8-F9DE-450B-9C87-5519CA782193}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7D242A8-F9DE-450B-9C87-5519CA782193}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

31
sharperang/LUITextbox.cs Normal file
View File

@ -0,0 +1,31 @@
using liblogtiny;
using System.ComponentModel;
namespace sharperang {
class LUITextbox : ILogTiny, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private string _LogBuffer = "";
public string LogBuffer {
//get { return _LogBuffer; }
get => _LogBuffer;
set {
if (value == _LogBuffer) return;
if (value == "!clearlog") _LogBuffer = "";
else _LogBuffer = value + "\n" + _LogBuffer;
OnPropertyChanged("LogBuffer");
}
}
public void ClearBuffer() => LogBuffer="!clearlog";
public void Trace(string msg) => Log(LogTiny.LogLevel.Trace, msg);
public void Debug(string msg) => Log(LogTiny.LogLevel.Debug, msg);
public void Verbose(string msg) => Log(LogTiny.LogLevel.Verbose, msg);
public void Info(string msg) => Log(LogTiny.LogLevel.Info, msg);
public void Warn(string msg) => Log(LogTiny.LogLevel.Warn, msg);
public void Error(string msg) => Log(LogTiny.LogLevel.Error, msg);
public void Critical(string msg) => Log(LogTiny.LogLevel.Critical, msg);
public void Log(LogTiny.LogLevel level, string msg) => LogBuffer=$"{level}: {msg}";
public void Raw(string msg) => LogBuffer=msg;
}
}

View File

@ -1,29 +0,0 @@
using System.ComponentModel;
namespace sharperang {
class LogBridge : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private string _LogBuffer = "";
public string LogBuffer {
//get { return _LogBuffer; }
get => _LogBuffer;
set {
if (value == _LogBuffer) return;
if (value == "!clearlog") _LogBuffer = "";
else _LogBuffer = value + "\n" + _LogBuffer;
OnPropertyChanged("LogBuffer");
}
}
public void ClearBuffer() => LogBuffer="!clearlog";
public void Trace(string line) => LogBuffer="TRCE: "+line;
public void Debug(string line) => LogBuffer="DEBG: "+line;
public void Verbose(string line) => LogBuffer="VERB: "+line;
public void Info(string line) => LogBuffer="INFO: "+line;
public void Warn(string line) => LogBuffer="WARN: "+line;
public void Err(string line) => LogBuffer="ERR!: "+line;
public void Critical(string line) => LogBuffer="CRIT: "+line;
}
}

View File

@ -1,6 +1,5 @@
//using libsharperang; using liblogtiny;
using libpaperang; using libpaperang;
using libpaperang.Interfaces;
using libpaperang.Main; using libpaperang.Main;
using System; using System;
using System.Windows; using System.Windows;
@ -12,42 +11,38 @@ using System.IO;
namespace sharperang { namespace sharperang {
public partial class MainWindow : Window { public partial class MainWindow : Window {
private LogBridge logger; private ILogTiny logger;
private BaseTypes.Connection mmjcx=BaseTypes.Connection.USB; private BaseTypes.Connection mmjcx=BaseTypes.Connection.USB;
private BaseTypes.Model mmjmd=BaseTypes.Model.T1; private BaseTypes.Model mmjmd=BaseTypes.Model.T1;
private Paperang mmj; private Paperang mmj;
//private USBPrinter printer=new USBPrinter(48);
private Bitmap bimg; private Bitmap bimg;
public MainWindow() { public MainWindow() {
InitializeComponent(); InitializeComponent();
logger = new LogBridge(); logger = new LUITextbox();
gMain.DataContext = logger; gMain.DataContext = (LUITextbox)logger;
logger.Info("Application started"); logger.Info("Application started");
} }
private void BtClearLog_Click(object sender, RoutedEventArgs e) => private void BtClearLog_Click(object sender, RoutedEventArgs e) =>
logger.ClearBuffer(); logger.Raw("!clearlog");
private void BtSetP1_Click(object sender, RoutedEventArgs e) => mmjmd = BaseTypes.Model.P1; private void BtSetP1_Click(object sender, RoutedEventArgs e) {
private void BtSetP2_Click(object sender, RoutedEventArgs e) => mmjmd = BaseTypes.Model.P2; mmjmd = BaseTypes.Model.P1;
logger.Info("Model type set to P1");
}
private void BtSetP2_Click(object sender, RoutedEventArgs e) {
mmjmd = BaseTypes.Model.P2;
logger.Info("Model type set to P2");
}
private void BtInitUSB_Click(object sender, RoutedEventArgs e) { private void BtInitUSB_Click(object sender, RoutedEventArgs e) {
mmj = new Paperang(mmjcx, mmjmd); mmj = new Paperang(mmjcx, mmjmd);
mmj.SetLogContext(logger);
logger.Verbose("# printers found: " + mmj.Printer.AvailablePrinters.Count); logger.Verbose("# printers found: " + mmj.Printer.AvailablePrinters.Count);
if(!mmj.Printer.PrinterAvailable) { if(!mmj.Printer.PrinterAvailable) {
logger.Err("Couldn't initialise printer as none is connected"); logger.Error("Couldn't initialise printer as none is connected");
return; return;
} }
logger.Info("USB Initialising"); logger.Info("USB Initialising");
mmj.Initialise(); mmj.Initialise();
//printer = new LibSharperang(logger);
/*
logger.Debug("IsPrinterPresent => "+printer.IsPrinterPresent());
logger.Debug("FoundPrinterGuids => "+printer.FoundPrinterGuids());
printer?.IDs?.ForEach(p => logger.Debug("FoundPrinterGuidAddrs "+p.ToString()+" => "+printer?.FoundPrinterGuidAddrs(p)));
logger.Debug("Open => "+printer?.Open());
logger.Debug("Claim => " + printer?.Claim());
logger.Debug("Init => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-', ' '));
printer.InitPrinter();
*/
logger.Debug("PrinterInitialised? " + mmj.Printer.PrinterInitialised); logger.Debug("PrinterInitialised? " + mmj.Printer.PrinterInitialised);
logger.Debug("Printer initialised and ready"); logger.Debug("Printer initialised and ready");
} }

View File

@ -155,7 +155,7 @@
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="LogBridge.cs" /> <Compile Include="LUITextbox.cs" />
<Compile Include="MainWindow.xaml.cs"> <Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon> <DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
@ -189,6 +189,10 @@
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\liblogtiny\liblogtiny.csproj">
<Project>{b7d242a8-f9de-450b-9c87-5519ca782193}</Project>
<Name>liblogtiny</Name>
</ProjectReference>
<ProjectReference Include="..\libpaperang\libpaperang.csproj"> <ProjectReference Include="..\libpaperang\libpaperang.csproj">
<Project>{a429cceb-9331-4cd9-b3c0-a8f736732dea}</Project> <Project>{a429cceb-9331-4cd9-b3c0-a8f736732dea}</Project>
<Name>libpaperang</Name> <Name>libpaperang</Name>