Replace Debug.Writes with ILogger.

This commit is contained in:
Eugene Wang
2025-12-25 23:13:07 -05:00
parent 34c03b441e
commit 39c0373d3c
8 changed files with 45 additions and 18 deletions

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<!--change these in each release-->
<VersionPrefix>4.0.0.0</VersionPrefix>
<VersionSuffix>alpha.25</VersionSuffix>
<VersionSuffix>alpha.26</VersionSuffix>
<!--keep it the same until major # changes-->
<AssemblyVersion>4.0.0.0</AssemblyVersion>

View File

@@ -1,4 +1,5 @@
using NTwain.Data;
using Microsoft.Extensions.Logging;
using NTwain.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -20,7 +21,7 @@ namespace NTwain.DSM
{
static IntPtr __dllPtr;
public static bool TryLoadCustomDSM()
public static bool TryLoadCustomDSM(ILogger logger)
{
if (__dllPtr == IntPtr.Zero)
{
@@ -46,11 +47,11 @@ namespace NTwain.DSM
if (__dllPtr != IntPtr.Zero)
{
Debug.WriteLine("Using our own dsm now :)");
logger.LogTrace("Using our own dsm now :)");
}
else
{
Debug.WriteLine("Will attempt to use default dsm :(");
logger.LogTrace("Will attempt to use default dsm :(");
}
}
return __dllPtr != IntPtr.Zero;

View File

@@ -1,4 +1,5 @@
#if WINDOWS || NETFRAMEWORK
using Microsoft.Extensions.Logging;
using NTwain.Data;
using System;
using System.ComponentModel;
@@ -102,14 +103,21 @@ namespace NTwain
void RunMessagePump()
{
Debug.WriteLine("TWAIN pump thread starting");
_twain?.Logger.LogDebug("Starting TWAIN message pump thread.");
_dummyForm = new DummyForm();
_dummyForm.FormClosed += (s, e) =>
{
_dummyForm = null;
};
Application.ThreadException += Application_ThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.Run(_dummyForm);
Debug.WriteLine("TWAIN pump thread ended");
_twain?.Logger.LogDebug("TWAIN message pump thread exiting.");
}
private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
_twain?.Logger.LogError(e.Exception, "Unhandled exception in TWAIN message pump thread.");
}
class DummyForm : Form
@@ -119,7 +127,7 @@ namespace NTwain
ShowInTaskbar = false;
Width = 1;
Height = 1;
WindowState = FormWindowState.Minimized;
//WindowState = FormWindowState.Minimized;
Text = "NTwain Internal Loop";
}

View File

@@ -35,6 +35,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.264">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -1,4 +1,5 @@
using NTwain.Data;
using Microsoft.Extensions.Logging;
using NTwain.Data;
using NTwain.Triplets;
using System;
using System.Diagnostics;
@@ -70,7 +71,7 @@ namespace NTwain
DG dg, DAT dat, MSG msg, IntPtr twnull
)
{
Debug.WriteLine($"Legacy callback got {msg}");
Logger.LogTrace("Legacy callback got {Msg}", msg);
HandleSourceMsg(msg);
return (ushort)TWRC.SUCCESS;
}
@@ -81,7 +82,7 @@ namespace NTwain
DG dg, DAT dat, MSG msg, IntPtr twnull
)
{
Debug.WriteLine($"OSX callback got {msg}");
Logger.LogTrace("OSX callback got {Msg}", msg);
HandleSourceMsg(msg);
return (ushort)TWRC.SUCCESS;
}
@@ -89,7 +90,8 @@ namespace NTwain
private void HandleSourceMsg(MSG msg, [CallerMemberName] string? caller = null)
{
Debug.WriteLine($"[thread {Environment.CurrentManagedThreadId}] {nameof(HandleSourceMsg)} called by {caller} at state {State} with {msg}.");
Logger.LogTrace("[thread {ThreadId}] {Caller} called by {Caller} at state {State} with {Msg}.",
Environment.CurrentManagedThreadId, nameof(HandleSourceMsg), caller, State, msg);
// the reason we post these to the background is
// if they're coming from UI message loop then

View File

@@ -1,4 +1,5 @@
#if WINDOWS || NETFRAMEWORK
using Microsoft.Extensions.Logging;
using NTwain.Data;
using NTwain.Native;
using NTwain.Triplets;
@@ -102,7 +103,8 @@ namespace NTwain
handled = rc == TWRC.DSEVENT;
if (_procEvent.TWMessage != 0 && (handled || rc == TWRC.NOTDSEVENT))
{
//Debug.WriteLine($"[thread {Environment.CurrentManagedThreadId}] CheckIfTwainMessage at state {State} with MSG={_procEvent.TWMessage}.");
//Logger.LogTrace("[thread {ThreadId}] CheckIfTwainMessage at state {State} with MSG={Msg}.",
// Environment.CurrentManagedThreadId, State, _procEvent.TWMessage);
HandleSourceMsg((MSG)_procEvent.TWMessage);
}
}

View File

@@ -1,4 +1,5 @@
using NTwain.Data;
using Microsoft.Extensions.Logging;
using NTwain.Data;
using NTwain.Native;
using NTwain.Triplets;
using System;
@@ -524,7 +525,7 @@ namespace NTwain
{
if (tries++ < 3)
{
Debug.WriteLine($"Using fileXfer timing workaround try {tries}.");
Logger.LogDebug("Using fileXfer timing workaround try {Tries}.", tries);
Thread.Sleep(500);
goto RETRY;
}

View File

@@ -1,6 +1,9 @@
using NTwain.Data;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NTwain.Data;
using NTwain.Triplets;
using System;
using System.IO.Packaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
@@ -23,10 +26,12 @@ namespace NTwain
/// Creates TWAIN session with explicit app info.
/// </summary>
/// <param name="appId"></param>
public TwainAppSession(TW_IDENTITY_LEGACY appId)
public TwainAppSession(TW_IDENTITY_LEGACY appId, ILogger? logger = null)
{
if (logger != null) _logger = logger;
#if WINDOWS || NETFRAMEWORK
DSM.DsmLoader.TryLoadCustomDSM();
DSM.DsmLoader.TryLoadCustomDSM(Logger);
#endif
_appIdentity = appId;
@@ -36,6 +41,13 @@ namespace NTwain
StartTransferThread();
}
private ILogger _logger = NullLogger.Instance;
public ILogger Logger
{
get { return _logger = NullLogger.Instance; }
set { _logger = value ?? NullLogger.Instance; }
}
internal IntPtr _hwnd;
internal TW_USERINTERFACE _userInterface; // kept around for disable to use