From b54997701d9b33354c9dfc962336becbfc1d3afe Mon Sep 17 00:00:00 2001 From: tmokmss Date: Sun, 14 Oct 2018 11:35:03 +0900 Subject: [PATCH] add error logging feature while converting --- Osu2Saber.sln | 8 ++--- Osu2Saber/Model/BatchProcessor.cs | 38 ++++++++++---------- Osu2Saber/Model/Logger.cs | 60 +++++++++++++++++++++++++++++++ Osu2Saber/Osu2Saber.csproj | 1 + 4 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 Osu2Saber/Model/Logger.cs diff --git a/Osu2Saber.sln b/Osu2Saber.sln index 05cf6bf..5137401 100644 --- a/Osu2Saber.sln +++ b/Osu2Saber.sln @@ -14,14 +14,14 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.AllDebug|Any CPU.ActiveCfg = Debug|Any CPU - {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.AllDebug|Any CPU.Build.0 = Debug|Any CPU + {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.AllDebug|Any CPU.ActiveCfg = AllDebug|Any CPU + {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.AllDebug|Any CPU.Build.0 = AllDebug|Any CPU {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD82070E-45D4-4CDD-ACFB-CF0FD1A46484}.Release|Any CPU.Build.0 = Release|Any CPU - {92F06458-CCF1-4574-BE26-A9FFE3B77310}.AllDebug|Any CPU.ActiveCfg = Debug|Any CPU - {92F06458-CCF1-4574-BE26-A9FFE3B77310}.AllDebug|Any CPU.Build.0 = Debug|Any CPU + {92F06458-CCF1-4574-BE26-A9FFE3B77310}.AllDebug|Any CPU.ActiveCfg = AllDebug|Any CPU + {92F06458-CCF1-4574-BE26-A9FFE3B77310}.AllDebug|Any CPU.Build.0 = AllDebug|Any CPU {92F06458-CCF1-4574-BE26-A9FFE3B77310}.Debug|Any CPU.ActiveCfg = Release|Any CPU {92F06458-CCF1-4574-BE26-A9FFE3B77310}.Debug|Any CPU.Build.0 = Release|Any CPU {92F06458-CCF1-4574-BE26-A9FFE3B77310}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Osu2Saber/Model/BatchProcessor.cs b/Osu2Saber/Model/BatchProcessor.cs index fb49a1b..fc4fe60 100644 --- a/Osu2Saber/Model/BatchProcessor.cs +++ b/Osu2Saber/Model/BatchProcessor.cs @@ -1,8 +1,7 @@ using Prism.Mvvm; +using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Linq; -using System.Threading; using System.Threading.Tasks; namespace Osu2Saber.Model @@ -11,6 +10,7 @@ class BatchProcessor : BindableBase { object progressLock = new object(); double progress; + Logger logger; public string[] TargetFiles { private set; get; } public string WorkDir { private set; get; } @@ -30,39 +30,41 @@ public BatchProcessor(string[] targetFiles, string workDir) { TargetFiles = targetFiles; WorkDir = workDir; + logger = new Logger(); } public Task BatchProcess() { - var tasks = new List(); - for (var i = 0; i < TargetFiles.Length; i++) - { - var x = i; - var task = Task.Run(() => Process(x)); - tasks.Add(task); - } + var tasks = TargetFiles.Select(file => Task.Run(() => Process(file))); return Task.WhenAll(tasks); } - void Process(int index) + void Process(string oszPath) { - var oszp = DecompressOsz(index); - if (oszp == null) return; + try + { + var oszp = DecompressOsz(oszPath); + if (oszp == null) return; - var o2b = ConvertBeatmap(oszp); - ConvertImgAudio(o2b); + var o2b = ConvertBeatmap(oszp); + ConvertImgAudio(o2b); + } + catch (Exception e) + { + logger.AddException(e, oszPath); + logger.Write(); + } } - OszProcessor DecompressOsz(int index) + OszProcessor DecompressOsz(string oszPath) { OszProcessor.WorkDir = WorkDir; - var oszPath = TargetFiles[index]; if (!oszPath.EndsWith("osz") && !oszPath.EndsWith("zip")) return null; var oszp = new OszProcessor(oszPath); if (oszp.OsuFiles.Length == 0) return null; - + ReportProgress(0.3); return oszp; } @@ -95,7 +97,7 @@ void ConvertImgAudio(Osu2BsConverter o2b) void ReportProgress(double add) { - lock(progressLock) + lock (progressLock) { Progress += add; } diff --git a/Osu2Saber/Model/Logger.cs b/Osu2Saber/Model/Logger.cs new file mode 100644 index 0000000..c5400c2 --- /dev/null +++ b/Osu2Saber/Model/Logger.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Osu2Saber.Model +{ + class Logger + { + List errors; + string fileName; + + public Logger() + { + errors = new List(); + fileName = $"Log_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt"; + } + + public void AddException(Exception e, string filePath) + { + lock (errors) + { + var error = new ErrorInfo { Exception = e, ArchiveName = Path.GetFileName(filePath) }; + errors.Add(error); + } + } + + public void Write() + { + lock (errors) + { + using (var writer = new StreamWriter(fileName, true)) + { + writer.Write(BuildErrorString()); + } + errors.Clear(); + } + } + + string BuildErrorString() + { + var sb = new StringBuilder(); + foreach (var error in errors) + { + sb.Append($"Error while converting - {error.ArchiveName}\n"); + sb.Append(error.Exception.ToString()); + sb.Append("\n\n"); + } + return sb.ToString(); + } + + class ErrorInfo + { + public Exception Exception; + public string ArchiveName; + } + } +} diff --git a/Osu2Saber/Osu2Saber.csproj b/Osu2Saber/Osu2Saber.csproj index 217e091..468e40f 100644 --- a/Osu2Saber/Osu2Saber.csproj +++ b/Osu2Saber/Osu2Saber.csproj @@ -124,6 +124,7 @@ +