MB2_Log.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Text;
  5. namespace DigitalOpus.MB.Core{
  6. public enum MB2_LogLevel{
  7. none,
  8. error,
  9. warn,
  10. info,
  11. debug,
  12. trace
  13. }
  14. public class MB2_Log {
  15. public static void Log(MB2_LogLevel l, String msg, MB2_LogLevel currentThreshold){
  16. if (l <= currentThreshold) {
  17. if (l == MB2_LogLevel.error) Debug.LogError(msg);
  18. if (l == MB2_LogLevel.warn) Debug.LogWarning(String.Format("frm={0} WARN {1}",Time.frameCount,msg));
  19. if (l == MB2_LogLevel.info) Debug.Log(String.Format("frm={0} INFO {1}",Time.frameCount,msg));
  20. if (l == MB2_LogLevel.debug) Debug.Log(String.Format("frm={0} DEBUG {1}",Time.frameCount,msg));
  21. if (l == MB2_LogLevel.trace) Debug.Log(String.Format("frm={0} TRACE {1}",Time.frameCount,msg));
  22. }
  23. }
  24. public static string Error(string msg, params object[] args){
  25. string s = String.Format(msg, args);
  26. string s2 = String.Format("f={0} ERROR {1}", Time.frameCount,s);
  27. Debug.LogError(s2);
  28. return s2;
  29. }
  30. public static string Warn(string msg, params object[] args){
  31. string s = String.Format(msg, args);
  32. string s2 = String.Format("f={0} WARN {1}", Time.frameCount,s);
  33. Debug.LogWarning(s2);
  34. return s2;
  35. }
  36. public static string Info(string msg, params object[] args){
  37. string s = String.Format(msg, args);
  38. string s2 = String.Format("f={0} INFO {1}", Time.frameCount,s);
  39. Debug.Log(s2);
  40. return s2;
  41. }
  42. public static string LogDebug(string msg, params object[] args){
  43. string s = String.Format(msg, args);
  44. string s2 = String.Format("f={0} DEBUG {1}", Time.frameCount,s);
  45. Debug.Log(s2);
  46. return s2;
  47. }
  48. public static string Trace(string msg, params object[] args){
  49. string s = String.Format(msg, args);
  50. string s2 = String.Format("f={0} TRACE {1}", Time.frameCount,s);
  51. Debug.Log(s2);
  52. return s2;
  53. }
  54. }
  55. /// <summary>
  56. /// LOD stores a buffer of log messages specific to an object. These log messages are also written out to
  57. /// the console.
  58. /// </summary>
  59. public class ObjectLog{
  60. int pos = 0;
  61. string[] logMessages;
  62. void _CacheLogMessage(string msg){
  63. if (logMessages.Length == 0) return;
  64. logMessages[pos] = msg;
  65. pos++;
  66. if (pos >= logMessages.Length) pos = 0;
  67. }
  68. public ObjectLog(short bufferSize){
  69. logMessages = new string[bufferSize];
  70. }
  71. public void Log(MB2_LogLevel l, String msg, MB2_LogLevel currentThreshold){
  72. MB2_Log.Log(l,msg, currentThreshold);
  73. _CacheLogMessage(msg);
  74. }
  75. public void Error(string msg, params object[] args){
  76. _CacheLogMessage(MB2_Log.Error(msg,args));
  77. }
  78. public void Warn(string msg, params object[] args){
  79. _CacheLogMessage(MB2_Log.Warn(msg,args));
  80. }
  81. public void Info(string msg, params object[] args){
  82. _CacheLogMessage(MB2_Log.Info(msg,args));
  83. }
  84. public void LogDebug(string msg, params object[] args){
  85. _CacheLogMessage(MB2_Log.LogDebug(msg,args));
  86. }
  87. public void Trace(string msg, params object[] args){
  88. _CacheLogMessage(MB2_Log.Trace(msg,args));
  89. }
  90. public string Dump(){
  91. StringBuilder sb = new StringBuilder();
  92. int startPos = 0;
  93. if (logMessages[logMessages.Length - 1] != null) startPos = pos;
  94. for (int i = 0; i < logMessages.Length; i++){
  95. int ii = (startPos + i) % logMessages.Length;
  96. if (logMessages[ii] == null) break;
  97. sb.AppendLine(logMessages[ii]);
  98. }
  99. return sb.ToString();
  100. }
  101. }
  102. }