Skip to content

Commit 4c205f7

Browse files
committed
Debug Chan will enforce history span
1 parent fd86f40 commit 4c205f7

File tree

10 files changed

+118
-44
lines changed

10 files changed

+118
-44
lines changed

Editor/Core/UI/LogWindow-DebugChan.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@ public void OnGenericMessage(string message, object sender, int messageCount){
1414
}
1515
}
1616

17-
void DrawLoggerTextView(){
17+
void DrawLoggerTextView(float time){
1818
string content;
1919
if(browsing && !useHistory){
2020
content = model.dcRange.Format();
2121
}else{
22-
content = EvalTextContent();
22+
content = EvalTextContent(time);
2323
}
2424
DrawTextView(content, ref dc_scroll);
2525
}
2626

27-
string EvalTextContent(){
27+
string EvalTextContent(float time){
2828
var logger = (Activ.Loggr.Logger<string, object>) DebugChan.logger;
2929
if(logger == null){
30-
return "Debug-Chan: not running";
30+
return ""; // "Debug-Chan: not running";
3131
}
3232
if(!Config.useSelection || model.selection == null){
3333
return "Debug-Chan: no selection";
3434
}
35-
if(useHistory && Ed.isPaused){
36-
var text = logger[model.selection]?.Format() ?? "Debug-Chan: No messages";
35+
if(useHistory && !isPlaying || Ed.isPaused){
36+
var startTime = time - Config.historySpan;
37+
var text = logger[model.selection]?.Format(since: startTime, time)
38+
?? "Debug-Chan: no messages";
3739
// NOTE: history does not include latest frame.
3840
var frame = logger.CurrentFrame(model.selection);
3941
if(frame != null) text += frame.Format();

Editor/Core/UI/LogWindow-Prolog.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public partial class LogWindow{ // Prolog
1818

1919
Vector2 p_scroll;
2020

21-
void DrawPrologView(){
21+
void DrawPrologView(float time){
2222
DrawPrologHeader();
23-
DrawPrologTextView();
23+
DrawPrologTextView(time);
2424
}
2525

2626
void DrawPrologHeader(){
@@ -34,12 +34,12 @@ void DrawPrologHeader(){
3434
EndHorizontal();
3535
}
3636

37-
void DrawPrologTextView(){
37+
void DrawPrologTextView(float time){
3838
string log;
3939
var rtype = rtypeOptions[Config.rtypeIndex];
4040
if(useHistory){
41-
var startTime = Time.time - Config.historySpan;
42-
log = model.GetPrologOutput(rtype, since: startTime);
41+
var startTime = time - Config.historySpan;
42+
log = model.GetPrologOutput(rtype, since: startTime, time);
4343
}else{
4444
log = model.GetPrologOutput(rtype);
4545
}

Editor/Core/UI/LogWindow.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public partial class LogWindow : EditorWindow{
1717
//
1818
static Font normalButtonFont;
1919
static Font _font;
20+
static float time;
2021

2122
string currentLog;
2223
int frame = -1; // Last frame while playing (store this?)
@@ -46,27 +47,35 @@ void OnGUI(){
4647
//
4748
model.current = Selection.activeGameObject;
4849
instance = this;
50+
if(isPlaying) time = Time.time;
4951
DrawScrubber();
50-
DrawLoggerTextView();
51-
if(Config.enableInjection) DrawPrologView();
52+
DrawLoggerTextView(time);
53+
if(Config.enableInjection) DrawPrologView(time);
5254
DrawFooter();
5355
}
5456

5557
void DrawFooter(){
5658
BeginHorizontal();
59+
// ◎
5760
Config.useSelection = ToggleLeft("Use Selection", Config.useSelection,
5861
GL.MaxWidth(100f));
62+
// ℋ ℌ
5963
Config.allFrames = ToggleLeft("History", Config.allFrames,
60-
GL.MaxWidth(60));
64+
GL.MaxWidth(64));
65+
// ☰▤
6166
DebugChan.logToConsole = Config.logToConsole
62-
= ToggleLeft("Log to console", Config.logToConsole,
63-
GL.MaxWidth(100));
67+
= ToggleLeft("Console", Config.logToConsole,
68+
GL.MaxWidth(100));
6469
//
65-
if(!isPlaying) DrawEnableInjection();
70+
if(!isPlaying){
71+
GL.FlexibleSpace();
72+
DrawEnableInjection();
73+
}
6674

6775
EndHorizontal();
6876
BeginHorizontal();
6977
//
78+
// ◈
7079
GL.Label("Trails - offset: ", GL.MaxWidth(88f));
7180
Config.trailOffset = FloatField(Config.trailOffset,
7281
GL.MaxWidth(30f));
@@ -87,8 +96,8 @@ void DrawFooter(){
8796

8897
void DrawEnableInjection(){
8998
Config.enableInjection = ToggleLeft(
90-
$"Instrument ({PrologLogger.injectionTimeMs}ms)",
91-
Config.enableInjection, GL.ExpandWidth(false));
99+
$" ({PrologLogger.injectionTimeMs}ms)",
100+
Config.enableInjection, GL.Width(30));
92101
}
93102

94103
void DrawTextView(string text, ref Vector2 scroll){
@@ -124,29 +133,36 @@ void DrawScrubber(){
124133
// TODO for now still broken; also, use case unclear
125134
//if(ScrubberButton("<")) SelectPrev();
126135
if(isPlaying){
127-
GL.Button($"#{frameNo:0000}", GL.MaxWidth(64f), GL.MinHeight(ScrubberButtonsHeight));
136+
GL.Button($"#{frameNo:0000}",
137+
GL.MaxWidth(64f),
138+
GL.MinHeight(ScrubberButtonsHeight));
128139
}else{
129-
GL.Button($"-----", GL.MaxWidth(64f), GL.MinHeight(ScrubberButtonsHeight));
140+
GL.Button($"-----",
141+
GL.MaxWidth(64f),
142+
GL.MinHeight(ScrubberButtonsHeight));
130143
}
131144
//if(ScrubberButton(">")) SelectNext();
132145
if(!isPlaying && ScrubberButton($"Clear")) Clear();
133146
GL.FlexibleSpace();
134147
EGL.LabelField("last", GL.Width(24));
135-
Config.historySpan = EGL.DelayedFloatField(Config.historySpan, GL.Width(32));
148+
Config.historySpan
149+
= EGL.DelayedFloatField(Config.historySpan, GL.Width(32));
136150
EGL.LabelField("s", GL.Width(16));
137151
style.font = normalButtonFont;
138152
EndHorizontal();
139153
}
140154

141155
bool ScrubberButton(string arg)
142-
=> GL.Button(arg, GL.ExpandWidth(false), GL.MinHeight(ScrubberButtonsHeight));
156+
=> GL.Button(arg, GL.ExpandWidth(false),
157+
GL.MinHeight(ScrubberButtonsHeight));
143158

144159
void ToggleAdvanced(){}
145160

146-
// Ref https://tinyurl.com/yyo8c35g which also demonstrates starting a 2D
147-
// GUI at handles location
161+
// Ref https://tinyurl.com/yyo8c35g which also demonstrates
162+
// starting a 2D GUI at handles location
148163
void OnSceneGUI(SceneView sceneView){
149-
var sel = PrologHistoryGUI.Draw(model.filtered, model.pgRange);
164+
var sel = PrologHistoryGUI.Draw(model.filtered,
165+
model.pgRange);
150166
if(Ed.isPaused || !isPlaying){
151167
model.pgRange = sel ?? model.pgRange;
152168
Repaint();
@@ -159,6 +175,7 @@ void OnSelectionChange()
159175
{ if(Ed.isPaused || !isPlaying) Repaint(); }
160176

161177
void Clear(){
178+
DebugChan.logger = null;
162179
PrologLogger.Clear();
163180
model.Clear();
164181
SceneView.RepaintAll();
@@ -184,11 +201,13 @@ public static void DisplayWindow(){
184201
static Font monofont{ get{
185202
if(_font) return _font;
186203
var avail = new []{
187-
"Menlo", "Consolas", "Inconsolata", "Bitstream Vera Sans Mono",
188-
"Oxygen Mono", "Ubuntu Mono", "Cousine", "Courier", "Courier New",
189-
"Lucida Console", "Monaco"
204+
"Menlo", "Consolas", "Inconsolata",
205+
"Bitstream Vera Sans Mono", "Oxygen Mono", "Ubuntu Mono",
206+
"Cousine", "Courier", "Courier New", "Lucida Console",
207+
"Monaco"
190208
}.Intersect(Font.GetOSInstalledFontNames()).First();
191-
return _font = Font.CreateDynamicFontFromOSFont(avail, FontSize);
209+
return _font = Font.CreateDynamicFontFromOSFont(avail,
210+
FontSize);
192211
}}
193212

194213
bool browsing

Editor/Core/UI/LogWindowModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ public string GetPrologOutput(string rtype){
6060
return PrologFormatter.State(source);
6161
}
6262

63-
public string GetPrologOutput(string rtype, float since){
63+
64+
public string GetPrologOutput(string rtype, float since, float currentTime){
6465
filter = new PrologFilter(selection, rtype);
65-
return PrologFormatter.Latest(source, since);
66+
return PrologFormatter.Latest(source, since, currentTime);
6667
}
6768

6869
public void OnPrologFrame(PrologFrame frame){

Editor/DebugChan/LoggingManager.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ static void OnPlayState(PlayModeStateChange state){
1515
case PlayModeStateChange.EnteredPlayMode:
1616
DebugChan.logger = new Logger<string, object>();
1717
DebugChan.logToConsole = Config.logToConsole;
18-
DebugChan.maxMessages = Config.maxMessages == 0 ? (int?)null : Config.maxMessages;
18+
DebugChan.maxMessages = Config.maxMessages == 0
19+
? (int?)null
20+
: Config.maxMessages;
1921
Activ.Loggr.UI.LogWindow.cumulatedMessageCount = 0;
2022
break;
21-
case PlayModeStateChange.ExitingPlayMode:
22-
DebugChan.logger = null;
23-
break;
23+
//case PlayModeStateChange.ExitingPlayMode:
24+
// DebugChan.logger = null;
25+
// break;
2426
}
2527
}
2628

Editor/Prolog/Format/Formatter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ public static string Latest(History history){
2323
return x.ToString();
2424
}
2525

26-
public static string Latest(History history, float startTime){
26+
public static string Latest(History history, float startTime, float time){
2727
var firstId = history.RangeId(startTime) - 1;
2828
// NOTE: still an error to have a negative id... probably
2929
if(firstId < 0) firstId = 0;
3030
//ebug.Log($"Format from {firstId}/{history.count} (φ0: {sinceFrame})");
3131
var x = new StringBuilder();
32-
var t = Time.time;
3332
for(int i = firstId; i < history.count; i++){
34-
var lapse = t - history[i].time;
33+
var lapse = time - history[i].time;
3534
x.Append($"\n[ {FrameRange(history, i)} ] {lapse:0.00}s ago "
3635
.PadRight(Config.LogLineLength, '-') + "\n\n");
3736
x.Append(history[i].Format(-1));

Editor/Prolog/Model/History.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public Frame Next(Frame x)
7575
public Frame Prev(Frame x)
7676
=> frames[ System.Math.Max(frames.IndexOf(x) - 1, 0) ];
7777

78-
// given the specified frame, return the
79-
// index of the first range containing frame φ
78+
// Return the index of the first range containing time 't'
8079
public int RangeId(float t){
8180
for(int i = 0; i < frames.Count; i++){
8281
if(ContainsTimeValue(rangeId: i, t)) return i;

Runtime/Core/Log.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEngine; // for UnityEngine.Time
22
using System.Collections.Generic;
3+
using System.Text;
34
using Ex = System.Exception;
45

56
namespace Activ.Loggr{
@@ -16,6 +17,12 @@ public Range<T> At(int φ){
1617
return null;
1718
}
1819

20+
// -------------------------------------------------------------
21+
22+
public int count => ranges.Count;
23+
24+
// -------------------------------------------------------------
25+
1926
public int? FirstStopAfter(int? frameIndex){
2027
if(!frameIndex.HasValue) return null;
2128
var i = frameIndex.Value;
@@ -51,7 +58,18 @@ public void LogMessage(T message, out int overhead){
5158
current.Add(message);
5259
}
5360

54-
public string Format() => ranges.Format();
61+
public string Format(float since, float time){
62+
int start = RangeId(since) - 1;
63+
if(start < 0) start = 0;
64+
var @out = new StringBuilder();
65+
var t = time;
66+
for(var i = start; i < count; i++){
67+
@out.Append(ranges[i].Format(t) + '\n');
68+
}
69+
return @out.ToString();
70+
}
71+
72+
// -------------------------------------------------------------
5573

5674
void FinalizeCurrentFrame(out int overhead){
5775
if(current == null){
@@ -73,6 +91,14 @@ void FinalizeCurrentFrame(out int overhead){
7391
}
7492
}
7593

94+
// Return the index of the first range containing time 't'
95+
int RangeId(float time){
96+
for(int i = ranges.Count - 1; i >= 0; i--){
97+
if(ranges[i].Contains(time)) return i;
98+
}
99+
return -1;
100+
}
101+
76102
Range<T> lastRange
77103
=> ranges.Count > 0 ? ranges[ranges.Count-1] : null;
78104

Runtime/Core/Range.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public Range(Stamp time, T[] messages){
1414
this.messages = messages;
1515
}
1616

17+
public bool Contains(float time){
18+
if(time <= start) return false;
19+
if(end == null) return true;
20+
return time < end;
21+
}
22+
23+
// NOTE: 'Add' or 'ExtendWith' may be better names
1724
public bool Include(Frame<T> arg){
1825
if(arg.time != end + 1) return false;
1926
var count = messages.Length;
@@ -27,6 +34,13 @@ public bool Include(Frame<T> arg){
2734

2835
public string Format() => FormatSpan() + "\n" + messages.Format();
2936

30-
string FormatSpan() => start.frame + " => " + end.frame;
37+
public string Format(float time)
38+
=> FormatSpan(time) + "\n" + messages.Format();
39+
40+
string FormatSpan()
41+
=> $"[ {start.frame}{end.frame} ]";
42+
43+
string FormatSpan(float time)
44+
=> $"[ {start.frame}{end.frame} ] {(time - start.time):0.00}s ago ";
3145

3246
}}

Runtime/Core/Stamp.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,23 @@ public static implicit operator int(Stamp arg)
3535
public static bool operator > (Stamp x, Stamp y)
3636
=> x.frame > y.frame;
3737

38+
public static bool operator > (Stamp x, float y)
39+
=> x.frame > y;
40+
41+
public static bool operator < (Stamp x, float y)
42+
=> x.frame < y;
43+
44+
public static bool operator > (float x, Stamp y)
45+
=> x > y.frame;
46+
47+
public static bool operator < (float x, Stamp y)
48+
=> x < y.frame;
49+
3850
public static bool operator < (Stamp x, Stamp y)
3951
=> x.frame < y.frame;
4052

4153
public static bool operator == (Stamp x, Stamp y)
42-
=> x.frame == y.frame;
54+
=> (x?.frame ?? -1) == (y?.frame ?? -1);
4355

4456
public static bool operator !=(Stamp x, Stamp y)
4557
=> x.frame != y.frame;

0 commit comments

Comments
 (0)