FileLogger.cs 6.0 KB

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