Skip to content

Commit

Permalink
add error logging feature while converting
Browse files Browse the repository at this point in the history
  • Loading branch information
tmokmss committed Oct 14, 2018
1 parent 2ba0f15 commit b549977
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Osu2Saber.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions Osu2Saber/Model/BatchProcessor.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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; }
Expand All @@ -30,39 +30,41 @@ public BatchProcessor(string[] targetFiles, string workDir)
{
TargetFiles = targetFiles;
WorkDir = workDir;
logger = new Logger();
}

public Task BatchProcess()
{
var tasks = new List<Task>();
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;
}
Expand Down Expand Up @@ -95,7 +97,7 @@ void ConvertImgAudio(Osu2BsConverter o2b)

void ReportProgress(double add)
{
lock(progressLock)
lock (progressLock)
{
Progress += add;
}
Expand Down
60 changes: 60 additions & 0 deletions Osu2Saber/Model/Logger.cs
Original file line number Diff line number Diff line change
@@ -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<ErrorInfo> errors;
string fileName;

public Logger()
{
errors = new List<ErrorInfo>();
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;
}
}
}
1 change: 1 addition & 0 deletions Osu2Saber/Osu2Saber.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
</Compile>
<Compile Include="Model\Algorithm\ConvertAlgorithm.cs" />
<Compile Include="Model\BatchProcessor.cs" />
<Compile Include="Model\Logger.cs" />
<Compile Include="Model\Mp3toOggConverter.cs" />
<Compile Include="Model\Osu2BsConverter.cs" />
<Compile Include="Model\OszProcessor.cs" />
Expand Down

0 comments on commit b549977

Please sign in to comment.