百度整站优化,百度推广手机登录,WordPress无图片主题,网站运营推广该如何做1 对于项目中一些比较长的日志#xff0c;比如前后端交互协议具体数据等#xff0c;这些日志内容可能会比较长#xff0c;在unity控制面板上查看不是十分方便#xff0c;我们可以对双击事件进行扩展#xff0c;将日志保存到一个文本中#xff0c;然后用系统默认的文本查看…1 对于项目中一些比较长的日志比如前后端交互协议具体数据等这些日志内容可能会比较长在unity控制面板上查看不是十分方便我们可以对双击事件进行扩展将日志保存到一个文本中然后用系统默认的文本查看工具查看这个日志 2 项目中如果用到了lua当我们在控制台输出lua文件的时候能不能双击日志用我们的代码编辑器软件打开日志输出位置
console日志双击然后用记事本打开显示
我们先用 OnOpenAsset 特性重写双击回调方法这个方法返回false会继续执行系统默认日志双击打开方法返回true则会中断。 我们想要的效果是用记事本打开这个特殊日志但是不需要跳转到对应的代码输出位置 首先我们先在lua里面把日志输出方法重写一下在日志输出的时候添加一个标签 “”,当我们检测到双击的日志信息包含这个标签则用记事本打开这一段日志我们也可以把一些特殊的日志先在代码中缓存在这个时候调用缓存读取那些特殊的日志进行输出显示
function printLongLog(fmt, ...)local logText string.format(fmt, ...)local debugInfo debug.getinfo(2)local str string.format(%s\nopen in file, logText, debugInfo.short_src, debugInfo.currentline)local logMessage concatStrs(str, debug.traceback(, 2))UnityEngine.Debug.Log(logMessage)
end 然后再C#代码中监听这一段日志 [OnOpenAsset(0)]public static bool OnAsset(int instanceID, int line){string assetPath AssetDatabase.GetAssetPath(instanceID);string fileExtension Path.GetExtension(assetPath);string stackTrace GetStackTrace();if (string.IsNullOrEmpty(stackTrace)){return false;}if (stackTrace.Contains(open in file)) //对于一些比较长的日志添加标签open in file双击的时候再文本中打开{openLog(stackTrace);return true;}return false;}获取console里面的日志
private static string GetStackTrace(){var consoleWindowType typeof(EditorWindow).Assembly.GetType(UnityEditor.ConsoleWindow);var fieldInfo consoleWindowType.GetField(ms_ConsoleWindow, BindingFlags.Static | BindingFlags.NonPublic);var consoleWindowInstance fieldInfo.GetValue(null);if (null ! consoleWindowInstance (object)EditorWindow.focusedWindow consoleWindowInstance){fieldInfo consoleWindowType.GetField(m_ActiveText, BindingFlags.Instance | BindingFlags.NonPublic);string activeText fieldInfo.GetValue(consoleWindowInstance).ToString();return activeText;}return ;}用记事本打开日志文件我们先把日志信息保存在本地然后调用系统方法打开这个文件 这里用到了一个方法 System.Diagnostics.Process.Start(fileName),参数是文件的完整路径调用系统默认方式打开改文件
private static void openLog(string stackTrace){string fileName GetFileSaveName();string saveDirPath Application.dataPath /../Debug;if (!Directory.Exists(saveDirPath)){Directory.CreateDirectory(saveDirPath);}string savePath saveDirPath / fileName;File.WriteAllText(savePath, stackTrace);System.Diagnostics.Process.Start(savePath);}private static string GetFileSaveName(){DateTime currentTime DateTime.Now;string customFormat currentTime.ToString(yyyy_MM_dd_HH_mm_ss);string fileName $debug_{customFormat}.txt;return fileName;}双击打开lua日志然后用lua编辑器定位到日志输出位置
对于lua输出的日志我们为了便于打开可以用类似上面的方法重新封装一个lua日志输出方法然后通关特殊标签的进行确定lua文件的名称和对应的行号。也可以进行拆分lua堆栈数据取到lua代码名称和对应堆栈。 添加标签方式的lua日志输出
function print(fmt, ...)local logText string.format(fmt, ...)local debugInfo debug.getinfo(2)local str string.format(%s\nfilePath%s/filePathline%s/line, logText, debugInfo.short_src, debugInfo.currentline)local logMessage concatStrs(str, debug.traceback(, 2))UnityEngine.Debug.Log(logMessage)
end获取文件名称和行号
static bool OpenLua(string logText){//获取lua文件路径Regex regex new Regex(filePath.*\/filePath);Match match regex.Match(logText);if (!match.Success){return false;}string filePath match.Groups[0].Value.Trim();int length filePath.Length - 10 - 11; //去掉开头和结尾的字符串 filePath /filePathfilePath filePath.Substring(10, length);filePath filePath.Replace(., /);if (!filePath.EndsWith(.lua)){filePath filePath .lua;}//获取日志行号Regex lineRegex new Regex(line.*\/line);match lineRegex.Match(logText);if (!match.Success){return false;}string luaLineString match.Groups[0].Value;luaLineString.Trim();length luaLineString.Length - 6 - 7;luaLineString luaLineString.Substring(6, length);int luaLine int.Parse(luaLineString.Trim());return OpenFileAtLineExternal(filePath, luaLine);}对于系统默认输出日志我们通关拆分字符串方式获取 static bool OpenLuaDefault(string logText){int index logText.IndexOf(stack traceback:);string temp logText.Substring(index, logText.Length - index);string[] arr temp.Split(:);if (arr.Length 3){return false;}string filePath arr[1].Trim();int luaLine int.Parse(arr[2]);filePath filePath .lua;return OpenFileAtLineExternal(filePath, luaLine);}有了文件名和行号我们就可以通关调用VScode或者Idea编辑器软件打开对应的文件并定位到指定的行
static void OpenFileWith(string fileName, int line){string editorPath EditorUserSettings.GetConfigValue(EXTERNAL_EDITOR_PATH_KEY);System.Diagnostics.Process proc new System.Diagnostics.Process();proc.StartInfo.FileName editorPath;string projectRootPath EditorUserSettings.GetConfigValue(LUA_PROJECT_ROOT_FOLDER_PATH_KEY);if (string.IsNullOrEmpty(projectRootPath)){SetLuaProjectRoot();return;}string procArgument ;if (editorPath.IndexOf(idea) ! -1) //idea{procArgument string.Format({0} --line {1} {2}, projectRootPath, line, fileName);}else if (editorPath.IndexOf(Code.exe) ! -1) // VSCode{string filePath Path.Combine(projectRootPath, fileName);procArgument string.Format(-g {0}:{1}:0, filePath, line);}else{procArgument string.Format({0}:{1}:0, fileName, line);}proc.StartInfo.Arguments procArgument;proc.Start(); }完整代码
lua 日志方法
function print(fmt, ...)local logText string.format(fmt, ...)local debugInfo debug.getinfo(2)local str string.format(%s\nfilePath%s/filePathline%s/line, logText, debugInfo.short_src, debugInfo.currentline)local logMessage concatStrs(str, debug.traceback(, 2))UnityEngine.Debug.Log(logMessage)
endfunction printLongLog(fmt, ...)local logText string.format(fmt, ...)local debugInfo debug.getinfo(2)local str string.format(%s\nopen in file, logText, debugInfo.short_src, debugInfo.currentline)local logMessage concatStrs(str, debug.traceback(, 2))UnityEngine.Debug.Log(logMessage)
endC#方法
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;public class ConsoleTools : MonoBehaviour
{public const string EXTERNAL_EDITOR_PATH_KEY mTv8;public const string LUA_PROJECT_ROOT_FOLDER_PATH_KEY obUd;[OnOpenAsset(0)]public static bool OnAsset(int instanceID, int line){string assetPath AssetDatabase.GetAssetPath(instanceID);string fileExtension Path.GetExtension(assetPath);// string fileName Path.GetFileNameWithoutExtension(assetPath);if(fileExtension ! .cs fileExtension ! .lua){return false; }string stackTrace GetStackTrace();if (string.IsNullOrEmpty(stackTrace)){return false;}if (stackTrace.Contains(open in file)) //对于一些比较长的日志添加标签open in file双击的时候再文本中打开{openLog(stackTrace);return true;}if (stackTrace.Contains(stack traceback:)) //lua输出堆栈信息会自带 “stack traceback:” 这描述这里用来筛选lua日志{bool isOpenSuccess false;if (stackTrace.Contains(filePath)){isOpenSuccess OpenLua(stackTrace);}else{isOpenSuccess OpenLuaDefault(stackTrace);}return isOpenSuccess;}return false;}/// summary/// 通关日志堆栈获取日志信息/// /summary/// returns/returnsprivate static string GetStackTrace(){var consoleWindowType typeof(EditorWindow).Assembly.GetType(UnityEditor.ConsoleWindow);var fieldInfo consoleWindowType.GetField(ms_ConsoleWindow, BindingFlags.Static | BindingFlags.NonPublic);var consoleWindowInstance fieldInfo.GetValue(null);if (null ! consoleWindowInstance (object)EditorWindow.focusedWindow consoleWindowInstance){fieldInfo consoleWindowType.GetField(m_ActiveText, BindingFlags.Instance | BindingFlags.NonPublic);string activeText fieldInfo.GetValue(consoleWindowInstance).ToString();return activeText;}return ;}/// summary/// 以文件个格式打开日志/// /summary/// param namestackTrace/paramprivate static void openLog(string stackTrace){string fileName GetFileSaveName();string saveDirPath Application.dataPath /../Debug;if (!Directory.Exists(saveDirPath)){Directory.CreateDirectory(saveDirPath);}string savePath saveDirPath / fileName;File.WriteAllText(savePath, stackTrace);System.Diagnostics.Process.Start(savePath);}private static string GetFileSaveName(){DateTime currentTime DateTime.Now;string customFormat currentTime.ToString(yyyy_MM_dd_HH_mm_ss);string fileName $debug_{customFormat}.txt;return fileName;}static void SetExternalEditorPath(){string path EditorUserSettings.GetConfigValue(EXTERNAL_EDITOR_PATH_KEY);path EditorUtility.OpenFilePanel(设置lua文件默认打开的编辑器软件路径选择exe文件,path,exe);if (path ! ){EditorUserSettings.SetConfigValue(EXTERNAL_EDITOR_PATH_KEY, path);Debug.Log(Set Editor Path: path);}} static void SetLuaProjectRoot(){string path EditorUserSettings.GetConfigValue(LUA_PROJECT_ROOT_FOLDER_PATH_KEY);path EditorUtility.OpenFolderPanel(设置lua项目根目录位置,path,);if (path ! ){EditorUserSettings.SetConfigValue(LUA_PROJECT_ROOT_FOLDER_PATH_KEY, path);Debug.Log(Set Editor Path: path);}}static bool OpenLua(string logText){//获取lua文件路径Regex regex new Regex(filePath.*\/filePath);Match match regex.Match(logText);if (!match.Success){return false;}string filePath match.Groups[0].Value.Trim();int length filePath.Length - 10 - 11; //去掉开头和结尾的字符串 filePath /filePathfilePath filePath.Substring(10, length);filePath filePath.Replace(., /);if (!filePath.EndsWith(.lua)){filePath filePath .lua;}//获取日志行号Regex lineRegex new Regex(line.*\/line);match lineRegex.Match(logText);if (!match.Success){return false;}string luaLineString match.Groups[0].Value;luaLineString.Trim();length luaLineString.Length - 6 - 7;luaLineString luaLineString.Substring(6, length);int luaLine int.Parse(luaLineString.Trim());return OpenFileAtLineExternal(filePath, luaLine);}/// summary/// 打开没有标签的日志对应的脚本/// /summary/// param namelogText/param/// returns/returnsstatic bool OpenLuaDefault(string logText){int index logText.IndexOf(stack traceback:);string temp logText.Substring(index, logText.Length - index);string[] arr temp.Split(:);if (arr.Length 3){return false;}string filePath arr[1].Trim();int luaLine int.Parse(arr[2]);filePath filePath .lua;return OpenFileAtLineExternal(filePath, luaLine);}/// summary/// 打开指定的文件/// /summary/// param namefileName文件名/param/// param nameline行号/param/// returns/returnsstatic bool OpenFileAtLineExternal(string fileName, int line){string editorPath EditorUserSettings.GetConfigValue(EXTERNAL_EDITOR_PATH_KEY);if (string.IsNullOrEmpty(editorPath) || !File.Exists(editorPath)){ // 没有path就弹出面板设置SetExternalEditorPath();}OpenFileWith(fileName, line);return true;}static void OpenFileWith(string fileName, int line){string editorPath EditorUserSettings.GetConfigValue(EXTERNAL_EDITOR_PATH_KEY);System.Diagnostics.Process proc new System.Diagnostics.Process();proc.StartInfo.FileName editorPath;string projectRootPath EditorUserSettings.GetConfigValue(LUA_PROJECT_ROOT_FOLDER_PATH_KEY);if (string.IsNullOrEmpty(projectRootPath)){SetLuaProjectRoot();return;}string procArgument ;if (editorPath.IndexOf(idea) ! -1) //idea{procArgument string.Format({0} --line {1} {2}, projectRootPath, line, fileName);}else if (editorPath.IndexOf(Code.exe) ! -1) // VSCode{string filePath Path.Combine(projectRootPath, fileName);procArgument string.Format(-g {0}:{1}:0, filePath, line);}else{procArgument string.Format({0}:{1}:0, fileName, line);}proc.StartInfo.Arguments procArgument;proc.Start(); }} 文章转载自: http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.dqkcn.cn.gov.cn.dqkcn.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.hlshn.cn.gov.cn.hlshn.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.mlnzx.cn.gov.cn.mlnzx.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.kxqfz.cn.gov.cn.kxqfz.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.wyjpt.cn.gov.cn.wyjpt.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.lyldhg.cn.gov.cn.lyldhg.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn