﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class I2LocTestLog
{
}

class TestLogger : ILogHandler
{
    public static TestLogger singleton;
    public static ILogHandler unityLogHandler;

    System.Text.StringBuilder text = new System.Text.StringBuilder();

    public static void Enable()
    {
        if (unityLogHandler == null)
            unityLogHandler = Debug.unityLogger.logHandler;

        if (singleton == null)
        {
            singleton = new TestLogger();
            Debug.unityLogger.logHandler = singleton;
        }
        singleton.text.Length = 0;
    }

    public static string Disable()
    {
        if (singleton == null)
            return null;
        var text = GetAllText();
        singleton = null;
        Debug.unityLogger.logHandler = unityLogHandler;
        return text;
    }

    public static string GetAllText( bool clear=false)
    {
        var s = singleton==null ? null : singleton.text.ToString();
        if (clear)
        {
            Clear();
        }
        return s;
    }

    public static void Clear()
    {
        if (singleton!=null)
            singleton.text.Length = 0;
    }

    public void LogException(System.Exception exception, UnityEngine.Object context)
    {
        Debug.unityLogger.LogException(exception, context);
    }

    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    {
        text.AppendLine(string.Format(format, args));
    }
}
