鸟语天空
写日志文件工具类
post by:追风剑情 2015-5-9 13:51

在游戏的数据目录下生成日志文件,方便技术跟踪问题。


using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;

public class LogFile
{
    // 日志级别枚举
    public enum LogLevel
    {
        verbose,
        debug,
        info,
        warn,
        error,
        assert
    }

    // 当前日志级别
    public static LogLevel logLevel = LogLevel.verbose;

    private static string LOG_FILE_PATH = Application.dataPath + "\\game.log";

    public static void Verbose(params string[] args)
    {
        Write(args);
    }

    public static void Debug(params string[] args)
    {
        if ((int)logLevel <= 1)
            Write(args);
    }

    public static void Info(params string[] args)
    {
        if ((int)logLevel <= 2)
            Write(args);
    }

    public static void Warn(params string[] args)
    {
        if ((int)logLevel <= 3)
            Write(args);
    }

    public static void Error(params string[] args)
    {
        if ((int)logLevel <= 4)
            Write(args);
    }

    public static void Assert(params string[] args)
    {
        if ((int)logLevel <= 5)
            Write(args);
    }

    private static void Write(params string[] args)
    {
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
        if (args.Length > 0)
        {
            for (int i = 0; i < args.Length; i++)
            {
                File.AppendAllText(LOG_FILE_PATH, args[i], Encoding.UTF8);
                File.AppendAllText(LOG_FILE_PATH, "\r\n", Encoding.UTF8);
            }
        }
#endif
    }
}



评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容