Skip to content

Commit

Permalink
Merge pull request #30 from FaithBeam/logging-setting
Browse files Browse the repository at this point in the history
Replace serilog with Microsoft logging
  • Loading branch information
FaithBeam authored Oct 19, 2024
2 parents a7aa703 + 49e6d99 commit 981fc10
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Serilog;
using Microsoft.Extensions.Logging;
using SharpHook;
using SharpHook.Native;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;

Expand Down Expand Up @@ -34,10 +35,11 @@ public interface IEventSimulatorService
void TapKeys(string? keys, int delay, CancellationToken cancellationToken);
}

public class EventSimulatorService(IEventSimulator eventSimulator) : IEventSimulatorService
public partial class EventSimulatorService(
ILogger<EventSimulatorService> logger,
IEventSimulator eventSimulator
) : IEventSimulatorService
{
private readonly ILogger _logger = Log.Logger.ForContext<EventSimulatorService>();

public void SimulateMousePress(MouseButton mb)
{
var t = new Thread(() => eventSimulator.SimulateMousePress(mb));
Expand All @@ -52,13 +54,13 @@ public void SimulateMouseRelease(MouseButton mb)

public void SimulateKeyPress(string? key)
{
_logger.Information("Simulate press {Key}", key);
LogSimulateKeyPress(logger, key);
eventSimulator.SimulateKeyPress(KeyCodes[key ?? throw new NullReferenceException(key)]);
}

public void SimulateKeyRelease(string? key)
{
_logger.Information("Simulate release {Key}", key);
LogSimulateKeyRelease(logger, key);
eventSimulator.SimulateKeyRelease(KeyCodes[key ?? throw new NullReferenceException(key)]);
}

Expand All @@ -69,7 +71,7 @@ public void SimulateKeyRelease(string? key)
/// <returns></returns>
public void SimulateKeyTap(string? key)
{
_logger.Information("Simulate key tap {Key}", key);
LogSimulateKeyTap(logger, key);
var keyCode = KeyCodes[key ?? throw new NullReferenceException(key)];
eventSimulator.SimulateKeyPress(keyCode);
eventSimulator.SimulateKeyRelease(keyCode);
Expand Down Expand Up @@ -198,7 +200,7 @@ public void TapKeys(string? keys, int delay, CancellationToken cancellationToken
{
if (cancellationToken.IsCancellationRequested)
{
_logger.Information("========STOPPING TAP KEYS===========");
LogStopTappingKeys(logger);
return;
}
if (delay > -1)
Expand Down Expand Up @@ -244,7 +246,7 @@ public void TapKeys(string? keys, int delay, CancellationToken cancellationToken
{
if (cancellationToken.IsCancellationRequested)
{
_logger.Information("========STOPPING TAP KEYS===========");
LogStopTappingKeys(logger);
return;
}
switch (poppedPk.Value)
Expand Down Expand Up @@ -520,6 +522,18 @@ private static List<ParsedKey> ParseKeys(string? keys)
{ "mb4", MouseButton.Button4 },
{ "mb5", MouseButton.Button5 },
};

[LoggerMessage(LogLevel.Information, "========STOPPING TAP KEYS===========")]
private static partial void LogStopTappingKeys(ILogger logger);

[LoggerMessage(LogLevel.Information, "Simulate key tap {Key}")]
private static partial void LogSimulateKeyTap(ILogger logger, string? key);

[LoggerMessage(LogLevel.Information, "Simulate press {Key}")]
private static partial void LogSimulateKeyPress(ILogger logger, string? key);

[LoggerMessage(LogLevel.Information, "Simulate release {Key}")]
private static partial void LogSimulateKeyRelease(ILogger logger, string? key);
}

internal class ParsedKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using Serilog;
using Microsoft.Extensions.Logging;
using SharpHook;
using SharpHook.Native;
using SharpHook.Reactive;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.EventArgs;
using YMouseButtonControl.Core.Services.Processes;
using YMouseButtonControl.Core.Services.Profiles;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;

Expand All @@ -27,12 +28,12 @@ public interface IMouseListener : IDisposable
/// Wrapper around sharphook for listening to mouse events
/// Converts mouse events to NewMouseHookEventArgs
/// </summary>
public class MouseListener : IMouseListener
public partial class MouseListener : IMouseListener
{
private readonly ILogger<MouseListener> _logger;
private readonly IReactiveGlobalHook _hook;
private readonly IProfilesService _profilesService;
private readonly ICurrentWindowService _currentWindowService;
private readonly ILogger _log = Log.Logger.ForContext<MouseListener>();
private Thread? _thread;
private readonly IDisposable? _mouseMovedDisposable;
private readonly IDisposable? _mousePressedDisposable;
Expand All @@ -44,11 +45,13 @@ public class MouseListener : IMouseListener
private readonly Subject<NewMouseWheelEventArgs> _mouseWheelSubject;

public MouseListener(
ILogger<MouseListener> logger,
IReactiveGlobalHook hook,
IProfilesService profilesService,
ICurrentWindowService currentWindowService
)
{
_logger = logger;
_hook = hook;
_profilesService = profilesService;
_currentWindowService = currentWindowService;
Expand Down Expand Up @@ -78,7 +81,7 @@ public void Run()
{
_thread = new Thread(() =>
{
_log.Information("Starting mouse listener");
LogStartup(_logger);
_hook.Run();
});
_thread.Start();
Expand Down Expand Up @@ -175,8 +178,9 @@ private void ConvertMouseReleasedEvent(MouseHookEventArgs e)
{
return;
}
_log.Information("Translate release {Button}", e.Data.Button);
_log.Information("ACTIVE WINDOW {Foreground}", _currentWindowService.ForegroundWindow);

LogTranslateRelease(_logger, e.Data.Button);
LogActiveWindow(_logger, _currentWindowService.ForegroundWindow);
var args = new NewMouseHookEventArgs(
(YMouseButton)e.Data.Button,
e.Data.X,
Expand All @@ -185,12 +189,12 @@ private void ConvertMouseReleasedEvent(MouseHookEventArgs e)
);
if (ShouldSuppressEvent(args))
{
_log.Information("Suppressing {Button}: Release", e.Data.Button);
LogSuppressingButtonRelease(_logger, e.Data.Button);
e.SuppressEvent = true;
}
else
{
_log.Information("Not suppressing {Button}: Release", e.Data.Button);
LogNotSuppressingButtonRelease(_logger, e.Data.Button);
}
_mouseReleasedSubject.OnNext(args);
}
Expand All @@ -201,8 +205,8 @@ private void ConvertMousePressedEvent(MouseHookEventArgs e)
{
return;
}
_log.Information("Translate press {Button}", e.Data.Button);
_log.Information("ACTIVE WINDOW {Foreground}", _currentWindowService.ForegroundWindow);
LogTranslateButton(_logger, e.Data.Button);
LogActiveWindow(_logger, _currentWindowService.ForegroundWindow);

var args = new NewMouseHookEventArgs(
(YMouseButton)e.Data.Button,
Expand All @@ -212,12 +216,12 @@ private void ConvertMousePressedEvent(MouseHookEventArgs e)
);
if (ShouldSuppressEvent(args))
{
_log.Information("Suppressing {Button}: Press", e.Data.Button);
LogSuppressingButtonRelease(_logger, e.Data.Button);
e.SuppressEvent = true;
}
else
{
_log.Information("Not suppressing {Button}: Press", e.Data.Button);
LogNotSuppressingButtonRelease(_logger, e.Data.Button);
}
_mousePressedSubject.OnNext(args);
}
Expand All @@ -242,4 +246,28 @@ public void Dispose()

_thread?.Join();
}

[LoggerMessage(LogLevel.Information, "Translate press {Button}")]
private static partial void LogTranslateButton(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "Suppressing {Button}: Press")]
private static partial void LogSuppressingButtonPress(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "Not suppressing {Button}: Press")]
private static partial void LogNotSuppressingButtonPress(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "Not suppressing {Button}: Release")]
public static partial void LogNotSuppressingButtonRelease(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "Suppressing {Button}: Release")]
public static partial void LogSuppressingButtonRelease(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "ACTIVE WINDOW {Foreground}")]
private static partial void LogActiveWindow(ILogger logger, string foreground);

[LoggerMessage(LogLevel.Information, "Translate release {Button}")]
private static partial void LogTranslateRelease(ILogger logger, MouseButton button);

[LoggerMessage(LogLevel.Information, "Starting mouse listener")]
private static partial void LogStartup(ILogger logger);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Threading;
using Serilog;
using Microsoft.Extensions.Logging;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Interfaces;
using YMouseButtonControl.Core.ViewModels.Models;

namespace YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations.SimulatedKeystrokesTypes;
Expand All @@ -11,15 +10,16 @@ public interface IStickyRepeatService
void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state);
}

public class StickyRepeatService(IEventSimulatorService eventSimulatorService)
: IStickyRepeatService
public partial class StickyRepeatService(
ILogger<StickyRepeatService> logger,
IEventSimulatorService eventSimulatorService
) : IStickyRepeatService
{
private Thread? _thread;
private bool _shouldStop;
private readonly object _lock = new();
private const int RepeatRateMs = 33;
private CancellationTokenSource? _cts;
private readonly ILogger _log = Log.Logger.ForContext<StickyRepeatService>();

public void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state)
{
Expand All @@ -36,7 +36,7 @@ public void StickyRepeat(BaseButtonMappingVm mapping, MouseButtonState state)
{
lock (_lock)
{
_log.Information("=====CANCELLATION REQUESTED=======");
LogCancellationRequested(logger);
_cts?.Cancel();
_shouldStop = true;
}
Expand All @@ -63,7 +63,7 @@ private void StartThread(BaseButtonMappingVm mapping)
{
if (_shouldStop)
{
_log.Information("=====CANCELLATION REQUESTED=======");
LogCancellationRequested(logger);
_cts.Cancel();
break;
}
Expand All @@ -75,4 +75,7 @@ private void StartThread(BaseButtonMappingVm mapping)

_thread.Start();
}

[LoggerMessage(LogLevel.Information, "=====CANCELLATION REQUESTED=======")]
private static partial void LogCancellationRequested(ILogger logger);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Serilog;
using Microsoft.Extensions.Logging;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Enums;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.EventArgs;
using YMouseButtonControl.Core.Services.KeyboardAndMouse.Implementations;
Expand All @@ -11,7 +11,8 @@

namespace YMouseButtonControl.Core.Services.KeyboardAndMouse;

public class KeyboardSimulatorWorker(
public partial class KeyboardSimulatorWorker(
ILogger<KeyboardSimulatorWorker> logger,
IProfilesService profilesService,
IMouseListener mouseListener,
ISkipProfileService skipProfileService,
Expand All @@ -24,7 +25,6 @@ public class KeyboardSimulatorWorker(
IRightClick rightClick
) : IDisposable
{
private readonly ILogger _log = Log.Logger.ForContext<KeyboardSimulatorWorker>();
private IDisposable? _onMousePressedDisposable;
private IDisposable? _onMouseReleasedDisposable;
private IDisposable? _onMouseWheelDisposable;
Expand All @@ -51,11 +51,10 @@ private void OnMousePressed(NewMouseHookEventArgs e)
{
if (skipProfileService.ShouldSkipProfile(p, e))
{
_log.Information("Skipped {Profile}", p.Name);
LogSkippedProfile(logger, p.Name);
continue;
}

_log.Information("{Profile}, Route {Button}", p.Name, e.Button);
LogMousePressedRoute(logger, p.Name, e.Button);
RouteMouseButton(e.Button, p, MouseButtonState.Pressed);
}
}
Expand Down Expand Up @@ -151,4 +150,14 @@ MouseButtonState state
}

private void OnMouseWheel(NewMouseWheelEventArgs e) { }

[LoggerMessage(LogLevel.Information, "Skipped {Profile}")]
private static partial void LogSkippedProfile(ILogger logger, string profile);

[LoggerMessage(LogLevel.Information, "{Profile}, Route {Button}")]
private static partial void LogMousePressedRoute(
ILogger logger,
string profile,
YMouseButton button
);
}
Loading

0 comments on commit 981fc10

Please sign in to comment.