Skip to content

Commit

Permalink
Add future TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Mar 1, 2024
1 parent c8d5c65 commit 65850d4
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 92 deletions.
5 changes: 3 additions & 2 deletions Editor/BetterLogger.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"rootNamespace": "Better.Logger.EditorAddons",
"references": [
"GUID:a19d71ac5231b4a45a527dfc93a21f58",
"GUID:19891d5296046644cbc12fcf3702cca3",
"GUID:a59e3daedde9ca94bba45364d4ead25f"
"GUID:1862b35041b066d42ab4d3caf773657b",
"GUID:ecdd0819e3429eb44b13a432679a1135",
"GUID:01df13aca8d01e24a911bcc3e8277031"
],
"includePlatforms": [
"Editor"
Expand Down
16 changes: 0 additions & 16 deletions Editor/Settings/LoggerSettingsTool.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/Settings/LoggerSettingsTool.cs.meta

This file was deleted.

22 changes: 8 additions & 14 deletions Editor/Settings/ValidationSettingProvider.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
using System.Collections.Generic;
using Better.EditorTools.SettingsTools;
using Better.Internal.Core.Runtime;
using Better.Logger.Runtime.Settings;
using Better.Tools.Runtime;
using Better.ProjectSettings.EditorAddons;
using UnityEditor;

namespace Better.Logger.EditorAddons.Settings
{
internal class ValidationSettingProvider : ProjectSettingsProvider<LoggerSettings>
internal class LoggerSettingProvider : DefaultProjectSettingsProvider<LoggerSettings>
{
private readonly Editor _editor;

public ValidationSettingProvider() : base(ProjectSettingsToolsContainer<LoggerSettingsTool>.Instance, SettingsScope.Project)
private const string Path = PrefixConstants.BetterPrefix + "/" + nameof(Logger);
public LoggerSettingProvider() : base(Path)
{
keywords = new HashSet<string>(new[] { "Better", "Logger", "Warnings", "Debug", "Error", "Exception", "Ignore", "Log" });
_editor = Editor.CreateEditor(_settings);
}

[MenuItem(LoggerSettingsTool.MenuItemPrefix + "/" + BetterEditorDefines.HighlightPrefix, false, 999)]
[MenuItem(Path + "/" + PrefixConstants.HighlightPrefix, false, 999)]
private static void Highlight()
{
SettingsService.OpenProjectSettings(ProjectSettingsToolsContainer<LoggerSettingsTool>.Instance.ProjectSettingKey);
}

protected override void DrawGUI()
{
_editor.OnInspectorGUI();
SettingsService.OpenProjectSettings(ProjectPath + Path);
}
}
}
5 changes: 3 additions & 2 deletions Runtime/BetterLogger.Runtime.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "BetterLogger.Runtime",
"rootNamespace": "Better.Logger.Runtime",
"references": [
"GUID:a59e3daedde9ca94bba45364d4ead25f",
"GUID:28da8d3b12e3efa47928e0c9070f853d"
"GUID:28da8d3b12e3efa47928e0c9070f853d",
"GUID:1862b35041b066d42ab4d3caf773657b",
"GUID:af7fffdf1d83bc842b0f6e3a01efda16"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
39 changes: 34 additions & 5 deletions Runtime/Logger/JumpStackTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace Better.Logger.Runtime
{
public class JumpStackFrame : IDisposable
{
private StackFrame _stackTrace;
private StackFrame _stackFrame;
private int _jumpCount;
private string _message;

//TODO: Maybe move to Settings
private const int MaxEvaluateDepth = 5;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Jump(int jumpCount = 1)
Expand All @@ -21,12 +24,38 @@ public JumpStackFrame()
{
Jump();
}

public StackFrame GetStackFrame()
{
Jump();
_stackTrace = new StackFrame(_jumpCount, false);
return _stackTrace;
var original = new StackFrame(_jumpCount, false);

_stackFrame = EvaluateFrame(original, _jumpCount) ?? original;

return _stackFrame;
}

private StackFrame EvaluateFrame(StackFrame original, int jumpCount)
{
var bufferDepth = 0;
while (bufferDepth <= MaxEvaluateDepth)
{
bufferDepth++;
if (original == null) return null;
var methodBase = original.GetMethod();
if (methodBase == null || !ShouldBeExcluded(methodBase)) return original;
jumpCount++;
var bufferStack = new StackFrame(jumpCount, false);

original = bufferStack;
}

return null;
}

private static bool ShouldBeExcluded(MethodBase methodBase)
{
return methodBase.GetCustomAttribute<DebuggerNonUserCodeAttribute>() != null || methodBase.GetCustomAttribute<DebuggerHiddenAttribute>() != null;
}

public MethodBase GetMethod()
Expand All @@ -39,7 +68,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_stackTrace = null;
_stackFrame = null;
}
}

Expand Down
72 changes: 29 additions & 43 deletions Runtime/Logger/LogBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Better.Extensions.Runtime;
using Better.Logger.Runtime.Settings;
using UnityEditor;
using UnityEngine;

namespace Better.Logger.Runtime
{
public static class LogBuilder
{
private static readonly LoggerSettings _settings;
private static LoggerSettings _settings;

private const int JumpCount = 4;
private const string MethodNameFormat = "{0}{1}";
private const string Null = "null";
private const string Unknown = "Unknown";

static LogBuilder()

[InitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod]
private static void Initialize()
{
_settings = Resources.Load<LoggerSettings>(nameof(LoggerSettings));
_settings = LoggerSettings.Instance;
}

private static string GetDeclaringTypeName(MethodBase methodBase)
public static string BuildLogObject(string message)
{
if (methodBase == null)
{
return Unknown;
}
if (!_settings.UseFormatting)
return message;

return methodBase.DeclaringType != null ? methodBase.DeclaringType.Name : "Global";
return BuildLogByFormat(_settings.LogFormat, message, JumpCount);
}

private static string GetMethodName(MethodBase methodBase)
public static void BuildLogException(Exception exception, string message)
{
if (methodBase == null)
if (!_settings.UseFormatting)
return;

string logObject;
if (LoggerUtility.TryGetFirstCaller(exception, out var info))
{
return Unknown;
logObject = GetLogBuilder(_settings.ExceptionFormat, message, info.ClassName, info.MethodName).ToString();
}

var name = methodBase.Name;
switch (name)
else
{
case ".ctor":
case ".cctor":
var typeName = GetDeclaringTypeName(methodBase);
return string.Format(MethodNameFormat, typeName, name);
logObject = BuildLogByFormat(_settings.ExceptionFormat, message, JumpCount);
}

return name;
exception.ReplaceExceptionMessageField(logObject);
}

private static StringBuilder GetLogBuilder(string logFormat, string message, MethodBase methodBase)
{
var className = GetDeclaringTypeName(methodBase);
var methodName = GetMethodName(methodBase);
var className = LoggerUtility.GetDeclaringTypeName(methodBase);
var methodName = LoggerUtility.GetMethodName(methodBase);
return GetLogBuilder(logFormat, message, className, methodName);
}

private static StringBuilder GetLogBuilder(string logFormat, string message, string className, string methodName)
{
var length = logFormat.Length + className.Length + methodName.Length + message.Length;
var builder = new StringBuilder(logFormat, length);

Expand All @@ -63,14 +66,6 @@ private static StringBuilder GetLogBuilder(string logFormat, string message, Met
return builder;
}

public static string BuildLogObject(string message)
{
if (!_settings.UseFormatting)
return message;

return BuildLogByFormat(_settings.LogFormat, message, JumpCount);
}

private static string BuildLogByFormat(string logFormat, string message, int jumpCount)
{
using (var stackFrame = new JumpStackFrame())
Expand All @@ -81,14 +76,5 @@ private static string BuildLogByFormat(string logFormat, string message, int jum
return GetLogBuilder(logFormat, message, methodBase).ToString();
}
}

public static void BuildLogException(Exception exception, string message)
{
if (!_settings.UseFormatting)
return;

var logObject = BuildLogByFormat(_settings.ExceptionFormat, message, JumpCount);
exception.ReplaceExceptionMessageField(logObject);
}
}
}
96 changes: 96 additions & 0 deletions Runtime/Logger/LoggerUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using Better.Extensions.Runtime;

namespace Better.Logger.Runtime
{
public static class LoggerUtility
{
private static readonly string _firstCallerPattern = $@"(?<{ClassNameTag}>\w+)\.(?<{MethodNameTag}>\w+)\s*\(\s*\)";
private static readonly string _lambdaNamePattern = @"<(.*?)>";
private const string MethodNameFormat = "{0}{1}";
private const string ClassNameTag = "ClassName";
private const string MethodNameTag = "MethodName";
private const string Unknown = "Unknown";

public static string GetDeclaringTypeName(MethodBase methodBase)
{
if (methodBase == null)
{
return Unknown;
}

var declaringType = methodBase.DeclaringType;
if (declaringType == null) return "Global";
if (declaringType.IsAnonymous())
{
declaringType = declaringType.ReflectedType;
}

return declaringType != null ? declaringType.Name : "Global";
}



public static bool TryGetFirstCaller(Exception exception, out (string ClassName, string MethodName) info)
{
if (string.IsNullOrEmpty(exception.StackTrace))
{
info = new(Unknown, Unknown);
return false;
}

// Use Regex to find matches in the input string
var match = Regex.Match(exception.StackTrace, _firstCallerPattern);

var classNameGroup = match.Groups[ClassNameTag];
var methodNameGroup = match.Groups[MethodNameTag];
if (!classNameGroup.Success || !methodNameGroup.Success)
{
info = new(Unknown, Unknown);
return false;
}

var className = classNameGroup.Value;
var methodName = methodNameGroup.Value;
info = new(className, methodName);
return true;
}

public static string GetMethodName(MethodBase methodBase)
{
if (methodBase == null)
{
return Unknown;
}

var name = ExtractMethodName(methodBase.Name);

switch (name)
{
case ".ctor":
case ".cctor":
var typeName = GetDeclaringTypeName(methodBase);
return string.Format(MethodNameFormat, typeName, name);
}

return name;
}


public static string ExtractMethodName(string methodName)
{
// Simplified regex pattern to match only within < and >
var match = Regex.Match(methodName, _lambdaNamePattern);

if (match.Success)
{
// Group 1 contains the method name
return match.Groups[1].Value;
}

return methodName;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Logger/LoggerUtility.cs.meta

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

12 changes: 8 additions & 4 deletions Runtime/Settings/LoggerSettings.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using Better.Tools.Runtime.Settings;
using Better.ProjectSettings.Runtime;
using Better.Singletons.Runtime.Attributes;
using UnityEngine;

namespace Better.Logger.Runtime.Settings
{
public class LoggerSettings : ProjectSettings
[ScriptableCreate(nameof(Better) + "/" + nameof(Logger))]
public class LoggerSettings : ScriptableSettings<LoggerSettings>
{
[TextArea(1, 5)] [SerializeField]
[TextArea(1, 5)]
[SerializeField]
private string _logFormat = $"[{LoggerDefinitions.ClassName}] {LoggerDefinitions.MethodName}: {LoggerDefinitions.Message}";
[TextArea(1, 5)] [SerializeField]
[TextArea(1, 5)]
[SerializeField]
private string _exceptionFormat = $"[{LoggerDefinitions.ClassName}] {LoggerDefinitions.MethodName}: {LoggerDefinitions.Message}";

[SerializeField]
Expand Down
Loading

0 comments on commit 65850d4

Please sign in to comment.