-
Notifications
You must be signed in to change notification settings - Fork 17
2.Log模块
dingxiaowei edited this page Jan 29, 2019
·
1 revision
为了让项目功能模块化,支持可复用性和可扩展性的原则,将跟游戏关联不大的功能模块单独拎出来添加到一个独立库工程。Log是游戏框架中比较重要和基础的一个功能模块。并且Log是调试的重要手段之一,游戏Debug版本在UI上显示Log也会显得尤为重要。
目前Log模块支持的功能:
- 支持UI可视化
- 支持是否显示日志
- 支持是否显示日志时间
- 支持显示不同级别(一般/警告/错误)的日志
- 是否将Log显示到本地持久化存储(持久化目录见 Debugger.LogFileDir)
- 支持是否打印堆栈日志
- 支持多参数的Log显示
- 可以在代码中控制Log显示(见前面三行),也可以在库工程变量控制。
- 可以显示三个等级的日志
demo\app\fcg_client\fcg_client\Projects
- UGUI 是UGUI的扩展工程库
- GFEditor 是游戏通用编辑工具扩展库
- GF 是游戏通用模块扩展库
- References 是工程依赖库以及导出库
- 打开对应工程编辑代码
- 编译工程
- 打开Unity菜单栏 Tools/CopyDll/CopyXXX
修改对应的工程之后运行对应的拷贝功能 弹出这个弹框说明拷贝成功,然后关闭弹框,等待Unity菊花转完,更新成功!
- 菜单控制功能是否开启
- 可以显示Log的文件名和行号
using System;
using System.IO;
using UnityEngine;
namespace GF.Debug
{
/// <summary>
/// 系统日志模块
/// </summary>
public class Debugger
{
/// <summary>
/// 是否启动日志
/// </summary>
public static bool EnableLog = true;
/// <summary>
/// 是否显示Log时间
/// </summary>
public static bool EnableTime = true;
/// <summary>
/// 是否将日志保存到本地
/// </summary>
public static bool EnableSave = false;
/// <summary>
/// 是否允许堆栈
/// </summary>
public static bool EnableStack = false;
public static bool UseUnityEngine = true;
public static string LogFileDir = Application.persistentDataPath + "/DebuggerLog/";
public static string LogFileName = "";
/// <summary>
/// 开头标记,用于对Unity默认log做区分
/// </summary>
public static string Prefix = "<color=#FFFF00>></color> ";
public static StreamWriter LogFileWriter = null;
private static string GetLogText(string tag, string message)
{
string str = "";
if (EnableTime)
{
str = DateTime.Now.ToString("HH:mm:ss.fff") + " ";
}
return (str + tag + "::" + message);
}
private static string GetLogTime()
{
string str = "";
if (EnableTime)
{
str = DateTime.Now.ToString("HH:mm:ss.fff") + " ";
}
return str;
}
public static void Log(object message)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.Log(Prefix + str, null);
LogToFile("[I]" + str, false);
}
public static void Log(object message, UnityEngine.Object context)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.Log(Prefix + str, context);
LogToFile("[I]" + str, false);
}
public static void Log(string tag, string message)
{
if (!Debugger.EnableLog)
return;
message = GetLogText(tag, message);
UnityEngine.Debug.Log(Prefix + message, null);
LogToFile("[I]" + message, false);
}
public static void Log(string tag, string format, params object[] args)
{
if (!Debugger.EnableLog)
return;
string logText = GetLogText(tag, string.Format(format, args));
UnityEngine.Debug.Log(Prefix + logText, null);
LogToFile("[I]" + logText, false);
}
public static void LogError(object message)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.LogError(Prefix + str, null);
LogToFile("[E]" + str, true);
}
public static void LogError(object message, UnityEngine.Object context)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.LogError(Prefix + str, context);
LogToFile("[E]" + str, true);
}
public static void LogError(string tag, string message)
{
if (!Debugger.EnableLog)
return;
message = GetLogText(tag, message);
UnityEngine.Debug.LogError(Prefix + message, null);
LogToFile("[E]" + message, true);
}
public static void LogError(string tag, string format, params object[] args)
{
if (!Debugger.EnableLog)
return;
string logText = GetLogText(tag, string.Format(format, args));
UnityEngine.Debug.LogError(Prefix + logText, null);
LogToFile("[E]" + logText, true);
}
/// <summary>
/// 将日志写入到文件中
/// </summary>
/// <param name="message"></param>
/// <param name="EnableStack"></param>
private static void LogToFile(string message, bool EnableStack = false)
{
if (!Debugger.EnableSave)
return;
if (LogFileWriter == null)
{
LogFileName = DateTime.Now.GetDateTimeFormats('s')[0].ToString();
LogFileName = LogFileName.Replace("-", "_");
LogFileName = LogFileName.Replace(":", "_");
LogFileName = LogFileName.Replace(" ", "");
LogFileName = LogFileName + ".log";
if (string.IsNullOrEmpty(LogFileDir))
{
try
{
if (UseUnityEngine)
{
LogFileDir = Application.persistentDataPath + "/DebuggerLog/";
}
else
{
LogFileDir = AppDomain.CurrentDomain.BaseDirectory + "/DebuggerLog/";
}
}
catch (Exception exception)
{
UnityEngine.Debug.Log(Prefix + "获取 Application.persistentDataPath 报错!" + exception.Message, null);
return;
}
}
string path = LogFileDir + LogFileName;
try
{
if (!Directory.Exists(LogFileDir))
{
Directory.CreateDirectory(LogFileDir);
}
LogFileWriter = File.AppendText(path);
LogFileWriter.AutoFlush = true;
}
catch (Exception exception2)
{
LogFileWriter = null;
UnityEngine.Debug.Log("LogToCache() " + exception2.Message + exception2.StackTrace, null);
return;
}
}
if (LogFileWriter != null)
{
try
{
LogFileWriter.WriteLine(message);
if ((EnableStack || Debugger.EnableStack) && UseUnityEngine)
{
LogFileWriter.WriteLine(StackTraceUtility.ExtractStackTrace());
}
}
catch (Exception)
{
}
}
}
public static void LogWarning(object message)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.LogWarning(Prefix + str, null);
LogToFile("[W]" + str, false);
}
public static void LogWarning(object message, UnityEngine.Object context)
{
if (!Debugger.EnableLog)
return;
string str = GetLogTime() + message;
UnityEngine.Debug.LogWarning(Prefix + str, context);
LogToFile("[W]" + str, false);
}
public static void LogWarning(string tag, string message)
{
if (!Debugger.EnableLog)
return;
message = GetLogText(tag, message);
UnityEngine.Debug.LogWarning(Prefix + message, null);
LogToFile("[W]" + message, false);
}
public static void LogWarning(string tag, string format, params object[] args)
{
if (!Debugger.EnableLog)
return;
string logText = GetLogText(tag, string.Format(format, args));
UnityEngine.Debug.LogWarning(Prefix + logText, null);
LogToFile("[W]" + logText, false);
}
}
}
Modify By Aladdin 2018.1.19 http://dingxiaowei.cn