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 7.2KB

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