11
2+ using CompactGUI . Logging . Core ;
3+ using Microsoft . Extensions . Logging ;
4+ using Microsoft . Extensions . Logging . Abstractions ;
25using Microsoft . Win32 . SafeHandles ;
36using System . Collections . Concurrent ;
47using System . Diagnostics ;
@@ -23,13 +26,14 @@ public class Compactor : ICompressor, IDisposable
2326 private readonly SemaphoreSlim pauseSemaphore = new SemaphoreSlim ( 1 , 2 ) ;
2427 private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource ( ) ;
2528
29+ private ILogger < Compactor > _logger ;
2630
27- public Compactor ( string folderPath , WOFCompressionAlgorithm compressionLevel , string [ ] excludedFileTypes )
31+ public Compactor ( string folderPath , WOFCompressionAlgorithm compressionLevel , string [ ] excludedFileTypes , ILogger < Compactor > ? logger = null )
2832 {
2933 workingDirectory = folderPath ;
3034 excludedFileExtensions = new HashSet < string > ( excludedFileTypes ) ;
3135 wofCompressionAlgorithm = compressionLevel ;
32-
36+ _logger = logger ?? NullLogger < Compactor > . Instance ;
3337 InitializeCompressionInfoPointer ( ) ;
3438 }
3539
@@ -47,14 +51,18 @@ public async Task<bool> RunAsync(List<string> filesList, IProgress<CompressionPr
4751 {
4852 if ( cancellationTokenSource . IsCancellationRequested ) { return false ; }
4953
54+ CompactorLog . BuildingWorkingFilesList ( _logger , workingDirectory ) ;
5055 var workingFiles = await BuildWorkingFilesList ( ) . ConfigureAwait ( false ) ;
5156 long totalFilesSize = workingFiles . Sum ( ( f ) => f . UncompressedSize ) ;
5257
5358 totalProcessedBytes = 0 ;
5459
55- if ( maxParallelism <= 0 ) maxParallelism = Environment . ProcessorCount ;
60+ var sw = Stopwatch . StartNew ( ) ;
61+
62+ if ( maxParallelism <= 0 ) maxParallelism = Environment . ProcessorCount ;
5663 ParallelOptions parallelOptions = new ( ) { MaxDegreeOfParallelism = maxParallelism , CancellationToken = cancellationTokenSource . Token } ;
5764
65+ CompactorLog . StartingCompression ( _logger , workingDirectory , wofCompressionAlgorithm . ToString ( ) , maxParallelism ) ;
5866 try
5967 {
6068 await Parallel . ForEachAsync ( workingFiles , parallelOptions ,
@@ -68,11 +76,15 @@ await Parallel.ForEachAsync(workingFiles, parallelOptions,
6876 catch ( OperationCanceledException ) { return false ; }
6977 catch ( Exception ) { return false ; }
7078
79+
80+ sw . Stop ( ) ;
81+ CompactorLog . CompressionCompleted ( _logger , Math . Round ( sw . Elapsed . TotalSeconds , 3 ) ) ;
7182 return true ;
7283 }
7384
7485 private async Task PauseAndProcessFile ( FileDetails file , long totalFilesSize , CancellationToken token , IProgress < CompressionProgress > progressMonitor )
7586 {
87+ CompactorLog . ProcessingFile ( _logger , file . FileName , file . UncompressedSize ) ;
7688
7789 await pauseSemaphore . WaitAsync ( token ) . ConfigureAwait ( false ) ;
7890 pauseSemaphore . Release ( ) ;
@@ -94,7 +106,7 @@ private async Task PauseAndProcessFile(FileDetails file, long totalFilesSize, Ca
94106 }
95107 catch ( Exception ex )
96108 {
97- Debug . WriteLine ( ex . Message ) ;
109+ CompactorLog . FileCompressionFailed ( _logger , filePath , ex . Message ) ;
98110 return null ;
99111 }
100112 }
@@ -123,18 +135,21 @@ public async Task<IEnumerable<FileDetails>> BuildWorkingFilesList()
123135
124136 public void Pause ( )
125137 {
138+ CompactorLog . CompressionPaused ( _logger ) ;
126139 pauseSemaphore . Wait ( cancellationTokenSource . Token ) ;
127140 }
128141
129142
130143 public void Resume ( )
131144 {
145+ CompactorLog . CompressionResumed ( _logger ) ;
132146 if ( pauseSemaphore . CurrentCount == 0 ) pauseSemaphore . Release ( ) ;
133147 }
134148
135149
136150 public void Cancel ( )
137151 {
152+ CompactorLog . CompressionCanceled ( _logger ) ;
138153 Resume ( ) ;
139154 cancellationTokenSource . Cancel ( ) ;
140155 }
0 commit comments