FileLog.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. public class FileLog : MonoBehaviour
  7. {
  8. public enum LogLevels : byte
  9. {
  10. Log = 0x0,
  11. Warning = 0x1,
  12. Error = 0x2,
  13. Exception = 0x3,
  14. Assert = 0x4
  15. }
  16. private static string _pathToLog = string.Empty;
  17. private static StringBuilder _items = new StringBuilder();
  18. private static DateTime _now;
  19. private static FileLog _instance;
  20. private static bool _catchSystemLog;
  21. private static string _fileName = "log";
  22. private static bool _stoped = true;
  23. private static bool _dateInFileName = true;
  24. private static bool _timeInEachRecord = true;
  25. private static LogLevels _minimumLevel = LogLevels.Log;
  26. private bool _writting;
  27. private static readonly object LOCKER = new object();
  28. private static string FileName
  29. {
  30. get
  31. {
  32. if (!_dateInFileName)
  33. {
  34. return string.Format("{0}.nekkilog", _fileName);
  35. }
  36. return string.Format("{3} {0}.{1}.{2}.nekkilog", _now.Year.ToString("0000"), _now.Month.ToString("00"), _now.Day.ToString("00"), _fileName);
  37. }
  38. }
  39. private static string Path
  40. {
  41. get
  42. {
  43. if (string.IsNullOrEmpty(_pathToLog))
  44. {
  45. return string.Format("{0}/{1}", Application.persistentDataPath, FileName);
  46. }
  47. return string.Format("{0}/{1}", _pathToLog, FileName);
  48. }
  49. }
  50. public static void Init(string pathToLog, string fileName, bool catchSystemLog, bool dateInFileName, bool timeInEachRecord, LogLevels minimumLevel)
  51. {
  52. if (_instance)
  53. {
  54. _instance.Write();
  55. }
  56. _pathToLog = pathToLog.Trim('/').Trim('\\');
  57. _minimumLevel = minimumLevel;
  58. _dateInFileName = dateInFileName;
  59. _timeInEachRecord = timeInEachRecord;
  60. _catchSystemLog = catchSystemLog;
  61. if (!string.IsNullOrEmpty(fileName))
  62. {
  63. _fileName = fileName;
  64. }
  65. CheckPersistance();
  66. _stoped = false;
  67. }
  68. private static void CheckPersistance()
  69. {
  70. if (_instance)
  71. {
  72. return;
  73. }
  74. var go = new GameObject("_log");
  75. _instance = go.AddComponent<FileLog>();
  76. DontDestroyOnLoad(go);
  77. _instance.Update();
  78. _instance.StartCoroutine(_instance.SaveToFile());
  79. Application.RegisterLogCallback(_unityLogCallback);
  80. }
  81. private void OnDestroy()
  82. {
  83. Stop();
  84. }
  85. private void OnEnable()
  86. {
  87. StopAllCoroutines();
  88. StartCoroutine(SaveToFile());
  89. }
  90. private static void _unityLogCallback(string condition, string stacktrace, LogType type)
  91. {
  92. if (!_catchSystemLog)
  93. {
  94. return;
  95. }
  96. switch (type)
  97. {
  98. case LogType.Error:
  99. Error(condition,stacktrace);
  100. break;
  101. case LogType.Assert:
  102. Assert(condition,stacktrace);
  103. break;
  104. case LogType.Warning:
  105. Warning(condition,stacktrace);
  106. break;
  107. case LogType.Log:
  108. Log(condition,stacktrace);
  109. break;
  110. case LogType.Exception:
  111. Exception(condition,stacktrace);
  112. break;
  113. }
  114. }
  115. private void Update()
  116. {
  117. if (_timeInEachRecord || _dateInFileName)
  118. {
  119. _now = DateTime.Now;
  120. }
  121. }
  122. public static void Log(object message, string stacktrace = null)
  123. {
  124. PostMesage(LogLevels.Log, message, stacktrace);
  125. }
  126. public static void Warning(object message, string stacktrace = null)
  127. {
  128. PostMesage(LogLevels.Warning, message, stacktrace);
  129. }
  130. public static void Error(object message, string stacktrace = null)
  131. {
  132. PostMesage(LogLevels.Error, message, stacktrace);
  133. }
  134. public static void Exception(Exception ex)
  135. {
  136. PostMesage(LogLevels.Exception, ex.Message, ex.StackTrace);
  137. }
  138. private static void Exception(object message, string stacktrace)
  139. {
  140. PostMesage(LogLevels.Exception, message, stacktrace);
  141. }
  142. public static void Assert(object message, string stacktrace = null)
  143. {
  144. PostMesage(LogLevels.Assert, message, stacktrace);
  145. }
  146. public static void Stop()
  147. {
  148. _instance.Write();
  149. _stoped = true;
  150. }
  151. private static string FormatMessage(LogLevels level, object condition, string stacktrace = null)
  152. {
  153. var message = string.IsNullOrEmpty(stacktrace) ? condition : string.Format("{0} at: {1}", condition, stacktrace);
  154. if (!_timeInEachRecord)
  155. {
  156. return string.Format("[{0}] {1}\n", level, message);
  157. }
  158. return string.Format("[{3}] [{0}:{1}:{2}] {4}\n", _now.Hour.ToString("00"), _now.Minute.ToString("00"), _now.Second.ToString("00"), level, message);
  159. }
  160. private static void PostMesage(LogLevels level, object message, string stacktrace = null)
  161. {
  162. if (_stoped)
  163. {
  164. Debug.LogWarning("you must init log system first!");
  165. return;
  166. }
  167. if (level < _minimumLevel)
  168. {
  169. return;
  170. }
  171. CheckPersistance();
  172. lock (LOCKER)
  173. {
  174. _items.Append(FormatMessage(level, message,stacktrace));
  175. }
  176. }
  177. private IEnumerator SaveToFile()
  178. {
  179. while (transform)
  180. {
  181. yield return new WaitForSeconds(1);
  182. Write();
  183. }
  184. }
  185. private void Write()
  186. {
  187. if (_writting)
  188. {
  189. return;
  190. }
  191. _writting = true;
  192. string contents;
  193. lock (LOCKER)
  194. {
  195. contents = _items.ToString();
  196. _items = new StringBuilder();
  197. }
  198. try
  199. {
  200. File.AppendAllText(Path, contents);
  201. }
  202. catch (Exception){ }
  203. _writting = false;
  204. }
  205. }