FileLogger.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Valve.VR.InteractionSystem;
  6. namespace Logging.Base
  7. {
  8. public class FileLogger
  9. {
  10. #region signleton
  11. private static Lazy<FileLogger>
  12. instance =
  13. new Lazy<FileLogger>
  14. (() => new FileLogger());
  15. public static FileLogger Instance => instance.Value;
  16. public static void DestroyInstance()
  17. {
  18. if (instance.IsValueCreated)
  19. {
  20. instance.Value.Dispose();
  21. instance =
  22. new Lazy<FileLogger>
  23. (() => new FileLogger());
  24. }
  25. }
  26. #endregion
  27. private readonly DateTime startTimestamp;
  28. private AsyncLogFileWriter writer;
  29. private readonly List<ILogable> logables;
  30. private bool writerAvailable = false;
  31. private readonly Dictionary<string, Dictionary<long, string[]>> waitingBuffers;
  32. public string SubFolderName { get; set; }
  33. public string Condition { get; set; }
  34. private FileLogger()
  35. {
  36. waitingBuffers = new Dictionary<string, Dictionary<long, string[]>>();
  37. startTimestamp = DateTime.Now;
  38. logables = new List<ILogable>();
  39. }
  40. public void RegisterLogable(ILogable l) => logables.Add(l);
  41. private IEnumerable<string> MergedHeaders()
  42. {
  43. var headers = new List<string> {"timestamp"};
  44. foreach (var l in logables)
  45. {
  46. headers.AddRange(l.HeaderNames);
  47. }
  48. return headers.ToArray();
  49. }
  50. private IEnumerable<long> MergeTimestamps()
  51. {
  52. var allTimestamps = new HashSet<long>();
  53. long minBegin = -1;
  54. long minEnd = -1;
  55. foreach (var l in logables)
  56. {
  57. var keys = l.BufferLines.Keys.ToArray();
  58. var begin = keys.Length > 0 ? keys.First() : -1;
  59. var end = keys.Length > 0 ? keys.Last() : -1;
  60. if (minBegin == -1 || begin >= 0 && begin < minBegin) minBegin = begin;
  61. if (minEnd == -1 || end >= 0 && end < minEnd) minEnd = end;
  62. }
  63. foreach (var l in logables)
  64. {
  65. var bufferLines = l.BufferLines;
  66. var timestamps = new HashSet<long>(bufferLines.Keys);
  67. var logableKey = l.Key;
  68. var hasWaiting = waitingBuffers.TryGetValue(logableKey, out var waiting);
  69. if (hasWaiting)
  70. {
  71. foreach (var waitingTs in waiting.Keys)
  72. {
  73. if (waitingTs <= minEnd)
  74. {
  75. allTimestamps.Add(waitingTs);
  76. }
  77. }
  78. }
  79. foreach (var t in timestamps)
  80. {
  81. if (t >= minBegin && t <= minEnd)
  82. {
  83. allTimestamps.Add(t);
  84. }
  85. else
  86. {
  87. if (waitingBuffers.ContainsKey(l.Key))
  88. {
  89. waitingBuffers[l.Key][t] = bufferLines[t];
  90. }
  91. else
  92. {
  93. waitingBuffers[l.Key] = new Dictionary<long, string[]> {{t, bufferLines[t]}};
  94. }
  95. }
  96. }
  97. }
  98. logables.ForEach(l => allTimestamps.UnionWith(l.BufferLines.Keys.Where(k => k >= minBegin && k <= minEnd)));
  99. return allTimestamps;
  100. }
  101. private IEnumerable<IEnumerable<string>> MergedLines()
  102. {
  103. var lines = new List<List<string>>();
  104. var allKeys = MergeTimestamps();
  105. var mergedDict = new SortedDictionary<long, List<string>>();
  106. foreach (var timestamp in allKeys)
  107. {
  108. var newItem = new List<string>();
  109. foreach (var l in logables)
  110. {
  111. var logableKey = l.Key;
  112. var hasWaiting = waitingBuffers.TryGetValue(logableKey, out var waiting);
  113. string[] linePart;
  114. if (l.BufferLines.ContainsKey(timestamp))
  115. {
  116. linePart = l.BufferLines[timestamp];
  117. }
  118. else if (hasWaiting && waiting.ContainsKey(timestamp))
  119. {
  120. linePart = waiting[timestamp];
  121. waiting.Remove(timestamp);
  122. }
  123. else
  124. {
  125. /*var lastAvailableKey =
  126. l.BufferLines.Keys.Where(k => k < timestamp).DefaultIfEmpty(-1).LastOrDefault();
  127. linePart = lastAvailableKey >= 0
  128. ? l.BufferLines[lastAvailableKey]
  129. : new string[l.HeaderNames.ToArray().Length];*/
  130. linePart = new string[l.HeaderNames.ToArray().Length];
  131. }
  132. newItem.AddRange(linePart);
  133. }
  134. mergedDict[timestamp] = newItem;
  135. }
  136. mergedDict.ForEach(item =>
  137. {
  138. var listItem = new List<string> {item.Key.ToString("D")};
  139. listItem.AddRange(item.Value);
  140. lines.Add(listItem);
  141. });
  142. return lines;
  143. }
  144. public async Task UpdateRegisteredLogs()
  145. {
  146. if (!writerAvailable)
  147. {
  148. var subfolder = SubFolderName != null ? $"/{SubFolderName}/" : "";
  149. var condition = $"_{Condition}" ?? "";
  150. writer = new AsyncLogFileWriter(
  151. $"Assets/Logs/{subfolder}{startTimestamp:yyyy-MM-dd_HHmmss}{condition}.tsv");
  152. await writer.WriteDataLine(MergedHeaders());
  153. writerAvailable = true;
  154. }
  155. await writer.WriteDataLines(MergedLines());
  156. logables.ForEach(l => l.ClearBuffer());
  157. }
  158. private void Dispose()
  159. {
  160. if (writerAvailable)
  161. {
  162. writer.Dispose();
  163. }
  164. logables.Clear();
  165. }
  166. }
  167. }