classes/New-LogObject.ps1

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
function New-LogObject {
    #####################
    # LogObject Class #
    #####################
    # Properties
    $LogObject = [PSCustomObject]@{
        'Logfile' = ''
        'Options' = ''
        'Status' = ''
        'Metadata' = ''
        'PrivateMethods'= [scriptblock]{
            ##########################
            # Object Private Methods
            ##########################
            function Rotate-Main {
                try {
                    # Rotate main file
                    # E.g. D:/console.log -> D:/console.log.1
                    if ($copy) {
                        Write-Verbose "Copying $my_fullname to $my_previous_fullname"

                        if (Test-Path $my_previous_fullname) {
                            # File exists.
                            Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
                        }else {
                            if (!$WhatIf) {
                                Copy-Item $my_fullname $my_previous_fullname -ErrorAction Stop
                            }
                            if ($copytruncate) {
                                Write-Verbose "Truncating $my_fullname"
                                if (!$WhatIf) {
                                    Clear-Content $my_fullname
                                }
                            }else {
                                Write-Verbose "Not truncating $my_fullname"
                            }
                            return $true
                        }
                    }else {
                        Write-Verbose "Renaming $my_fullname to $my_previous_fullname"
                        if (Test-Path $my_previous_fullname) {
                            # File exists.
                            Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
                        }else {
                            if (!$WhatIf) {
                                Move-Item $my_fullname $my_previous_fullname -Force
                            }
                            if ($create) {
                                Write-Verbose "Creating new log file $my_fullname"
                                if (!$WhatIf) {
                                    $newitem = New-Item $my_fullname -ItemType File | Out-Null
                                    if ($newitem) {

                                    }
                                }
                            }
                            return $true
                        }
                    }
                }catch {
                    throw
                }

                $false
            }

            function Rotate-Previous-Files-Incremental
            {
                # This function is only used for incremental index extensions, E.g. console.log.1 -> console.log.2. It is not used for date extensions.
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [int]$rotate_compressed_files
                ,
                    [Parameter(Mandatory=$True,Position=4)]
                    [int]$max_index = 0
                )

                [Regex]$regex = if ($rotate_compressed_files) {
                                    $my_previous_compressed_captures_regex
                                }else {
                                    $my_previous_noncompressed_captures_regex
                                }
                $previous_name_prototype =  if ($compress) {
                                                $my_previous_compressed_name_prototype
                                            }else {
                                                $my_previous_name_prototype
                                            }
                $match = $regex.Match($previous_name_prototype)
                if ($match.success) {
                    $prefix = $match.Groups['prefix'].Value
                    $suffix = $match.Groups['suffix'].Value -as [int]
                    $extension = if ($match.Groups['extension']) { $match.Groups['extension'].Value } else { '' }
                    $compressextension = if ($match.Groups['compressextension']) { $match.Groups['compressextension'].Value } else { '' }

                    if ($suffix) {
                        $SLASH = [IO.Path]::DirectorySeparatorChar
                        foreach ($i in @($max_index..0)) {
                            # Construct filenames with their index, and extension if provided
                            # E.g. D:\console.log.5 or D:\console.log.5.7z
                            $source_fullName = Join-Path $my_previous_directory "$prefix.$i$extension$compressextension"
                            # E.g. D:\console.log.6 or D:\console.log.6.7z
                            $destination_fullName = Join-Path $my_previous_directory "$prefix.$($i+1)$extension$compressextension"

                            # Rename old logs
                            Write-Verbose "Renaming $source_fullName to $destination_fullName (rotatecount $rotate, logstart $start, i $i)"
                            if ($WhatIf) { continue }
                            if (Test-Path $source_fullName) {
                                try {
                                    Move-Item -Path $source_fullName -Destination $destination_fullName -Force
                                }catch {
                                    throw
                                }
                            }else {
                                Write-Verbose "Old log $source_fullName does not exist."
                            }
                        }
                    }
                }
            }

            function Rename-File-Within-Compressed-Archive {
                # TODO: Not using this function for now, because this always recreates an archive, dumping a lot to the disk.

                # Only needed for 7z for now
                if ($compresscmd -notmatch '7za?') {
                    return
                }

                Get-Files $my_previous_compressed_regex $my_previous_directory | & {
                    begin {
                    }process {
                        $directory =$_.Directory.FullName
                        $fullName = $_.FullName
                        $baseName = $_.BaseName

                        $params = @( 'rn', $fullName, '*', $baseName )
                        try {
                            Write-Verbose "Rename log inside compressed archive $fullName to $baseName"
                            if ($WhatIf) {
                                continue
                            }

                            $scriptblock = {
                                param ($cd, $cmd, [string[]]$params)
                                Set-Location $cd
                                Write-Output "cd: $cd"
                                Write-Output "cmd: $cmd"
                                Write-Output "params: "
                                $params | Out-String | ForEach-Object { Write-Output $_.Trim() }
                                & $cmd $params
                            }
                            $job = Start-Job -ScriptBlock $scriptblock -ArgumentList $directory,$compresscmd,$params -ErrorAction Stop
                            $output = Receive-Job -Job $job -Wait -ErrorAction Stop
                            if ($job.State -eq 'Failed') {
                                throw "Renaming failed because: $($job.ChildJobs[0].JobStateInfo.Reason.Message)"
                            }else {
                                Write-Verbose "Renaming log within compressed archive $fullName successful. Output: `n$output"
                            }
                        }catch {
                            #Write-Verbose "Renaming failed for log within compressed archive $fullName ."
                            Write-Verbose "Renaming log within compressed archive $fullName failed."
                            Write-Error -ErrorRecord $_ -ErrorAction Continue
                        }
                    }
                }
            }

            function Compress-File {
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [string]$compressed_fullname
                ,
                    [Parameter(Mandatory=$True,Position=1)]
                    [string]$filter
                )

                $compressed = $false

                # E.g. 7z.exe a -t7z D:\console.log.7z D:\console.log
                # E.g. gzip.exe D:\console.log
                $compressoptions = @( $compressoptions -split '\s' | Where-Object { $_.Trim() } )

                $params = if ($compresscmd -match '7z') {
                            $compressoptions + $compressed_fullname + $filter
                        }else {
                            $compressoptions + $filter
                        }
                # Remove empty parameters
                $params = $params | Where-Object { $_ }

                try {
                    Write-Verbose "Compressing log with: $compresscmd"
                    Write-Verbose "Compress command line: $compresscmd $( $params -join ' ' )"
                    if ($WhatIf) {
                        return
                    }

                    $output = & $compresscmd $params
                    if (Test-Path $compressed_fullname) {
                        Write-Verbose "Compression successful. Output: `n$output"

                        $compressed = $true
                    }else {
                        Write-Verbose "Compression failed. Output: `n$output"
                        throw "Compressed file was not created."
                    }

                    # TODO: Not using jobs for now, because they are slow.
                    <#
                    $scriptblock = {
                        param ($cd, $cmd, [string[]]$params)
                        Set-Location $cd
                        #Write-Output "cd: $cd"
                        #Write-Output "cmd: $cmd"
                        #Write-Output "params: "
                        #$params | Out-String | ForEach-Object { Write-Output $_.Trim() }
                        & $cmd $params
                    }
                    $job = Start-Job -ScriptBlock $scriptblock -ArgumentList $logfile.Directory.FullName,$compresscmd,$params -ErrorAction Stop
                    $output = Receive-Job -Job $job -Wait -ErrorAction Stop

                    if ($job.State -eq 'Failed') {
                        throw "Compression failed because: $($job.ChildJobs[0].JobStateInfo.Reason.Message)"
                    }else {
                        if (Test-Path $compressed_fullname) {
                            Write-Verbose "Compression successful. Output: `n$output"

                            $compressed = $true
                        }else {
                            Write-Verbose "Compression failed. Output: `n$output"
                            throw "Compressed file was not created."
                        }
                    }
                    #>

                }catch {
                    Write-Verbose "Compression failed."
                    throw
                }

                # Remove the previous file
                if ($compressed) {
                    if (Test-Path $filter) {
                        Purge-File $filter
                        #Write-Verbose "Removed $filter"
                    }
                }
            }

            function Uncompress-File {
                # TODO: Not using uncompress because we're not doing mail for now.
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [string]$compressed_fullname
                )

                $uncompressed = $false

                # E.g. Extract as a file: 7z.exe x -t7z D:\console.log.7z
                # E.g. Extract to stdout: 7z.exe x -so D:\console.log.7z
                $uncompressoptions = $uncompressoptions.Split(' ') | Where-Object { $_.Trim() }
                $params = $uncompressoptions + $compressed_fullname

                try {
                    $stdout = & $compresscmd $params
                    if ($stdout) {
                        Write-Verbose "Uncompression successful. Output: `n$output"

                        # Store the file
                        $stdout | Out-File -Encoding utf8

                        $uncompressed = $true
                    }else {
                        Write-Verbose "Uncompression failed. Output: `n$output"
                        throw "Uncompressed file was not created."
                    }

                    # TODO: Not using jobs for now, because they are slow.
                    <#
                    $scriptblock = {
                        param ($cd, $cmd, [string[]]$params)
                        Set-Location $cd
                        #Write-Output "cd: $cd"
                        #Write-Output "cmd: $cmd"
                        #Write-Output "params: "
                        #$params | Out-String | ForEach-Object { Write-Verbose $_.Trim() }
                        & $cmd $params
                    }
                    $job = Start-Job -ScriptBlock $scriptblock -ArgumentList (Get-Item $compressed_fullname).Directory.FullName,$uncompresscmd,$params -ErrorAction Stop
                    $output = Receive-Job -Job $job -Wait -ErrorAction Stop
                    if ($job.State -eq 'Failed') {
                        throw "Compression failed because: $($job.ChildJobs[0].JobStateInfo.Reason.Message)"
                    }else {
                        if (Test-Path $compressed_fullname) {
                            Write-Verbose "Uncompression successful. Output: `n$output"
                            $uncompressed = $true
                        }else {
                            Write-Verbose "Uncompression failed. Output: `n$output"
                            throw "Uncompressed file was not created."
                        }
                    }
                    #>

                }catch {
                    Write-Verbose "Uncompression failed."
                    throw
                }

                # Remove the compressed file
                if ($uncompressed) {
                    if (Test-Path $compressed_fullname) {
                        Remove-Item $compressed_fullname
                        Write-Verbose "Removed $compressed_fullname"
                    }
                }
            }

            function Notify-Purge {
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [string]$file_fullname
                )

                if ( !(Test-Path $file_fullname) ) {
                    Write-Verbose "log $file_fullname doesn't exist -- won't try to dispose of it "
                }
            }

            function Purge-File {
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [string]$file_fullname
                )

                if (Test-Path $file_fullname) {
                    Write-Verbose "Removing old log $file_fullname"

                    # Run preremove script
                    if ($preremove) {
                        Write-Verbose "Running preremove script"
                        try {
                            Start-Script $preremove $file_fullname -ErrorAction $CallerEA
                        }catch {
                            Write-Error "Failed to run preremove script." -ErrorAction Continue
                            throw
                        }
                    }

                    # Delete file
                    if (!$WhatIf) {
                        Remove-Item $file_fullname
                    }else {
                        # For debugging to simulate deleted file
                        $debug_my_prevfilespurged_fullnames.Add($file_fullname) | Out-Null
                    }
                }
            }

            function Remove-Old-Files {
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [AllowNull()]
                    [Array]$files
                ,
                    [Parameter(Mandatory=$True,Position=1)]
                    [ValidateRange(0, [int]::MaxValue)]
                    [int]$keep_count
                ,
                    [Parameter(Mandatory=$True,Position=2)]
                    [int]$oldest_is_first_when_name_sorted
                )

                $files_count =  if ($files) {
                                    $files.Count
                                }else {
                                    0
                                }
                if ($files_count) {
                    if ($keep_count -ge $files_count) {
                        # If keeping 365 copies, and I only have 5.
                        # If keeping 5 copies, and I have 5
                        Write-Verbose "No more old files to remove $($_.FullName)"
                        return
                    }

                    $oldfiles_count = $files_count - $keep_count
                    $oldfiles = if ($oldest_is_first_when_name_sorted) {
                        # Datetime. Exclude the last x items, when sorted by name ascending.
                        #$files | Sort-Object -Property Name | Select-Object -SkipLast $keep_count
                        $files | Sort-Object -Property Name | Select-Object -First $oldfiles_count
                    }else {
                        # Descending index. Exclude the last x items, when sorted by name descending.
                        #$files | Sort-Object { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) } -Descending | Select-Object -SkipLast $keep_count
                        $files | Sort-Object { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) } -Descending | Select-Object -First $oldfiles_count
                    }
                    $oldfiles | ForEach-Object {
                        Write-Verbose "Removing $($_.FullName)"
                        Purge-File $_.FullName
                    }
                }
            }

        }
        'HelperMethods' = [scriptblock]{
            ##########################
            # Object Private Helper Methods
            ##########################
            function Get-Files {
                param (
                    [Parameter(Mandatory=$True,Position=0)]
                    [string]$regex,
                    [Parameter(Mandatory=$True,Position=1)]
                    [string]$directory
                )
                Get-ChildItem $directory | Where-Object { $_.Name -match $regex }
            }
        }
    }
    # Methods
    $LogObject | Add-Member -Name 'New' -MemberType ScriptMethod -Value {
        <# Constructs a Log Object (hashtable) representing a rotatable log. File, options and metadata (keys) mapped to their data.
        @{
            'Logfile' = $logfile
            'Options' = @{
                'compress' = $true;
                'rotate' = '5';
                ...
            }
            'Status' = @{
                'preprerotate' = $false
                'prerotate' = $false
                'rotate' = $false
                'postrotate' = $false
                'postpostrotate' = $false
                'rotation_datetime' = (Get-Date).ToLocalTime()
            }
            'Metadata' = @{
                my_name = 'console.log.';
                $my_extension = '.log';
                $my_stem = '.log';
                ...
            }
        }
        #>

        param ([System.IO.FileInfo]$logfile, [hashtable]$options, [string]$lastRotationDate)

        # Unpack the block's options into variables
        $options.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $options[$_]
        }

        #$VerbosePreference = $oldVerbosePreference

        # Builds a Constructed Log Object, for log files that should be rotated
        if ($logfile) {
            # E.g. '\' for WinNT, '/' for nix
            $SLASH = [IO.Path]::DirectorySeparatorChar

            # E.g. 'console.log' and 'D:\console.log'
            $my_name = $logfile.Name
            $my_fullname = $logfile.FullName

            # E.g. '.log'
            $my_extension = $logfile.Extension

            # Are we preserving the extension?
            $_preserve_extension = $extension -and $my_extension -and ($my_extension -eq $extension)
            Write-Verbose (& { if (!$_preserve_extension) {  "Not preserving extension." } else { "Preserving extension: $extension" } })

            # If we're preserving extension (which can consist of multiple .), we need the stam of the filename.
            # E.g. 'console'. It's the same as $_.BaseName
            $my_stem =  if ($extension) {
                            # E.g. '(.*)\.log' will capture 'console', when extension is '.log'
                            $my_stem_regex = [Regex]"(.*)$( [Regex]::Escape($extension) )$"
                            $matches = $my_stem_regex.Match($my_name)
                            $matches.Groups[1].Value
                        }else {
                            $logfile.BaseName
                        }

            # E.g. 'console\.log'
            $my_name_regex = [Regex]::Escape($my_name)

            # E.g. '\.log'
            $extension_regex = [Regex]::Escape($extension)

            # E.g. 'console'
            $my_stem_regex = [Regex]::Escape($my_stem)

            # Get current directory. E.g. 'D:\data'
            $my_directory = $logfile.Directory.FullName

            # Validate our olddir is a directory, and resolve it to an absolute path
            if ($olddir) {
                # Try relative location, then try absolute

                if ([System.IO.Path]::IsPathRooted($olddir)) {
                    # Absolute path
                }else {
                    # Relative path. Check for existance of an olddir in the same directory as the log file
                    $olddir = Join-Path $my_directory $olddir
                }
                if ( !(Test-Path $olddir -PathType Container) ) {
                    throw "Invalid olddir: $olddir. Not using olddir. Skipping log $($logfile.FullName)!"
                }
            }

            # Get previous directory. E.g. D:\data or D:\data\olddir
            $my_previous_directory = if ($olddir) { $olddir } else { $my_directory }

            # Check directories' permissions, skip over if insufficient permissions.
            foreach ($dir in $my_directory,$my_previous_directory) {
                try {
                    $_outfile = Join-Path $dir ".test$(Get-Date -Format 'yyyyMMdd')"
                    [io.file]::OpenWrite($_outfile).close()
                    Remove-Item $_outfile
                }catch {
                    throw "Insufficient permissions on $dir. Ensure the user $($env:username) has read and write permissions on the directory."
                }
            }

            # Build our glob patterns
            $my_date = ''
            $my_date_regex = ''
            if ($dateext -or !$dateext) {
                # Generate the date extension

                # Trim the strftime datetime format string
                $_dateformat_tmp = $dateformat
                $dateformat = $dateformat.Trim()
                Write-Verbose "Converted dateformat from '$_dateformat_tmp' to '$dateformat'"

                # Convert the strftime datetime format string (specifiers: %Y, %m, and %d) to a .NET datetime format string (specifiers: yyyy, mm, dd). Then use it to get the current datetime as a string
                # E.g. '2017-12-25'
                #$_format = $dateformat.replace('%Y', 'yyyy').replace('%m', 'MM').replace('%d', 'dd')
                #$my_date = Get-Date -Format $_format

                # Replace specifier %s with unix epoch first in the strftime datetime format string. Then use it to get the current datetime as a string.
                # E.g. '2017-12-25'
                $_unix_epoch = [Math]::Floor([decimal](Get-Date (Get-Date).ToUniversalTime() -uformat "%s"))
                $_uformat = $dateformat.replace('%s', $_unix_epoch)
                $my_date = Get-Date -UFormat $_uformat
                Write-Verbose "Determined date extension to be '$my_date'"

                # Determine glob
                # E.g. [0-9]{4}-[0-9]{2}-[0-9]{2}
                $_unix_epoch_length = $_unix_epoch.ToString().Length
                $my_date_regex = $dateformat.Replace('%Y', '[0-9]{4}').Replace('%m', '[0-9]{2}').Replace('%d', '[0-9]{2}').Replace('%s', "[0-9]{$_unix_epoch_length}")
                Write-Verbose "Determined date glob pattern: '$my_date_regex'"
            }

            # Build our previous name and previous fullname
            $my_previous_name = & {
                if ($_preserve_extension) {
                    if ($dateext) {
                        # E.g. 'console-2017-11-20.log'
                        "$my_stem$my_date$extension"
                    }else {
                        # E.g. 'console.1.log'
                        "$my_stem.$start$extension"
                    }
                }else {
                    if ($dateext) {
                        # E.g. 'console.log-2017-11-20'
                        "$my_name$my_date"
                    }else {
                        # E.g. 'console.log.1'
                        "$my_name.$start"
                    }
                }
            }
            $my_previous_fullname = Join-Path $my_previous_directory $my_previous_name

            # Determine the to-be-rotated log's compressed file name, if we are going to
            # E.g. 'D:\console.log.1.7z'
            $my_previous_compressed_fullname =  if ($compress) {
                                                    "$my_previous_fullname$compressext"
                                                } else {
                                                    ''
                                                }

            # Build prototype names
            # E.g. 'console.log.1'
            # E.g. 'console.log.1.7z'
            $my_previous_name_prototype = $my_previous_name
            $my_previous_compressed_name_prototype =    if ($compress) {
                                                            "$my_previous_name$compressext"
                                                        }

            $my_index_regex = "\d{1,$( $rotate.ToString().Length )}";
            $my_previous_noncompressed_regex = if ($_preserve_extension) {
                                                    # E.g. '^console\-[0-9]{4}-[0-9]{2}-[0-9]{2}\.log$' or '^console\.\d{1,2}\.log$'
                                                    if ($dateext) {
                                                        "^$my_stem_regex$my_date_regex$extension_regex$"
                                                    }else {
                                                        "^$my_stem_regex\.$my_index_regex$extension_regex$"
                                                    }
                                                }else {
                                                    # E.g. '^console\.log\-[0-9]{4}-[0-9]{2}-[0-9]{2}$' or '^console\.log\.\d{1,2}$'
                                                    if ($dateext) {
                                                        "^$my_name_regex$my_date_regex$"
                                                    }else {
                                                        "^$my_name_regex\.$my_index_regex$"
                                                    }
                                                }
            # E.g. '^console\.log\.\d{1,2}\.7z$' or '^console\.log\-[0-9]{4}-[0-9]{2}-[0-9]{2}\.7z$'
            $my_previous_compressed_regex = ($my_previous_noncompressed_regex -replace ".$") + [Regex]::Escape($compressext) + "$"

            # The same as above, but with capture groups.
            $my_previous_noncompressed_captures_regex = if ($_preserve_extension) {
                                                            # E.g. '^(?<prefix>console)(?<suffix>\-[0-9]{4}-[0-9]{2}-[0-9]{2})(?<extension>\.log)$' or '^(?<prefix>console)\.(?<suffix>\d{1,2})(?<extension>\.log)$'
                                                            if ($dateext) {
                                                                "^(?<prefix>$my_stem_regex)(?<suffix>$my_date_regex)(?<extension>$extension_regex)$"
                                                            }else {
                                                                "^(?<prefix>$my_stem_regex)\.(?<suffix>$my_index_regex)(?<extension>$extension_regex)$"
                                                            }
                                                        }else {
                                                            # E.g. '^(?<prefix>console\.log)(?<suffix>\-[0-9]{4}-[0-9]{2}-[0-9]{2})$' or '^(?<prefix>console\.log)\.(?<suffix>\d{1,2})$'
                                                            if ($dateext) {
                                                                "^(?<prefix>$my_name_regex)(?<suffix>$my_date_regex)$"
                                                            }else {
                                                                "^(?<prefix>$my_name_regex)\.(?<suffix>$my_index_regex)$"
                                                            }
                                                        }
            # E.g. '^(?<prefix>console\.log)\.(?<suffix>\d{1,2})(?<compressextension>\.7z)$' or '^(?<prefix>console\.log)(?<suffix>\-[0-9]{4}-[0-9]{2}-[0-9]{2})(?<compressextension>\.7z)$'
            $my_previous_compressed_captures_regex = ($my_previous_noncompressed_captures_regex -replace ".$") + "(?<compressextension>$( [Regex]::Escape($compressext) ))$"

            # Get all my existing files
            $my_prevfiles = if ($compress) {
                                # Get all my existing non-compressed files in this folder. E.g. filter by '*.7z'
                                Get-ChildItem $my_previous_directory | Where-Object { $_.Name -match $my_previous_compressed_regex }
                            }else {
                                # Get all my existing compressed files in this folder. E.g. 'console.log.x.7z', where x is a number
                                Get-ChildItem $my_previous_directory | Where-Object { $_.Name -match $my_previous_noncompressed_regex }
                            }

            # The expired file name. Only used for non-date extension.
            $my_expired_fullName =  if ($_preserve_extension) {
                                        if ($compress) {
                                            # E.g. 'D:\console.6.log.7z'
                                            Join-Path $my_previous_directory "$my_stem.$($start+$rotate)$extension$compressext"
                                        }else {
                                            # E.g. 'D:\console.6.log'
                                            Join-Path $my_previous_directory "$my_stem.$($start+$rotate)$extension"
                                        }
                                    }else {
                                        if ($compress) {
                                            # E.g. 'D:\console.log.6.7z'
                                            Join-Path  $my_previous_directory "$my_name.$($start+$rotate)$compressext"
                                        }else {
                                            # E.g. 'D:\console.log.6'
                                            Join-Path $my_previous_directory "$my_name.$($start+$rotate)"
                                        }
                                    }


            # Rotate? ALL CONDITIONS BELOW
            $should_rotate = & {

                # If forced, no processing of rotation conditions needed. Go ahead and rotate.
                if ($force) {
                    return $true
                }

                # If never rotated before, go ahead
                #if (!$lastRotationDate) {
                # return $true
                #}

                # Don't rotate if log file size is 0, and we specified to not rotate empty files.
                if (!$ifempty) {
                    $my_size = $logfile.Length
                    if (!$my_size) {
                        Write-Verbose "Will not rotate log: $my_name. File size is 0."
                        return $false
                    }
                }

                # Don't rotate if my size is smaller than size threshold
                if ($size) {
                    $my_size = ($logfile | Measure-Object -Property Length -Sum -Average -Maximum -Minimum).Sum
                    if ($my_size -le (Get-Size-Bytes $size)) {
                        Write-Verbose "Will not rotate log: $my_name. File's size ($my_size) is less than defined ($size)."
                        return $false
                    }
                }

                # Don't rotate if we haven't met time thresholds by daily / weekly / monthy / yearly options.
                # If minsize specified along with time thresholds, don't rotate if either time or minsize thresholds are unmet.
                if ($daily -or $weekly -or $monthly -or $yearly) {
                    $time_interval_over = & {
                        # If it's our first time, considered to have met the time threshold
                        if (!$lastRotationDate) {
                            return $true
                        }

                        $_now_dt = (Get-Date).ToLocalTime()
                        # Not using CreationTime, but using state file now.
                        #$_my_newest_file = $my_prevfiles | Sort-Object -Property CreationTime -Descending | Select-Object -First 1
                        #$lastRotationDate = $_my_newest_file.CreationTime.ToLocalTime()
                        $_last_dt = Get-Date -Date $lastRotationDate
                        $_days_ago = (New-TimeSpan -Start $_last_dt -End $_now_dt ).Days
                        if ($daily) {
                            if ($_days_ago -ge 1) {
                                Write-Verbose "Time interval over. Last rotation occured a day or more ago. Last rotation: $($_last_dt.ToString('s'))"
                                return $true
                            }else {
                                Write-Verbose "Time interval not over. Last rotation occured less than a day ago. Last rotation: $($_last_dt.ToString('s'))"
                            }
                        }elseif ($weekly) {
                            if ($_days_ago -ge 7) {
                                Write-Verbose "Time interval over. Last rotation occured a week or more ago. Last rotation: $($_last_dt.ToString('s'))"
                                return $true
                            }elseif ( ($_days_ago -lt 7) -and ($_now_dt.DayOfWeek.value__ -lt $_last_dt.DayOfWeek.value__) ) {
                                Write-Verbose "Time interval over. Current weekday is less than weekday of last rotation."
                                return $true
                            }else {
                                Write-Verbose "Time interval not over. Last rotation occured less than a week ago. Last rotation: $($_last_dt.ToString('s'))"
                            }
                        }elseif ($monthly) {
                            if ($_now_dt.Month -gt $_last_dt.Month) {
                                Write-Verbose "Time interval over. This is the first time logrotate is run this month. "
                                return $true
                            }elseif ($_now_dt.Year -gt $_last_dt.Year) {
                                # The case where the current year is a new year
                                Write-Verbose "Time interval over. Last rotation occured last year. Last rotation: $($_last_dt.ToString('s'))"
                                return $true
                            }else {
                                Write-Verbose "Time interval not over. Last rotation already occured this month. Last rotation: $($_last_dt.ToString('s'))"
                            }
                        }elseif ($yearly) {
                            if ($_now_dt.Year -ne $_last_dt.Year) {
                                Write-Verbose "Time interval over. Last rotation occured on a different year as this year. Last rotation: $($_last_dt.ToString('s'))"
                                return $true
                            }else {
                                Write-Verbose "Time interval not over. Last rotation already occured this year. Last rotation: $($_last_dt.ToString('s'))"
                            }
                        }
                        $false
                    }
                    if ($time_interval_over) {
                        # If minsize is specified, both time and minsize thresholds will be considered
                        if ($minsize) {
                            $my_size = ($logfile | Measure-Object -Property Length -Sum -Average -Maximum -Minimum).Sum
                            Write-Verbose "my_size: $my_size"
                            if ($my_size -ge $minsize) {
                                # Minsize threshold met
                                Write-Verbose "Will rotate log: $my_name. Time interval over, and minsize met. File's size ($my_size) is less than minsize ($minsize)"
                                return $true
                            }else {
                                # Minsize threshold unmet
                                Write-Verbose "Will not rotate log: $my_name. Time interval over, but minsize not met. File's size ($my_size) is less than defined ($minsize)."
                                return $false
                            }
                        }else {
                            # No minsize specified. Only time threshold met.
                        }

                        # Time threshold met. Will rotate.
                        return $true
                    }else {
                        # Haven't met time threshold. Don't rotate.
                        return $false
                    }
                }

                # True by default. No conditions stopped us from moving on.
                $true
            }

            # Assign properties to the Log Object
            if ($should_rotate) {

                $_logObject = $LogObject.psobject.copy()
                $_logObject.Logfile = $logfile;
                $_logObject.Options = $options;
                $_logObject.Status = @{
                    'preprerotate' = $false
                    'prerotate' = $false
                    'rotate' = $false
                    'postrotate' = $false
                    'postpostrotate' = $false
                    'rotation_datetime' = (Get-Date).ToLocalTime()
                }
                $_logObject.Metadata = @{
                    'my_name' = $my_name
                    'my_extension' = $my_extension
                    'my_stem' = $my_stem
                    'my_directory' = $my_directory
                    'my_previous_directory' = $my_previous_directory
                    'my_previous_name' = $my_previous_name
                    'my_previous_fullname' = $my_previous_fullname
                    'my_date' = $my_date

                    'my_name_regex' = $my_name_regex
                    'my_date_regex' = $my_date_regex
                    'my_previous_noncompressed_regex' = $my_previous_noncompressed_regex
                    'my_previous_compressed_regex' = $my_previous_compressed_regex
                    'my_previous_noncompressed_captures_regex' = $my_previous_noncompressed_captures_regex
                    'my_previous_compressed_captures_regex' = $my_previous_compressed_captures_regex
                    'my_previous_compressed_fullname' = $my_previous_compressed_fullname

                    'my_prevfiles' = $my_prevfiles

                    'my_previous_name_prototype' = $my_previous_name_prototype
                    'my_previous_compressed_name_prototype' = $my_previous_compressed_name_prototype

                    'my_expired_fullName' = $my_expired_fullName

                    # For debug mode
                    'debug_my_prevfilespurged_fullnames' = [System.Collections.ArrayList]@()

                    'my_fullname' = $my_fullname
                    'SLASH' = $SLASH
                }

                return $_logObject
            }
        }else {
            if ($nomissingok) {
                throw "Specified log $logfile is not a file."
            }
        }
        $null
    }
    $LogObject | Add-Member -Name 'PrePrerotate' -MemberType ScriptMethod -Value {
        # Unpack Object properties
        Set-Variable -Name 'logfile' -Value $this.Logfile
        $this.Options.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Options[$_]
        }
        $this.Metadata.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Metadata[$_]
        }

        # Unpack Object methods
        . $this.PrivateMethods
        . $this.HelperMethods

        Write-Verbose "rotating log $my_fullname, log->rotateCount is $rotate"
        Write-Verbose "date suffix '$my_date'"
        Write-Verbose "date glob pattern '$my_date_regex'"

        if (!$compress) {
            # Normal rotation without compression

            if (!$dateext) {
                # Rotate all previous files
                # D:\console.log.1.7z -> D:\console.log.2.7z
                Rotate-Previous-Files-Incremental 0 ($start+$rotate-1)

                Notify-Purge $my_expired_fullName

                $this.Status.preprerotate = $true
            }else {
                # Delete old logs
                if ($rotate -gt 0) {
                    Remove-Old-Files (Get-Files $my_previous_noncompressed_regex $my_previous_directory) $rotate 1
                }
                if (Test-Path $my_previous_fullname) {
                    Write-Verbose "Destination $my_previous_fullname already exists, skipping rotation"
                }else {
                    $this.Status.preprerotate = $true
                }
            }
        }elseif ($compress) {
            # Compress

            if (!$dateext) {

                if (!$delaycompress) {
                    # Rotate all previous compressed files
                    # D:\console.log.1.7z -> D:\console.log.2.7z
                    Rotate-Previous-Files-Incremental 1 ($start+$rotate-1)

                    # TODO: Not using for now. See the comments in function.
                    #Rename-File-Within-Compressed-Archive

                    Notify-Purge $my_expired_fullName

                    $this.Status.preprerotate = $true
                }else {
                    # Flag to indicate safe to rotate
                    $_skip_rotate = $false
                    if (Test-Path $my_previous_fullname) {
                        if (Test-Path $my_previous_compressed_fullname) {
                            # File exists.
                            Write-Verbose "Error creating output file $my_previous_compressed_fullname : file exists"
                            $_skip_rotate = $true
                        }else {
                            # Compress previous file, Remove compression source file
                            # D:\console.log.1 -> D:\console.log.1.7z
                            Compress-File $my_previous_compressed_fullname $my_previous_fullname
                        }
                    }

                    if ($_skip_rotate) {
                        Notify-Purge $my_expired_fullName
                    }else {
                        # Rotate all previous compressed files
                        # D:\console.log.1.7z -> D:\console.log.2.7z
                        Rotate-Previous-Files-Incremental 1 ($start+$rotate-1)

                        # Disabled for now, because this always recreates an archive, dumping a lot to the disk.
                        #Rename-File-Within-Compressed-Archive

                        Notify-Purge $my_expired_fullName

                        $this.Status.preprerotate = $true
                    }
                }
            }else {

                # Date extension
                if (!$delaycompress) {

                    # Delete old logs
                    if ($rotate -gt 0) {
                        Remove-Old-Files (Get-Files $my_previous_compressed_regex $my_previous_directory) $rotate 1
                    }

                    if (Test-Path $my_previous_compressed_fullname) {
                        # File exists.
                        Write-Verbose "Destination file $my_previous_compressed_fullname already exists, skipping rotation"
                    }else {
                        $this.Status.preprerotate = $true
                    }
                }else {
                    # Compress any uncompressed previous files
                    $_skip_rotate = $false
                    Get-Files $my_previous_noncompressed_regex $my_previous_directory | ForEach-Object {
                        # Skip over the rest of pipeline
                        if ($_skip_rotate) {
                            return
                        }

                        # E.g. console.log-2017-11-25
                        $_fullname = $_.FullName
                        # E.g. console.log-2017-11-25.7z
                        $_compressed_fullname = "$_fullname$compressext"

                        if ( Test-Path $_compressed_fullname ) {
                            Write-Verbose "Error creating output file $_compressed_fullname`: File exists"

                            $_skip_rotate = $true
                        }else {
                            # Compress previous file, Remove compression source file
                            # D:\console.log-2017-11-25 -> D:\console.log-2017-11-25.7z
                            Compress-File $_compressed_fullname $_fullname
                        }
                    }

                    if ($_skip_rotate) {
                        # Don't proceed any further
                    }else {
                        # Delete old logs
                        if ($rotate -gt 0) {
                            Remove-Old-Files (Get-Files $my_previous_compressed_regex $my_previous_directory) $rotate 1
                        }
                        if ( (Test-Path $my_previous_fullname) -or (Test-Path $my_previous_compressed_fullname) ) {
                            # File exists.
                            Write-Verbose "Destination file $my_previous_fullname already exists, skipping rotation"
                        }else {
                            $this.Status.preprerotate = $true
                        }
                    }
                }

            } # End if ($dateext)

        } # End if ($compress)

        # Removed this - Script will spit stdout on the Pipeline
        #$this.Status.preprerotate
    }
    $LogObject | Add-Member -Name 'Prerotate' -MemberType ScriptMethod -Value {
        # Unpack Object properties
        $prerotate = $this.Options['prerotate']
        $my_fullname = $this.Metadata['my_fullname']

        if ($prerotate) {
            Write-Verbose "Running prerotate script"
            try {
                Start-Script $prerotate $my_fullname -ErrorAction Stop
            }catch {
                Write-Error  "Failed to run prerotate script." -ErrorAction Continue
                throw
            }

            $this.Status.prerotate = $true
        }

        # Removed this - Script will spit stdout on the Pipeline
        #$this.Status.prerotate
    }
    $LogObject | Add-Member -Name 'RotateMainOnly' -MemberType ScriptMethod -Value {
        # Unpack Object properties
        Set-Variable -Name 'logfile' -Value $this.Logfile
        $this.Options.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Options[$_]
        }
        $this.Metadata.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Metadata[$_]
        }

        # Unpack Object methods
        . $this.PrivateMethods

        $this.Status.rotate = Rotate-Main
        # Removed this - Script will spit stdout on the Pipeline
        #$this.Status.rotate
    }
    $LogObject | Add-Member -Name 'Postrotate' -MemberType ScriptMethod -Value {
        # Unpack Object properties
        $postrotate = $this.Options['postrotate']
        $my_fullname = $this.Metadata['my_fullname']

        if ($postrotate) {
            Write-Verbose "Running postrotate script"
            try {
                Start-Script $postrotate $my_fullname -ErrorAction $CallerEA
            }catch {
                Write-Error "Failed to run postrotate script." -ErrorAction Continue
                throw
            }
            $this.Status.postrotate = $true
        }

        # Removed this - Script will spit stdout on the Pipeline
        #$this.Status.postrotate
    }
    $LogObject | Add-Member -Name 'PostPostRotate' -MemberType ScriptMethod -Value {
        # Unpack Object properties
        Set-Variable -Name 'logfile' -Value $this.Logfile
        $this.Options.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Options[$_]
        }
        $this.Metadata.Keys | ForEach-Object {
            Set-Variable -Name $_ -Value $this.Metadata[$_]
        }

        # Unpack Object methods
        . $this.PrivateMethods
        . $this.HelperMethods

        if (!$compress) {
            # Normal rotation without compression

            if (!$dateext) {
                # Non-dateext: we'll just purge a single file. At least that's how it works the actual logrotate.

                # Remove expired file
                Purge-File $my_expired_fullName

                $this.Status.postpostrotate = $true
            }else {
                # Remove expired files
                if ($rotate -gt 0) {
                    $keep_prev_count = $rotate
                    $prev_files = Get-Files $my_previous_noncompressed_regex $my_previous_directory | Where-Object {
                                                                                                            !$WhatIf -or
                                                                                                            ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
                                                                                                        }

                    Remove-Old-Files $prev_files $keep_prev_count 1
                }

                $this.Status.postpostrotate = $true
            }
        }elseif ($compress) {
            # Compress

            if (!$dateext) {
                # Non-dateext: we'll just purge a single file. At least that's how it works the actual logrotate.

                if (!$delaycompress) {
                    # Compress previous file, Remove compression source file
                    # D:\console.log.1 -> D:\console.log.1.7z
                    Compress-File $my_previous_compressed_fullname $my_previous_fullname

                    # Remove expired file
                    Purge-File $my_expired_fullName
                }else {
                    # Remove expired file
                    Purge-File $my_expired_fullName
                }
            }else {
                $keep_prev_compressed_count = $rotate

                # Date extension
                if (!$delaycompress) {
                    # Compress previous file, Remove compression source file
                    # console.log.1 -> console.log.1.7z
                    Compress-File $my_previous_compressed_fullname $my_previous_fullname
                }else {
                    if ($rotate -gt 0) {
                        # One log is non-compressed because it's delayed.
                        $keep_prev_compressed_count = $rotate - 1
                    }
                }

                # Delete old compressed logs
                if ($rotate -gt 0) {
                    $prev_files = Get-Files $my_previous_compressed_regex $my_previous_directory | Where-Object {
                        !$WhatIf -or
                        ($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
                    }
                    Remove-Old-Files $prev_files $keep_prev_compressed_count 1
                }

            } # End if ($dateext)


            $this.Status.postpostrotate = $true

        } # End if ($compress)

        # Removed this - Script will spit stdout on the Pipeline
        #$this.Status.postpostrotate
    }

    $LogObject
}