public static void SafeAdd(ref List <LogStruct> list, LogStruct log) { int count = list.Count; list.Add(log); //if (count >= LogDefault.MAX_LEN4_SCREEN) //{ // int diff = count - LogDefault.MAX_LEN4_SCREEN; // list.RemoveRange(0, diff); //} }
public static void AddLog(LogStruct log) { SafeAdd(ref LogList, log); if (log.Type == LogType.Log) { SafeAdd(ref InfoList, log); } else if (log.Type == LogType.Error) { SafeAdd(ref ErrorList, log); } }
public static string GetStringLog(LogStruct log) { LogUtils.SB.Clear(); LogUtils.SB.Append("<color=#"); LogUtils.SB.Append(ColorUtility.ToHtmlStringRGB(LogUtils.GetColor(log.LogColor))); LogUtils.SB.Append(">"); LogUtils.SB.Append("["); LogUtils.SB.Append(log.Tag); LogUtils.SB.Append("]"); LogUtils.SB.Append("["); LogUtils.SB.Append(log.Time); LogUtils.SB.Append("]"); LogUtils.SB.Append(":"); LogUtils.SB.Append(log.Msg); LogUtils.SB.Append("</color>"); return(LogUtils.SB.ToString()); }
public static List <LogStruct> GetFilterLog() { tempList4Filter.Clear(); tempList4Filter2.Clear(); bool hasTag = true; if (LogConfig.Tag.Equals(LogDefault.TAG_NAME_ALL)) { hasTag = false; } //log level,log tag过滤 switch (LogConfig.Level) { case LogDefault.LEVEL_ALL: if (hasTag) { if (TagLogDic[LogConfig.Tag].Count > 0) { tempList4Filter.AddRange(TagLogDic[LogConfig.Tag]); } } else { if (LogList.Count > 0) { tempList4Filter.AddRange(LogList); } } break; case LogDefault.LEVEL_INFO: if (hasTag) { if (TagInfoLogDic[LogConfig.Tag].Count > 0) { tempList4Filter.AddRange(TagInfoLogDic[LogConfig.Tag]); } } else { if (InfoList.Count > 0) { tempList4Filter.AddRange(InfoList); } } break; case LogDefault.LEVEL_ERROR: if (hasTag) { if (TagErrorLogDic[LogConfig.Tag].Count > 0) { tempList4Filter.AddRange(TagErrorLogDic[LogConfig.Tag]); } } else { //Debug.Log("count------:"+ ErrorList.Count); if (ErrorList.Count > 0) { tempList4Filter.AddRange(ErrorList); } } break; case LogDefault.LEVEL_NOTHING: tempList4Filter.Clear(); break; } if (!LogConfig.FilterRegex.Equals(LogDefault.REGEX_DEFAULT)) { //log 正则过滤 Regex regex = new Regex(LogConfig.FilterRegex); for (int i = 0; i < tempList4Filter.Count; i++) { LogStruct logStruct = tempList4Filter[i]; if (regex.IsMatch(tempList4Filter[i].Msg)) { //Debug.Log("删除:" + tempList4Filter[i].Msg); tempList4Filter2.Add(logStruct); } } tempList4Filter2.Reverse(); return(tempList4Filter2); } else { //无正则过滤 tempList4Filter.Reverse(); return(tempList4Filter); } }
public static void SafeAdd(ref Dictionary <string, List <LogStruct> > dic, string tag, LogStruct log) { if (!dic.ContainsKey(tag)) { dic.Add(tag, new List <LogStruct>()); } if (null == dic[tag]) { dic[tag] = new List <LogStruct>(); } var list = dic[tag]; list.Add(log); }
public static void I(string msg, string tag = LogDefault.TAG_NAME_DEFALUT, LogColorEnum color = LogColorEnum.green) { if (!LogConfig.BeShowLog) { return; } LogStruct logStruct = new LogStruct(); logStruct.Msg = msg; logStruct.Tag = tag; logStruct.Time = LogUtils.GetCurTime(); logStruct.LogColor = color; logStruct.Type = LogType.Log; LogUtils.SB.Clear(); var stack = new System.Diagnostics.StackTrace(true); var frames = stack.GetFrames(); int length = frames.Length; for (int i = 0; i < length; i++) { string fileName = frames[i].GetFileName(); fileName = System.IO.Path.GetFileName(fileName); var method = frames[i].GetMethod(); Type classType = method.ReflectedType; string _nameSpace = classType.Namespace; string className = classType.Name; string methodName = method.Name; int lineNum = frames[i].GetFileLineNumber(); LogUtils.SB.Append(_nameSpace); LogUtils.SB.Append("."); LogUtils.SB.Append(className); LogUtils.SB.Append(":"); LogUtils.SB.Append(methodName); LogUtils.SB.Append("()"); LogUtils.SB.Append(" ( at "); LogUtils.SB.Append(fileName); LogUtils.SB.Append(":"); LogUtils.SB.Append(lineNum); LogUtils.SB.Append("\n"); } logStruct.StackTrace = LogUtils.SB.ToString(); LogDataMgr.AddLog(logStruct); //NINFO 之所以screen console分开处理,因为MsgRec不能接收到tag,color等信息 //这也是难点,所以要么输出到screen,要么到控制台,两者同时存在也没意义 if (LogConfig.BeWrite2Screen) { Ins.RefreshCurLogList(); if (LogConfig.BeWirte2Remote) { Buffer.Clear(); Buffer.PutString(GetStringLog(logStruct)); //Buffer.PutString("我"); byte[] bs = Buffer.ToArray(); //for (int i = 0; i < bs.Length; i++) //{ // Debug.Log("客户端发送i:"+i+"--->"+bs[i]); //} LogHttpClient.GetIns().Post((short)125, bs, null); } if (LogConfig.BeWrite2File) { byte[] bs = Buffer.ToArray(); FileHelper.WriteBytes2File_Append(LogConfig.LogFileSavePath, bs); } } else { Debug.Log(GetStringLog(logStruct)); } }
/// <summary> /// 处理unity log /// 注意,这里只用于向screen输出unity本身的log信息(就是Debug.Log) /// 不想screen输出时这里就不要使用 /// </summary> /// <param name="msg"></param> /// <param name="stack"></param> /// <param name="type"></param> private void MsgRec(string msg, string stack, UnityEngine.LogType type) { if (!LogConfig.BeShowLog) { return; } LogStruct logStruct = new LogStruct(); logStruct.Msg = msg; logStruct.Tag = LogDefault.TAG_NAME_UNITY; logStruct.Time = LogUtils.GetCurTime(); logStruct.Type = type; LogUtils.SB.Clear(); logStruct.StackTrace = stack; switch (type) { case LogType.Error: case LogType.Assert: case LogType.Exception: logStruct.LogColor = LogColorEnum.red; break; case LogType.Warning: logStruct.LogColor = LogColorEnum.yellow; break; case LogType.Log: logStruct.LogColor = LogColorEnum.green; break; } if (LogConfig.BeWirte2Remote) { Buffer.Clear(); Buffer.PutString(GetStringLog(logStruct)); //Buffer.PutString("我"); byte[] bs = Buffer.ToArray(); //for (int i = 0; i < bs.Length; i++) //{ // Debug.Log("客户端发送i:"+i+"--->"+bs[i]); //} LogHttpClient.GetIns().Post((short)125, bs, null); } if (LogConfig.BeWrite2File) { byte[] bs = Buffer.ToArray(); FileHelper.WriteBytes2File_Append(LogConfig.LogFileSavePath, bs); } LogDataMgr.AddLog(logStruct); RefreshCurLogList(); }
public static void E(string msg, string tag = LogDefault.TAG_NAME_DEFALUT, LogColorEnum color = LogColorEnum.red) { if (!LogConfig.BeShowLog) { return; } LogStruct logStruct = new LogStruct(); logStruct.Msg = msg; logStruct.Tag = tag; logStruct.Time = LogUtils.GetCurTime(); logStruct.LogColor = color; logStruct.Type = LogType.Error; LogUtils.SB.Clear(); var stack = new System.Diagnostics.StackTrace(true); var frames = stack.GetFrames(); int length = frames.Length; for (int i = 0; i < length; i++) { string fileName = frames[i].GetFileName(); fileName = System.IO.Path.GetFileName(fileName); var method = frames[i].GetMethod(); Type classType = method.ReflectedType; string _nameSpace = classType.Namespace; string className = classType.Name; string methodName = method.Name; int lineNum = frames[i].GetFileLineNumber(); LogUtils.SB.Append(_nameSpace); LogUtils.SB.Append("."); LogUtils.SB.Append(className); LogUtils.SB.Append(":"); LogUtils.SB.Append(methodName); LogUtils.SB.Append("()"); LogUtils.SB.Append(" ( at "); LogUtils.SB.Append(fileName); LogUtils.SB.Append(":"); LogUtils.SB.Append(lineNum); LogUtils.SB.Append("\n"); } logStruct.StackTrace = LogUtils.SB.ToString(); LogDataMgr.AddLog(logStruct); if (LogConfig.BeWrite2Screen) { Ins.RefreshCurLogList(); if (LogConfig.BeWirte2Remote) { Buffer.Clear(); Buffer.PutString(GetStringLog(logStruct)); byte[] bs = Buffer.ToArray(); LogHttpClient.GetIns().Post((short)125, bs, null); } if (LogConfig.BeWrite2File) { byte[] bs = Buffer.ToArray(); FileHelper.WriteBytes2File_Append(LogConfig.LogFileSavePath, bs); } } else { //MsgRec 会做log文件保存,和向远程传送log,所以使用cosonle输出不用加这两项 Debug.Log(GetStringLog(logStruct)); } }