Skip to content

Commit

Permalink
(hopefully) Fix exception logging for YAMDCC service
Browse files Browse the repository at this point in the history
While developing the keyboard backlight feature, the service seemed to hang instead of crash without logging the exception
  • Loading branch information
Sparronator9999 committed Sep 7, 2024
1 parent 6db5a46 commit 9d259a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
19 changes: 18 additions & 1 deletion YAMDCC.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,35 @@
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.ServiceProcess;
using YAMDCC.Logs;

namespace YAMDCC.Service
{
internal static class Program
{
/// <summary>
/// The <see cref="Logger"/> instance to write logs to.
/// </summary>
private static readonly Logger Log = new Logger
{
ConsoleLogLevel = LogLevel.None,
FileLogLevel = LogLevel.Debug,
};

/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
ServiceBase.Run(new svcFanControl());
AppDomain.CurrentDomain.UnhandledException += LogUnhandledException;
ServiceBase.Run(new svcFanControl(Log));
}

private static void LogUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Fatal(Strings.GetString("svcException"), e.ExceptionObject);
}
}
}
20 changes: 4 additions & 16 deletions YAMDCC.Service/svcFanControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ internal sealed partial class svcFanControl : ServiceBase
/// </summary>
private readonly NamedPipeServer<ServiceCommand, ServiceResponse> IPCServer;

/// <summary>
/// The <see cref="Logger"/> instance to write logs to.
/// </summary>
private static readonly Logger Log = new Logger
{
ConsoleLogLevel = LogLevel.None,
FileLogLevel = LogLevel.Debug,
};
private readonly Logger Log;

private readonly EC _EC;
#endregion
Expand All @@ -65,11 +58,11 @@ internal sealed partial class svcFanControl : ServiceBase
/// Initialises a new instance of the <see cref="svcFanControl"/> class.
/// </summary>
/// <param name="logger">The <see cref="Logger"/> instance to write logs to.</param>
public svcFanControl()
public svcFanControl(Logger logger)
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += LogUnhandledException;

Log = logger;
_EC = new EC();

PipeSecurity security = new PipeSecurity();
Expand Down Expand Up @@ -357,7 +350,7 @@ private void ApplySettings()
/// <param name="expected_args">The expected number of arguments. Must be zero or positive.</param>
/// <param name="args_out">The parsed arguments. Will be empty if parsing fails.</param>
/// <returns></returns>
private static bool ParseArgs(string args_in, int expected_args, out int[] args_out)
private bool ParseArgs(string args_in, int expected_args, out int[] args_out)
{
args_out = new int[expected_args];

Expand Down Expand Up @@ -648,10 +641,5 @@ private int SetKeyLightBright(string name, string args)
}
return 2;
}

private static void LogUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Fatal(Strings.GetString("svcException"), e.ExceptionObject);
}
}
}

0 comments on commit 9d259a2

Please sign in to comment.