Skip to content

Commit

Permalink
fix: fixing log settings that have no namespace (#1014)
Browse files Browse the repository at this point in the history
- removing duplicates
- updating settings from factory
- making sure classes with no namespace save/load correctly
  • Loading branch information
James-Frowen committed Dec 29, 2021
1 parent 88badd6 commit ead317f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Assets/Mirage/Editor/Logging/LogLevelsGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static LogSettingsSO DrawCreateNewButton()
}

readonly LogSettingsSO settings;
readonly LogSettingChecker checker;
readonly Dictionary<string, bool> folderOutState = new Dictionary<string, bool>();

/// <summary>
Expand All @@ -46,10 +47,13 @@ public static LogSettingsSO DrawCreateNewButton()
public LogLevelsGUI(LogSettingsSO settings)
{
this.settings = settings;
checker = new LogSettingChecker(settings);
}

public void Draw()
{
checker.Refresh();

guiChanged = false;

EditorGUI.BeginChangeCheck();
Expand Down Expand Up @@ -92,7 +96,7 @@ private void DrawAllLevelDropdown()

private void DrawGroup(IGrouping<string, LogSettingsSO.LoggerSettings> group)
{
string NameSpace = group.Key;
string NameSpace = group.Key ?? "<none>";
if (!folderOutState.ContainsKey(NameSpace))
folderOutState[NameSpace] = false;

Expand Down
55 changes: 55 additions & 0 deletions Assets/Mirage/Editor/Logging/LogSettingChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using Mirage.Logging;
using UnityEngine;

namespace Mirage.EditorScripts.Logging
{
/// <summary>
/// Removes duplicates and updates log settings from LogFactory
/// </summary>
public class LogSettingChecker
{
readonly LogSettingsSO settings;
readonly HashSet<string> duplicateChecker = new HashSet<string>();

public LogSettingChecker(LogSettingsSO settings)
{
this.settings = settings;
}

public void Refresh()
{
duplicateChecker.Clear();
RemoveDuplicates();
AddNewFromFactory();
}

void RemoveDuplicates()
{
for (int i = 0; i < settings.LogLevels.Count; i++)
{
bool added = duplicateChecker.Add(settings.LogLevels[i].FullName);
// is duplicate, remove it
if (!added)
{
settings.LogLevels.RemoveAt(i);
i--;
}
}
}

void AddNewFromFactory()
{
// try add new types
foreach (KeyValuePair<string, ILogger> logger in LogFactory.Loggers)
{
bool added = duplicateChecker.Add(logger.Key);
// is new, add it
if (added)
{
settings.LogLevels.Add(new LogSettingsSO.LoggerSettings(logger.Key, logger.Value.filterLogType));
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirage/Editor/Logging/LogSettingChecker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 27 additions & 3 deletions Assets/Mirage/Runtime/Logging/LogSettingsSO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ public class LoggerSettings
{
public string Name;
public string Namespace;
public string FullName => $"{Namespace}.{Name}";

private string fullNameCache;
public string FullName => fullNameCache;

static string CreateFullName(string name, string space)
{
// special case when namespace is null we just return null
// see GetNameAndNameSapceFromFullname
if (space == null)
return name;
else
return $"{space}.{name}";
}

public LogType logLevel;

Expand All @@ -23,15 +35,26 @@ public LoggerSettings(string name, string Namespace, LogType level)
Name = name;
this.Namespace = Namespace;
logLevel = level;
fullNameCache = CreateFullName(Name, Namespace);
}
public LoggerSettings(string fullname, LogType level)
{
(Name, Namespace) = GetNameAndNameSapceFromFullname(fullname);
(Name, Namespace) = GetNameAndNameSpaceFromFullname(fullname);
logLevel = level;
fullNameCache = CreateFullName(Name, Namespace);
}

private static (string name, string @namespace) GetNameAndNameSapceFromFullname(string fullname)
private static (string name, string @namespace) GetNameAndNameSpaceFromFullname(string fullname)
{
// NOTE we need to be able to recreate fullname from name/namespace
// so we cant always just use empty string for no namespace

if (!fullname.Contains("."))
{
// if no `.` then return null
return (fullname, null);
}

string[] parts = fullname.Split('.');
string name = parts.Last();

Expand All @@ -45,6 +68,7 @@ private static (string name, string @namespace) GetNameAndNameSapceFromFullname(
@namespace = string.Join(".", parts.Take(parts.Length - 1));
}

Debug.Assert(CreateFullName(name, @namespace) == fullname, "Could not re-create full name from created parted");
return (name, @namespace);
}
}
Expand Down

0 comments on commit ead317f

Please sign in to comment.