Skip to content

Commit

Permalink
Adds logging for debugging. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Aflalo committed Aug 24, 2015
1 parent 24b7301 commit 5341bee
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
27 changes: 19 additions & 8 deletions SoundSwitch/Framework/AutoStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,40 @@ namespace SoundSwitch.Framework
public static class AutoStart
{
private static readonly RegistryKey SstartupKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

/// <summary>
/// Enable autostarting using registry for the current user
/// Enable autostarting using registry for the current user
/// </summary>
public static void EnableAutoStart()
{
SstartupKey?.SetValue(Application.ProductName, Application.ExecutablePath);
using (AppLogger.Log.DebugCall())
{
SstartupKey?.SetValue(Application.ProductName, Application.ExecutablePath);
}
}

/// <summary>
/// Disable autostarting using registry for the current user
/// Disable autostarting using registry for the current user
/// </summary>
public static void DisableAutoStart()
{
SstartupKey?.DeleteValue(Application.ProductName, false);
using (AppLogger.Log.DebugCall())
{
SstartupKey?.DeleteValue(Application.ProductName, false);
}
}

/// <summary>
/// Check if the application is set to start with Windows
/// Check if the application is set to start with Windows
/// </summary>
/// <returns></returns>
public static bool IsAutoStarted()
{
return SstartupKey.GetValue(Application.ProductName).ToString() == Application.ExecutablePath;
using (AppLogger.Log.DebugCall())
{
return SstartupKey.GetValue(Application.ProductName).ToString() == Application.ExecutablePath;
}
}

}
}
5 changes: 5 additions & 0 deletions SoundSwitch/Framework/SoundSwitchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ public void Save()
ConfigurationManager.SaveConfiguration(this);
}
}

public override string ToString()
{
return $"{GetType().Name}({FileLocation})";
}
}
}
25 changes: 20 additions & 5 deletions SoundSwitch/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public class Main

public Main(SoundSwitchConfiguration configuration)
{
_configuration = configuration;

using (AppLogger.Log.DebugCall())
{
AppLogger.Log.Debug("Setting configuration",configuration);
_configuration = configuration;

RegisterForRestart();
RegisterRecovery();

RegisterForRestart();
RegisterRecovery();
}
}
/// <summary>
/// Register the configured hotkeys
Expand All @@ -53,7 +57,18 @@ public void InitializeHotkeys()
WindowsAPIAdapter.HotKeyPressed += HandleHotkeyPress;
}

public HashSet<string> SelectedDevicesList => _configuration.SelectedDeviceList;
public HashSet<string> SelectedDevicesList
{
get
{
using (AppLogger.Log.DebugCall())
{
return _configuration.SelectedDeviceList;
}
}
}



public List<AudioDeviceWrapper> AvailableAudioDevices
{
Expand Down
27 changes: 19 additions & 8 deletions SoundSwitch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,36 @@ private static void Main()
}
}
};
AppLogger.Log.Info("Load Configuration");
var config = ConfigurationManager.LoadConfiguration<SoundSwitchConfiguration>();
AppLogger.Log.Info("Set Exception Handler");
WindowsAPIAdapter.AddThreadExceptionHandler(Application_ThreadException);
using (var icon = new TrayIcon(new Main(config)))
AppLogger.Log.Info("Set Tray Icon with Main");
try
{
if (config.FirstRun)
using (var icon = new TrayIcon(new Main(config)))
{
icon.ShowSettings();
config.FirstRun = false;
AppLogger.Log.Info("First run");
if (config.FirstRun)
{
icon.ShowSettings();
config.FirstRun = false;
AppLogger.Log.Info("First run");
}
Application.Run();
WindowsAPIAdapter.Stop();
}
Application.Run();
WindowsAPIAdapter.Stop();
}
catch (Exception ex)
{
Application_ThreadException(null, new ThreadExceptionEventArgs(ex));
}

}
}

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
AppLogger.Log.Fatal("Exception Occured", e);
AppLogger.Log.Fatal("Exception Occured", e.Exception);
var message =
$"It seems {Application.ProductName} has crashed.\nDo you want to save a log of the error that ocurred?\nThis could be useful to fix bugs. Please post this file in the issues section.";
var result = MessageBox.Show(message, $"{Application.ProductName} crashed...", MessageBoxButtons.YesNo,
Expand Down
2 changes: 1 addition & 1 deletion SoundSwitch/SoundSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<Optimize>false</Optimize>
<Prefer32Bit>false</Prefer32Bit>
<UseVSHostingProcess>false</UseVSHostingProcess>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>..\x64\Release\</OutputPath>
Expand Down
2 changes: 2 additions & 0 deletions SoundSwitch/Util/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public TrayIcon(Main main)
{
using (AppLogger.Log.DebugCall())
{
AppLogger.Log.Debug("Star TrayIcon ctor");
_trayIcon.ContextMenuStrip = _settingsMenu;
_main = main;
AppLogger.Log.Debug("Set Main ", _main);
_availableAudioDeviceWrappers = _main.AvailableAudioDevices;

_settingsMenu.Items.Add("Playback Devices", null, (sender, e) =>
Expand Down

0 comments on commit 5341bee

Please sign in to comment.