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.

Print.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(this string str1, string 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. logger.Log(toPrint, showCurrentStack == true ? new StackTrace().ToString() : null, LogType.Error);
  123. }
  124. public static void LogError(string txt, string stack)
  125. {
  126. string toPrint;
  127. lock (_stringBuilder)
  128. {
  129. _stringBuilder.Length = 0;
  130. _stringBuilder.Append("-!!!!!!-> ");
  131. _stringBuilder.Append(txt);
  132. toPrint = _stringBuilder.ToString();
  133. }
  134. logger.Log(toPrint, stack, LogType.Error);
  135. }
  136. public static void LogException(Exception e)
  137. {
  138. LogException(e, null);
  139. }
  140. public static void LogException(Exception e, UnityEngine.Object obj)
  141. {
  142. UnityEngine.Debug.LogException(e, obj);
  143. }
  144. public static void LogWarning(string txt)
  145. {
  146. string toPrint;
  147. lock (_stringBuilder)
  148. {
  149. _stringBuilder.Length = 0;
  150. _stringBuilder.Append("------> ");
  151. _stringBuilder.Append(txt);
  152. toPrint = _stringBuilder.ToString();
  153. }
  154. logger.Log(toPrint, null, LogType.Warning);
  155. }
  156. /// <summary>
  157. /// Use this function if you don't want the message to be batched
  158. /// </summary>
  159. /// <param name="txt"></param>
  160. public static void SystemLog(string txt)
  161. {
  162. string toPrint;
  163. lock (_stringBuilder)
  164. {
  165. string currentTimeString = DateTime.UtcNow.ToLongTimeString(); //ensure includes seconds
  166. string processTimeString = (DateTime.UtcNow - Process.GetCurrentProcess().StartTime).ToString();
  167. _stringBuilder.Length = 0;
  168. _stringBuilder.Append("[").Append(currentTimeString);
  169. _stringBuilder.Append("][").Append(processTimeString);
  170. _stringBuilder.Length = _stringBuilder.Length - 3; //remove some precision that we don't need
  171. _stringBuilder.Append("] ").AppendLine(txt);
  172. toPrint = _stringBuilder.ToString();
  173. }
  174. #if !UNITY_EDITOR
  175. System.Console.WriteLine(toPrint);
  176. #else
  177. UnityEngine.Debug.Log(toPrint);
  178. #endif
  179. }
  180. }
  181. }