<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

public class LogEventArgs : EventArgs
{
    public LogEventArgs(int seqID, LogType type, string content, float time)
    {
        SeqID = seqID;
        LogType = type;
        Content = content;
        Time = time;
    }

    public int SeqID = 0;
    public LogType LogType;
    public string Content = "";
    public float Time = 0.0f;
}

public delegate void LogTargetHandler(object sender, LogEventArgs args);

public class LogService : IDisposable
{
    public event LogTargetHandler LogTargets;

    public LogService(bool logIntoFile)
    {
        RegisterCallback();

        if (logIntoFile)
        {
            try
            {
                DateTime dt = DateTime.Now;

                string logDir = SysUtil.CombinePaths(Application.persistentDataPath, "log", SysUtil.FormatDateAsFileNameString(dt));
                Directory.CreateDirectory(logDir);

                string logPath = Path.Combine(logDir, SysUtil.FormatTimeAsFileNameString(dt) + ".txt");

                _logWriter = new FileInfo(logPath).CreateText();
                _logWriter.AutoFlush = true;
                _logPath = logPath;

                Log.Info("'Log Into File' enabled, file opened successfully. ('{0}')", _logPath);
            }
            catch (System.Exception ex)
            {
                Log.Info("'Log Into File' enabled but failed to open file.");
                Log.Exception(ex);
            }
        }
        else
        {
            Log.Info("'Log Into File' disabled.");
        }
    }

    public void Dispose()
    {
        Log.Info("destroying log service...");

        if (_logWriter != null)
        {
            _logWriter.Close();
        }

#if UNITY_5_0
        Application.logMessageReceivedThreaded -= Logging.OnLogReceived;
#endif

        _disposed = true;
    }

    private void RegisterCallback()
    {
#if UNITY_5_0
        Application.logMessageReceivedThreaded += Logging.OnLogReceived;
#else
        Application.RegisterLogCallbackThreaded(OnLogReceived);
#endif
    }

    private void OnLogReceived(string condition, string stackTrace, LogType type)
    {
        if (_disposed || _isWriting)
            return;

        _isWriting = true;

        ++_seqID;

        switch (type)
        {
            case LogType.Assert:
                _assertCount++;
                break;
            case LogType.Error:
                _errorCount++;
                break;
            case LogType.Exception:
                _exceptionCount++;
                break;

            case LogType.Warning:
            case LogType.Log:
            default:
                break;
        }

        try
        {
            if (_logWriter != null)
            {
                _logWriter.WriteLine("{0:0.00} {1}: {2}", Time.realtimeSinceStartup, type, condition);
            }

            foreach (LogTargetHandler Caster in LogTargets.GetInvocationList())
            {
                ISynchronizeInvoke SyncInvoke = Caster.Target as ISynchronizeInvoke;
                LogEventArgs args = new LogEventArgs(_seqID, type, condition, Time.realtimeSinceStartup);

                if (SyncInvoke != null && SyncInvoke.InvokeRequired)
                    SyncInvoke.Invoke(Caster, new object[] { this, args });
                else
                    Caster(this, args);
            }
        }
        catch (System.Exception ex)
        {
            Log.Exception(ex); // this should at least print to Unity Editor (but skip the file writing due to earlier writing failure)            	
        }

        _isWriting = false;
    }

    private string _logPath;
    private StreamWriter _logWriter;

    private ushort _seqID = 0;
    private int _assertCount = 0;
    private int _errorCount = 0;
    private int _exceptionCount = 0;

    private bool _disposed = false;
    private bool _isWriting = false;
}<commit_msg>添加日志记录路径<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

public class LogEventArgs : EventArgs
{
    public LogEventArgs(int seqID, LogType type, string content, float time)
    {
        SeqID = seqID;
        LogType = type;
        Content = content;
        Time = time;
    }

    public int SeqID = 0;
    public LogType LogType;
    public string Content = "";
    public float Time = 0.0f;
}

public delegate void LogTargetHandler(object sender, LogEventArgs args);

public class LogService : IDisposable
{
    public event LogTargetHandler LogTargets;

    public LogService(bool logIntoFile)
    {
        RegisterCallback();

        if (logIntoFile)
        {
            try
            {
                DateTime dt = DateTime.Now;

                string logDir = SysUtil.CombinePaths(Application.persistentDataPath, "log", SysUtil.FormatDateAsFileNameString(dt));
                Directory.CreateDirectory(logDir);

                string logPath = Path.Combine(logDir, SysUtil.FormatTimeAsFileNameString(dt) + ".txt");

                _logWriter = new FileInfo(logPath).CreateText();
                _logWriter.AutoFlush = true;
                _logPath = logPath;

                Log.Info("'Log Into File' enabled, file opened successfully. ('{0}')", _logPath);
                LastLogFile = _logPath;
            }
            catch (System.Exception ex)
            {
                Log.Info("'Log Into File' enabled but failed to open file.");
                Log.Exception(ex);
            }
        }
        else
        {
            Log.Info("'Log Into File' disabled.");
            LastLogFile = "";
        }
    }

    public void Dispose()
    {
        Log.Info("destroying log service...");

        if (_logWriter != null)
        {
            _logWriter.Close();
        }

#if UNITY_5_0
        Application.logMessageReceivedThreaded -= Logging.OnLogReceived;
#endif

        _disposed = true;
    }

    private void RegisterCallback()
    {
#if UNITY_5_0
        Application.logMessageReceivedThreaded += Logging.OnLogReceived;
#else
        Application.RegisterLogCallbackThreaded(OnLogReceived);
#endif
    }

    private void OnLogReceived(string condition, string stackTrace, LogType type)
    {
        if (_disposed || _isWriting)
            return;

        _isWriting = true;

        ++_seqID;

        switch (type)
        {
            case LogType.Assert:
                _assertCount++;
                break;
            case LogType.Error:
                _errorCount++;
                break;
            case LogType.Exception:
                _exceptionCount++;
                break;

            case LogType.Warning:
            case LogType.Log:
            default:
                break;
        }

        try
        {
            if (_logWriter != null)
            {
                _logWriter.WriteLine("{0:0.00} {1}: {2}", Time.realtimeSinceStartup, type, condition);
            }

            foreach (LogTargetHandler Caster in LogTargets.GetInvocationList())
            {
                ISynchronizeInvoke SyncInvoke = Caster.Target as ISynchronizeInvoke;
                LogEventArgs args = new LogEventArgs(_seqID, type, condition, Time.realtimeSinceStartup);

                if (SyncInvoke != null && SyncInvoke.InvokeRequired)
                    SyncInvoke.Invoke(Caster, new object[] { this, args });
                else
                    Caster(this, args);
            }
        }
        catch (System.Exception ex)
        {
            Log.Exception(ex); // this should at least print to Unity Editor (but skip the file writing due to earlier writing failure)            	
        }

        _isWriting = false;
    }

    private string _logPath;
    private StreamWriter _logWriter;

    private ushort _seqID = 0;
    private int _assertCount = 0;
    private int _errorCount = 0;
    private int _exceptionCount = 0;

    private bool _disposed = false;
    private bool _isWriting = false;

    public static string LastLogFile { get; set; }
}
{'fraction_non_alphanumeric': 0.08507007921998781, 'fraction_numerical': 0.0043875685557586835, 'mean_word_length': 1.7611036339165544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2052709', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2132, 'n_words': 557}