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,32 +51,47 @@ 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 {
60- await Parallel . ForEachAsync ( workingFiles , parallelOptions ,
68+ await Parallel . ForEachAsync ( workingFiles , parallelOptions ,
6169 ( file , ctx ) =>
6270 {
6371 ctx . ThrowIfCancellationRequested ( ) ;
6472
6573 return new ValueTask ( PauseAndProcessFile ( file , totalFilesSize , cancellationTokenSource . Token , progressMonitor ) ) ;
6674 } ) . ConfigureAwait ( false ) ;
6775 }
68- catch ( OperationCanceledException ) { return false ; }
69- catch ( Exception ) { return false ; }
76+ catch ( OperationCanceledException ) {
77+ CompactorLog . CompressionCanceled ( _logger ) ;
78+ return false ;
79+ }
80+ catch ( Exception ex ) {
81+ CompactorLog . CompressionFailed ( _logger , ex . Message ) ;
82+ return false ;
83+ }
84+ finally { sw . Stop ( ) ; }
85+
7086
87+
88+ CompactorLog . CompressionCompleted ( _logger , Math . Round ( sw . Elapsed . TotalSeconds , 3 ) ) ;
7189 return true ;
7290 }
7391
7492 private async Task PauseAndProcessFile ( FileDetails file , long totalFilesSize , CancellationToken token , IProgress < CompressionProgress > progressMonitor )
7593 {
94+ CompactorLog . ProcessingFile ( _logger , file . FileName , file . UncompressedSize ) ;
7695
7796 await pauseSemaphore . WaitAsync ( token ) . ConfigureAwait ( false ) ;
7897 pauseSemaphore . Release ( ) ;
@@ -94,7 +113,7 @@ private async Task PauseAndProcessFile(FileDetails file, long totalFilesSize, Ca
94113 }
95114 catch ( Exception ex )
96115 {
97- Debug . WriteLine ( ex . Message ) ;
116+ CompactorLog . FileCompressionFailed ( _logger , filePath , ex . Message ) ;
98117 return null ;
99118 }
100119 }
@@ -103,7 +122,7 @@ public async Task<IEnumerable<FileDetails>> BuildWorkingFilesList()
103122 {
104123 uint clusterSize = SharedMethods . GetClusterSize ( workingDirectory ) ;
105124
106- var analyser = new Analyser ( workingDirectory ) ;
125+ var analyser = new Analyser ( workingDirectory , NullLogger < Analyser > . Instance ) ;
107126 var ret = await analyser . AnalyseFolder ( cancellationTokenSource . Token ) ;
108127
109128 var filesList = analyser . FileCompressionDetailsList
@@ -123,13 +142,15 @@ public async Task<IEnumerable<FileDetails>> BuildWorkingFilesList()
123142
124143 public void Pause ( )
125144 {
145+ CompactorLog . CompressionPaused ( _logger ) ;
126146 pauseSemaphore . Wait ( cancellationTokenSource . Token ) ;
127147 }
128148
129149
130150 public void Resume ( )
131151 {
132152 if ( pauseSemaphore . CurrentCount == 0 ) pauseSemaphore . Release ( ) ;
153+ CompactorLog . CompressionResumed ( _logger ) ;
133154 }
134155
135156
0 commit comments