public void Start()
        {
            // ObservableLogger.Listener.LogToUnityDebug()を呼び出すことで、
            // 発行されたログをUnityEditorのコンソールに出力してくれるようになる
            // なお複数回呼び出すとログが重複して出力されてしまう
            // 多重実行に注意
            UniRx.Diagnostics.ObservableLogger.Listener.LogToUnityDebug();

            // ObservableLogger.ListenerはIObservable<LogEntry>
            // これを購読することで発行されたログを購読できる
            UniRx.Diagnostics.ObservableLogger.Listener
            // ログレベルでフィルタリング
            .Where(x => x.LogType == LogType.Error || x.LogType == LogType.Exception)
            // 1フレームの間に発行されたログを集約する
            .BatchFrame()
            //発行されたログをファイルに書き出す
            .Subscribe(WriteAsync)
            .AddTo(this);

            // loggerを用いて任意のLogメッセージを発行可能
            // 発行したLogメッセージは ObservableLogger.Listener より購読可能
            _logger.Debug("Debugメッセージ");
            _logger.Error("エラーが発生しました");
            _logger.Exception(new Exception("例外が発生しました"));
        }
 public void Run()
 {
     // Debug is write only DebugBuild.
     logger.Debug("Debug Message");
     // or other logging methods
     logger.Log("Message");
     logger.Exception(new Exception("test exception"));
 }
Beispiel #3
0
    void Start()
    {
        ObservableLogger.Listener.LogToUnityDebug();

        ObservableLogger.Listener
        .Where(_ => _.LogType == LogType.Exception)
        .Subscribe(_ => print(_.Exception.Message));

        _logger.Debug("debug message");
        _logger.Log("log");
        _logger.Exception(new Exception("exception"));
    }
Beispiel #4
0
        public void OnGUI()
        {
            var xpos = 0;
            var ypos = 0;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Clear"))
            {
                logtext.Length = 0;
                disposables.Clear();
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Co"))
            {
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Every"))
            {
                Observable.EveryGameObjectUpdate().Subscribe(x => Debug.Log("EveryGameObjectUpdate" + x));
                Observable.EveryUpdate().Subscribe(x => Debug.Log("EveryUpdate:" + x));
                Observable.EveryAfterUpdate().Subscribe(x => Debug.Log("EveryAfterUpdate:" + x));
                UnityEngine.Debug.Log("---");
                Observable.EveryLateUpdate().Subscribe(x => Debug.Log("EveryLateUpdate:" + x));
                Observable.EveryEndOfFrame().Subscribe(x => Debug.Log("EveryEndOfFrame:" + x));
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Select"))
            {
                Source.Select(x => x * 100).Subscribe(x => logger.Debug(x)).AddTo(disposables);
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "SubscribePerf1"))
            {
                var list = Enumerable.Range(1, 10000).Select(x => new ReactiveProperty <int>(x)).ToArray();

                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Subscribe();
                    }
                    sw.Stop();
                    logger.Debug("Direct Subscribe:" + sw.Elapsed.TotalMilliseconds + "ms");
                }

                {
                    var r = UnityEngine.Random.Range(1, 100);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Value = r;
                    }
                    sw.Stop();
                    logger.Debug("Push Direct Perf:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "SubscribePerf2"))
            {
                var list = Enumerable.Range(1, 10000).Select(x => new ReactiveProperty <int>(x)).ToArray();

                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Select(x => x).Subscribe();
                    }
                    sw.Stop();
                    logger.Debug("Select.Subscribe:" + sw.Elapsed.TotalMilliseconds + "ms");
                }

                {
                    var r = UnityEngine.Random.Range(1, 100);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Value = r;
                    }
                    sw.Stop();
                    logger.Debug("Push Select Perf:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Subscribe Perf:"))
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                var sw = System.Diagnostics.Stopwatch.StartNew();

                var list = Enumerable.Range(1, 10000).Select(x => new Subject <int>()).ToArray();
                foreach (var item in list)
                {
                    item.Where(x => x % 2 == 0).Select(x => x * x).Take(10).Subscribe();
                }

                sw.Stop();
                logger.Debug("Subscribe Perf:" + sw.Elapsed.TotalMilliseconds + "ms");
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "OnNext Perf"))
            {
                var list = Enumerable.Range(1, 10000).Select(x => new ReactiveProperty <int>(0)).ToArray();

                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Where(x => x % 2 == 0).Select(x => x * x).Take(10).Subscribe();
                    }
                    sw.Stop();
                    logger.Debug("Subscribe + OnNext Perf:" + sw.Elapsed.TotalMilliseconds + "ms");
                }

                {
                    var r = UnityEngine.Random.Range(1, 100);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();
                    foreach (var item in list)
                    {
                        item.Value = r;
                    }
                    sw.Stop();
                    logger.Debug("OnNext Perf:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Check Compare Perf"))
            {
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();

                    for (int i = 0; i < 10000; i++)
                    {
                        var s = new Subject <long>();
                        Interlocked.CompareExchange(ref subj, s, null);
                    }

                    logger.Debug("Interlocked:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();

                    for (int i = 0; i < 10000; i++)
                    {
                        lock (gate)
                        {
                            if (subj == null)
                            {
                                subj = new Subject <long>();
                            }
                        }
                        subj = null;
                    }

                    logger.Debug("Lock:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    var sw = System.Diagnostics.Stopwatch.StartNew();

                    for (int i = 0; i < 10000; i++)
                    {
                        if (subj == null)
                        {
                            subj = new Subject <long>();
                        }
                        subj = null;
                    }

                    logger.Debug("Plain:" + sw.Elapsed.TotalMilliseconds + "ms");
                }
            }
            ypos += 100;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ObserveOn Check:"))
            {
                var s = new Subject <int>();

                Debug.Log("Before ObserveOn:" + Time.frameCount);
                s.ObserveOnMainThread()
                .Subscribe(x =>
                {
                    Debug.Log("Subscribe:" + Time.frameCount + "|" + x);
                });

                s.OnNext(997);
                s.OnNext(998);
                s.OnNext(999);
                Debug.Log("After OnNext:" + Time.frameCount);
            }
            ypos += 100;


            //if (GUI.Button(new Rect(xpos, ypos, 100, 100), "CurrentThreadScheduler"))
            //{
            //    try
            //    {
            //        Scheduler.CurrentThread.Schedule(() =>
            //        {
            //            try
            //            {
            //                logtext.AppendLine("test threadscheduler");
            //            }
            //            catch (Exception ex)
            //            {
            //                logtext.AppendLine("innner ex" + ex.ToString());
            //            }
            //        });
            //    }
            //    catch (Exception ex)
            //    {
            //        logtext.AppendLine("outer ex" + ex.ToString());
            //    }
            //}
            //ypos += 100;
            //if (GUI.Button(new Rect(xpos, ypos, 100, 100), "EveryUpdate"))
            //{
            //    Observable.EveryUpdate()
            //        .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()))
            //        .AddTo(disposables);
            //}
            //ypos += 100;
            //if (GUI.Button(new Rect(xpos, ypos, 100, 100), "FromCoroutinePure"))
            //{
            //    Observable.Create<Unit>(observer =>
            //    {
            //        var cancel = new BooleanDisposable();

            //        MainThreadDispatcher.StartCoroutine(Hoge(observer));

            //        return cancel;
            //    })
            //    .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()));
            //}
            //ypos += 100;
            //if (GUI.Button(new Rect(xpos, ypos, 100, 100), "FromCoroutine"))
            //{
            //    Observable.FromCoroutine<Unit>(Hoge)
            //    .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()));
            //}


            /*
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale-1"))
             * {
             *  Time.timeScale -= 1f;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale+1"))
             * {
             *  Time.timeScale += 1f;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale=0"))
             * {
             *  Time.timeScale = 0;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale=100"))
             * {
             *  Time.timeScale = 100;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler0"))
             * {
             *  logger.Debug("run");
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(5000), () =>
             *  {
             *      logger.Debug(DateTime.Now);
             *  });
             * }
             *
             * xpos += 100;
             * ypos = 0;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler1"))
             * {
             *  logger.Debug("Before Start");
             *  Scheduler.MainThread.Schedule(() => logger.Debug("immediate"));
             *  Scheduler.MainThread.Schedule(TimeSpan.Zero, () => logger.Debug("zero span"));
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(1), () => logger.Debug("0.1 span"));
             *  logger.Debug("After Start");
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler2"))
             * {
             *  logger.Debug("M:Before Start");
             *  Scheduler.MainThread.Schedule(TimeSpan.FromSeconds(5), () => logger.Debug("M:after 5 minutes"));
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(5500), () => logger.Debug("M:after 5.5 minutes"));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Realtime"))
             * {
             *  logger.Debug("R:Before Start");
             *  Scheduler.MainThreadIgnoreTimeScale.Schedule(TimeSpan.FromSeconds(5), () => logger.Debug("R:after 5 minutes"));
             *  Scheduler.MainThreadIgnoreTimeScale.Schedule(TimeSpan.FromMilliseconds(5500), () => logger.Debug("R:after 5.5 minutes"));
             * }
             *
             #if !UNITY_METRO
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ManagedThreadId"))
             * {
             *  logger.Debug("Current:" + Thread.CurrentThread.ManagedThreadId);
             *  new Thread(_ => logger.Debug("NewThread:" + Thread.CurrentThread.ManagedThreadId)).Start();
             *  ThreadPool.QueueUserWorkItem(_ =>
             *  {
             *      logger.Debug("ThraedPool:" + Thread.CurrentThread.ManagedThreadId);
             *      this.transform.position = new Vector3(0, 0, 0); // exception
             *  });
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ThreadStatic"))
             * {
             *  logger.Debug(threadstaticobj != null);
             *  new Thread(_ => logger.Debug(threadstaticobj != null)).Start();
             *  ThreadPool.QueueUserWorkItem(_ => logger.Debug(threadstaticobj != null));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Log"))
             * {
             *  logger.Debug("test", this);
             *  ThreadPool.QueueUserWorkItem(_ => logger.Debug("test2", this));
             * }
             *
             #endif
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "POST"))
             * {
             *  var form = new WWWForm();
             *  form.AddField("test", "abcdefg");
             *  ObservableWWW.PostWWW("http://localhost:53395/Handler1.ashx", form, new Hash
             * {
             *  {"aaaa", "bbb"},
             *  {"User-Agent", "HugaHuga"}
             * })
             *  .Subscribe(x => logger.Debug(x.text));
             * }
             *
             * xpos += 100;
             * ypos = 0;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Yield"))
             * {
             *  yieldCancel = Observable.FromCoroutineValue<string>(StringYield, false)
             *      .Subscribe(x => logger.Debug(x), ex => logger.Debug("E-x:" + ex));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "YieldCancel"))
             * {
             *  yieldCancel.Dispose();
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ThreadPool"))
             * {
             *  Observable.Timer(TimeSpan.FromMilliseconds(400), Scheduler.ThreadPool)
             *      .ObserveOnMainThread()
             *      .Subscribe(x => logger.Debug(x));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Subscribe"))
             * {
             *  subscriber.InitSubscriptions();
             *  logger.Debug("Subscribe++ : " + subscriber.SubscriptionCount);
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Push"))
             * {
             *  Publisher.foo();
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Unsubscriber"))
             * {
             *  subscriber.RemoveSubscriptions();
             *  logger.Debug("UnsubscribeAll : " + subscriber.SubscriptionCount);
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "DistinctUntilChanged"))
             * {
             *  new[] { "hoge", null, null, "huga", "huga", "hoge" }
             *      .ToObservable()
             *      .DistinctUntilChanged()
             *      .Subscribe(x => logger.Debug(x));
             * }
             * */

            // Time

            var sb = new StringBuilder();

            sb.AppendLine("CaptureFramerate:" + Time.captureFramerate);
            sb.AppendLine("deltaTime:" + Time.deltaTime);
            sb.AppendLine("fixedDeltaTime:" + Time.fixedDeltaTime);
            sb.AppendLine("fixedTime:" + Time.fixedTime);
            sb.AppendLine("frameCount:" + Time.frameCount);
            sb.AppendLine("maximumDeltaTime:" + Time.maximumDeltaTime);
            sb.AppendLine("realtimeSinceStartup:" + Time.realtimeSinceStartup);
            sb.AppendLine("renderedFrameCount:" + Time.renderedFrameCount);
            sb.AppendLine("smoothDeltaTime:" + Time.smoothDeltaTime);
            sb.AppendLine("time:" + Time.time);
            sb.AppendLine("timeScale:" + Time.timeScale);
            sb.AppendLine("timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
            sb.AppendLine("unscaledDeltaTime:" + Time.unscaledDeltaTime);
            sb.AppendLine("unscaledTime:" + Time.unscaledTime);

            //GUI.Box(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), "Time");
            //GUI.Label(new Rect(Screen.width - 290, Screen.height - 290, 290, 290), sb.ToString());

            // logtext only
            GUI.Box(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), "logtext");
            GUI.Label(new Rect(Screen.width - 290, Screen.height - 290, 290, 290), logtext.ToString());

            // Log
            //GUI.Box(new Rect(Screen.width - 300, 0, 300, 300), "Log");
            //GUI.Label(new Rect(Screen.width - 290, 10, 290, 290), logtext.ToString());
        }