Skip to content

Commit e76d143

Browse files
committed
Debug stream set up and logging
1 parent ff6ac71 commit e76d143

19 files changed

+406
-63
lines changed

Assets/Plugins/UniRx/Scripts/UnityEngineBridge/Diagnostics/ObservableDebugExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static IObservable<T> Debug<T>(this IObservable<T> source, string label =
1616
.Dematerialize()
1717
.DoOnCancel(() => UnityEngine.Debug.Log(l + "OnCancel"))
1818
.DoOnSubscribe(() => UnityEngine.Debug.Log(l + "OnSubscribe"));
19-
2019
#else
2120
return source;
2221
#endif
@@ -33,7 +32,6 @@ public static IObservable<T> Debug<T>(this IObservable<T> source, UniRx.Diagnost
3332
.Dematerialize()
3433
.DoOnCancel(() => logger.Debug("OnCancel"))
3534
.DoOnSubscribe(() => logger.Debug("OnSubscribe"));
36-
3735
#else
3836
return source;
3937
#endif

Assets/Scripts/AI/Behavior Logger.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UniRx;
7+
8+
namespace Assets.Scripts.AI.Behavior_Logger
9+
{
10+
public static class BehaviorDebugExtensions
11+
{
12+
public static IObservable<T> Debug<T>(this IObservable<T> source, BehaviorLogger logger)
13+
{
14+
#if DEBUG
15+
return source.Materialize()
16+
.Do(x => logger.Debug(x.ToString()))
17+
.Dematerialize()
18+
.DoOnCancel(() => logger.Debug("OnCancel"))
19+
.DoOnSubscribe(() => logger.Debug("OnSubscribe"));
20+
#else
21+
return source;
22+
#endif
23+
}
24+
25+
}
26+
}

Assets/Scripts/AI/Behavior Logger/BehaviorDebugExtensions.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Assets.Scripts.AI;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace Assets.Scripts.AI.Behavior_Logger
8+
{
9+
public struct BehaviorLogEntry
10+
{
11+
// requires
12+
public string LoggerName { get; private set; }
13+
public LogType LogType { get; private set; }
14+
public string Message { get; private set; }
15+
public DateTime Timestamp { get; private set; }
16+
17+
// options
18+
/// <summary>
19+
/// [Optional] Used to keep track of frames for debugging.
20+
/// </summary>
21+
public int TickNumber { get; private set; }
22+
23+
public BehaviorState NewState { get; private set; }
24+
25+
/// <summary>[Optional]</summary>
26+
public BehaviorTreeElement Context { get; private set; }
27+
/// <summary>[Optional]</summary>
28+
public Exception Exception { get; private set; }
29+
/// <summary>[Optional]</summary>
30+
public string StackTrace { get; private set; }
31+
/// <summary>[Optional]</summary>
32+
public object State { get; private set; }
33+
34+
public BehaviorLogEntry(string loggerName, LogType logType, DateTime timestamp, string message, BehaviorState newState = BehaviorState.Null, int ticknum = -1,
35+
BehaviorTreeElement context = null, Exception exception = null, string stackTrace = null, object state = null)
36+
: this()
37+
{
38+
this.LoggerName = loggerName;
39+
this.LogType = logType;
40+
this.Timestamp = timestamp;
41+
this.Message = message;
42+
this.NewState = newState;
43+
this.TickNumber = ticknum;
44+
this.Context = context;
45+
this.Exception = exception;
46+
this.StackTrace = stackTrace;
47+
this.State = state;
48+
}
49+
50+
public override string ToString()
51+
{
52+
var plusEx = (Exception != null) ? (Environment.NewLine + Exception.ToString()) : "";
53+
return "[" + Timestamp.ToString() + "]"
54+
+ "[" + LoggerName + "]"
55+
+ "[" + LogType.ToString() + "]"
56+
+ Message
57+
+ plusEx;
58+
}
59+
}
60+
}

Assets/Scripts/AI/Behavior Logger/BehaviorLogEntry.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using UniRx.Diagnostics;
5+
using UnityEngine;
6+
7+
namespace Assets.Scripts.AI.Behavior_Logger
8+
{
9+
public partial class BehaviorLogger
10+
{
11+
static bool isInitialized = false;
12+
static bool isDebugBuild = false;
13+
14+
public string Name { get; private set; }
15+
protected readonly Action<BehaviorLogEntry> logPublisher;
16+
17+
public BehaviorLogger(string loggerName)
18+
{
19+
this.Name = loggerName;
20+
this.logPublisher = ObservableBehaviorLogger.RegisterLogger(this);
21+
}
22+
23+
/// <summary>Output LogType.Log but only enabled if isDebugBuild</summary>
24+
public virtual void Debug(object message, BehaviorTreeElement context = null)
25+
{
26+
if (!isInitialized)
27+
{
28+
isInitialized = true;
29+
isDebugBuild = UnityEngine.Debug.isDebugBuild;
30+
}
31+
32+
if (isDebugBuild)
33+
{
34+
logPublisher(new BehaviorLogEntry(
35+
message: (message != null) ? message.ToString() : "",
36+
logType: LogType.Log,
37+
timestamp: DateTime.Now,
38+
loggerName: Name,
39+
context: context));
40+
}
41+
}
42+
43+
/// <summary>Output LogType.Log but only enables isDebugBuild</summary>
44+
public virtual void DebugFormat(string format, params object[] args)
45+
{
46+
if (!isInitialized)
47+
{
48+
isInitialized = true;
49+
isDebugBuild = UnityEngine.Debug.isDebugBuild;
50+
}
51+
52+
if (isDebugBuild)
53+
{
54+
logPublisher(new BehaviorLogEntry(
55+
message: (format != null) ? string.Format(format, args) : "",
56+
logType: LogType.Log,
57+
timestamp: DateTime.Now,
58+
loggerName: Name,
59+
context: null));
60+
}
61+
}
62+
63+
public virtual void Log(object message, BehaviorTreeElement context = null)
64+
{
65+
logPublisher(new BehaviorLogEntry(
66+
message: (message != null) ? message.ToString() : "",
67+
logType: LogType.Log,
68+
timestamp: DateTime.Now,
69+
loggerName: Name,
70+
context: context));
71+
}
72+
73+
public virtual void LogFormat(string format, params object[] args)
74+
{
75+
logPublisher(new BehaviorLogEntry(
76+
message: (format != null) ? string.Format(format, args) : "",
77+
logType: LogType.Log,
78+
timestamp: DateTime.Now,
79+
loggerName: Name,
80+
context: null));
81+
}
82+
83+
public virtual void Warning(object message, BehaviorTreeElement context = null)
84+
{
85+
logPublisher(new BehaviorLogEntry(
86+
message: (message != null) ? message.ToString() : "",
87+
logType: LogType.Warning,
88+
timestamp: DateTime.Now,
89+
loggerName: Name,
90+
context: context));
91+
}
92+
93+
public virtual void WarningFormat(string format, params object[] args)
94+
{
95+
logPublisher(new BehaviorLogEntry(
96+
message: (format != null) ? string.Format(format, args) : "",
97+
logType: LogType.Warning,
98+
timestamp: DateTime.Now,
99+
loggerName: Name,
100+
context: null));
101+
}
102+
103+
public virtual void Error(object message, BehaviorTreeElement context = null)
104+
{
105+
logPublisher(new BehaviorLogEntry(
106+
message: (message != null) ? message.ToString() : "",
107+
logType: LogType.Error,
108+
timestamp: DateTime.Now,
109+
loggerName: Name,
110+
context: context));
111+
}
112+
113+
public virtual void ErrorFormat(string format, params object[] args)
114+
{
115+
logPublisher(new BehaviorLogEntry(
116+
message: (format != null) ? string.Format(format, args) : "",
117+
logType: LogType.Error,
118+
timestamp: DateTime.Now,
119+
loggerName: Name,
120+
context: null));
121+
}
122+
123+
public virtual void Exception(Exception exception, BehaviorTreeElement context = null)
124+
{
125+
logPublisher(new BehaviorLogEntry(
126+
message: (exception != null) ? exception.ToString() : "",
127+
exception: exception,
128+
logType: LogType.Exception,
129+
timestamp: DateTime.Now,
130+
loggerName: Name,
131+
context: context));
132+
}
133+
134+
/// <summary>Publish raw LogEntry.</summary>
135+
public virtual void Raw(BehaviorLogEntry logEntry)
136+
{
137+
logPublisher(logEntry);
138+
}
139+
}
140+
}

Assets/Scripts/AI/Behavior Logger/BehaviorLogger.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using UniRx;
5+
using UnityEngine;
6+
7+
namespace Assets.Scripts.AI.Behavior_Logger
8+
{
9+
public class ObservableBehaviorLogger : IObservable<BehaviorLogEntry>
10+
{
11+
static readonly Subject<BehaviorLogEntry> logPublisher = new Subject<BehaviorLogEntry>();
12+
13+
public static readonly ObservableBehaviorLogger Listener = new ObservableBehaviorLogger();
14+
15+
private ObservableBehaviorLogger()
16+
{ }
17+
18+
public static Action<BehaviorLogEntry> RegisterLogger(BehaviorLogger logger)
19+
{
20+
if (logger.Name == null) throw new ArgumentNullException("logger.Name is null");
21+
22+
return logPublisher.OnNext;
23+
}
24+
25+
public IDisposable Subscribe(IObserver<BehaviorLogEntry> observer)
26+
{
27+
return logPublisher.Subscribe(observer);
28+
}
29+
}
30+
}

Assets/Scripts/AI/Behavior Logger/ObservableBehaviorLogger.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)