Mirror of Svelto.ECS because we're a fan of it
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.

234 lines
6.3KB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using UnityEngine;
  5. public static class FastConcatUtility
  6. {
  7. static readonly StringBuilder _stringBuilder = new StringBuilder(256);
  8. public static string FastConcat<T>(this string str1, T str2)
  9. {
  10. lock (_stringBuilder)
  11. {
  12. _stringBuilder.Length = 0;
  13. _stringBuilder.Append(str1);
  14. _stringBuilder.Append(str2);
  15. return _stringBuilder.ToString();
  16. }
  17. }
  18. public static string FastConcat(this string str1, string str2, string str3)
  19. {
  20. lock (_stringBuilder)
  21. {
  22. _stringBuilder.Length = 0;
  23. _stringBuilder.Append(str1);
  24. _stringBuilder.Append(str2);
  25. _stringBuilder.Append(str3);
  26. return _stringBuilder.ToString();
  27. }
  28. }
  29. public static string FastConcat(this string str1, string str2, string str3, string str4)
  30. {
  31. lock (_stringBuilder)
  32. {
  33. _stringBuilder.Length = 0;
  34. _stringBuilder.Append(str1);
  35. _stringBuilder.Append(str2);
  36. _stringBuilder.Append(str3);
  37. _stringBuilder.Append(str4);
  38. return _stringBuilder.ToString();
  39. }
  40. }
  41. public static string FastConcat(this string str1, string str2, string str3, string str4, string str5)
  42. {
  43. lock (_stringBuilder)
  44. {
  45. _stringBuilder.Length = 0;
  46. _stringBuilder.Append(str1);
  47. _stringBuilder.Append(str2);
  48. _stringBuilder.Append(str3);
  49. _stringBuilder.Append(str4);
  50. _stringBuilder.Append(str5);
  51. return _stringBuilder.ToString();
  52. }
  53. }
  54. public static string FastJoin(this string[] str)
  55. {
  56. lock (_stringBuilder)
  57. {
  58. _stringBuilder.Length = 0;
  59. for (int i = 0; i < str.Length; i++)
  60. _stringBuilder.Append(str[i]);
  61. return _stringBuilder.ToString();
  62. }
  63. }
  64. public static string FastJoin(this string[] str, string str1)
  65. {
  66. lock (_stringBuilder)
  67. {
  68. _stringBuilder.Length = 0;
  69. for (int i = 0; i < str.Length; i++)
  70. _stringBuilder.Append(str[i]);
  71. _stringBuilder.Append(str1);
  72. return _stringBuilder.ToString();
  73. }
  74. }
  75. }
  76. namespace Utility
  77. {
  78. public interface ILogger
  79. {
  80. void Log (string txt, string stack = null, LogType type = LogType.Log);
  81. }
  82. public class SlowLogger : ILogger
  83. {
  84. public void Log(string txt, string stack = null, LogType type = LogType.Log)
  85. {
  86. switch (type)
  87. {
  88. case LogType.Log:
  89. UnityEngine.Debug.Log(stack != null ? txt.FastConcat(stack) : txt);
  90. break;
  91. case LogType.Exception:
  92. UnityEngine.Debug.LogError("Log of exceptions not supported");
  93. break;
  94. case LogType.Warning:
  95. UnityEngine.Debug.LogWarning(stack != null ? txt.FastConcat(stack) : txt);
  96. break;
  97. case LogType.Error:
  98. UnityEngine.Debug.LogError(stack != null ? txt.FastConcat(stack) : txt);
  99. break;
  100. }
  101. }
  102. }
  103. public static class Console
  104. {
  105. static StringBuilder _stringBuilder = new StringBuilder(256);
  106. public static ILogger logger = new SlowLogger();
  107. public static volatile bool BatchLog = false;
  108. public static void Log(string txt)
  109. {
  110. logger.Log(txt);
  111. }
  112. public static void LogError(string txt, bool showCurrentStack = true)
  113. {
  114. string toPrint;
  115. lock (_stringBuilder)
  116. {
  117. _stringBuilder.Length = 0;
  118. _stringBuilder.Append("-!!!!!!-> ");
  119. _stringBuilder.Append(txt);
  120. toPrint = _stringBuilder.ToString();
  121. }
  122. #if !NETFX_CORE
  123. logger.Log(toPrint, showCurrentStack == true ? new StackTrace().ToString() : null, LogType.Error);
  124. #else
  125. logger.Log(toPrint, null, LogType.Error);
  126. #endif
  127. }
  128. public static void LogError(string txt, string stack)
  129. {
  130. string toPrint;
  131. lock (_stringBuilder)
  132. {
  133. _stringBuilder.Length = 0;
  134. _stringBuilder.Append("-!!!!!!-> ");
  135. _stringBuilder.Append(txt);
  136. toPrint = _stringBuilder.ToString();
  137. }
  138. logger.Log(toPrint, stack, LogType.Error);
  139. }
  140. public static void LogException(Exception e)
  141. {
  142. LogException(e, null);
  143. }
  144. public static void LogException(Exception e, UnityEngine.Object obj)
  145. {
  146. UnityEngine.Debug.LogException(e, obj);
  147. }
  148. public static void LogWarning(string txt)
  149. {
  150. string toPrint;
  151. lock (_stringBuilder)
  152. {
  153. _stringBuilder.Length = 0;
  154. _stringBuilder.Append("------> ");
  155. _stringBuilder.Append(txt);
  156. toPrint = _stringBuilder.ToString();
  157. }
  158. logger.Log(toPrint, null, LogType.Warning);
  159. }
  160. /// <summary>
  161. /// Use this function if you don't want the message to be batched
  162. /// </summary>
  163. /// <param name="txt"></param>
  164. public static void SystemLog(string txt)
  165. {
  166. #if !NETFX_CORE
  167. string toPrint;
  168. lock (_stringBuilder)
  169. {
  170. string currentTimeString = DateTime.UtcNow.ToLongTimeString(); //ensure includes seconds
  171. string processTimeString = (DateTime.UtcNow - Process.GetCurrentProcess().StartTime).ToString();
  172. _stringBuilder.Length = 0;
  173. _stringBuilder.Append("[").Append(currentTimeString);
  174. _stringBuilder.Append("][").Append(processTimeString);
  175. _stringBuilder.Length = _stringBuilder.Length - 3; //remove some precision that we don't need
  176. _stringBuilder.Append("] ").AppendLine(txt);
  177. toPrint = _stringBuilder.ToString();
  178. }
  179. #if !UNITY_EDITOR
  180. System.Console.WriteLine(toPrint);
  181. #else
  182. UnityEngine.Debug.Log(toPrint);
  183. #endif
  184. #else
  185. UnityEngine.Debug.Log(txt);
  186. #endif
  187. }
  188. }
  189. }