chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
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.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 bool showError = true;
 
        [ObservableProperty]
        private bool showWarn = true;
 
        [ObservableProperty]
        private bool showInfo = true;
 
        [ObservableProperty]
        private bool showDebug = true;
 
        [ObservableProperty]
        private CollectionViewSource viewSource;
 
        public LogControl()
        {
            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));
        }
 
        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();
        }
 
        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
                    };
                    Datasource.Add(item);
                    log.ScrollIntoView(item);
                }
            }, DispatcherPriority.Background);
        }
 
        public void Flush()
        {
 
        }
 
        private void CollectionViewSource_Filter(object sender, System.Windows.Data.FilterEventArgs e)
        {
            if (e.Item is LogItem item)
            {
                switch (item.Type)
                {
                    case 10:
                        e.Accepted = ShowError;
                        break;
                    case 20:
                        e.Accepted = ShowWarn;
                        break;
                    case 30:
                        e.Accepted = ShowInfo;
                        break;
                    case 40:
                        e.Accepted = ShowDebug;
                        break;
                    default:
                        e.Accepted = true;
                        break;
                }
            }
        }
 
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Datasource.Clear();
        }
    }
}