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.

155 lines
4.3KB

  1. using System;
  2. #if NETFX_CORE
  3. using Windows.System.Diagnostics;
  4. #else
  5. using System.Diagnostics;
  6. #endif
  7. using System.Text;
  8. namespace Utility
  9. {
  10. public static class Console
  11. {
  12. static StringBuilder _stringBuilder = new StringBuilder(256);
  13. public static ILogger logger;
  14. public static volatile bool BatchLog = false;
  15. //Hack, have to find the right solution
  16. public static Action<Exception, string, string> onException;
  17. static Console()
  18. {
  19. #if UNITY_5_3_OR_NEWER || UNITY_5
  20. logger = new SlowLoggerUnity();
  21. onException = (e, message, stack) =>
  22. {
  23. UnityEngine.Debug.LogException(e, null);
  24. };
  25. #else
  26. logger = new SimpleLogger();
  27. #endif
  28. }
  29. public static void Log(string txt)
  30. {
  31. logger.Log(txt);
  32. }
  33. public static void LogError(string txt)
  34. {
  35. string toPrint;
  36. lock (_stringBuilder)
  37. {
  38. _stringBuilder.Length = 0;
  39. _stringBuilder.Append("-!!!!!!-> ");
  40. _stringBuilder.Append(txt);
  41. toPrint = _stringBuilder.ToString();
  42. }
  43. logger.Log(toPrint, null, LogType.Error);
  44. }
  45. public static void LogError(string txt, string stack)
  46. {
  47. string toPrint;
  48. lock (_stringBuilder)
  49. {
  50. _stringBuilder.Length = 0;
  51. _stringBuilder.Append("-!!!!!!-> ");
  52. _stringBuilder.Append(txt);
  53. toPrint = _stringBuilder.ToString();
  54. }
  55. logger.Log(toPrint, stack, LogType.Error);
  56. }
  57. public static void LogException(Exception e)
  58. {
  59. string toPrint;
  60. string stackTrace;
  61. lock (_stringBuilder)
  62. {
  63. _stringBuilder.Length = 0;
  64. _stringBuilder.Append("-!!!!!!-> ").Append(e.Message);
  65. stackTrace = e.StackTrace;
  66. if (e.InnerException != null)
  67. {
  68. e = e.InnerException;
  69. _stringBuilder.Append(" Inner Message: ").Append(e.Message).Append(" Inner Stacktrace:")
  70. .Append(e.StackTrace);
  71. stackTrace = e.StackTrace;
  72. }
  73. toPrint = _stringBuilder.ToString();
  74. }
  75. if (onException != null)
  76. onException(e, toPrint, stackTrace);
  77. }
  78. public static void LogWarning(string txt)
  79. {
  80. string toPrint;
  81. lock (_stringBuilder)
  82. {
  83. _stringBuilder.Length = 0;
  84. _stringBuilder.Append("------> ");
  85. _stringBuilder.Append(txt);
  86. toPrint = _stringBuilder.ToString();
  87. }
  88. logger.Log(toPrint, null, LogType.Warning);
  89. }
  90. /// <summary>
  91. /// Use this function if you don't want the message to be batched
  92. /// </summary>
  93. /// <param name="txt"></param>
  94. public static void SystemLog(string txt)
  95. {
  96. string toPrint;
  97. lock (_stringBuilder)
  98. {
  99. #if NETFX_CORE
  100. string currentTimeString = DateTime.UtcNow.ToString("dd/mm/yy hh:ii:ss");
  101. string processTimeString = (DateTime.UtcNow - ProcessDiagnosticInfo.GetForCurrentProcess().ProcessStartTime.DateTime).ToString();
  102. #else
  103. string currentTimeString = DateTime.UtcNow.ToLongTimeString(); //ensure includes seconds
  104. string processTimeString = (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString();
  105. #endif
  106. _stringBuilder.Length = 0;
  107. _stringBuilder.Append("[").Append(currentTimeString);
  108. _stringBuilder.Append("][").Append(processTimeString);
  109. _stringBuilder.Length = _stringBuilder.Length - 3; //remove some precision that we don't need
  110. _stringBuilder.Append("] ").AppendLine(txt);
  111. toPrint = _stringBuilder.ToString();
  112. }
  113. #if !UNITY_EDITOR
  114. #if !NETFX_CORE
  115. System.Console.WriteLine(toPrint);
  116. #else
  117. //find a way to adopt a logger externally, if this is still needed
  118. #endif
  119. #else
  120. UnityEngine.Debug.Log(toPrint);
  121. #endif
  122. }
  123. }
  124. }