using System.Collections.Generic; using Crestron.SimplSharp; using Newtonsoft.Json; namespace PepperDash.Core.Logging { /// /// Represents a DebugContextCollection /// public class DebugContextCollection { /// /// To prevent threading issues with the DeviceDebugSettings collection /// private readonly CCriticalSection _deviceDebugSettingsLock; [JsonProperty("items")] private readonly Dictionary _items; /// /// Collection of the debug settings for each device where the dictionary key is the device key /// [JsonProperty("deviceDebugSettings")] private Dictionary DeviceDebugSettings { get; set; } /// /// Default constructor /// public DebugContextCollection() { _deviceDebugSettingsLock = new CCriticalSection(); DeviceDebugSettings = new Dictionary(); _items = new Dictionary(); } /// /// Sets the level of a given context item, and adds that item if it does not /// exist /// /// /// /// /// SetLevel method /// public void SetLevel(string contextKey, int level) { if (level < 0 || level > 2) return; GetOrCreateItem(contextKey).Level = level; } /// /// Gets a level or creates it if not existing /// /// /// /// /// GetOrCreateItem method /// public DebugContextItem GetOrCreateItem(string contextKey) { if (!_items.ContainsKey(contextKey)) _items[contextKey] = new DebugContextItem { Level = 0 }; return _items[contextKey]; } /// /// sets the settings for a device or creates a new entry /// /// /// /// /// /// SetDebugSettingsForKey method /// public void SetDebugSettingsForKey(string deviceKey, object settings) { try { _deviceDebugSettingsLock.Enter(); if (DeviceDebugSettings.ContainsKey(deviceKey)) { DeviceDebugSettings[deviceKey] = settings; } else DeviceDebugSettings.Add(deviceKey, settings); } finally { _deviceDebugSettingsLock.Leave(); } } /// /// Gets the device settings for a device by key or returns null /// /// /// /// /// GetDebugSettingsForKey method /// public object GetDebugSettingsForKey(string deviceKey) { return DeviceDebugSettings[deviceKey]; } } /// /// Contains information about /// public class DebugContextItem { /// /// The level of debug messages to print /// [JsonProperty("level")] public int Level { get; set; } /// /// Property to tell the program not to intitialize when it boots, if desired /// [JsonProperty("doNotLoadOnNextBoot")] public bool DoNotLoadOnNextBoot { get; set; } } }