Created
July 12, 2018 10:48
-
-
Save lonewolfwilliams/3958476de110255534b1c514d9c7b87f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Reflection; | |
| using UnityEngine; | |
| /** | |
| * It works like this: | |
| * Add a [LogGroup("groupname")] to every class you want to group | |
| * | |
| * Use Logwithgroup like this: | |
| * | |
| * Application.logMessageReceived += LogWithGroup.GetLogMessageReceivedHandler(GroupLogMessageReceived); | |
| * private void GroupLogMessageReceived(string condition, string stackTrace, LogType type, string className, string group) | |
| * { | |
| * //do whatever you want for your custom logger here... | |
| * //(in unity-land condition == message) the rest should be self-explanatory :) | |
| * } | |
| */ | |
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | |
| public class LogGroup : Attribute | |
| { | |
| private string groupName; | |
| public string GetGroupName() | |
| { | |
| return groupName; | |
| } | |
| public LogGroup(string groupName) | |
| { | |
| this.groupName = groupName; | |
| } | |
| } | |
| public delegate void GroupLogMessageReceived(string condition, string stackTrace, LogType type, string className, string group); | |
| public class LogWithGroup | |
| { | |
| //adds class and group information to an existing logMessageHandler | |
| public static Application.LogCallback GetLogMessageReceivedHandler(GroupLogMessageReceived groupLogMessageHandler) | |
| { | |
| return (string condition, string stackTrace, LogType type) => | |
| { | |
| //get class name | |
| var st = new StackTrace(); | |
| var sf = st.GetFrames(); | |
| var f = getFirstStackFrameWithGroupAttribute(from:sf); | |
| var _class = f.GetMethod().DeclaringType; | |
| var group = GetGroupNameFromAttributeOn(_class); | |
| groupLogMessageHandler(condition, stackTrace, type, _class.Name, group); | |
| }; | |
| } | |
| public static Assembly FindScriptAssembly() | |
| { | |
| Assembly found = null; | |
| var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
| for (int i=0; i<assemblies.Length; i++) | |
| { | |
| if(assemblies[i].GetName().Name == @"Assembly-CSharp") | |
| { | |
| found = assemblies[i]; | |
| break; | |
| } | |
| } | |
| return found; | |
| } | |
| public static String[] ExtractFilterNames(Assembly from) | |
| { | |
| var filters = new List<String>(); | |
| var classes = from.GetTypes(); | |
| for(int i=0; i<classes.Length; i++) | |
| { | |
| var grp = GetGroupNameFromAttributeOn(classes[i]); | |
| if(String.IsNullOrEmpty(grp)) continue; | |
| filters.Add(grp); | |
| } | |
| return filters.ToArray(); | |
| } | |
| public static String GetGroupNameFromAttributeOn(Type _class) | |
| { | |
| //get group | |
| var attributes = _class.GetCustomAttributes(false); | |
| var en = attributes.GetEnumerator(); | |
| var group = string.Empty; | |
| while(en.MoveNext()) | |
| { | |
| if(en.Current is LogGroup) | |
| { | |
| group = (en.Current as LogGroup).GetGroupName(); | |
| return group; | |
| } | |
| } | |
| return String.Empty; | |
| } | |
| public static StackFrame getFirstStackFrameWithGroupAttribute(StackFrame[] from) | |
| { | |
| var f = from[from.Length-1]; | |
| for(int i=0; i<from.Length; i++) | |
| { | |
| String groupName = GetGroupNameFromAttributeOn(from[i].GetMethod().DeclaringType); | |
| if(groupName == String.Empty) continue; | |
| f = from[i]; | |
| break; | |
| } | |
| return f; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment