alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTap.Diagnostic;
using OpenTapEditor.Util;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using UtilLib;
 
namespace OpenTapEditor
{
    public enum LevelType
    {
        /// <summary>
        ///     Recoverable error.
        /// </summary>
        Error = 10,
        /// <summary>
        ///     Noncritical problem.
        /// </summary>
        Warning = 20,
        /// <summary>
        ///     Informational message.
        /// </summary>
        Info = 30,
        /// <summary>
        ///     Debugging trace.
        /// </summary>
        Debug = 40
    }
 
    public class LogItem
    {
 
        public Event e { get; internal set; }
 
        public int Type => e.EventType;
 
        public override string ToString()
        {
            return $"{(LevelType)e.EventType} {new DateTime(e.Timestamp).ToString("HH:mm:ss.fff")} : {e.Source} : {e.Message}";
        }
 
    }
    /// <summary>
    /// LogControl.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class LogControl : UserControl, ILogListener
    {
 
        [ObservableProperty]
        private ObservableCollection<LogItem> datasource = new ObservableCollection<LogItem>();
 
        [ObservableProperty]
        private string searchText = string.Empty;
 
        [ObservableProperty]
        private bool showError = true;
 
        [ObservableProperty]
        private bool showWarn = true;
 
        [ObservableProperty]
        private bool showInfo = true;
 
        [ObservableProperty]
        private bool showDebug = true;
 
        [ObservableProperty]
        private bool showAll = false;
 
        [ObservableProperty]
        private CollectionViewSource viewSource;
 
        private readonly SolidColorBrush _debugBrush;
        private readonly SolidColorBrush _successBrush;
        private readonly SolidColorBrush _warnBrush;
        private readonly SolidColorBrush _errorBrush;
 
        public LogControl()
        {
 
            _debugBrush = new SolidColorBrush(Color.FromArgb(0xaa, 0x71, 0x80, 0x8A)); // 灰色
            _successBrush = new SolidColorBrush(Color.FromArgb(0xaa, 0x30, 0xC5, 0x30));
            _warnBrush = new SolidColorBrush(Color.FromArgb(0xaa, 0xD6, 0x9E, 0x2E));  // 橙黄色
            _errorBrush = new SolidColorBrush(Color.FromArgb(0xaa, 0xC5, 0x30, 0x30));   // 红色
 
            DataContext = this;
            ViewSource = new CollectionViewSource();
            ViewSource.Filter += CollectionViewSource_Filter;
            BindingOperations.SetBinding(viewSource, CollectionViewSource.SourceProperty,
                new Binding(nameof(Datasource))
                {
                    Source = this,
                    Mode = BindingMode.TwoWay,
                    NotifyOnSourceUpdated = true,
                });
            InitializeComponent();
            InitFromSetting();
        }
 
        private void InitFromSetting()
        {
            ShowError = bool.Parse(IniHelper.ReadIniData("Log", nameof(ShowError), "True", PathHelper.SettingIniPath));
            ShowWarn = bool.Parse(IniHelper.ReadIniData("Log", nameof(ShowWarn), "True", PathHelper.SettingIniPath));
            ShowInfo = bool.Parse(IniHelper.ReadIniData("Log", nameof(ShowInfo), "True", PathHelper.SettingIniPath));
            ShowDebug = bool.Parse(IniHelper.ReadIniData("Log", nameof(ShowDebug), "True", PathHelper.SettingIniPath));
            ShowAll = bool.Parse(IniHelper.ReadIniData("Log", nameof(ShowAll), "False", PathHelper.SettingIniPath));
        }
 
        private void WriteSetting(string name, object value)
        {
            IniHelper.WriteIniData("Log", name, value.ToString(), PathHelper.SettingIniPath);
        }
 
        partial void OnShowErrorChanged(bool value)
        {
            WriteSetting(nameof(ShowError), value);
            ViewSource.View.Refresh();
        }
 
        partial void OnShowWarnChanged(bool value)
        {
            WriteSetting(nameof(ShowWarn), value);
            ViewSource.View.Refresh();
        }
 
        partial void OnShowInfoChanged(bool value)
        {
            WriteSetting(nameof(ShowInfo), value);
            ViewSource.View.Refresh();
        }
 
        partial void OnShowDebugChanged(bool value)
        {
            WriteSetting(nameof(ShowDebug), value);
            ViewSource.View.Refresh();
        }
 
        partial void OnShowAllChanged(bool value)
        {
            WriteSetting(nameof(ShowAll), value);
            ViewSource.View.Refresh();
        }
 
        partial void OnSearchTextChanged(string value)
        {
            ViewSource.View.Refresh();
        }
 
        public void EventsLogged(IEnumerable<Event> Events)
        {
            foreach (var e in Events)
            {
                HandleLog(e);
            }
        }
 
        private void HandleLog(Event e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                lock (this)
                {
                    var item = new LogItem()
                    {
                        e = e
                    };
                    while (Datasource.Count >= 1000)
                    {
                        Datasource.RemoveAt(0);
                    }
                    Datasource.Add(item);
                    log.ScrollIntoView(item);
                }
            }, DispatcherPriority.Background);
        }
 
        public void Flush()
        {
            Log.Flush();
        }
 
        private void CollectionViewSource_Filter(object sender, System.Windows.Data.FilterEventArgs e)
        {
            if (e.Item is LogItem item)
            {
                bool res = true;
                switch (item.Type)
                {
                    case 10:
                        res = ShowError || ShowAll;
                        break;
                    case 20:
                        res = ShowWarn || ShowAll;
                        break;
                    case 30:
                        res = ShowInfo || ShowAll;
                        break;
                    case 40:
                        res = ShowDebug || ShowAll;
                        break;
                    default:
                        res = true;
                        break;
                }
                if (!string.IsNullOrEmpty(SearchText))
                {
                    res &= item.ToString().Contains(SearchText);
                }
                e.Accepted = res;
            }
        }
 
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Datasource.Clear();
        }
 
        private volatile bool showingResult = false;
        public void ShowResult(Verdict verdict)
        {
            if (showingResult)
            {
                return;
            }
            showingResult = true;
            Task.Run(() =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    string title = "PASS";
                    resultShow.Background = _successBrush;
                    if (verdict == Verdict.Aborted)
                    {
                        resultShow.Background = _warnBrush;
                        title = "Aborted";
                    }
                    else if (verdict == Verdict.NotSet)
                    {
                        resultShow.Background = _debugBrush;
                        title = "NotSet";
                    }
                    else if (verdict == Verdict.Fail)
                    {
                        resultShow.Background = _errorBrush;
                        title = "FAIL";
                    }
                    else if (verdict == Verdict.Error)
                    {
                        resultShow.Background = _errorBrush;
                        title = "Error";
                    }
                    resultLabel.Content = title;
                    resultShow.Visibility = System.Windows.Visibility.Visible;
                });
                Thread.Sleep(3000);
                Dispatcher.BeginInvoke(() =>
                {
                    resultShow.Visibility = System.Windows.Visibility.Collapsed;
                });
                showingResult = false;
            });
        }
    }
}