Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.0 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. public class VRTK_Logger : MonoBehaviour
  12. {
  13. public enum LogLevels
  14. {
  15. Trace,
  16. Debug,
  17. Info,
  18. Warn,
  19. Error,
  20. Fatal,
  21. None
  22. }
  23. public enum CommonMessageKeys
  24. {
  25. NOT_DEFINED,
  26. REQUIRED_COMPONENT_MISSING_FROM_SCENE,
  27. REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT,
  28. REQUIRED_COMPONENT_MISSING_FROM_PARAMETER,
  29. REQUIRED_COMPONENT_MISSING_NOT_INJECTED,
  30. COULD_NOT_FIND_OBJECT_FOR_ACTION,
  31. SDK_OBJECT_NOT_FOUND,
  32. SDK_NOT_FOUND,
  33. SDK_MANAGER_ERRORS,
  34. SCRIPTING_DEFINE_SYMBOLS_ADDED,
  35. SCRIPTING_DEFINE_SYMBOLS_REMOVED,
  36. SCRIPTING_DEFINE_SYMBOLS_NOT_FOUND
  37. }
  38. public static VRTK_Logger instance = null;
  39. public static Dictionary<CommonMessageKeys, string> commonMessages = new Dictionary<CommonMessageKeys, string>()
  40. {
  41. { CommonMessageKeys.NOT_DEFINED, "`{0}` not defined{1}." },
  42. { CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_SCENE, "`{0}` requires the `{1}` component to be available in the scene{2}." },
  43. { CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "`{0}` requires the `{1}` component to be attached to {2} GameObject{3}." },
  44. { CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_PARAMETER, "`{0}` requires a `{1}` component to be specified as the `{2}` parameter{3}." },
  45. { CommonMessageKeys.REQUIRED_COMPONENT_MISSING_NOT_INJECTED, "`{0}` requires the `{1}` component. Either the `{2}` parameter is not set or no `{1}` component is attached to {3} GameObject{4}." },
  46. { CommonMessageKeys.COULD_NOT_FIND_OBJECT_FOR_ACTION, "The `{0}` could not automatically find {1} to {2}." },
  47. { CommonMessageKeys.SDK_OBJECT_NOT_FOUND, "No {0} could be found. Have you selected a valid {1} in the SDK Manager? If you are unsure, then click the GameObject with the `VRTK_SDKManager` script attached to it in Edit Mode and select a {1} from the dropdown." },
  48. { CommonMessageKeys.SDK_NOT_FOUND, "The SDK '{0}' doesn't exist anymore. The fallback SDK '{1}' will be used instead." },
  49. { CommonMessageKeys.SDK_MANAGER_ERRORS, "The current SDK Manager setup is causing the following errors:\n\n{0}" },
  50. { CommonMessageKeys.SCRIPTING_DEFINE_SYMBOLS_ADDED, "Scripting Define Symbols added to [Project Settings->Player] for {0}: {1}" },
  51. { CommonMessageKeys.SCRIPTING_DEFINE_SYMBOLS_REMOVED, "Scripting Define Symbols removed from [Project Settings->Player] for {0}: {1}" }
  52. };
  53. public static Dictionary<CommonMessageKeys, int> commonMessageParts = new Dictionary<CommonMessageKeys, int>();
  54. public LogLevels minLevel = LogLevels.Trace;
  55. public bool throwExceptions = true;
  56. #if UNITY_EDITOR
  57. [InitializeOnLoadMethod]
  58. #endif
  59. public static void CreateIfNotExists()
  60. {
  61. if (instance == null)
  62. {
  63. GameObject loggerObject = new GameObject(VRTK_SharedMethods.GenerateVRTKObjectName(true, "Logger"))
  64. {
  65. hideFlags = HideFlags.DontSaveInEditor | HideFlags.HideInHierarchy
  66. };
  67. instance = loggerObject.AddComponent<VRTK_Logger>();
  68. }
  69. if (commonMessageParts.Count != commonMessages.Count)
  70. {
  71. commonMessageParts.Clear();
  72. foreach (KeyValuePair<CommonMessageKeys, string> commonMessage in commonMessages)
  73. {
  74. int bitCount = Regex.Matches(commonMessage.Value, @"(?<!\{)\{([0-9]+).*?\}(?!})")
  75. .Cast<Match>().DefaultIfEmpty()
  76. .Max(m => m == null ? -1 : int.Parse(m.Groups[1].Value)) + 1;
  77. commonMessageParts.Add(commonMessage.Key, bitCount);
  78. }
  79. }
  80. }
  81. public static string GetCommonMessage(CommonMessageKeys messageKey, params object[] parameters)
  82. {
  83. CreateIfNotExists();
  84. string returnMessage = "";
  85. string outputMessage = VRTK_SharedMethods.GetDictionaryValue(commonMessages, messageKey);
  86. if (outputMessage != null)
  87. {
  88. int outputMessageParts = VRTK_SharedMethods.GetDictionaryValue(commonMessageParts, messageKey);
  89. if (parameters.Length != outputMessageParts)
  90. {
  91. Array.Resize(ref parameters, outputMessageParts);
  92. }
  93. returnMessage = string.Format(outputMessage, parameters);
  94. }
  95. return returnMessage;
  96. }
  97. public static void Trace(string message)
  98. {
  99. Log(LogLevels.Trace, message);
  100. }
  101. public static void Debug(string message)
  102. {
  103. Log(LogLevels.Debug, message);
  104. }
  105. public static void Info(string message)
  106. {
  107. Log(LogLevels.Info, message);
  108. }
  109. public static void Warn(string message)
  110. {
  111. Log(LogLevels.Warn, message);
  112. }
  113. public static void Error(string message, bool forcePause = false)
  114. {
  115. Log(LogLevels.Error, message, forcePause);
  116. }
  117. public static void Fatal(string message, bool forcePause = false)
  118. {
  119. Log(LogLevels.Fatal, message, forcePause);
  120. }
  121. public static void Fatal(Exception exception, bool forcePause = false)
  122. {
  123. Log(LogLevels.Fatal, exception.Message, forcePause);
  124. }
  125. public static void Log(LogLevels level, string message, bool forcePause = false)
  126. {
  127. #if VRTK_NO_LOGGING
  128. return;
  129. #endif
  130. CreateIfNotExists();
  131. if (instance.minLevel > level)
  132. {
  133. return;
  134. }
  135. switch (level)
  136. {
  137. case LogLevels.Trace:
  138. case LogLevels.Debug:
  139. case LogLevels.Info:
  140. UnityEngine.Debug.Log(message);
  141. break;
  142. case LogLevels.Warn:
  143. UnityEngine.Debug.LogWarning(message);
  144. break;
  145. case LogLevels.Error:
  146. case LogLevels.Fatal:
  147. if (forcePause)
  148. {
  149. UnityEngine.Debug.Break();
  150. }
  151. if (instance.throwExceptions)
  152. {
  153. throw new Exception(message);
  154. }
  155. else
  156. {
  157. UnityEngine.Debug.LogError(message);
  158. }
  159. break;
  160. }
  161. }
  162. protected virtual void Awake()
  163. {
  164. instance = this;
  165. DontDestroyOnLoad(gameObject);
  166. }
  167. }
  168. }