From 1328095c168ea41883b3b29b55de3bce55f64a09 Mon Sep 17 00:00:00 2001 From: Carina Date: Sat, 11 Jan 2025 11:50:04 +0800 Subject: [PATCH] [1] Remove file name extension check for formatting CLEF/JSON data. [2] Fix behavior of switching formatting CLEF/JSON data in LogDataSourceOptionsDialog. --- .../Controls/LogDataSourceOptionsDialog.axaml | 6 ----- .../LogDataSourceOptionsDialog.axaml.cs | 27 ++++++++----------- .../Logs/DataSources/FileLogDataSource.cs | 19 +++++++------ .../DataSources/ILogDataSourceProvider.cs | 7 +++++ ULogViewer/Strings/Default.axaml | 3 --- ULogViewer/Strings/zh-CN.axaml | 3 --- ULogViewer/Strings/zh-TW.axaml | 3 --- 7 files changed, 29 insertions(+), 39 deletions(-) diff --git a/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml b/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml index 4745e51d..43ab8610 100644 --- a/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml +++ b/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml @@ -233,9 +233,6 @@ - @@ -256,9 +253,6 @@ - diff --git a/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml.cs b/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml.cs index 77d03e95..563fc4a9 100644 --- a/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml.cs +++ b/ULogViewer/Controls/LogDataSourceOptionsDialog.axaml.cs @@ -139,8 +139,14 @@ public LogDataSourceOptionsDialog() this.encodingComboBox = this.Get(nameof(encodingComboBox)); this.environmentVariablesListBox = this.Get(nameof(environmentVariablesListBox)); this.fileNameTextBox = this.Get(nameof(fileNameTextBox)); - this.formatJsonDataSwitch = this.Get(nameof(formatJsonDataSwitch)); - this.formatXmlDataSwitch = this.Get(nameof(formatXmlDataSwitch)); + this.formatJsonDataSwitch = this.Get(nameof(formatJsonDataSwitch)).Also(it => + { + it.IsCheckedChanged += this.OnFormatDataSwitchIsCheckedChanged; + }); + this.formatXmlDataSwitch = this.Get(nameof(formatXmlDataSwitch)).Also(it => + { + it.IsCheckedChanged += this.OnFormatDataSwitchIsCheckedChanged; + }); this.includeStderrSwitch = this.Get(nameof(includeStderrSwitch)); this.ipAddressTextBox = this.Get(nameof(ipAddressTextBox)); this.isResourceOnAzureSwitch = this.Get(nameof(isResourceOnAzureSwitch)); @@ -607,9 +613,7 @@ void OnEditorControlPropertyChanged(object? sender, AvaloniaPropertyChangedEvent // Called when check state of one of switch for formatting data changed. void OnFormatDataSwitchIsCheckedChanged(object? sender, RoutedEventArgs e) { - if (sender is not ToggleSwitch toggleSwitch - || !toggleSwitch.IsEnabled - || this.GetValue(IsFileNameSupportedProperty)) + if (sender is not ToggleSwitch toggleSwitch || !toggleSwitch.IsEnabled) return; if (!toggleSwitch.IsChecked.GetValueOrDefault()) { @@ -726,18 +730,9 @@ protected override void OnOpening(EventArgs e) this.initFocusedControl ??= this.fileNameTextBox; } if (this.GetValue(IsFormatJsonDataSupportedProperty)) - { this.formatJsonDataSwitch.IsChecked = options.FormatJsonData; - this.formatJsonDataSwitch.IsCheckedChanged += this.OnFormatDataSwitchIsCheckedChanged; - } - if (this.GetValue(IsFormatXmlDataSupportedProperty)) - { - if (!this.GetValue(IsFormatJsonDataSupportedProperty) || !options.FormatJsonData) - this.formatXmlDataSwitch.IsChecked = options.FormatXmlData; - else - this.formatXmlDataSwitch.IsEnabled = false; - this.formatXmlDataSwitch.IsCheckedChanged += this.OnFormatDataSwitchIsCheckedChanged; - } + if (this.GetValue(IsFormatXmlDataSupportedProperty)) + this.formatXmlDataSwitch.IsChecked = options.FormatXmlData; if (this.GetValue(IsIncludeStandardErrorSupportedProperty)) this.includeStderrSwitch.IsChecked = options.IncludeStandardError; if (this.IsWorkingDirectorySupported) diff --git a/ULogViewer/Logs/DataSources/FileLogDataSource.cs b/ULogViewer/Logs/DataSources/FileLogDataSource.cs index 25c2107f..f5fb3c0a 100644 --- a/ULogViewer/Logs/DataSources/FileLogDataSource.cs +++ b/ULogViewer/Logs/DataSources/FileLogDataSource.cs @@ -82,15 +82,18 @@ protected override void OnReaderClosed() this.Logger.LogWarning("Use temp file '{tempFilePath}'", tempFilePath); return new FileStream(tempFilePath ?? fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite).Let(stream => { - var reader = Path.GetExtension(fileName).ToLower() switch + TextReader reader; + if (options.FormatJsonData) + reader = new FormattedJsonTextReader(new StreamReader(stream, encoding)); + else { - ".gz" => new GZipStream(stream, CompressionMode.Decompress).Let(gzipStream => - new StreamReader(gzipStream, encoding)), - ".json" => options.FormatJsonData - ? new FormattedJsonTextReader(new StreamReader(stream, encoding)) - : new StreamReader(stream, encoding), - _ => (TextReader)new StreamReader(stream, encoding), - }; + reader = Path.GetExtension(fileName).ToLower() switch + { + ".gz" => new GZipStream(stream, CompressionMode.Decompress).Let(gzipStream => + new StreamReader(gzipStream, encoding)), + _ => (TextReader)new StreamReader(stream, encoding), + }; + } this.tempFilePath = tempFilePath; result = LogDataSourceState.ReaderOpened; return reader; diff --git a/ULogViewer/Logs/DataSources/ILogDataSourceProvider.cs b/ULogViewer/Logs/DataSources/ILogDataSourceProvider.cs index 5dac4fb4..6b1867bf 100644 --- a/ULogViewer/Logs/DataSources/ILogDataSourceProvider.cs +++ b/ULogViewer/Logs/DataSources/ILogDataSourceProvider.cs @@ -467,6 +467,13 @@ public static LogDataSourceOptions Load(JsonElement jsonObject) break; } } + if (options.FormatClefData) + { + options.FormatJsonData = false; + options.FormatXmlData = false; + } + else if (options.FormatJsonData) + options.FormatXmlData = false; return options; } finally diff --git a/ULogViewer/Strings/Default.axaml b/ULogViewer/Strings/Default.axaml index 17794280..c48bed19 100644 --- a/ULogViewer/Strings/Default.axaml +++ b/ULogViewer/Strings/Default.axaml @@ -490,11 +490,8 @@ return False The environment variables '{0}' is already added before. File path Format CLEF data when reading - Available only for file with .clef extension. Format JSON data when reading - Available only for file with .json extension. Format XML data when reading - Available only for file with .xml extension. Include Standard Error (stderr) IP address This is the resource on Azure diff --git a/ULogViewer/Strings/zh-CN.axaml b/ULogViewer/Strings/zh-CN.axaml index b54929d3..460843f7 100644 --- a/ULogViewer/Strings/zh-CN.axaml +++ b/ULogViewer/Strings/zh-CN.axaml @@ -474,11 +474,8 @@ return False 环境变量 '{0}' 先前已加入。 文件路径 读取时格式化 CLEF 资料 - 仅针对扩展名为 .clef 之文件有效。 读取时格式化 JSON 资料 - 仅针对扩展名为 .json 之文件有效。 读取时格式化 XML 资料 - 仅针对扩展名为 .xml 之文件有效。 包含标准错误输出 (stderr) IP 地址 这是 Azure 上的资源 diff --git a/ULogViewer/Strings/zh-TW.axaml b/ULogViewer/Strings/zh-TW.axaml index 7fda94d7..fd1783ef 100644 --- a/ULogViewer/Strings/zh-TW.axaml +++ b/ULogViewer/Strings/zh-TW.axaml @@ -467,11 +467,8 @@ return False 環境變數 '{0}' 先前已加入。 檔案路徑 讀取時格式化 CLEF 資料 - 僅針對副檔名為 .clef 之檔案有效。 讀取時格式化 JSON 資料 - 僅針對副檔名為 .json 之檔案有效。 讀取時格式化 XML 資料 - 僅針對副檔名為 .xml 之檔案有效。 包含標準錯誤輸出 (stderr) IP 位址 這是 Azure 上的資源