2
0

FileLogger.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. private FileLogger()
  33. {
  34. waitingBuffers = new Dictionary<string, Dictionary<long, string[]>>();
  35. startTimestamp = DateTime.Now;
  36. logables = new List<ILogable>();
  37. }
  38. public void RegisterLogable(ILogable l) => logables.Add(l);
  39. private IEnumerable<string> MergedHeaders()
  40. {
  41. var headers = new List<string> {"timestamp"};
  42. foreach (var l in logables)
  43. {
  44. headers.AddRange(l.HeaderNames);
  45. }
  46. return headers.ToArray();
  47. }
  48. private IEnumerable<long> MergeTimestamps()
  49. {
  50. var allTimestamps = new HashSet<long>();
  51. long minBegin = -1;
  52. long minEnd = -1;
  53. foreach (var l in logables)
  54. {
  55. var keys = l.BufferLines.Keys.ToArray();
  56. var begin = keys.Length > 0 ? keys.First() : -1;
  57. var end = keys.Length > 0 ? keys.Last() : -1;
  58. if (minBegin == -1 || begin >= 0 && begin < minBegin) minBegin = begin;
  59. if (minEnd == -1 || end >= 0 && end < minEnd) minEnd = end;
  60. }
  61. foreach (var l in logables)
  62. {
  63. var bufferLines = l.BufferLines;
  64. var timestamps = new HashSet<long>(bufferLines.Keys);
  65. var logableKey = l.Key;
  66. var hasWaiting = waitingBuffers.TryGetValue(logableKey, out var waiting);
  67. if (hasWaiting)
  68. {
  69. foreach (var waitingTs in waiting.Keys)
  70. {
  71. if (waitingTs <= minEnd)
  72. {
  73. allTimestamps.Add(waitingTs);
  74. }
  75. }
  76. }
  77. foreach (var t in timestamps)
  78. {
  79. if (t >= minBegin && t <= minEnd)
  80. {
  81. allTimestamps.Add(t);
  82. }
  83. else
  84. {
  85. if (waitingBuffers.ContainsKey(l.Key))
  86. {
  87. waitingBuffers[l.Key][t] = bufferLines[t];
  88. }
  89. else
  90. {
  91. waitingBuffers[l.Key] = new Dictionary<long, string[]> {{t, bufferLines[t]}};
  92. }
  93. }
  94. }
  95. }
  96. logables.ForEach(l => allTimestamps.UnionWith(l.BufferLines.Keys.Where(k => k >= minBegin && k <= minEnd)));
  97. return allTimestamps;
  98. }
  99. private IEnumerable<IEnumerable<string>> MergedLines()
  100. {
  101. var lines = new List<List<string>>();
  102. var allKeys = MergeTimestamps();
  103. var mergedDict = new SortedDictionary<long, List<string>>();
  104. foreach (var timestamp in allKeys)
  105. {
  106. var newItem = new List<string>();
  107. foreach (var l in logables)
  108. {
  109. var logableKey = l.Key;
  110. var hasWaiting = waitingBuffers.TryGetValue(logableKey, out var waiting);
  111. string[] linePart;
  112. if (l.BufferLines.ContainsKey(timestamp))
  113. {
  114. linePart = l.BufferLines[timestamp];
  115. }
  116. else if (hasWaiting && waiting.ContainsKey(timestamp))
  117. {
  118. linePart = waiting[timestamp];
  119. waiting.Remove(timestamp);
  120. }
  121. else
  122. {
  123. /*var lastAvailableKey =
  124. l.BufferLines.Keys.Where(k => k < timestamp).DefaultIfEmpty(-1).LastOrDefault();
  125. linePart = lastAvailableKey >= 0
  126. ? l.BufferLines[lastAvailableKey]
  127. : new string[l.HeaderNames.ToArray().Length];*/
  128. linePart = new string[l.HeaderNames.ToArray().Length];
  129. }
  130. newItem.AddRange(linePart);
  131. }
  132. mergedDict[timestamp] = newItem;
  133. }
  134. mergedDict.ForEach(item =>
  135. {
  136. var listItem = new List<string> {item.Key.ToString("D")};
  137. listItem.AddRange(item.Value);
  138. lines.Add(listItem);
  139. });
  140. return lines;
  141. }
  142. public async Task UpdateRegisteredLogs()
  143. {
  144. if (!writerAvailable)
  145. {
  146. writer = new AsyncLogFileWriter($"Assets/Logs/{startTimestamp:yyyy-MM-dd_HHmmss}.tsv");
  147. await writer.WriteDataLine(MergedHeaders());
  148. writerAvailable = true;
  149. }
  150. await writer.WriteDataLines(MergedLines());
  151. logables.ForEach(l => l.ClearBuffer());
  152. }
  153. private void Dispose()
  154. {
  155. if (writerAvailable)
  156. {
  157. writer.Dispose();
  158. }
  159. logables.Clear();
  160. }
  161. }
  162. }