Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
rework log to remove all dependencies
add support for loading translations from addons
  • Loading branch information
radj307 committed Nov 2, 2023
1 parent 2e12bf8 commit 7187f19
Show file tree
Hide file tree
Showing 45 changed files with 950 additions and 785 deletions.
48 changes: 32 additions & 16 deletions VolumeControl.Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ public class Config : AppConfig.ConfigurationFile
/// <summary>
/// Creates a new <see cref="Config"/> instance.
/// </summary>
/// <remarks>The first time this is called, the <see cref="AppConfig.Configuration.Default"/> property is set to that instance; all subsequent calls do not update this property.</remarks>
public Config(string filePath) : base(filePath) { }
/// <remarks>The first time this is called, the <see cref="AppConfig.Configuration.DefaultInstance"/> property is set to that instance; all subsequent calls do not update this property.</remarks>
public Config(string filePath) : base(filePath)
{
PropertyChanged += UpdateFLogState;
}
#endregion Constructor

#region Properties
Expand All @@ -43,7 +46,7 @@ public Config(string filePath) : base(filePath) { }
/// </summary>
public void ResumeAutoSave()
{
if (FLog.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.FilterEventType(EventType.TRACE))
FLog.Trace($"Enabled {nameof(Config)} autosave.");

_autoSaveEnabled = true;
Expand All @@ -54,7 +57,7 @@ public void ResumeAutoSave()
/// </summary>
public void PauseAutoSave()
{
if (FLog.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.FilterEventType(EventType.TRACE))
FLog.Trace($"Disabled {nameof(Config)} autosave.");

_autoSaveEnabled = false;
Expand Down Expand Up @@ -85,7 +88,7 @@ public void AttachReflectivePropertyChangedHandlers()
/// <inheritdoc/>
public override void Save(Formatting formatting = Formatting.Indented)
{
if (FLog.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.FilterEventType(EventType.TRACE))
FLog.Trace($"Saved {nameof(Config)}");
base.Save(formatting);
}
Expand All @@ -94,10 +97,31 @@ public override void Save(Formatting formatting = Formatting.Indented)
#region EventHandlers
private void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (FLog.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.FilterEventType(EventType.TRACE))
FLog.Trace($"Config property '{e.PropertyName}' was modified.");
Save();
}
private void UpdateFLogState(object? sender, PropertyChangedEventArgs e)
{
if (!FLog.IsInitialized) return;

// update the log's properties
if (e.PropertyName != null)
{
if (e.PropertyName.Equals(nameof(EnableLogging), StringComparison.Ordinal))
{
FLog.Log.EndpointEnabled = EnableLogging;
}
else if (e.PropertyName.Equals(nameof(LogFilter), StringComparison.Ordinal))
{
FLog.Log.EventTypeFilter = LogFilter;
}
else if (e.PropertyName.Equals(nameof(LogPath), StringComparison.Ordinal))
{
FLog.ChangeLogPath(LogPath);
}
}
}
private void PropertyWithPropertyChangedEvents_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (!_autoSaveEnabled) return;
Expand Down Expand Up @@ -332,14 +356,6 @@ private void PropertyWithCollectionChangedEvents_CollectionChanged(object? sende
/// </summary>
public bool ShowPeakMeters { get; set; } = true;
/// <summary>
/// The minimum boundary shown on peak meters.
/// </summary>
public const double PeakMeterMinValue = 0.0;
/// <summary>
/// The maximum boundary shown on peak meters.
/// </summary>
public const double PeakMeterMaxValue = 1.0;
/// <summary>
/// Gets or sets whether volume &amp; mute controls are visible in the Audio Devices list.
/// </summary>
public bool EnableDeviceControl { get; set; } = false;
Expand Down Expand Up @@ -367,10 +383,10 @@ private void PropertyWithCollectionChangedEvents_CollectionChanged(object? sende
// ^^^^^^^
// DO NOT RENAME THIS WITHOUT ALSO RENAMING IT IN VolumeControl.Log.SettingsInterface
/// <summary>
/// Gets or sets the <see cref="Log.Enum.EventType"/> filter used for messages.<br/>
/// Gets or sets the <see cref="Log.EventType"/> filter used for messages.<br/>
/// See <see cref="SettingsInterface.LogFilter"/>
/// </summary>
public Log.Enum.EventType LogFilter { get; set; } = Log.Enum.EventType.INFO | Log.Enum.EventType.WARN | Log.Enum.EventType.ERROR | Log.Enum.EventType.FATAL | Log.Enum.EventType.CRITICAL;
public EventType LogFilter { get; set; } = EventType.INFO | EventType.WARN | EventType.ERROR | EventType.FATAL | EventType.CRITICAL;
// ^^^^^^^^^
// DO NOT RENAME THIS WITHOUT ALSO RENAMING IT IN VolumeControl.Log.SettingsInterface
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion VolumeControl.Core/Input/Actions/HotkeyActionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private IActionSettingInstance[] CreateActionSettingInstances(IActionSettingInst
}
catch (Exception ex)
{
if (FLog.Log.FilterEventType(Log.Enum.EventType.ERROR))
if (FLog.Log.FilterEventType(EventType.ERROR))
FLog.Log.Error($"Failed to instantiate action setting \"{Name}\" due to an exception:", ex);
}
}
Expand Down
4 changes: 2 additions & 2 deletions VolumeControl.Core/Input/Actions/HotkeyActionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public void Invoke(object? sender, HotkeyPressedEventArgs e)
try
{
HotkeyActionDefinition.Invoke_Unsafe(sender, e);
if (FLog.Log.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.Log.FilterEventType(EventType.TRACE))
FLog.Log.Trace($"Successfully executed action \"{Name}\".");
}
catch (Exception ex)
{
if (FLog.Log.FilterEventType(Log.Enum.EventType.ERROR))
if (FLog.Log.FilterEventType(EventType.ERROR))
FLog.Log.Error($"Action \"{Name}\" triggered an exception:", ex);
}
}
Expand Down
34 changes: 19 additions & 15 deletions VolumeControl.Core/Input/HotkeyActionAddonLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace VolumeControl.Core.Input
/// </summary>
public static class HotkeyActionAddonLoader
{
#region LoadProviders
/// <summary>
/// Loads DataTemplate providers from the specified <paramref name="types"/> and registers them with the <paramref name="provider"/>
/// Loads DataTemplate providers from the specified <paramref name="types"/> and registers them with the <paramref name="provider"/>.
/// </summary>
/// <param name="provider">The <see cref="TemplateProviderManager"/> instance to load the provider types into.</param>
/// <param name="types">Any number of <see cref="Type"/> instances that represent classes with the <see cref="DataTemplateProviderAttribute"/>.</param>
Expand All @@ -36,11 +37,12 @@ public static void LoadProviders(ref TemplateProviderManager provider, params Ty
}
catch (Exception ex)
{
FLog.Error($"[ActionLoader] Failed to load {nameof(DataTemplate)} provider type \"{type}\" due to an exception:", ex);
FLog.Error($"[ActionAddonLoader] Failed to load {nameof(DataTemplate)} provider type \"{type}\" due to an exception:", ex);
}
}

}
#endregion LoadProviders

#region ValidateMethodIsEligibleAsAction
private enum EMethodValidationState : byte
Expand Down Expand Up @@ -116,6 +118,7 @@ private static EMethodValidationState ValidateMethodIsEligibleAsAction(MethodInf
}
#endregion ValidateMethodIsEligibleAsAction

#region LoadActions
/// <summary>
/// Loads hotkey actions from the specified <paramref name="types"/>.
/// </summary>
Expand Down Expand Up @@ -145,7 +148,7 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
// if this type doesn't have any public methods, skip it
if (publicMethods.Length == 0)
{
FLog.Error($"[ActionLoader] {type.FullName} doesn't contain any publicly-accessible methods marked with {typeof(HotkeyActionAttribute).FullName}!");
FLog.Error($"[ActionAddonLoader] {type.FullName} doesn't contain any publicly-accessible methods marked with {typeof(HotkeyActionAttribute).FullName}!");
continue;
}

Expand All @@ -168,15 +171,15 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
{
// this doesn't need more information because ValidateMethodIsEligibleAsAction
// logs all of the problems in detail anyway.
FLog.Error($"[ActionLoader] {method.GetFullMethodName()} was skipped because it is invalid.");
FLog.Error($"[ActionAddonLoader] {method.GetFullMethodName()} was skipped because it is invalid.");
continue;
}

// get the action setting definitions for this method
List<ActionSettingDefinition> actionSettingDefs = new();

if (FLog.FilterEventType(Log.Enum.EventType.DEBUG))
FLog.Debug($"[ActionLoader] Loading action setting definitions for \"{method.GetFullMethodName()}\"");
if (FLog.FilterEventType(EventType.DEBUG))
FLog.Debug($"[ActionAddonLoader] Loading action setting definitions for \"{method.GetFullMethodName()}\"");

foreach (var actionSettingAttribute in method.GetCustomAttributes<HotkeyActionSettingAttribute>())
{
Expand All @@ -192,7 +195,7 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
}
catch (Exception ex)
{
FLog.Error($"[ActionLoader] ", ex);
FLog.Error($"[ActionAddonLoader] ", ex);
}

if (dataTemplate == null)
Expand All @@ -212,7 +215,7 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
.GroupBy(d => d.Name)
.Where(g => g.Count() > 1)
.Select(g => $"\"{g.Key}\""));
FLog.Error($"[ActionLoader] {method.GetFullMethodName()} was skipped because multiple settings have the same name: {duplicateNames}!");
FLog.Error($"[ActionAddonLoader] {method.GetFullMethodName()} was skipped because multiple settings have the same name: {duplicateNames}!");
continue;
}

Expand All @@ -227,7 +230,7 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
}
catch (Exception ex)
{
FLog.Error($"[ActionLoader] {method.GetFullMethodName()} was skipped because constructor of type {type.Name} threw an exception:", ex);
FLog.Error($"[ActionAddonLoader] {method.GetFullMethodName()} was skipped because constructor of type {type.Name} threw an exception:", ex);
continue;
}

Expand Down Expand Up @@ -255,11 +258,11 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
l.Add(hotkeyActionDefinition);
++loadedActionsFromTypeCount;

if (FLog.Log.FilterEventType(Log.Enum.EventType.TRACE))
if (FLog.Log.FilterEventType(EventType.TRACE))
{
List<object?> lines = new();

var lineHeader = "[ActionLoader] ";
var lineHeader = "[ActionAddonLoader] ";
lines.Add($"{lineHeader}Loaded {method.GetFullMethodName()}.");
lineHeader = new string(' ', lineHeader.Length);
for (int k = 0, k_max = actionSettingDefs.Count; k < k_max; ++k)
Expand All @@ -273,14 +276,15 @@ public static HotkeyActionDefinition[] LoadActions(TemplateProviderManager provi
}
} //< enumerate public methods

if (FLog.Log.FilterEventType(Log.Enum.EventType.DEBUG))
FLog.Log.Debug($"[ActionLoader] Loaded {loadedActionsFromTypeCount}{(loadedActionsFromTypeCount == methodsWithActionAttrCount ? "" : $"/{methodsWithActionAttrCount}")} actions from {type.FullName}");
if (FLog.Log.FilterEventType(EventType.DEBUG))
FLog.Log.Debug($"[ActionAddonLoader] Loaded {loadedActionsFromTypeCount}{(loadedActionsFromTypeCount == methodsWithActionAttrCount ? "" : $"/{methodsWithActionAttrCount}")} actions from {type.FullName}");
} //< enumerate public types

if (FLog.Log.FilterEventType(Log.Enum.EventType.DEBUG))
FLog.Log.Debug($"[ActionLoader] Loaded {l.Count} total actions from {typesWithGroupAttrCount} action groups.");
if (FLog.Log.FilterEventType(EventType.DEBUG))
FLog.Log.Debug($"[ActionAddonLoader] Loaded {l.Count} total actions from {typesWithGroupAttrCount} action groups.");

return l.ToArray();
}
#endregion LoadActions
}
}
8 changes: 4 additions & 4 deletions VolumeControl.Core/Input/Json/HotkeyJsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public THotkey CreateInstance<THotkey>(HotkeyActionManager actionManager, bool d
}
else
{
if (FLog.FilterEventType(Log.Enum.EventType.ERROR))
if (FLog.FilterEventType(EventType.ERROR))
FLog.Error($"Couldn't find an action with identifier \"{ActionIdentifier}\"!");
}
}
Expand All @@ -99,7 +99,7 @@ private static IActionSettingInstance[] ActionSettingsDictionaryToArray(Dictiona

if (settingDefinition == null)
{
if (FLog.FilterEventType(Log.Enum.EventType.WARN))
if (FLog.FilterEventType(EventType.WARN))
FLog.Warning($"There is no action setting definition associated with JSON key '{name}' for action \"{actionDefinition.Identifier}\".");
continue;
}
Expand All @@ -112,7 +112,7 @@ private static IActionSettingInstance[] ActionSettingsDictionaryToArray(Dictiona
}
catch (Exception ex)
{
if (FLog.FilterEventType(Log.Enum.EventType.ERROR))
if (FLog.FilterEventType(EventType.ERROR))
FLog.Error($"An exception occurred while creating action setting \"{name}\" with value \"{value}\" for action \"{actionDefinition.Identifier}\":", ex);
#if DEBUG
throw; //< rethrow exception in DEBUG configuration
Expand All @@ -123,7 +123,7 @@ private static IActionSettingInstance[] ActionSettingsDictionaryToArray(Dictiona

if (settingInstance == null)
{
if (FLog.FilterEventType(Log.Enum.EventType.ERROR))
if (FLog.FilterEventType(EventType.ERROR))
FLog.Error($"An unknown error occurred while creating action setting \"{name}\" with value \"{value}\" for action \"{actionDefinition.Identifier}\"!");
continue;
}
Expand Down
Loading

0 comments on commit 7187f19

Please sign in to comment.