From ad2e5cb7da1a1a23a956f1291eadc21800c43068 Mon Sep 17 00:00:00 2001 From: hhhxiao <1417599491@qq.com> Date: Mon, 22 Jul 2024 21:05:47 +0800 Subject: [PATCH] Feat: Show log in error window --- MainWindow.xaml.cs | 77 ++++++++++++++++++++++++++++++++------------ core/DBTool.cs | 6 ++-- core/Glossary.cs | 2 +- core/LangFile.cs | 8 ++--- core/TextExporter.cs | 12 +++---- core/Translator.cs | 9 ++++-- core/Utils.cs | 2 +- 7 files changed, 77 insertions(+), 39 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index eadbe6f..ae3b5ee 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -1,19 +1,15 @@ -using System.Text; -using System.Windows; +using System.Windows; using System.Collections.ObjectModel; using NLog; using SoulsModTranslator.core; -using DataFormats = System.Windows.DataFormats; -using DragDropEffects = System.Windows.DragDropEffects; -using DragEventArgs = System.Windows.DragEventArgs; -using MessageBox = System.Windows.Forms.MessageBox; using System.IO; using AdonisUI.Controls; using Button = System.Windows.Controls.Button; -using MessageBoxButton = AdonisUI.Controls.MessageBoxButton; using MessageBoxImage = AdonisUI.Controls.MessageBoxImage; using System.Diagnostics; using System.Windows.Navigation; +using Org.BouncyCastle.Bcpg.Sig; +using NLog.Targets; namespace SoulsModTranslator { @@ -26,16 +22,43 @@ public partial class MainWindow : AdonisWindow private static readonly string GlossaryPath = Path.Combine(Directory.GetCurrentDirectory(), "glossaries"); private static readonly string SoftwareName = "魂游MOD翻译工具 v2.7"; + private static MemoryTarget MemoryTarget = new MemoryTarget + { + Name = "memory", + Layout = "[${level}] ${message}" + }; + private static void ShowTaskResult(bool success, string succMsg, string failMsg) { - var caption = success ? "提示" : "错误"; - var icon = success ? MessageBoxImage.Information : MessageBoxImage.Error; - AdonisUI.Controls.MessageBox.Show(success ? succMsg : failMsg, caption, MessageBoxButton.OK, icon); + // var caption = success ? "提示" : "错误"; + // var icon = success ? MessageBoxImage.Information : MessageBoxImage.Error; + if (success) + { + AdonisUI.Controls.MessageBox.Show(success ? succMsg : failMsg, "提示", + AdonisUI.Controls.MessageBoxButton.OK, MessageBoxImage.Information); + return; + } + + var log = String.Join(Environment.NewLine, MemoryTarget.Logs.ToArray()); + var messageBox = new MessageBoxModel + { + Text = failMsg + "\n\n" + log, + Caption = "错误", + Icon = MessageBoxImage.Information, + Buttons = + new[] + { + AdonisUI.Controls.MessageBoxButtons.Custom("复制日志并前往Github反馈", "github"), + AdonisUI.Controls.MessageBoxButtons.Custom("复制日志并前往b站反馈", "bilibili"), + AdonisUI.Controls.MessageBoxButtons.Custom("关闭", "close"), + }, + }; + AdonisUI.Controls.MessageBox.Show(messageBox); } private static List LoadDbFiles() { - Logger.Info($"数据库路径是:{DbPath}"); + Logger.Info($"数据库根目录:{DbPath}"); if (!Directory.Exists(DbPath)) { return new List(); @@ -45,6 +68,13 @@ private static List LoadDbFiles() return (from file in files where Path.GetExtension(file).Equals(".json") select Path.GetFileName(file)).ToList(); } + public void CreateArrayLogger() + { + var config = LogManager.Configuration; + config.AddTarget(MemoryTarget); + config.AddRule(LogLevel.Info, LogLevel.Fatal, MemoryTarget); + LogManager.Configuration = config; + } private void SwitchTab(string name) { @@ -72,7 +102,9 @@ private void SwitchTab(string name) public MainWindow() { - Logger.Info("\n\n\n===========================New Instance==================================="); + + Logger.Info("\n\n===========================New Instance==================================="); + CreateArrayLogger(); InitializeComponent(); // Glossaries = new ObservableCollection(); @@ -87,7 +119,6 @@ public MainWindow() SwitchTab("Translate"); this.Title = SoftwareName; AboutTitleLabel.Content = SoftwareName + " By hhhxiao"; - if (DbList.Count != 0) return; ShowTaskResult(false, "", "找不到数据库文件,请检查软件完整性"); this.Close(); @@ -138,9 +169,11 @@ private async void MergeDbBtn_OnClick(object sender, RoutedEventArgs e) if (result != System.Windows.Forms.DialogResult.OK) return; //save path var savePath = ""; - var saveDialog = new SaveFileDialog(); - saveDialog.Filter = "Json文件(*.json)|*"; - saveDialog.FileName = "Untitled.json"; + var saveDialog = new SaveFileDialog + { + Filter = "Json文件(*.json)|*", + FileName = "Untitled.json" + }; var saveResult = saveDialog.ShowDialog(); if (saveResult != System.Windows.Forms.DialogResult.OK) return; savePath = saveDialog.FileName; @@ -157,10 +190,12 @@ private void TranslateTab_OnClick(object sender, RoutedEventArgs e) private void GlossaryAdd_onClick(object sender, RoutedEventArgs e) { - var dialog = new System.Windows.Forms.OpenFileDialog(); - dialog.InitialDirectory = GlossaryPath; - dialog.Filter = "Json 文件 (*.json)|*.json|所有文件|*.*"; - dialog.Multiselect = true; + var dialog = new System.Windows.Forms.OpenFileDialog + { + InitialDirectory = GlossaryPath, + Filter = "Json 文件 (*.json)|*.json|所有文件|*.*", + Multiselect = true + }; var result = dialog.ShowDialog(); if (result != System.Windows.Forms.DialogResult.OK) return; foreach (var item in dialog.FileNames) @@ -255,7 +290,7 @@ private async void ExportBtn_onClick(object sender, RoutedEventArgs e) dialog.FileName = exportAsExcel ? "text.xlsx" : "text.txt"; if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; var result = await Task.Run(() => TextExporter.Export(dialog.FileName, res, exportAsExcel, resort, false)); - Logger.Info($"成功导出未翻译文本 {dialog.FileName}"); + Logger.Info($"成功导出未翻译文本:{dialog.FileName}"); ShowTaskResult(result, "导出成功", "导出失败"); } diff --git a/core/DBTool.cs b/core/DBTool.cs index a1667b5..f5dfca6 100644 --- a/core/DBTool.cs +++ b/core/DBTool.cs @@ -44,7 +44,7 @@ public static bool CreateDb(string srcPath, string destPath, string savePath) var sourceDict = ReadKeyValue(srcLang); var destDict = ReadKeyValue(destLang); - Logger.Info($"从源语言中读取{sourceDict.Count}个文本,从目标语言中读取{destDict.Count}个文本"); + Logger.Info($"从源语言中读取{sourceDict.Count}条文本,从目标语言中读取{destDict.Count}条文本"); var dbDict = new Dictionary(); foreach (var (key, srcValue) in sourceDict) { @@ -68,7 +68,7 @@ public static bool MergeDB(string[] fileNames, string savePath) foreach (var file in fileNames) { var db = Utils.LoadJsonToMap(file); - Logger.Info($" - {file}: {db.Count}"); + Logger.Info($" - 文件:{file}: 大小:{db.Count}"); count += db.Count; foreach (var kv in db) { @@ -79,7 +79,7 @@ public static bool MergeDB(string[] fileNames, string savePath) } } - Logger.Info($"{count}->{newDB.Count}"); + Logger.Info($"新数据库的大小:{count}->{newDB.Count}"); Utils.SaveMapAsJson(newDB, savePath); return true; } diff --git a/core/Glossary.cs b/core/Glossary.cs index bb05a7f..7d8a322 100644 --- a/core/Glossary.cs +++ b/core/Glossary.cs @@ -31,7 +31,7 @@ class RegexMatchRule public Glossary(bool caseInsensitive) { _IgnoreCase = caseInsensitive; - Logger.Info($"忽视术语表大小写:{_IgnoreCase}"); + Logger.Debug($"忽视术语表大小写:{_IgnoreCase}"); } public bool Load(IEnumerable glossaryFileNames) diff --git a/core/LangFile.cs b/core/LangFile.cs index b19e881..23d248b 100644 --- a/core/LangFile.cs +++ b/core/LangFile.cs @@ -25,7 +25,7 @@ public bool Load(string langRootPath) { if (!Directory.Exists(langRootPath)) { - Logger.Error($"目录不存在: {langRootPath}"); + Logger.Error($"目录不存在:{langRootPath}"); return false; } @@ -36,7 +36,7 @@ public bool Load(string langRootPath) foreach (var bndFile in info.GetFiles()) { if (!bndFile.Name.EndsWith(".msgbnd.dcx")) continue; - Logger.Info($"发现 msgbnd.dcx 文件: {bndFile.Name}"); + Logger.Info($"发现msgbnd.dcx文件: {bndFile.Name}"); if (bndFile.Name == "ngword.msgbnd.dcx") continue; //跳过这个ng单词 try @@ -46,7 +46,7 @@ public bool Load(string langRootPath) { Logger.Error($"发现重复的FMG语言文件,文件夹{langRootPath}中是否存在与{bndFile.Name}相同的dcx" + $"文件(比如黑暗之魂III和艾尔登法环)请根据需求删除(或修改后缀名)其中一个"); - Logger.Error($"推荐做法为新建文件夹并将除了item_dlc02.msgbnd.dcx以及item_dlc02.msgbnd.dcx之外的dcx文件移动到该文件夹内"); + Logger.Error($"推荐做法为新建文件夹并将除了item_dlc02.msgbnd.dcx以及menu_dlc02.msgbnd.dcx之外的dcx文件移动到该文件夹内"); return false; } @@ -54,7 +54,7 @@ public bool Load(string langRootPath) } catch (Exception e) { - Logger.Error("不合法的dcx文件格式。 " + e.Message); + Logger.Error("不合法的dcx文件格式.\n\n" + e.Message); return false; } } diff --git a/core/TextExporter.cs b/core/TextExporter.cs index ccabe05..87cbdae 100644 --- a/core/TextExporter.cs +++ b/core/TextExporter.cs @@ -123,12 +123,12 @@ private static void CompressedExport(Stream file, ExportResult exportResult) public static bool Export(string fileName, ExportResult exportResult, bool excel, bool resort, bool compressed) { - Logger.Info("Exporter:" + fileName); - Logger.Info($" Phase list size: {exportResult.PhaseList.Count}"); - Logger.Info($" 导出句子条数: {exportResult.SentenceList.Count}"); - Logger.Info(" 是否导出为Excel: " + excel); - Logger.Info(" 是否重排: " + resort); - Logger.Info(" 是否压缩存储(未实装): " + compressed); + Logger.Info("开始导出:" + fileName); + Logger.Info($" - 导出短语条数: {exportResult.PhaseList.Count}"); + Logger.Info($" - 导出句子条数: {exportResult.SentenceList.Count}"); + Logger.Info(" - 是否导出为Excel: " + excel); + Logger.Info(" - 是否重排: " + resort); + Logger.Info(" - 是否压缩存储(未实装): " + compressed); if (resort) { exportResult.SentenceList = exportResult.SentenceList.OrderByDescending(item => Utils.GetChineseCharacterRatio(item.Text)) diff --git a/core/Translator.cs b/core/Translator.cs index a04466a..3d0f231 100644 --- a/core/Translator.cs +++ b/core/Translator.cs @@ -136,7 +136,7 @@ private static Action CreateTraverser(DB db, Action LoadJsonToMap(string fileName) } catch (Exception e) { - Logger.Error($"无法读取Json文件{fileName}", e); + Logger.Error($"无法读取Json文件:{fileName}", e); } return dict ?? new Dictionary(); }