Skip to content

Commit d29769a

Browse files
author
Joshua Reason
committed
Started working on logging
1 parent 9151c30 commit d29769a

13 files changed

+342
-28
lines changed

Assets/ScriptableVariables/Editor/Diagnostics.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using UnityEditor;
6+
using UnityEngine;
7+
using static UnityEngine.GraphicsBuffer;
8+
9+
namespace Variables.Diagnostics.Editor
10+
{
11+
public class LogEditor
12+
{
13+
14+
private static GUIStyle s_linkLabel;
15+
private static GUIStyle s_darkBackground;
16+
17+
18+
private Variable m_target;
19+
private Vector2 m_scrollPosition;
20+
private StackTrace m_expandedLog;
21+
22+
public LogEditor(Variable target)
23+
{
24+
m_target = target;
25+
26+
27+
}
28+
29+
public void OnGUI()
30+
{
31+
32+
33+
if (s_linkLabel == null)
34+
SetUpStyles();
35+
36+
//Draw line + title
37+
EditorGUILayout.Space();
38+
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
39+
GUILayout.Label("Diagnostics", EditorStyles.boldLabel);
40+
41+
DrawBoolToggle();
42+
43+
if (!m_target.LogOutput)
44+
return;
45+
46+
//Create Scroll that lists will be drawn in
47+
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition, EditorStyles.helpBox, GUILayout.MaxHeight(EditorGUIUtility.singleLineHeight * 10));
48+
49+
//Get all logs
50+
var Logs = Diagnostics.StackLogger.GetUsage(m_target);
51+
if (Logs.Count == 0)
52+
GUILayout.FlexibleSpace();
53+
54+
bool isOdd = false;
55+
foreach (var log in Logs)
56+
{
57+
var isExpanded = log == m_expandedLog;
58+
isOdd = !isOdd;
59+
isExpanded = DrawStackFrame(log, isOdd, isExpanded);
60+
61+
if (isExpanded)
62+
m_expandedLog = log;
63+
else if (m_expandedLog == log)
64+
m_expandedLog = null;
65+
}
66+
67+
GUILayout.EndScrollView();
68+
}
69+
70+
71+
private bool DrawStackFrame(StackTrace trace, bool isOdd, bool isExpanded)
72+
{
73+
74+
var frame = trace.GetFrame(1);
75+
var frames = trace.GetFrames();
76+
string reltaivePath = GetPathRelative(frame.GetFileName(), Application.dataPath);
77+
78+
GUIStyle style = isOdd ? EditorStyles.label : s_darkBackground;
79+
var rect = EditorGUILayout.BeginVertical(style);
80+
GUILayout.BeginHorizontal();
81+
82+
if (GUILayout.Button(GUIContent.none, EditorStyles.foldout, GUILayout.Width(10)))
83+
isExpanded = !isExpanded;
84+
85+
GUILayout.BeginVertical();
86+
87+
if (isExpanded)
88+
DrawFrames(frames);
89+
else
90+
DrawFrames(frame);
91+
92+
93+
GUILayout.EndVertical();
94+
GUILayout.EndHorizontal();
95+
EditorGUILayout.EndVertical();
96+
97+
return isExpanded;
98+
}
99+
100+
private void DrawFrames(params StackFrame[] frames)
101+
{
102+
103+
foreach (var frame in frames)
104+
{
105+
//if (typeof(Variable).IsAssignableFrom(frame.GetMethod().DeclaringType))
106+
107+
string relativePath = GetPathRelative(frame.GetFileName(), Application.dataPath);
108+
109+
GUILayout.Button($"{frame.GetMethod().DeclaringType.FullName}:{frame.GetMethod().Name}", EditorStyles.boldLabel, GUILayout.Height(EditorGUIUtility.singleLineHeight * 0.5f));
110+
if (GUILayout.Button($" {relativePath}:{frame.GetFileLineNumber()}", s_linkLabel, GUILayout.Height(EditorGUIUtility.singleLineHeight * 0.75f)))
111+
{
112+
var file = AssetDatabase.LoadAssetAtPath<TextAsset>(relativePath);
113+
AssetDatabase.OpenAsset(file, frame.GetFileLineNumber(), frame.GetFileColumnNumber());
114+
115+
}
116+
117+
GUILayout.Space(EditorGUIUtility.singleLineHeight * 0.5f);
118+
}
119+
}
120+
121+
122+
private string GetPathRelative(string path, string relativeTo)
123+
{
124+
return new Uri(relativeTo).MakeRelativeUri(new Uri(path)).OriginalString;
125+
}
126+
127+
private void DrawBoolToggle()
128+
{
129+
m_target.LogOutput = EditorGUILayout.Toggle("Log Usage", m_target.LogOutput);
130+
}
131+
132+
private static void SetUpStyles()
133+
{
134+
s_linkLabel = new GUIStyle(EditorStyles.linkLabel);
135+
s_linkLabel.fontSize = (int)(EditorStyles.linkLabel.fontSize * 0.95f);
136+
137+
s_darkBackground = new GUIStyle(EditorStyles.label);
138+
var consoleBackground = new Texture2D(1, 1, TextureFormat.RGBAFloat, false);
139+
consoleBackground.SetPixel(0, 0, new Color(0.75f, 0.75f, 0.75f, 1f));
140+
consoleBackground.Apply();
141+
s_darkBackground.normal.background= consoleBackground;
142+
}
143+
144+
}
145+
}

Assets/ScriptableVariables/Editor/Diagnostics/LogEditor.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ScriptableVariables/Editor/VariableInspector.cs

+29-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
using System.Collections;
66
using System.Reflection;
77
using Inspector = UnityEditor.Editor;
8-
8+
using Codice.Client.BaseCommands.BranchExplorer;
9+
using Variables.Diagnostics.Editor;
910

1011
namespace Variables.Editor
1112
{
@@ -24,14 +25,30 @@ public class VariableInspector : Inspector
2425

2526
//Name of Variable Type
2627
private string m_TypeName;
28+
29+
//scroll position in stacktracebox
30+
private Vector2 m_stackTraceScroll;
31+
32+
private LogEditor m_logEditor;
33+
34+
#endregion
35+
36+
37+
#region Getters
38+
39+
protected new Variable target => base.target as Variable;
40+
2741
#endregion
2842

43+
2944
#region Unity Functions
3045
public void OnEnable()
3146
{
3247
//set up values
3348
m_value = serializedObject.FindProperty("m_value");
3449
m_TypeName = VariableMenuUtility.CachedAttributes[target.GetType()].GetNameOnly();
50+
51+
m_logEditor = new LogEditor(target);
3552
}
3653

3754
public override void OnInspectorGUI()
@@ -44,7 +61,7 @@ public override void OnInspectorGUI()
4461
m_value.isExpanded = true; //force property to be expanded (e.g. Quaternions)
4562

4663
EditorGUI.BeginChangeCheck();
47-
64+
4865
//display and apply property
4966
EditorGUILayout.PropertyField(m_value, new GUIContent(m_value.propertyType.ToString()), true);
5067

@@ -54,11 +71,15 @@ public override void OnInspectorGUI()
5471
if (EditorGUI.EndChangeCheck())
5572
{
5673
EditorVariableUtility.InvokeVariableEvent(target);
57-
}
74+
}
5875
}
5976

6077
EditorGUILayout.Space();
61-
78+
79+
m_logEditor?.OnGUI();
80+
81+
82+
6283

6384
}
6485
#endregion
@@ -87,7 +108,9 @@ private void TypeGUI()
87108
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
88109
}
89110

90-
111+
112+
113+
91114
#endregion GUI Functions
92115

93116
#region Helper Functions
@@ -119,7 +142,7 @@ private void ChangeType(Type type)
119142
Selection.activeObject = newAsset;
120143
}
121144

122-
}
145+
}
123146
#endregion
124147

125148
}

Assets/ScriptableVariables/Runtime/Diagnostics.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using UnityEngine;
7+
using Debug = UnityEngine.Debug;
8+
9+
namespace Variables.Diagnostics
10+
{
11+
public static class StackLogger
12+
{
13+
14+
public enum FunctionType {Get, Set}
15+
16+
17+
private static Dictionary<Variable,List<StackTrace>> s_AllLogs = new Dictionary<Variable,List<StackTrace>>();
18+
19+
static readonly Type[] TYPES_TO_IGNORE = { typeof(StackLogger), typeof(Variable) };
20+
21+
22+
public static void LogUsage(Variable variable)
23+
{
24+
25+
if (!s_AllLogs.TryGetValue(variable, out List<StackTrace> logs))
26+
{
27+
logs= new List<StackTrace>();
28+
s_AllLogs.Add(variable, logs);
29+
}
30+
31+
var st = new StackTrace(3,true);
32+
33+
//Debug.Log(PrintStackTrace(st));
34+
logs.Add(st);
35+
}
36+
37+
public static List<StackTrace> GetUsage(Variable variable)
38+
{
39+
if(s_AllLogs.TryGetValue(variable, out List<StackTrace> logs))
40+
return logs;
41+
42+
return new List<StackTrace>();
43+
}
44+
45+
public static string PrintStackTrace(StackTrace ST)
46+
{
47+
string retVal = "";
48+
49+
foreach(StackFrame frame in ST.GetFrames())
50+
{
51+
Debug.Log($"{frame.GetMethod().DeclaringType} == {typeof(StackLogger)} = {frame.GetMethod().DeclaringType == typeof(StackLogger)}");
52+
if (TYPES_TO_IGNORE.Contains(frame.GetMethod().DeclaringType))
53+
continue;
54+
55+
56+
retVal += StackFrameToString(frame);
57+
58+
retVal += "\r\n";
59+
}
60+
return retVal;
61+
}
62+
63+
public static string StackFrameToString(StackFrame frame)
64+
{
65+
var methodData = frame.GetMethod();
66+
return $"{frame.GetMethod().DeclaringType.FullName}:{methodData.Name} (at {frame.GetFileName()}:{frame.GetFileLineNumber()})";
67+
}
68+
69+
70+
71+
72+
}
73+
}
74+

Assets/ScriptableVariables/Runtime/Diagnostics/StackLogger.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ScriptableVariables/Runtime/Variable.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class Variable<T> : Variable
2424
[HideInInspector]
2525
public bool allowValueRepeating;
2626

27+
28+
2729
/// <summary>
2830
/// Current value of the variable
2931
/// </summary>
@@ -64,6 +66,16 @@ public void SetValue(T value)
6466
if (allowValueRepeating || !EqualityComparer<T>.Default.Equals(GetValue(), value))
6567
{
6668
m_value = value;
69+
70+
#if UNITY_EDITOR
71+
if (LogOutput)
72+
{
73+
Variables.Diagnostics.StackLogger.LogUsage(this);
74+
Debug.Log($"Set {name} to {value}");
75+
}
76+
#endif
77+
78+
6779
OnValueChanged?.Invoke(value);
6880
}
6981

@@ -90,7 +102,8 @@ public T GetValue()
90102
[System.Serializable]
91103
public class Variable : ScriptableObject
92104
{
93-
105+
[SerializeField]
106+
public bool LogOutput;
94107
}
95108

96109

0 commit comments

Comments
 (0)