Skip to content

Commit ae37689

Browse files
committed
Prolog will enforce history span
1 parent ccbd5d0 commit ae37689

File tree

8 files changed

+111
-34
lines changed

8 files changed

+111
-34
lines changed

Editor/Core/UI/LogWindow-Prolog.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ void DrawPrologHeader(){
3535
}
3636

3737
void DrawPrologTextView(){
38-
string log = model.Output(useHistory, rtypeOptions[Config.rtypeIndex]);
38+
string log;
39+
var rtype = rtypeOptions[Config.rtypeIndex];
40+
if(useHistory){
41+
var startTime = Time.time - Config.historySpan;
42+
log = model.GetPrologOutput(rtype, since: startTime);
43+
}else{
44+
log = model.GetPrologOutput(rtype);
45+
}
46+
3947
if(currentLog != log && Config.step) Ed.isPaused = true;
4048
currentLog = log;
4149
DrawTextView(browsing ? model.pgRange.Format() : log, ref p_scroll);

Editor/Core/UI/LogWindowModel.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ public void SetCurrentFrame(int? frameIndex){
5555
}
5656
}
5757

58-
public string Output(bool useHistory, string rtype){
58+
public string GetPrologOutput(string rtype){
5959
filter = new PrologFilter(selection, rtype);
60-
return useHistory ? PrologFormatter.Latest(source)
61-
: PrologFormatter.State(source);
60+
return PrologFormatter.State(source);
61+
}
62+
63+
public string GetPrologOutput(string rtype, float since){
64+
filter = new PrologFilter(selection, rtype);
65+
return PrologFormatter.Latest(source, since);
6266
}
6367

6468
public void OnPrologFrame(PrologFrame frame){

Editor/Prolog/Format/Formatter.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,29 @@ public static string Latest(History history){
2323
return x.ToString();
2424
}
2525

26+
public static string Latest(History history, float startTime){
27+
// TODO arbitrary conversion
28+
var firstId = history.RangeId(startTime) - 1;
29+
// NOTE: still an error to have a negative id... probably
30+
if(firstId < 0) firstId = 0;
31+
//ebug.Log($"Format from {firstId}/{history.count} (φ0: {sinceFrame})");
32+
var x = new StringBuilder();
33+
var t = Time.time;
34+
for(int i = firstId; i < history.count; i++){
35+
var lapse = t - history[i].time;
36+
x.Append($"\n[ {FrameRange(history, i)} ] {lapse:0.00}s ago "
37+
.PadRight(Config.LogLineLength, '-') + "\n\n");
38+
x.Append(history[i].Format(-1));
39+
}
40+
return x.ToString();
41+
}
42+
2643
static string FrameRange(History history, int i){
2744
int begin = history[i].index, end = history.End(i);
2845
if(begin == end) return begin.ToString();
2946
// Upon exiting play mode, frame count is set to zero while components
3047
// are disabled, as a result, the end index of a range may be -1
31-
return end >= 0 ? $"{begin}{end}" : $"{begin}({end})";
48+
return end >= 0 ? $"{begin}{end}" : $"{begin}({end})";
3249
}
3350

3451
}}

Editor/Prolog/Logger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public static void Clear(){
4343
}
4444

4545
public static void Log(object src, string message)
46-
{ if(isPlaying) Process(new Message(Time.frameCount, src, message)); }
46+
{ if(isPlaying) Process(new Message(Time.frameCount, Time.time, src, message)); }
4747

4848
public static void LogStatic(string type, string message)
49-
{ if(isPlaying) Process(new Message(Time.frameCount, type, message)); }
49+
{ if(isPlaying) Process(new Message(Time.frameCount, Time.time, type, message)); }
5050

5151
static void Process(Message msg){
5252
if(frame + msg) return;

Editor/Prolog/Message.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ namespace Activ.Prolog{
44
public class Message{
55

66
public int frame;
7+
public float time;
78
public GameObjectInfo owner;
89
public object source;
910
public string _sourceType,
1011
message;
1112

12-
public Message(int frame, object source, string msg){
13+
public Message(int frame, float time, object source, string msg){
14+
this.time = time;
1315
this.frame = frame;
1416
this.source = source;
1517
this.message = msg;

Editor/Prolog/Model/Frame.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class Frame{
1414
public Frame(Message msg){
1515
messages.Add(msg);
1616
index = msg.frame;
17+
//time = messages[0].time;
1718
}
1819

1920
Frame(Frame source, Filter filter){
@@ -29,8 +30,11 @@ public Frame(Message msg){
2930
}
3031

3132
public bool empty => messages.Count == 0;
33+
3234
public int count => messages.Count;
3335

36+
public float time => messages[0].time;
37+
3438
public static Frame operator * (Frame self, Filter filter)
3539
=> filter.isNeutral ? self : new Frame(self, filter);
3640

Editor/Prolog/Model/History.cs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class History{
1010
string path;
1111
Filter filter = new Filter(null, "any");
1212

13+
// -------------------------------------------------------------
14+
1315
public History(string path = null){
1416
this.path = path;
1517
this.frames = new List<Frame>();
@@ -22,13 +24,31 @@ public History(Filter filter){
2224
foreach(var frame in Logger.frames){ var self = this + frame; }
2325
}
2426

25-
public Frame At(int φ){
26-
foreach(var range in frames){
27-
if(range.index >= φ) return range;
28-
}
29-
return null;
27+
// Public properties -------------------------------------------
28+
29+
public int count => frames.Count;
30+
31+
public bool empty => !this == 0;
32+
33+
public Frame last{
34+
get => empty ? null : frames[!this-1];
35+
set => frames.Add(value);
3036
}
3137

38+
public Frame this[int i] => frames[i];
39+
40+
public Frame this[int i, bool @default]
41+
=> (i >= frames.Count && @default) ? null : frames[i];
42+
43+
// Public functions --------------------------------------------
44+
45+
public Frame At(int φ) => this[RangeId(φ)];
46+
47+
public void Clear() => frames = new List<Frame>();
48+
49+
public int End(int i) // End of range at i
50+
=> i == !this - 1 ? Time.frameCount : frames[i + 1].index - 1;
51+
3252
// TODO support for selection here is... quirky?
3353
public int? FirstStopAfter(int? frameIndex, object src){
3454
if(!frameIndex.HasValue) return null;
@@ -49,25 +69,31 @@ public Frame At(int φ){
4969
return null;
5070
}
5171

52-
public bool empty => !this == 0;
53-
54-
public Frame last{
55-
get => empty ? null : frames[!this-1];
56-
set => frames.Add(value);
57-
}
58-
59-
public Frame this[int i] => frames[i];
60-
61-
public void Clear() => frames = new List<Frame>();
62-
6372
public Frame Next(Frame x)
6473
=> frames[ System.Math.Min(frames.IndexOf(x) + 1, frames.Count-1) ];
6574

6675
public Frame Prev(Frame x)
6776
=> frames[ System.Math.Max(frames.IndexOf(x) - 1, 0) ];
6877

69-
public int End(int i) // End of range at i
70-
=> i == !this - 1 ? Time.frameCount : frames[i + 1].index - 1;
78+
// given the specified frame, return the
79+
// index of the first range containing frame φ
80+
public int RangeId(float t){
81+
for(int i = 0; i < frames.Count; i++){
82+
if(ContainsTimeValue(rangeId: i, t)) return i;
83+
}
84+
return -1;
85+
}
86+
87+
// given the specified frame, return the
88+
// index of the first range containing frame φ
89+
public int RangeId(int φ){
90+
for(int i = 0; i < frames.Count; i++){
91+
if(ContainsFrameIndex(rangeId: i, φ)) return i;
92+
}
93+
return -1;
94+
}
95+
96+
// Operators ---------------------------------------------------
7197

7298
public static History operator + (History self, Frame frame){
7399
if(frame == null) return self;
@@ -87,6 +113,24 @@ public int End(int i) // End of range at i
87113
public static History operator / (History self, Filter filter)
88114
=> (self != null && self.filter == filter) ? self : new History(filter);
89115

116+
// PRIVATE -----------------------------------------------------
117+
118+
bool ContainsTimeValue(int rangeId, float time){
119+
var f0 = frames[rangeId];
120+
var f1 = this[rangeId + 1, @default: true];
121+
if(time < f0.time) return false;
122+
if(f1 == null) return true;
123+
return time < f1.time;
124+
}
125+
126+
bool ContainsFrameIndex(int rangeId, int φ){
127+
var f0 = frames[rangeId];
128+
var f1 = rangeId >= count - 1 ? null : frames[rangeId + 1];
129+
if(φ < f0.index) return false;
130+
if(f1 == null) return true;
131+
return φ < f1.index;
132+
}
133+
90134
static void print(string str) => UnityEngine.Debug.Log(str);
91135

92136
}}

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ Git URL
33
https://github.com/eelstork/Debug-Chan.git
44
```
55

6-
NOTE: Fork of *active-logic/prolog*; mainly because I need to share chanelled logging while roadwork is happening.
6+
NOTE: Fork of *active-logic/prolog* while roadwork is happening.
77

88
# Debug-Chan
99

10-
A handy logging tool for Unity game development:
10+
Handy logging/tracing tool for Unity game development:
1111

12-
- Channeled logging on a per game object basis
13-
- Automated call tracing (optional)
12+
- Channeled logging (per game object traces)
13+
- Automated trace generation (experimental/optional)
1414

15-
Tired of pesky `Debug.Log`? Would prefer beautiful logs that let you see what the @#%^@^$ your game objects are doing without getting in the way?
15+
Via UPM: `https://github.com/eelstork/Debug-Chan.git`
1616

17-
Debug-Chan is for you.
18-
19-
## Get started with chaneled logging
17+
## Get started with channeled logging
2018

2119
[pending]
2220

0 commit comments

Comments
 (0)