Files
RobustToolbox/Robust.Shared/Log/LogManager.cs
T
AcruidandGitHub 2183cd7ca1 Massive Namespace Cleanup (#1544)
* Removed the Interfaces folder.
* All objects inside the GameObjects subfolders are now in the GameObjects namespace.
* Added a Resharper DotSettings file to mark the GameObjects subfolders as not providing namespaces.
* Simplified Robust.client.Graphics namespace.
* Automated remove redundant using statements.
2021-02-10 23:27:19 -08:00

86 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
namespace Robust.Shared.Log
{
// Sealed. New functionality should be added with handlers.
public partial class LogManager : ILogManager, IDisposable
{
public const string SawmillProperty = "Sawmill";
public const string ROOT = "root";
private readonly Sawmill rootSawmill;
public ISawmill RootSawmill => rootSawmill;
private readonly Dictionary<string, Sawmill> sawmills = new();
private readonly ReaderWriterLockSlim _sawmillsLock = new();
public ISawmill GetSawmill(string name)
{
_sawmillsLock.EnterReadLock();
try
{
if (sawmills.TryGetValue(name, out var sawmill))
{
return sawmill;
}
}
finally
{
_sawmillsLock.ExitReadLock();
}
_sawmillsLock.EnterWriteLock();
try
{
return _getSawmillUnlocked(name);
}
finally
{
_sawmillsLock.ExitWriteLock();
}
}
private Sawmill _getSawmillUnlocked(string name)
{
if (sawmills.TryGetValue(name, out var sawmill))
{
return sawmill;
}
var index = name.LastIndexOf('.');
string parentName;
if (index == -1)
{
parentName = ROOT;
}
else
{
parentName = name.Substring(0, index);
}
var parent = _getSawmillUnlocked(parentName);
sawmill = new Sawmill(parent, name);
sawmills.Add(name, sawmill);
return sawmill;
}
public LogManager()
{
rootSawmill = new Sawmill(null, ROOT)
{
Level = LogLevel.Debug,
};
sawmills[ROOT] = rootSawmill;
}
public void Dispose()
{
foreach (Sawmill p in sawmills.Values)
{
p.Dispose();
}
}
}
}