Gumby.Log.psm1

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
# abstain from using module Assert here as it might use the Log module

enum LogMessageType {
    Comment
    Warning
    Error
    Success
    Failure
    BeginSection
    EndSection
}

# Design considerations:
# I want to enable the use of Log calls without having to worry about performance. Therefore, Log
# calls in the absence of log listeners should have a negligible overhead.
#
# Should taking of time stamps be centralized in the log dispatcher?
# I think not for not all log outputs might utilize them (e.g. a file-based listener might while a
# screen-based listener might not).
#
# Should verbosity-gating be centralized in the log dispatcher?
# I think not. It is conceivable to have a run with multiple listeners that operate with different
# verbosity settings (e.g. a screen-based listener that shows only "important" output while a
# file-based listener logs everything).
#
# Should the log dispatcher keep track of nesting depth (sections)?
# I think not for not all log outputs might utilize them (e.g. an XML-based log listener might
# represent sections with respective tags rather than through indentation).

# Would 'Trace' be a better name?
class Log {
    static [void] Comment([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Comment, $Message)
    }

    static [void] Trace([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Comment, $Message)
    }

    static [void] Warning([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Warning, $Message)
        ++[Log]::_warningCount
    }

    static [void] Error([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Error, $Message)
        ++[Log]::_errorCount
    }

    static [void] Success([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Success, $Message)
        ++[Log]::_successCount
    }

    static [void] Failure([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::Failure, $Message)
        ++[Log]::_failureCount
    }

    static [void] BeginSection([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::BeginSection, $Message)
    }

    static [void] EndSection([string] $Message) {
        [Log]::DispatchMessage([LogMessageType]::EndSection, $Message)
    }

    static hidden [void] DispatchMessage([LogMessageType] $MessageType, [string] $Message) {
        foreach ($l in [Log]::Listeners) { $l.ProcessMessage($MessageType, $Message) }
    }

    static [uint32] WarningCount() { return [Log]::_warningCount }
    static [uint32] ErrorCount() { return [Log]::_errorCount }
    static [uint32] SuccessCount() { return [Log]::_successCount }
    static [uint32] FailureCount() { return [Log]::_failureCount }

    static [void] Reset() {
        [Log]::_errorCount = 0
        [Log]::_warningCount = 0
        [Log]::_successCount = 0
        [Log]::_failureCount = 0
    }

    # ArrayList over standard array for mutability
    static [System.Collections.ArrayList] $Listeners = [System.Collections.ArrayList]::new()

    static hidden [uint32] $_warningCount = 0
    static hidden [uint32] $_errorCount = 0
    static hidden [uint32] $_successCount = 0
    static hidden [uint32] $_failureCount = 0
}

class LogListenerBase {
    [void] ProcessMessage([LogMessageType] $MessageType, [string] $Message) {
        throw "calling abstract message"
    }
}

class LogObserver : LogListenerBase{
    LogObserver([scriptblock] $onProcessMessage) {
        $this.OnProcessMessage = $onProcessMessage
    }

    [void] ProcessMessage([LogMessageType] $MessageType, [string] $Message) {
        $this.OnProcessMessage.Invoke($MessageType, $Message)
    }

    hidden [scriptblock] $OnProcessMessage
}

class FileLogListener : LogListenerBase {
    FileLogListener([string] $fileName) {
        $this._fileName = $fileName
    }

    [void] ProcessMessage([LogMessageType] $messageType, [string] $message) {
        if ($messageType -eq [LogMessageType]::EndSection) {
            if ($this._nestingLevel -eq 0) { throw "mismatching log sections" }
            --$this._nestingLevel
        }

        Write-Output ("{0,-14} {1}{2}: {3}" -f
            (Get-Date).ToString("HH:mm:ss.FFFFF"),
            <# indent #> (" " * 4 * $this._nestingLevel),
            [FileLogListener]::GetMessageTypeString($messageType),
            $message
        ) >> $this._fileName

        if ($messageType -eq [LogMessageType]::BeginSection) { ++$this._nestingLevel }
    }

    static hidden [string] GetMessageTypeString([LogMessageType] $messageType) {
        switch ($messageType) {
            ([LogMessageType]::Comment) { return "COMMENT" }
            ([LogMessageType]::Warning) { return "WARNING" }
            ([LogMessageType]::Error) { return "ERROR" }
            ([LogMessageType]::Success) { return "SUCCESS" }
            ([LogMessageType]::Failure) { return "FAILURE" }
            ([LogMessageType]::BeginSection) { return "BEGIN" }
            ([LogMessageType]::EndSection) { return "END" }
        }
        throw "unknown message type '$messageType'"
    }

    hidden [string] $_fileName
    hidden [uint32] $_nestingLevel = 0
}

class ConsoleLogListener : LogListenerBase {
    [void] ProcessMessage([LogMessageType] $MessageType, [string] $Message) {
        switch ($MessageType) {
            ([LogMessageType]::Comment) {
                Write-Host $Message
                break
            }

            ([LogMessageType]::Warning) {
                Write-Host -ForegroundColor Yellow $Message
                break
            }

            ([LogMessageType]::Error) {
                Write-Host -ForegroundColor Red $Message
                break
            }

            ([LogMessageType]::Success) {
                Write-Host -ForegroundColor Green $Message
                break
            }

            ([LogMessageType]::Failure) {
                Write-Host -ForegroundColor Red $Message
                break
            }
        }
    }
}

class LogInterceptor : LogListenerBase {

    LogInterceptor([scriptblock] $onProcessMessage) {
        $this.onProcessMessage = $onProcessMessage
        $this.listeners = [Log]::Listeners
        [Log]::Listeners = [System.Collections.ArrayList]::new(@($this))
    }

    [void] ProcessMessage([LogMessageType] $MessageType, [string] $Message) {
        $this.onProcessMessage.Invoke($this, $MessageType, $Message)
    }

    [void] DispatchMessage([LogMessageType] $MessageType, [string] $Message) {
        foreach ($l in $this.listeners) { $l.ProcessMessage($MessageType, $Message) }
    }

    [void] Dispose() {
        [Log]::Listeners = $this.listeners
    }

    hidden [System.Collections.ArrayList] $listeners
    hidden [scriptblock] $onProcessMessage
}