-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProgressReporter.cs
35 lines (29 loc) · 1001 Bytes
/
ProgressReporter.cs
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
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace ShibuyaTools.Core;
public class ProgressReporter(ILogger logger)
{
private readonly Stopwatch stopwatch = Stopwatch.StartNew();
public void Restart() => stopwatch.Restart();
public void ReportProgress(ProgressPayload<long> progress)
{
if (stopwatch.Elapsed.TotalSeconds >= 1)
{
logger.LogDebug(
"written {count} of {total} ({progress:0.00}%)",
FormatLength(progress.Position),
FormatLength(progress.Total),
progress.Position * 100.0 / progress.Total);
stopwatch.Restart();
}
}
private static string FormatLength(float length)
{
if (length < 1024) return $"{length:0.00}B";
length /= 1024;
if (length < 1024) return $"{length:0.00}KB";
length /= 1024;
if (length < 1024) return $"{length:0.00}MB";
return $"{length:0.00}GB";
}
}