Repository navigation
Expand file tree
/
Copy pathLogStore.cs
More file actions
59 lines (49 loc) · 1.67 KB
/
Copy pathLogStore.cs
File metadata and controls
59 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Text;
namespace FastbootEnhance
{
/// <summary>
/// The session log. Everything fastboot prints, and what the app did about it, lands here;
/// the Logs page and the Flash page's live log both listen to it. It replaces the old
/// pop-up logger, which only recorded while its window happened to be open.
/// </summary>
static class LogStore
{
const int MaxChars = 2000000;
const int TrimTo = 1500000;
static readonly object gate = new object();
static readonly StringBuilder text = new StringBuilder();
/// <summary>Raised on the writer's thread with the stamped line.</summary>
public static event Action<string> LineAdded;
/// <summary>Raised after <see cref="Clear"/>.</summary>
public static event Action Cleared;
public static void Append(string line)
{
if (line == null)
return;
string stamped = DateTime.Now.ToString("HH:mm:ss") + " " + line;
lock (gate)
{
text.Append(stamped).Append('\n');
if (text.Length > MaxChars)
text.Remove(0, text.Length - TrimTo);
}
Action<string> handler = LineAdded;
if (handler != null)
handler(stamped);
}
public static string Snapshot()
{
lock (gate)
return text.ToString();
}
public static void Clear()
{
lock (gate)
text.Clear();
Action handler = Cleared;
if (handler != null)
handler();
}
}
}